From b0003d8680669ac7c3b0ca202f9d7442d57ad42e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 5 Sep 2023 15:03:13 +0200 Subject: tests: revice XDG unit tests --- source/CMakeLists.txt | 1 + source/tests/CMakeLists.txt | 6 ++ source/tests/lib/system/CMakeLists.txt | 11 +++ source/tests/lib/system/src/xdg.cpp | 171 +++++++++++++++++++++++++++++++++ source/tests/wanda/driver.cpp | 23 ----- source/tests/wanda/test_suite_xdg.cpp | 96 ------------------ 6 files changed, 189 insertions(+), 119 deletions(-) create mode 100644 source/tests/CMakeLists.txt create mode 100644 source/tests/lib/system/CMakeLists.txt create mode 100644 source/tests/lib/system/src/xdg.cpp delete mode 100644 source/tests/wanda/driver.cpp delete mode 100644 source/tests/wanda/test_suite_xdg.cpp (limited to 'source') diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 431dc9b..38a8208 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -7,3 +7,4 @@ project("wanda" add_subdirectory("app") add_subdirectory("lib") +add_subdirectory("tests") \ No newline at end of file diff --git a/source/tests/CMakeLists.txt b/source/tests/CMakeLists.txt new file mode 100644 index 0000000..57961c1 --- /dev/null +++ b/source/tests/CMakeLists.txt @@ -0,0 +1,6 @@ +find_package("Catch2") + +include("Catch") +enable_testing() + +add_subdirectory("lib/system") \ No newline at end of file diff --git a/source/tests/lib/system/CMakeLists.txt b/source/tests/lib/system/CMakeLists.txt new file mode 100644 index 0000000..ba0ff1a --- /dev/null +++ b/source/tests/lib/system/CMakeLists.txt @@ -0,0 +1,11 @@ +add_executable("wanda-tests-system" + "src/xdg.cpp" +) + +target_link_libraries("wanda-tests-system" + "wanda::system" + + "Catch2::Catch2WithMain" +) + +catch_discover_tests("wanda-tests-system") \ No newline at end of file diff --git a/source/tests/lib/system/src/xdg.cpp b/source/tests/lib/system/src/xdg.cpp new file mode 100644 index 0000000..5872dc6 --- /dev/null +++ b/source/tests/lib/system/src/xdg.cpp @@ -0,0 +1,171 @@ +#include "wanda/system/xdg.hpp" + +#include "wanda/system/environment.hpp" + +#include +#include + +#include +#include +#include + +using namespace std::string_literals; + +namespace wanda::system::test +{ + + TEST_CASE("XDG variable names match the specification", "[system][xdg][spec]") + { + REQUIRE(xdg_variable(xdg_directory::data_home) == "XDG_DATA_HOME"); + REQUIRE(xdg_variable(xdg_directory::config_home) == "XDG_CONFIG_HOME"); + REQUIRE(xdg_variable(xdg_directory::cache_home) == "XDG_CACHE_HOME"); + REQUIRE(xdg_variable(xdg_directory::runtime_dir) == "XDG_RUNTIME_DIR"); + } + + SCENARIO("The values of the XDG variables depend on the environment", "[system][xdg][spec]") + { + auto const home_directory = std::filesystem::path{"/home/test"}; + auto const run_directory = std::filesystem::path{"/run/user"}; + auto const uid = std::to_string(::getuid()); + + GIVEN("An environment that only contains HOME") + { + auto home = "HOME="s + home_directory.native(); + char const * env_data[] = {home.c_str(), nullptr}; + auto env = environment{env_data}; + + THEN("the data home is '/.local/share") + { + REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share"); + } + + THEN("the config home is '/.config") + { + REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config"); + } + + THEN("the cache home is '/.cache") + { + REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache"); + } + + THEN("the runtime directory is '/'") + { + REQUIRE(xdg_path_for(xdg_directory::runtime_dir, env) == run_directory / uid); + } + } + + GIVEN("An environment that only contains HOME and XDG_DATA_HOME") + { + auto home = "HOME="s + home_directory.native(); + auto data_home = "XDG_DATA_HOME=/home/test/xdg_data_home"s; + char const * env_data[] = {home.c_str(), data_home.c_str(), nullptr}; + auto env = environment{env_data}; + + THEN("the data home is '$XDG_DATA_HOME") + { + REQUIRE(xdg_path_for(xdg_directory::data_home, env) == env["XDG_DATA_HOME"]); + } + + THEN("the config home is '/.config") + { + REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config"); + } + + THEN("the cache home is '/.cache") + { + REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache"); + } + + THEN("the runtime directory is '/'") + { + REQUIRE(xdg_path_for(xdg_directory::runtime_dir, env) == run_directory / uid); + } + } + + GIVEN("An environment that only contains HOME and XDG_CONFIG_HOME") + { + auto home = "HOME="s + home_directory.native(); + auto config_home = "XDG_CONFIG_HOME=/home/test/xdg_config_home"s; + char const * env_data[] = {home.c_str(), config_home.c_str(), nullptr}; + auto env = environment{env_data}; + + THEN("the data home is '/.local/share") + { + REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share"); + } + + THEN("the config home is '$XDG_CONFIG_HOME") + { + REQUIRE(xdg_path_for(xdg_directory::config_home, env) == env["XDG_CONFIG_HOME"]); + } + + THEN("the cache home is '/.cache") + { + REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache"); + } + + THEN("the runtime directory is '/'") + { + REQUIRE(xdg_path_for(xdg_directory::runtime_dir, env) == run_directory / uid); + } + } + + GIVEN("An environment that only contains HOME and XDG_CACHE_HOME") + { + auto home = "HOME="s + home_directory.native(); + auto cache_home = "XDG_CACHE_HOME=/home/test/xdg_cache_home"s; + char const * env_data[] = {home.c_str(), cache_home.c_str(), nullptr}; + auto env = environment{env_data}; + + THEN("the data home is '/.local/share") + { + REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share"); + } + + THEN("the cache home is '/.config") + { + REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config"); + } + + THEN("the config home is '$XDG_CACHE_HOME") + { + REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == env["XDG_CACHE_HOME"]); + } + + THEN("the runtime directory is '/'") + { + REQUIRE(xdg_path_for(xdg_directory::runtime_dir, env) == run_directory / uid); + } + } + + GIVEN("An environment that only contains HOME and XDG_RUNTIME_DIR") + { + auto home = "HOME="s + home_directory.native(); + auto runtime_dir = "XDG_RUNTIME_DIR=/home/test/xdg_runtime_dir"s; + char const * env_data[] = {home.c_str(), runtime_dir.c_str(), nullptr}; + auto env = environment{env_data}; + + THEN("the data home is '/.local/share") + { + REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share"); + } + + THEN("the config home is '/.config") + { + REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config"); + } + + THEN("the cache home is '/.cache") + { + REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache"); + } + + THEN("the runtime directory is '$XDG_RUNTIME_DIR'") + { + REQUIRE(xdg_path_for(xdg_directory::runtime_dir, env) == env["XDG_RUNTIME_DIR"]); + } + } + } + +} // namespace wanda::system::test \ No newline at end of file diff --git a/source/tests/wanda/driver.cpp b/source/tests/wanda/driver.cpp deleted file mode 100644 index e0dd7c8..0000000 --- a/source/tests/wanda/driver.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "cute/cute.h" -#include "cute/cute_runner.h" -#include "cute/tap_listener.h" - -#include -#include -#include -#include -#include -#include - -namespace wanda::test -{ - std::pair suite(); -} - -int main(int argc, char const *const *argv) -{ - auto listener = cute::tap_listener<>{std::cout}; - auto runner = cute::makeRunner(listener, argc, argv); - auto suite = wanda::test::suite(); - return !runner(suite.first, suite.second.c_str()); -} \ No newline at end of file diff --git a/source/tests/wanda/test_suite_xdg.cpp b/source/tests/wanda/test_suite_xdg.cpp deleted file mode 100644 index 70597f7..0000000 --- a/source/tests/wanda/test_suite_xdg.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include - -#include - -#include - -#include -#include -#include - -namespace wanda::test -{ - -namespace -{ - char const * home_only_environment[2] = {"HOME=/home/cute"}; - char const * xdg_data_home_environment[2] = {"XDG_DATA_HOME=/home/cute/xdg_data_home"}; - char const * xdg_config_home_environment[2] = {"XDG_CONFIG_HOME=/home/cute/xdg_config_home"}; - char const * xdg_cache_home_environment[2] = {"XDG_CACHE_HOME=/home/cute/xdg_cache_home"}; - char const * xdg_runtime_dir_environment[2] = {"XDG_RUNTIME_DIR=/home/cute/xdg_runtime_dir"}; -} - -void test_xdg_variables() -{ - ASSERT_EQUAL("XDG_DATA_HOME", xdg_variable(xdg_directory::data_home)); - ASSERT_EQUAL("XDG_CONFIG_HOME", xdg_variable(xdg_directory::config_home)); - ASSERT_EQUAL("XDG_CACHE_HOME", xdg_variable(xdg_directory::cache_home)); - ASSERT_EQUAL("XDG_RUNTIME_DIR", xdg_variable(xdg_directory::runtime_dir)); -} - -void test_xdg_path_for_data_home_without_xdg_data_home_in_environment() -{ - auto env = environment{home_only_environment}; - ASSERT_EQUAL("/home/cute/.local/share", xdg_path_for(xdg_directory::data_home, env)); -} - -void test_xdg_path_for_data_home_with_xdg_data_home_in_environment() -{ - auto env = environment{xdg_data_home_environment}; - ASSERT_EQUAL("/home/cute/xdg_data_home", xdg_path_for(xdg_directory::data_home, env)); -} - -void test_xdg_path_for_config_home_without_xdg_config_home_in_environment() -{ - auto env = environment{home_only_environment}; - ASSERT_EQUAL("/home/cute/.config", xdg_path_for(xdg_directory::config_home, env)); -} - -void test_xdg_path_for_config_home_with_xdg_config_home_in_environment() -{ - auto env = environment{xdg_config_home_environment}; - ASSERT_EQUAL("/home/cute/xdg_config_home", xdg_path_for(xdg_directory::config_home, env)); -} - -void test_xdg_path_for_cache_home_without_xdg_cache_home_in_environment() -{ - auto env = environment{home_only_environment}; - ASSERT_EQUAL("/home/cute/.cache", xdg_path_for(xdg_directory::cache_home, env)); -} - -void test_xdg_path_for_cache_home_with_xdg_cache_home_in_environment() -{ - auto env = environment{xdg_cache_home_environment}; - ASSERT_EQUAL("/home/cute/xdg_cache_home", xdg_path_for(xdg_directory::cache_home, env)); -} - -void test_xdg_path_for_runtime_dir_without_xdg_runtime_dir_in_environment() -{ - auto env = environment{home_only_environment}; - auto expected = std::filesystem::path{"/run/user"} / std::to_string(::getuid()); - ASSERT_EQUAL(expected, xdg_path_for(xdg_directory::runtime_dir, env)); -} - -void test_xdg_path_for_runtime_dir_with_xdg_runtime_dir_in_environment() -{ - auto env = environment{xdg_runtime_dir_environment}; - ASSERT_EQUAL("/home/cute/xdg_runtime_dir", xdg_path_for(xdg_directory::runtime_dir, env)); -} - -std::pair suite() -{ - return std::make_pair(cute::suite{ - CUTE(test_xdg_variables), - CUTE(test_xdg_path_for_data_home_without_xdg_data_home_in_environment), - CUTE(test_xdg_path_for_data_home_with_xdg_data_home_in_environment), - CUTE(test_xdg_path_for_config_home_without_xdg_config_home_in_environment), - CUTE(test_xdg_path_for_config_home_with_xdg_config_home_in_environment), - CUTE(test_xdg_path_for_cache_home_without_xdg_cache_home_in_environment), - CUTE(test_xdg_path_for_cache_home_with_xdg_cache_home_in_environment), - CUTE(test_xdg_path_for_runtime_dir_without_xdg_runtime_dir_in_environment), - CUTE(test_xdg_path_for_runtime_dir_with_xdg_runtime_dir_in_environment), - }, - "XDG Utilities"); -} - -} // namespace wanda \ No newline at end of file -- cgit v1.2.3