You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
5.4 KiB

#include "filesystem.hpp"
#include <glib.h>
int main() {
{
auto path = filesystem::get_home_path();
g_assert(!path.empty());
g_assert(boost::filesystem::exists(path));
g_assert(boost::filesystem::is_directory(path));
}
{
auto path = filesystem::get_current_path();
g_assert(!path.empty());
g_assert(boost::filesystem::exists(path));
g_assert(boost::filesystem::is_directory(path));
}
{
auto paths = filesystem::get_executable_search_paths();
g_assert(!paths.empty());
for(auto &path : paths) {
g_assert(!path.empty());
if(path.string() != "C:\\msys64\\usr\\local\\bin") { // Workaround for MSYS2
g_assert(boost::filesystem::exists(path));
g_assert(boost::filesystem::is_directory(path));
}
}
}
{
auto original = "test () '\"";
auto escaped = filesystem::escape_argument(original);
g_assert_cmpstr(escaped.c_str(), ==, "test\\ \\(\\)\\ \\'\\\"");
auto unescaped = filesystem::unescape_argument(escaped);
g_assert_cmpstr(unescaped.c_str(), ==, original);
}
{
auto unescaped = filesystem::unescape_argument("'test \\()\"\\''");
g_assert_cmpstr(unescaped.c_str(), ==, "test \\()\"'");
}
{
auto unescaped = filesystem::unescape_argument("\"test \\'()\\\"\"");
g_assert_cmpstr(unescaped.c_str(), ==, "test \\'()\"");
}
{
auto unescaped = filesystem::unescape_argument("\\\\");
g_assert_cmpstr(unescaped.c_str(), ==, "\\");
}
{
auto unescaped = filesystem::unescape_argument("\\\\\\ ");
g_assert_cmpstr(unescaped.c_str(), ==, "\\ ");
}
{
auto unescaped = filesystem::unescape_argument("\\\\\\ \\ \\ \\\\");
g_assert_cmpstr(unescaped.c_str(), ==, "\\ \\");
}
{
auto unescaped = filesystem::unescape_argument("c:\\a\\ b\\c");
g_assert_cmpstr(unescaped.c_str(), ==, "c:\\a b\\c");
}
{
auto unescaped = filesystem::unescape_argument("\"\\\\\\\"\"");
g_assert_cmpstr(unescaped.c_str(), ==, "\\\"");
}
{
auto unescaped = filesystem::unescape_argument("\"\\\"\"");
g_assert_cmpstr(unescaped.c_str(), ==, "\"");
}
{
auto unescaped = filesystem::unescape_argument("\"a\\b\"");
g_assert_cmpstr(unescaped.c_str(), ==, "a\\b");
}
auto tests_path = boost::filesystem::canonical(JUCI_TESTS_PATH);
{
g_assert(filesystem::file_in_path(tests_path / "filesystem_test.cc", tests_path));
g_assert(!filesystem::file_in_path(boost::filesystem::canonical(tests_path / ".." / "CMakeLists.txt"), tests_path));
}
auto license_file = boost::filesystem::canonical(tests_path / ".." / "LICENSE");
{
g_assert(filesystem::find_file_in_path_parents("LICENSE", tests_path) == license_file);
}
{
g_assert(filesystem::get_normal_path(tests_path / ".." / "LICENSE") == license_file);
g_assert(filesystem::get_normal_path("/foo") == "/foo");
g_assert(filesystem::get_normal_path("/foo/") == "/foo");
g_assert(filesystem::get_normal_path("/foo/.") == "/foo");
g_assert(filesystem::get_normal_path("/foo/./bar/..////") == "/foo");
g_assert(filesystem::get_normal_path("/foo/.///bar/../") == "/foo");
g_assert(filesystem::get_normal_path("../foo") == "../foo");
g_assert(filesystem::get_normal_path("../../foo") == "../../foo");
g_assert(filesystem::get_normal_path("../././foo") == "../foo");
g_assert(filesystem::get_normal_path("/../foo") == "/../foo");
}
{
boost::filesystem::path relative_path = "filesystem_test.cc";
g_assert(filesystem::get_relative_path(tests_path / relative_path, tests_path) == relative_path);
g_assert(filesystem::get_relative_path(tests_path / "test" / relative_path, tests_path) == boost::filesystem::path("test") / relative_path);
g_assert(filesystem::get_relative_path("/test/test/test.cc", "/test/base") == boost::filesystem::path("..") / "test" / "test.cc");
g_assert(filesystem::get_relative_path("/test/test/test/test.cc", "/test/base") == boost::filesystem::path("..") / "test" / "test" / "test.cc");
g_assert(filesystem::get_relative_path("/test2/test.cc", "/test/base") == boost::filesystem::path("..") / ".." / "test2" / "test.cc");
}
{
g_assert(filesystem::get_absolute_path("./test1/test2", "/home") == boost::filesystem::path("/") / "home" / "." / "test1" / "test2");
g_assert(filesystem::get_absolute_path("../test1/test2", "/home") == boost::filesystem::path("/") / "home" / ".." / "test1" / "test2");
g_assert(filesystem::get_absolute_path("test1/test2", "/home") == boost::filesystem::path("/") / "home" / "test1" / "test2");
g_assert(filesystem::get_absolute_path("/test1/test2", "/home") == boost::filesystem::path("/") / "test1" / "test2");
g_assert(filesystem::get_absolute_path("~/test1/test2", "/home") == boost::filesystem::path("~") / "test1" / "test2");
}
{
boost::filesystem::path path = "/ro ot/te stæøå.txt";
auto uri = filesystem::get_uri_from_path(path);
g_assert(uri == "file:///ro%20ot/te%20st%C3%A6%C3%B8%C3%A5.txt");
g_assert(path == filesystem::get_path_from_uri(uri));
}
{
g_assert(!filesystem::is_executable(filesystem::get_home_path()));
g_assert(!filesystem::is_executable(filesystem::get_current_path()));
g_assert(!filesystem::is_executable(tests_path));
#ifdef _WIN32
g_assert(filesystem::is_executable(tests_path / ".." / "LICENSE"));
#else
g_assert(!filesystem::is_executable(tests_path / ".." / "LICENSE"));
#endif
auto ls = filesystem::find_executable("ls");
g_assert(!ls.empty());
g_assert(filesystem::is_executable(ls));
}
}