1 #include "filesystem.hpp"
2 #include <glib.h>
3 
main()4 int main() {
5   {
6     auto path = filesystem::get_home_path();
7     g_assert(!path.empty());
8     g_assert(boost::filesystem::exists(path));
9     g_assert(boost::filesystem::is_directory(path));
10   }
11   {
12     auto path = filesystem::get_current_path();
13     g_assert(!path.empty());
14     g_assert(boost::filesystem::exists(path));
15     g_assert(boost::filesystem::is_directory(path));
16   }
17 
18   {
19     auto paths = filesystem::get_executable_search_paths();
20     g_assert(!paths.empty());
21     size_t count = 0;
22     for(auto &path : paths) {
23       g_assert(!path.empty());
24       if(boost::filesystem::exists(path)) {
25         g_assert(boost::filesystem::is_directory(path));
26         ++count;
27       }
28     }
29     g_assert(count > 0);
30   }
31 
32   {
33     auto original = "test () '\"";
34     auto escaped = filesystem::escape_argument(original);
35     g_assert_cmpstr(escaped.c_str(), ==, "test\\ \\(\\)\\ \\'\\\"");
36     auto unescaped = filesystem::unescape_argument(escaped);
37     g_assert_cmpstr(unescaped.c_str(), ==, original);
38   }
39   {
40     auto unescaped = filesystem::unescape_argument("'test \\()\"\\''");
41     g_assert_cmpstr(unescaped.c_str(), ==, "test \\()\"'");
42   }
43   {
44     auto unescaped = filesystem::unescape_argument("\"test \\'()\\\"\"");
45     g_assert_cmpstr(unescaped.c_str(), ==, "test \\'()\"");
46   }
47   {
48     auto unescaped = filesystem::unescape_argument("\\\\");
49     g_assert_cmpstr(unescaped.c_str(), ==, "\\");
50   }
51   {
52     auto unescaped = filesystem::unescape_argument("\\\\\\ ");
53     g_assert_cmpstr(unescaped.c_str(), ==, "\\ ");
54   }
55   {
56     auto unescaped = filesystem::unescape_argument("\\\\\\ \\ \\ \\\\");
57     g_assert_cmpstr(unescaped.c_str(), ==, "\\   \\");
58   }
59   {
60     auto unescaped = filesystem::unescape_argument("c:\\a\\ b\\c");
61     g_assert_cmpstr(unescaped.c_str(), ==, "c:\\a b\\c");
62   }
63   {
64     auto unescaped = filesystem::unescape_argument("\"\\\\\\\"\"");
65     g_assert_cmpstr(unescaped.c_str(), ==, "\\\"");
66   }
67   {
68     auto unescaped = filesystem::unescape_argument("\"\\\"\"");
69     g_assert_cmpstr(unescaped.c_str(), ==, "\"");
70   }
71   {
72     auto unescaped = filesystem::unescape_argument("\"a\\b\"");
73     g_assert_cmpstr(unescaped.c_str(), ==, "a\\b");
74   }
75 
76   auto tests_path = boost::filesystem::canonical(JUCI_TESTS_PATH);
77   {
78     g_assert(filesystem::file_in_path(tests_path / "filesystem_test.cc", tests_path));
79     g_assert(!filesystem::file_in_path(boost::filesystem::canonical(tests_path / ".." / "CMakeLists.txt"), tests_path));
80   }
81 
82   auto license_file = boost::filesystem::canonical(tests_path / ".." / "LICENSE");
83   {
84     g_assert(filesystem::find_file_in_path_parents("LICENSE", tests_path) == license_file);
85   }
86 
87   {
88     g_assert(filesystem::get_normal_path(tests_path / ".." / "LICENSE") == license_file);
89     g_assert(filesystem::get_normal_path("/foo") == "/foo");
90     g_assert(filesystem::get_normal_path("/foo/") == "/foo");
91     g_assert(filesystem::get_normal_path("/foo/.") == "/foo");
92     g_assert(filesystem::get_normal_path("/foo/./bar/..////") == "/foo");
93     g_assert(filesystem::get_normal_path("/foo/.///bar/../") == "/foo");
94     g_assert(filesystem::get_normal_path("../foo") == "../foo");
95     g_assert(filesystem::get_normal_path("../../foo") == "../../foo");
96     g_assert(filesystem::get_normal_path("../././foo") == "../foo");
97     g_assert(filesystem::get_normal_path("/../foo") == "/../foo");
98   }
99 
100   {
101     boost::filesystem::path relative_path = "filesystem_test.cc";
102     g_assert(filesystem::get_relative_path(tests_path / relative_path, tests_path) == relative_path);
103     g_assert(filesystem::get_relative_path(tests_path / "test" / relative_path, tests_path) == boost::filesystem::path("test") / relative_path);
104 
105     g_assert(filesystem::get_relative_path("/test/test/test.cc", "/test/base") == boost::filesystem::path("..") / "test" / "test.cc");
106     g_assert(filesystem::get_relative_path("/test/test/test/test.cc", "/test/base") == boost::filesystem::path("..") / "test" / "test" / "test.cc");
107     g_assert(filesystem::get_relative_path("/test2/test.cc", "/test/base") == boost::filesystem::path("..") / ".." / "test2" / "test.cc");
108   }
109 
110   {
111     g_assert(filesystem::get_absolute_path("./test1/test2", "/home") == boost::filesystem::path("/") / "home" / "." / "test1" / "test2");
112     g_assert(filesystem::get_absolute_path("../test1/test2", "/home") == boost::filesystem::path("/") / "home" / ".." / "test1" / "test2");
113     g_assert(filesystem::get_absolute_path("test1/test2", "/home") == boost::filesystem::path("/") / "home" / "test1" / "test2");
114     g_assert(filesystem::get_absolute_path("/test1/test2", "/home") == boost::filesystem::path("/") / "test1" / "test2");
115     g_assert(filesystem::get_absolute_path("~/test1/test2", "/home") == boost::filesystem::path("~") / "test1" / "test2");
116   }
117 
118   {
119     boost::filesystem::path path = "/ro ot/te stæøå.txt";
120     auto uri = filesystem::get_uri_from_path(path);
121     g_assert(uri == "file:///ro%20ot/te%20st%C3%A6%C3%B8%C3%A5.txt");
122     g_assert(path == filesystem::get_path_from_uri(uri));
123   }
124 
125 #ifdef _WIN32
126   {
127     std::string uri = "file:///c:/Users/test";
128     g_assert(filesystem::get_path_from_uri(uri) == "C:/Users/test");
129 
130     boost::filesystem::path path = "C:/Users/test";
131     g_assert(filesystem::get_uri_from_path(path) == "file:///C:/Users/test");
132   }
133 #endif
134 
135   {
136     auto ls = filesystem::get_executable("ls");
137     g_assert(ls == "ls");
138 
139     auto non_existing_executable = filesystem::get_executable("non_existing_executable");
140     g_assert(non_existing_executable == "non_existing_executable");
141   }
142 
143   {
144     g_assert(!filesystem::is_executable(filesystem::get_home_path()));
145     g_assert(!filesystem::is_executable(filesystem::get_current_path()));
146     g_assert(!filesystem::is_executable(tests_path));
147 #ifdef _WIN32
148     g_assert(filesystem::is_executable(tests_path / ".." / "LICENSE"));
149 #else
150     g_assert(!filesystem::is_executable(tests_path / ".." / "LICENSE"));
151 #endif
152     auto ls = filesystem::find_executable("ls");
153     g_assert(!ls.empty());
154     g_assert(filesystem::is_executable(ls));
155   }
156 }
157