1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-03 02:01:48 +02:00

More support for std::filepath in libnixutil

We're not replacing `Path` in exposed definitions in many cases, but
just adding alternatives. This will allow us to "top down" change `Path`
to `std::fileysystem::path`, and then we can remove the `Path`-using
utilities which will become unused.

Also add some test files which we forgot to include in the libutil unit
tests `meson.build`.

Co-Authored-By: siddhantCodes <siddhantk232@gmail.com>
This commit is contained in:
John Ericson 2024-08-26 12:24:37 -04:00
parent dbabfc92d4
commit a97a08411c
37 changed files with 258 additions and 120 deletions

View file

@ -12,8 +12,8 @@
#include <numeric>
#ifdef _WIN32
# define FS_SEP "\\"
# define FS_ROOT "C:" FS_SEP // Need a mounted one, C drive is likely
# define FS_SEP L"\\"
# define FS_ROOT L"C:" FS_SEP // Need a mounted one, C drive is likely
#else
# define FS_SEP "/"
# define FS_ROOT FS_SEP
@ -23,6 +23,12 @@
# define PATH_MAX 4096
#endif
#ifdef _WIN32
# define GET_CWD _wgetcwd
#else
# define GET_CWD getcwd
#endif
namespace nix {
/* ----------- tests for file-system.hh -------------------------------------*/
@ -33,34 +39,34 @@ namespace nix {
TEST(absPath, doesntChangeRoot)
{
auto p = absPath(FS_ROOT);
auto p = absPath(std::filesystem::path{FS_ROOT});
ASSERT_EQ(p, FS_ROOT);
}
TEST(absPath, turnsEmptyPathIntoCWD)
{
char cwd[PATH_MAX + 1];
auto p = absPath("");
OsChar cwd[PATH_MAX + 1];
auto p = absPath(std::filesystem::path{""});
ASSERT_EQ(p, getcwd((char *) &cwd, PATH_MAX));
ASSERT_EQ(p, GET_CWD((OsChar *) &cwd, PATH_MAX));
}
TEST(absPath, usesOptionalBasePathWhenGiven)
{
char _cwd[PATH_MAX + 1];
char * cwd = getcwd((char *) &_cwd, PATH_MAX);
OsChar _cwd[PATH_MAX + 1];
OsChar * cwd = GET_CWD((OsChar *) &_cwd, PATH_MAX);
auto p = absPath("", cwd);
auto p = absPath(std::filesystem::path{""}.string(), std::filesystem::path{cwd}.string());
ASSERT_EQ(p, cwd);
ASSERT_EQ(p, std::filesystem::path{cwd}.string());
}
TEST(absPath, isIdempotent)
{
char _cwd[PATH_MAX + 1];
char * cwd = getcwd((char *) &_cwd, PATH_MAX);
auto p1 = absPath(cwd);
OsChar _cwd[PATH_MAX + 1];
OsChar * cwd = GET_CWD((OsChar *) &_cwd, PATH_MAX);
auto p1 = absPath(std::filesystem::path{cwd});
auto p2 = absPath(p1);
ASSERT_EQ(p1, p2);
@ -68,8 +74,8 @@ TEST(absPath, isIdempotent)
TEST(absPath, pathIsCanonicalised)
{
auto path = FS_ROOT "some/path/with/trailing/dot/.";
auto p1 = absPath(path);
auto path = FS_ROOT OS_STR("some/path/with/trailing/dot/.");
auto p1 = absPath(std::filesystem::path{path});
auto p2 = absPath(p1);
ASSERT_EQ(p1, FS_ROOT "some" FS_SEP "path" FS_SEP "with" FS_SEP "trailing" FS_SEP "dot");
@ -82,26 +88,26 @@ TEST(absPath, pathIsCanonicalised)
TEST(canonPath, removesTrailingSlashes)
{
auto path = FS_ROOT "this/is/a/path//";
auto p = canonPath(path);
std::filesystem::path path = FS_ROOT "this/is/a/path//";
auto p = canonPath(path.string());
ASSERT_EQ(p, FS_ROOT "this" FS_SEP "is" FS_SEP "a" FS_SEP "path");
ASSERT_EQ(p, std::filesystem::path{FS_ROOT "this" FS_SEP "is" FS_SEP "a" FS_SEP "path"}.string());
}
TEST(canonPath, removesDots)
{
auto path = FS_ROOT "this/./is/a/path/./";
auto p = canonPath(path);
std::filesystem::path path = FS_ROOT "this/./is/a/path/./";
auto p = canonPath(path.string());
ASSERT_EQ(p, FS_ROOT "this" FS_SEP "is" FS_SEP "a" FS_SEP "path");
ASSERT_EQ(p, std::filesystem::path{FS_ROOT "this" FS_SEP "is" FS_SEP "a" FS_SEP "path"}.string());
}
TEST(canonPath, removesDots2)
{
auto path = FS_ROOT "this/a/../is/a////path/foo/..";
auto p = canonPath(path);
std::filesystem::path path = FS_ROOT "this/a/../is/a////path/foo/..";
auto p = canonPath(path.string());
ASSERT_EQ(p, FS_ROOT "this" FS_SEP "is" FS_SEP "a" FS_SEP "path");
ASSERT_EQ(p, std::filesystem::path{FS_ROOT "this" FS_SEP "is" FS_SEP "a" FS_SEP "path"}.string());
}
TEST(canonPath, requiresAbsolutePath)
@ -243,7 +249,7 @@ TEST(isDirOrInDir, DISABLED_shouldWork)
TEST(pathExists, rootExists)
{
ASSERT_TRUE(pathExists(FS_ROOT));
ASSERT_TRUE(pathExists(std::filesystem::path{FS_ROOT}.string()));
}
TEST(pathExists, cwdExists)