-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdg_user_dirs.cpp
133 lines (95 loc) · 3.49 KB
/
xdg_user_dirs.cpp
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
#include <sstream>
#include <iostream>
#include <catch2/catch_test_macros.hpp>
#include <dire/detail/getenv.hpp>
#include <dire/detail/system_dirs/xdg_user_dirs.hpp>
#include "test_env.hpp"
TEST_CASE("Xdg user dirs test", "[xdg_user_dirs]")
{
tenv::set("HOME", "/home/user/");
tenv::set("XDG_DATA_HOME", "/home/xdg/.local/share");
tenv::set("XDG_CONFIG_HOME", "/home/xdg/.config/");
tenv::set("XDG_STATE_HOME", "/home/xdg/.local/state");
tenv::set("XDG_CACHE_HOME", "/home/xdg/.cache");
tenv::set("XDG_RUNTIME_DIR", "/run/xdg/2137");
// Note that this is not officially supported by the xdg spec
tenv::set("XDG_BIN_HOME", "/home/xdg/.local/bin");
SECTION("Reads dirs properly")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
XDG_DOCUMENTS_DIR = "$HOME/Documents"
XDG_DESKTOP_DIR = "$HOME/Desktop"
XDG_DOWNLOAD_DIR = "/home/bob/Downloads"
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs["DOCUMENTS"] == "/home/user/Documents");
REQUIRE(xdg_dirs["DESKTOP"] == "/home/user/Desktop");
REQUIRE(xdg_dirs["DOWNLOAD"] == "/home/bob/Downloads");
REQUIRE(xdg_dirs.size() == 3);
}
SECTION("Omits comments")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
# This is a comment
XDG_DESKTOP_DIR="$HOME/Desktop"
# Also a comment lol
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs.size() == 1);
REQUIRE(xdg_dirs["DESKTOP"] == "/home/user/Desktop");
}
SECTION("Unescapes properly")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
XDG_DESKTOP_DIR="$HOME/De\\s\$ktop"
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs.size() == 1);
REQUIRE(xdg_dirs["DESKTOP"] == "/home/user/De\\s$ktop");
}
SECTION("Omits empty")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
XDG_DESKTOP_DIR=""
XDG_DOWNLOAD_DIR = ""
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs.empty());
}
SECTION("Omits non-xdg")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
PLENTY_OF="$HOME/Downloads"
# Keys here right?
XDG_NOT_HERE_LOl="$HOME/"
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs.empty());
}
SECTION("Omits just $HOME")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
XDG_DESKTOP_DIR = "$HOME/"
XDG_DOCUMENTS_DIR = "$HOME"
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs.empty());
}
SECTION("Omits invalid pairs")
{
static auto const home = dire::detail::getenv("HOME").value();
auto stream = std::stringstream(R"DIRS(
XDG_DESKTOP_DIR
A = "
= ""
)DIRS");
auto xdg_dirs = dire::detail::xdg::all_from_stream(stream, home);
REQUIRE(xdg_dirs.empty());
}
}