Skip to content

Commit

Permalink
os/xdg: Add tests for os::font_paths() interactions w/ env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Nov 23, 2023
1 parent 046c275 commit 403de1d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
12 changes: 12 additions & 0 deletions os/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ cc_test(
"//etest",
],
)

cc_test(
name = "xdg_linux_test",
size = "small",
srcs = ["xdg_linux_test.cpp"],
copts = HASTUR_COPTS,
target_compatible_with = ["@platforms//os:linux"],
deps = [
":xdg",
"//etest",
],
)
62 changes: 62 additions & 0 deletions os/xdg_linux_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-FileCopyrightText: 2021-2023 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

// https://man7.org/linux/man-pages/man3/setenv.3.html
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
// NOLINTNEXTLINE: The only option is to mess with this reserved identifier.
#define _POSIX_C_SOURCE 200112L

#include "os/xdg.h"

#include "etest/etest2.h"

// This is the header POSIX says we need to include.
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <stdlib.h>

// NOLINTBEGIN(concurrency-mt-unsafe): No threads here.

int main() {
// Ensure that the system's environment doesn't affect the test result.
unsetenv("HOME");
unsetenv("XDG_DATA_HOME");

etest::Suite s{"os::xdg/linux"};

auto const font_paths_without_env_vars = os::font_paths();

s.add_test("HOME", [&](etest::IActions &a) {
static constexpr auto kHome = "/home";
setenv("HOME", kHome, false);

a.expect(std::ranges::find_if(font_paths_without_env_vars, [](auto const &path) {
return path.contains(kHome);
}) == font_paths_without_env_vars.end());

auto font_paths = os::font_paths();
a.expect(std::ranges::find_if(font_paths, [](auto const &path) { return path.contains(kHome); })
!= font_paths.end());
unsetenv("HOME");
});

s.add_test("XDG_DATA_HOME", [&](etest::IActions &a) {
static constexpr auto kXdgDataHome = "/xdg_data_home";
setenv("XDG_DATA_HOME", kXdgDataHome, false);

a.expect(std::ranges::find_if(font_paths_without_env_vars, [](auto const &path) {
return path.contains(kXdgDataHome);
}) == font_paths_without_env_vars.end());

auto font_paths = os::font_paths();
a.expect(std::ranges::find_if(font_paths, [](auto const &path) { return path.contains(kXdgDataHome); })
!= font_paths.end());
unsetenv("XDG_DATA_HOME");
});

return s.run();
}

// NOLINTEND(concurrency-mt-unsafe)

0 comments on commit 403de1d

Please sign in to comment.