summaryrefslogtreecommitdiff
path: root/unittest/test_Util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'unittest/test_Util.cpp')
-rw-r--r--unittest/test_Util.cpp133
1 files changed, 114 insertions, 19 deletions
diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp
index 917c137..5fd5239 100644
--- a/unittest/test_Util.cpp
+++ b/unittest/test_Util.cpp
@@ -146,6 +146,16 @@ TEST_CASE("Util::dir_name")
CHECK(Util::dir_name("/") == "/");
CHECK(Util::dir_name("/foo") == "/");
CHECK(Util::dir_name("/foo/bar/f.txt") == "/foo/bar");
+
+#ifdef _WIN32
+ CHECK(Util::dir_name("C:/x/y") == "C:/x");
+ CHECK(Util::dir_name("X:/x/y") == "X:/x");
+ CHECK(Util::dir_name("C:\\x\\y") == "C:\\x");
+ CHECK(Util::dir_name("C:/x") == "C:/");
+ CHECK(Util::dir_name("C:\\x") == "C:\\");
+ CHECK(Util::dir_name("C:/") == "C:/");
+ CHECK(Util::dir_name("C:\\") == "C:\\");
+#endif
}
TEST_CASE("Util::strip_ansi_csi_seqs")
@@ -366,43 +376,40 @@ TEST_CASE("Util::get_level_1_files")
Util::write_file("0/1/file_c", "12");
Util::write_file("0/f/c/file_d", "123");
- std::vector<std::shared_ptr<CacheFile>> files;
auto null_receiver = [](double) {};
SUBCASE("nonexistent subdirectory")
{
- Util::get_level_1_files("2", null_receiver, files);
+ const auto files = Util::get_level_1_files("2", null_receiver);
CHECK(files.empty());
}
SUBCASE("empty subdirectory")
{
- Util::get_level_1_files("e", null_receiver, files);
+ const auto files = Util::get_level_1_files("e", null_receiver);
CHECK(files.empty());
}
SUBCASE("simple case")
{
- Util::get_level_1_files("0", null_receiver, files);
+ auto files = Util::get_level_1_files("0", null_receiver);
REQUIRE(files.size() == 4);
// Files within a level are in arbitrary order, sort them to be able to
// verify them.
- std::sort(files.begin(),
- files.end(),
- [](const std::shared_ptr<CacheFile>& f1,
- const std::shared_ptr<CacheFile>& f2) {
- return f1->path() < f2->path();
- });
-
- CHECK(files[0]->path() == os_path("0/1/file_b"));
- CHECK(files[0]->lstat().size() == 1);
- CHECK(files[1]->path() == os_path("0/1/file_c"));
- CHECK(files[1]->lstat().size() == 2);
- CHECK(files[2]->path() == os_path("0/f/c/file_d"));
- CHECK(files[2]->lstat().size() == 3);
- CHECK(files[3]->path() == os_path("0/file_a"));
- CHECK(files[3]->lstat().size() == 0);
+ std::sort(
+ files.begin(), files.end(), [](const CacheFile& f1, const CacheFile& f2) {
+ return f1.path() < f2.path();
+ });
+
+ CHECK(files[0].path() == os_path("0/1/file_b"));
+ CHECK(files[0].lstat().size() == 1);
+ CHECK(files[1].path() == os_path("0/1/file_c"));
+ CHECK(files[1].lstat().size() == 2);
+ CHECK(files[2].path() == os_path("0/f/c/file_d"));
+ CHECK(files[2].lstat().size() == 3);
+ CHECK(files[3].path() == os_path("0/file_a"));
+ CHECK(files[3].lstat().size() == 0);
}
}
@@ -443,6 +450,31 @@ TEST_CASE("Util::get_path_in_cache")
== "/zz/ccache/A/B/C/D/EF.suffix");
}
+TEST_CASE("Util::hard_link")
+{
+ TestContext test_context;
+
+ SUBCASE("Link file to nonexistent destination")
+ {
+ Util::write_file("old", "content");
+ CHECK_NOTHROW(Util::hard_link("old", "new"));
+ CHECK(Util::read_file("new") == "content");
+ }
+
+ SUBCASE("Link file to existing destination")
+ {
+ Util::write_file("old", "content");
+ Util::write_file("new", "other content");
+ CHECK_NOTHROW(Util::hard_link("old", "new"));
+ CHECK(Util::read_file("new") == "content");
+ }
+
+ SUBCASE("Link nonexistent file")
+ {
+ CHECK_THROWS_AS(Util::hard_link("old", "new"), Error);
+ }
+}
+
TEST_CASE("Util::int_to_big_endian")
{
uint8_t bytes[8];
@@ -529,6 +561,69 @@ TEST_CASE("Util::is_dir_separator")
#endif
}
+TEST_CASE("Util::make_relative_path")
+{
+ using Util::make_relative_path;
+
+ const TestContext test_context;
+
+ const std::string cwd = Util::get_actual_cwd();
+ const std::string actual_cwd = FMT("{}/d", cwd);
+#ifdef _WIN32
+ const std::string apparent_cwd = actual_cwd;
+#else
+ const std::string apparent_cwd = FMT("{}/s", cwd);
+#endif
+
+ REQUIRE(Util::create_dir("d"));
+#ifndef _WIN32
+ REQUIRE(symlink("d", "s") == 0);
+#endif
+ REQUIRE(chdir("d") == 0);
+ Util::setenv("PWD", apparent_cwd);
+
+ SUBCASE("No base directory")
+ {
+ CHECK(make_relative_path("", "/a", "/a", "/a/x") == "/a/x");
+ }
+
+ SUBCASE("Path matches neither actual nor apparent CWD")
+ {
+#ifdef _WIN32
+ CHECK(make_relative_path("C:/", "C:/a", "C:/b", "C:/x") == "C:/x");
+#else
+ CHECK(make_relative_path("/", "/a", "/b", "/x") == "/x");
+#endif
+ }
+
+ SUBCASE("Match of actual CWD")
+ {
+#ifdef _WIN32
+ CHECK(
+ make_relative_path(
+ actual_cwd.substr(0, 3), actual_cwd, apparent_cwd, actual_cwd + "/x")
+ == "./x");
+#else
+ CHECK(make_relative_path("/", actual_cwd, apparent_cwd, actual_cwd + "/x")
+ == "./x");
+#endif
+ }
+
+#ifndef _WIN32
+ SUBCASE("Match of apparent CWD")
+ {
+ CHECK(make_relative_path("/", actual_cwd, apparent_cwd, apparent_cwd + "/x")
+ == "./x");
+ }
+
+ SUBCASE("Match if using resolved (using realpath(3)) path")
+ {
+ CHECK(make_relative_path("/", actual_cwd, actual_cwd, apparent_cwd + "/x")
+ == "./x");
+ }
+#endif
+}
+
TEST_CASE("Util::matches_dir_prefix_or_file")
{
CHECK(!Util::matches_dir_prefix_or_file("", ""));