aboutsummaryrefslogtreecommitdiff
path: root/source/lib/tests/xdg.cpp
blob: 5872dc684d0d51c0a64747fd93c88ab05d8a3886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "wanda/system/xdg.hpp"

#include "wanda/system/environment.hpp"

#include <catch2/catch_all.hpp>
#include <unistd.h>

#include <filesystem>
#include <string>
#include <utility>

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 '<home_directory>/.local/share")
      {
        REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share");
      }

      THEN("the config home is '<home_directory>/.config")
      {
        REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config");
      }

      THEN("the cache home is '<home_directory>/.cache")
      {
        REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache");
      }

      THEN("the runtime directory is '<run_directory>/<uid>'")
      {
        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 '<home_directory>/.config")
      {
        REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config");
      }

      THEN("the cache home is '<home_directory>/.cache")
      {
        REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache");
      }

      THEN("the runtime directory is '<run_directory>/<uid>'")
      {
        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 '<home_directory>/.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 '<home_directory>/.cache")
      {
        REQUIRE(xdg_path_for(xdg_directory::cache_home, env) == home_directory / ".cache");
      }

      THEN("the runtime directory is '<run_directory>/<uid>'")
      {
        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 '<home_directory>/.local/share")
      {
        REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share");
      }

      THEN("the cache home is '<home_directory>/.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 '<run_directory>/<uid>'")
      {
        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 '<home_directory>/.local/share")
      {
        REQUIRE(xdg_path_for(xdg_directory::data_home, env) == home_directory / ".local/share");
      }

      THEN("the config home is '<home_directory>/.config")
      {
        REQUIRE(xdg_path_for(xdg_directory::config_home, env) == home_directory / ".config");
      }

      THEN("the cache home is '<home_directory>/.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