-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os/xdg: Add tests for os::font_paths() interactions w/ env vars
- Loading branch information
1 parent
046c275
commit 403de1d
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |