2022-05-04 12:32:21 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-11-04 19:35:38 +00:00
|
|
|
#include "git.hh"
|
|
|
|
#include "memory-source-accessor.hh"
|
|
|
|
|
|
|
|
#include "tests/characterization.hh"
|
|
|
|
|
2022-05-04 12:32:21 +00:00
|
|
|
namespace nix {
|
|
|
|
|
2023-11-04 19:35:38 +00:00
|
|
|
using namespace git;
|
|
|
|
|
|
|
|
class GitTest : public CharacterizationTest
|
|
|
|
{
|
2023-08-25 14:20:28 +00:00
|
|
|
Path unitTestData = getUnitTestData() + "/git";
|
2022-05-04 12:32:21 +00:00
|
|
|
|
2023-11-04 19:35:38 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
Path goldenMaster(std::string_view testStem) const override {
|
|
|
|
return unitTestData + "/" + testStem;
|
2022-05-04 12:32:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 19:35:38 +00:00
|
|
|
/**
|
|
|
|
* We set these in tests rather than the regular globals so we don't have
|
|
|
|
* to worry about race conditions if the tests run concurrently.
|
|
|
|
*/
|
|
|
|
ExperimentalFeatureSettings mockXpSettings;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
mockXpSettings.set("experimental-features", "git-hashing");
|
2022-05-04 12:32:21 +00:00
|
|
|
}
|
2023-11-04 19:35:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(GitMode, gitMode_directory) {
|
|
|
|
Mode m = Mode::Directory;
|
|
|
|
RawMode r = 0040000;
|
|
|
|
ASSERT_EQ(static_cast<RawMode>(m), r);
|
|
|
|
ASSERT_EQ(decodeMode(r), std::optional { m });
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(GitMode, gitMode_executable) {
|
|
|
|
Mode m = Mode::Executable;
|
|
|
|
RawMode r = 0100755;
|
|
|
|
ASSERT_EQ(static_cast<RawMode>(m), r);
|
|
|
|
ASSERT_EQ(decodeMode(r), std::optional { m });
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(GitMode, gitMode_regular) {
|
|
|
|
Mode m = Mode::Regular;
|
|
|
|
RawMode r = 0100644;
|
|
|
|
ASSERT_EQ(static_cast<RawMode>(m), r);
|
|
|
|
ASSERT_EQ(decodeMode(r), std::optional { m });
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(GitMode, gitMode_symlink) {
|
|
|
|
Mode m = Mode::Symlink;
|
|
|
|
RawMode r = 0120000;
|
|
|
|
ASSERT_EQ(static_cast<RawMode>(m), r);
|
|
|
|
ASSERT_EQ(decodeMode(r), std::optional { m });
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(GitTest, blob_read) {
|
|
|
|
readTest("hello-world-blob.bin", [&](const auto & encoded) {
|
|
|
|
StringSource in { encoded };
|
|
|
|
StringSink out;
|
|
|
|
RegularFileSink out2 { out };
|
2023-12-20 19:47:05 +00:00
|
|
|
ASSERT_EQ(parseObjectType(in, mockXpSettings), ObjectType::Blob);
|
|
|
|
parseBlob(out2, "", in, false, mockXpSettings);
|
2023-11-04 19:35:38 +00:00
|
|
|
|
|
|
|
auto expected = readFile(goldenMaster("hello-world.bin"));
|
|
|
|
|
|
|
|
ASSERT_EQ(out.s, expected);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GitTest, blob_write) {
|
|
|
|
writeTest("hello-world-blob.bin", [&]() {
|
|
|
|
auto decoded = readFile(goldenMaster("hello-world.bin"));
|
|
|
|
StringSink s;
|
|
|
|
dumpBlobPrefix(decoded.size(), s, mockXpSettings);
|
|
|
|
s(decoded);
|
|
|
|
return s.s;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This data is for "shallow" tree tests. However, we use "real" hashes
|
2023-08-25 14:20:28 +00:00
|
|
|
* so that we can check our test data in a small shell script test test
|
|
|
|
* (`tests/unit/libutil/data/git/check-data.sh`).
|
2023-11-04 19:35:38 +00:00
|
|
|
*/
|
|
|
|
const static Tree tree = {
|
|
|
|
{
|
|
|
|
"Foo",
|
|
|
|
{
|
|
|
|
.mode = Mode::Regular,
|
|
|
|
// hello world with special chars from above
|
2023-11-28 13:20:27 +00:00
|
|
|
.hash = Hash::parseAny("63ddb340119baf8492d2da53af47e8c7cfcd5eb2", HashAlgorithm::SHA1),
|
2023-11-04 19:35:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bAr",
|
|
|
|
{
|
|
|
|
.mode = Mode::Executable,
|
|
|
|
// ditto
|
2023-11-28 13:20:27 +00:00
|
|
|
.hash = Hash::parseAny("63ddb340119baf8492d2da53af47e8c7cfcd5eb2", HashAlgorithm::SHA1),
|
2023-11-04 19:35:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"baZ/",
|
|
|
|
{
|
|
|
|
.mode = Mode::Directory,
|
|
|
|
// Empty directory hash
|
2023-11-28 13:20:27 +00:00
|
|
|
.hash = Hash::parseAny("4b825dc642cb6eb9a060e54bf8d69288fbee4904", HashAlgorithm::SHA1),
|
2023-11-04 19:35:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(GitTest, tree_read) {
|
|
|
|
readTest("tree.bin", [&](const auto & encoded) {
|
|
|
|
StringSource in { encoded };
|
2024-01-22 22:59:34 +00:00
|
|
|
NullFileSystemObjectSink out;
|
2023-11-04 19:35:38 +00:00
|
|
|
Tree got;
|
2023-12-20 19:47:05 +00:00
|
|
|
ASSERT_EQ(parseObjectType(in, mockXpSettings), ObjectType::Tree);
|
|
|
|
parseTree(out, "", in, [&](auto & name, auto entry) {
|
2023-11-04 19:35:38 +00:00
|
|
|
auto name2 = name;
|
|
|
|
if (entry.mode == Mode::Directory)
|
|
|
|
name2 += '/';
|
|
|
|
got.insert_or_assign(name2, std::move(entry));
|
|
|
|
}, mockXpSettings);
|
|
|
|
|
|
|
|
ASSERT_EQ(got, tree);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GitTest, tree_write) {
|
|
|
|
writeTest("tree.bin", [&]() {
|
|
|
|
StringSink s;
|
|
|
|
dumpTree(tree, s, mockXpSettings);
|
|
|
|
return s.s;
|
|
|
|
});
|
2022-05-04 12:32:21 +00:00
|
|
|
}
|
|
|
|
|
2023-11-04 19:35:38 +00:00
|
|
|
TEST_F(GitTest, both_roundrip) {
|
|
|
|
using File = MemorySourceAccessor::File;
|
|
|
|
|
|
|
|
MemorySourceAccessor files;
|
|
|
|
files.root = File::Directory {
|
|
|
|
.contents {
|
|
|
|
{
|
|
|
|
"foo",
|
|
|
|
File::Regular {
|
|
|
|
.contents = "hello\n\0\n\tworld!",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bar",
|
|
|
|
File::Directory {
|
|
|
|
.contents = {
|
|
|
|
{
|
|
|
|
"baz",
|
|
|
|
File::Regular {
|
|
|
|
.executable = true,
|
|
|
|
.contents = "good day,\n\0\n\tworld!",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<Hash, std::string> cas;
|
|
|
|
|
|
|
|
std::function<DumpHook> dumpHook;
|
|
|
|
dumpHook = [&](const CanonPath & path) {
|
|
|
|
StringSink s;
|
2023-11-28 13:20:27 +00:00
|
|
|
HashSink hashSink { HashAlgorithm::SHA1 };
|
2023-11-04 19:35:38 +00:00
|
|
|
TeeSink s2 { s, hashSink };
|
|
|
|
auto mode = dump(
|
|
|
|
files, path, s2, dumpHook,
|
|
|
|
defaultPathFilter, mockXpSettings);
|
|
|
|
auto hash = hashSink.finish().first;
|
|
|
|
cas.insert_or_assign(hash, std::move(s.s));
|
|
|
|
return TreeEntry {
|
|
|
|
.mode = mode,
|
|
|
|
.hash = hash,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
auto root = dumpHook(CanonPath::root);
|
|
|
|
|
|
|
|
MemorySourceAccessor files2;
|
|
|
|
|
|
|
|
MemorySink sinkFiles2 { files2 };
|
|
|
|
|
2023-12-20 19:47:05 +00:00
|
|
|
std::function<void(const Path, const Hash &, bool)> mkSinkHook;
|
|
|
|
mkSinkHook = [&](auto prefix, auto & hash, auto executable) {
|
2023-11-04 19:35:38 +00:00
|
|
|
StringSource in { cas[hash] };
|
2023-12-20 19:47:05 +00:00
|
|
|
parse(
|
|
|
|
sinkFiles2, prefix, in, executable,
|
|
|
|
[&](const Path & name, const auto & entry) {
|
|
|
|
mkSinkHook(
|
|
|
|
prefix + "/" + name,
|
|
|
|
entry.hash,
|
|
|
|
entry.mode == Mode::Executable);
|
|
|
|
},
|
|
|
|
mockXpSettings);
|
2023-11-04 19:35:38 +00:00
|
|
|
};
|
|
|
|
|
2023-12-20 19:47:05 +00:00
|
|
|
mkSinkHook("", root.hash, false);
|
2023-11-04 19:35:38 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(files, files2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GitLsRemote, parseSymrefLineWithReference) {
|
|
|
|
auto line = "ref: refs/head/main HEAD";
|
|
|
|
auto res = parseLsRemoteLine(line);
|
|
|
|
ASSERT_TRUE(res.has_value());
|
|
|
|
ASSERT_EQ(res->kind, LsRemoteRefLine::Kind::Symbolic);
|
|
|
|
ASSERT_EQ(res->target, "refs/head/main");
|
|
|
|
ASSERT_EQ(res->reference, "HEAD");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GitLsRemote, parseSymrefLineWithNoReference) {
|
|
|
|
auto line = "ref: refs/head/main";
|
|
|
|
auto res = parseLsRemoteLine(line);
|
|
|
|
ASSERT_TRUE(res.has_value());
|
|
|
|
ASSERT_EQ(res->kind, LsRemoteRefLine::Kind::Symbolic);
|
|
|
|
ASSERT_EQ(res->target, "refs/head/main");
|
|
|
|
ASSERT_EQ(res->reference, std::nullopt);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GitLsRemote, parseObjectRefLine) {
|
|
|
|
auto line = "abc123 refs/head/main";
|
|
|
|
auto res = parseLsRemoteLine(line);
|
|
|
|
ASSERT_TRUE(res.has_value());
|
|
|
|
ASSERT_EQ(res->kind, LsRemoteRefLine::Kind::Object);
|
|
|
|
ASSERT_EQ(res->target, "abc123");
|
|
|
|
ASSERT_EQ(res->reference, "refs/head/main");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|