mirror of
https://github.com/NixOS/nix
synced 2025-07-07 06: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:
parent
dbabfc92d4
commit
a97a08411c
37 changed files with 258 additions and 120 deletions
|
@ -10,11 +10,11 @@ using nlohmann::json;
|
|||
|
||||
class PublicKeyTest : public CharacterizationTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/public-key";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "public-key";
|
||||
|
||||
public:
|
||||
Path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData + "/" + testStem;
|
||||
std::filesystem::path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData / testStem;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
#include <filesystem>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
namespace fs { using namespace std::filesystem; }
|
||||
|
||||
namespace nixC {
|
||||
class nix_api_store_test : public nix_api_util_context
|
||||
|
|
|
@ -12,10 +12,10 @@ namespace nix {
|
|||
template<class Proto, const char * protocolDir>
|
||||
class ProtoTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/" + protocolDir;
|
||||
std::filesystem::path unitTestData = getUnitTestData() / protocolDir;
|
||||
|
||||
Path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData + "/" + testStem + ".bin";
|
||||
std::filesystem::path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData / (std::string { testStem + ".bin" });
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -16,12 +16,12 @@ using nlohmann::json;
|
|||
|
||||
class DerivationAdvancedAttrsTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/derivation";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "derivation";
|
||||
|
||||
public:
|
||||
Path goldenMaster(std::string_view testStem) const override
|
||||
std::filesystem::path goldenMaster(std::string_view testStem) const override
|
||||
{
|
||||
return unitTestData + "/" + testStem;
|
||||
return unitTestData / testStem;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -13,11 +13,11 @@ using nlohmann::json;
|
|||
|
||||
class DerivationTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/derivation";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "derivation";
|
||||
|
||||
public:
|
||||
Path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData + "/" + testStem;
|
||||
std::filesystem::path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData / testStem;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,6 +13,8 @@ using testing::Eq;
|
|||
using testing::Field;
|
||||
using testing::SizeIs;
|
||||
|
||||
namespace nix::fs { using namespace std::filesystem; }
|
||||
|
||||
using namespace nix;
|
||||
|
||||
TEST(machines, getMachinesWithEmptyBuilders) {
|
||||
|
@ -135,10 +137,10 @@ TEST(machines, getMachinesWithIncorrectFormat) {
|
|||
}
|
||||
|
||||
TEST(machines, getMachinesWithCorrectFileReference) {
|
||||
auto path = absPath(getUnitTestData() + "/machines/valid");
|
||||
ASSERT_TRUE(pathExists(path));
|
||||
auto path = fs::weakly_canonical(getUnitTestData() / "machines/valid");
|
||||
ASSERT_TRUE(fs::exists(path));
|
||||
|
||||
auto actual = Machine::parseConfig({}, "@" + path);
|
||||
auto actual = Machine::parseConfig({}, "@" + path.string());
|
||||
ASSERT_THAT(actual, SizeIs(3));
|
||||
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl"))));
|
||||
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@itchy.labs.cs.uu.nl"))));
|
||||
|
@ -146,20 +148,22 @@ TEST(machines, getMachinesWithCorrectFileReference) {
|
|||
}
|
||||
|
||||
TEST(machines, getMachinesWithCorrectFileReferenceToEmptyFile) {
|
||||
auto path = "/dev/null";
|
||||
ASSERT_TRUE(pathExists(path));
|
||||
fs::path path = "/dev/null";
|
||||
ASSERT_TRUE(fs::exists(path));
|
||||
|
||||
auto actual = Machine::parseConfig({}, std::string{"@"} + path);
|
||||
auto actual = Machine::parseConfig({}, "@" + path.string());
|
||||
ASSERT_THAT(actual, SizeIs(0));
|
||||
}
|
||||
|
||||
TEST(machines, getMachinesWithIncorrectFileReference) {
|
||||
auto actual = Machine::parseConfig({}, "@" + absPath("/not/a/file"));
|
||||
auto path = fs::weakly_canonical("/not/a/file");
|
||||
ASSERT_TRUE(!fs::exists(path));
|
||||
auto actual = Machine::parseConfig({}, "@" + path.string());
|
||||
ASSERT_THAT(actual, SizeIs(0));
|
||||
}
|
||||
|
||||
TEST(machines, getMachinesWithCorrectFileReferenceToIncorrectFile) {
|
||||
EXPECT_THROW(
|
||||
Machine::parseConfig({}, "@" + absPath(getUnitTestData() + "/machines/bad_format")),
|
||||
Machine::parseConfig({}, "@" + fs::weakly_canonical(getUnitTestData() / "machines" / "bad_format").string()),
|
||||
FormatError);
|
||||
}
|
||||
|
|
|
@ -13,10 +13,10 @@ using nlohmann::json;
|
|||
|
||||
class NarInfoTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/nar-info";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "nar-info";
|
||||
|
||||
Path goldenMaster(PathView testStem) const override {
|
||||
return unitTestData + "/" + testStem + ".json";
|
||||
std::filesystem::path goldenMaster(PathView testStem) const override {
|
||||
return unitTestData / (testStem + ".json");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -12,10 +12,10 @@ using nlohmann::json;
|
|||
|
||||
class PathInfoTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/path-info";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "path-info";
|
||||
|
||||
Path goldenMaster(PathView testStem) const override {
|
||||
return unitTestData + "/" + testStem + ".json";
|
||||
std::filesystem::path goldenMaster(PathView testStem) const override {
|
||||
return unitTestData / (testStem + ".json");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -13,11 +13,11 @@ using nlohmann::json;
|
|||
|
||||
class StoreReferenceTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/store-reference";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "store-reference";
|
||||
|
||||
Path goldenMaster(PathView testStem) const override
|
||||
std::filesystem::path goldenMaster(PathView testStem) const override
|
||||
{
|
||||
return unitTestData + "/" + testStem + ".txt";
|
||||
return unitTestData / (testStem + ".txt");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace nix {
|
|||
* The path to the unit test data directory. See the contributing guide
|
||||
* in the manual for further details.
|
||||
*/
|
||||
static inline Path getUnitTestData() {
|
||||
static inline std::filesystem::path getUnitTestData() {
|
||||
return getEnv("_NIX_TEST_UNIT_DATA").value();
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ protected:
|
|||
* While the "golden master" for this characterization test is
|
||||
* located. It should not be shared with any other test.
|
||||
*/
|
||||
virtual Path goldenMaster(PathView testStem) const = 0;
|
||||
virtual std::filesystem::path goldenMaster(PathView testStem) const = 0;
|
||||
|
||||
public:
|
||||
/**
|
||||
|
@ -77,7 +77,7 @@ public:
|
|||
|
||||
if (testAccept())
|
||||
{
|
||||
createDirs(dirOf(file));
|
||||
std::filesystem::create_directories(file.parent_path());
|
||||
writeFile2(file, got);
|
||||
GTEST_SKIP()
|
||||
<< "Updating golden master "
|
||||
|
@ -97,10 +97,10 @@ public:
|
|||
{
|
||||
writeTest(
|
||||
testStem, test,
|
||||
[](const Path & f) -> std::string {
|
||||
[](const std::filesystem::path & f) -> std::string {
|
||||
return readFile(f);
|
||||
},
|
||||
[](const Path & f, const std::string & c) {
|
||||
[](const std::filesystem::path & f, const std::string & c) {
|
||||
return writeFile(f, c);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -11,12 +11,12 @@ using namespace git;
|
|||
|
||||
class GitTest : public CharacterizationTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/git";
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "git";
|
||||
|
||||
public:
|
||||
|
||||
Path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData + "/" + testStem;
|
||||
std::filesystem::path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData / std::string(testStem);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -48,12 +48,14 @@ subdir('build-utils-meson/diagnostics')
|
|||
sources = files(
|
||||
'args.cc',
|
||||
'canon-path.cc',
|
||||
'checked-arithmetic.cc',
|
||||
'chunked-vector.cc',
|
||||
'closure.cc',
|
||||
'compression.cc',
|
||||
'config.cc',
|
||||
'executable-path.cc',
|
||||
'file-content-address.cc',
|
||||
'file-system.cc',
|
||||
'git.cc',
|
||||
'hash.cc',
|
||||
'hilite.cc',
|
||||
|
@ -62,6 +64,7 @@ sources = files(
|
|||
'lru-cache.cc',
|
||||
'nix_api_util.cc',
|
||||
'pool.cc',
|
||||
'position.cc',
|
||||
'processes.cc',
|
||||
'references.cc',
|
||||
'spawn.cc',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue