|
| 1 | +#include "git-utils.hh" |
| 2 | +#include "file-system.hh" |
| 3 | +#include "gmock/gmock.h" |
| 4 | +#include <git2/global.h> |
| 5 | +#include <git2/repository.h> |
| 6 | +#include <git2/types.h> |
| 7 | +#include <gtest/gtest.h> |
| 8 | +#include "fs-sink.hh" |
| 9 | +#include "serialise.hh" |
| 10 | + |
| 11 | +namespace nix { |
| 12 | + |
| 13 | +class GitUtilsTest : public ::testing::Test |
| 14 | +{ |
| 15 | + // We use a single repository for all tests. |
| 16 | + Path tmpDir; |
| 17 | + std::unique_ptr<AutoDelete> delTmpDir; |
| 18 | + |
| 19 | +public: |
| 20 | + void SetUp() override |
| 21 | + { |
| 22 | + tmpDir = createTempDir(); |
| 23 | + delTmpDir = std::make_unique<AutoDelete>(tmpDir, true); |
| 24 | + |
| 25 | + // Create the repo with libgit2 |
| 26 | + git_libgit2_init(); |
| 27 | + git_repository * repo = nullptr; |
| 28 | + auto r = git_repository_init(&repo, tmpDir.c_str(), 0); |
| 29 | + ASSERT_EQ(r, 0); |
| 30 | + git_repository_free(repo); |
| 31 | + } |
| 32 | + |
| 33 | + void TearDown() override |
| 34 | + { |
| 35 | + // Destroy the AutoDelete, triggering removal |
| 36 | + // not AutoDelete::reset(), which would cancel the deletion. |
| 37 | + delTmpDir.reset(); |
| 38 | + } |
| 39 | + |
| 40 | + ref<GitRepo> openRepo() |
| 41 | + { |
| 42 | + return GitRepo::openRepo(tmpDir, true, false); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +void writeString(CreateRegularFileSink & fileSink, std::string contents, bool executable) |
| 47 | +{ |
| 48 | + if (executable) |
| 49 | + fileSink.isExecutable(); |
| 50 | + fileSink.preallocateContents(contents.size()); |
| 51 | + fileSink(contents); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_F(GitUtilsTest, sink_basic) |
| 55 | +{ |
| 56 | + auto repo = openRepo(); |
| 57 | + auto sink = repo->getFileSystemObjectSink(); |
| 58 | + |
| 59 | + // TODO/Question: It seems a little odd that we use the tarball-like convention of requiring a top-level directory |
| 60 | + // here |
| 61 | + // The sync method does not document this behavior, should probably renamed because it's not very |
| 62 | + // general, and I can't imagine that "non-conventional" archives or any other source to be handled by |
| 63 | + // this sink. |
| 64 | + |
| 65 | + sink->createDirectory(CanonPath("foo-1.1")); |
| 66 | + |
| 67 | + sink->createRegularFile(CanonPath("foo-1.1/hello"), [](CreateRegularFileSink & fileSink) { |
| 68 | + writeString(fileSink, "hello world", false); |
| 69 | + }); |
| 70 | + sink->createRegularFile(CanonPath("foo-1.1/bye"), [](CreateRegularFileSink & fileSink) { |
| 71 | + writeString(fileSink, "thanks for all the fish", false); |
| 72 | + }); |
| 73 | + sink->createSymlink(CanonPath("foo-1.1/bye-link"), "bye"); |
| 74 | + sink->createDirectory(CanonPath("foo-1.1/empty")); |
| 75 | + sink->createDirectory(CanonPath("foo-1.1/links")); |
| 76 | + sink->createHardlink(CanonPath("foo-1.1/links/foo"), CanonPath("foo-1.1/hello")); |
| 77 | + |
| 78 | + // sink->createHardlink("foo-1.1/links/foo-2", CanonPath("foo-1.1/hello")); |
| 79 | + |
| 80 | + auto result = sink->sync(); |
| 81 | + auto accessor = repo->getAccessor(result, false); |
| 82 | + auto entries = accessor->readDirectory(CanonPath::root); |
| 83 | + ASSERT_EQ(entries.size(), 5); |
| 84 | + ASSERT_EQ(accessor->readFile(CanonPath("hello")), "hello world"); |
| 85 | + ASSERT_EQ(accessor->readFile(CanonPath("bye")), "thanks for all the fish"); |
| 86 | + ASSERT_EQ(accessor->readLink(CanonPath("bye-link")), "bye"); |
| 87 | + ASSERT_EQ(accessor->readDirectory(CanonPath("empty")).size(), 0); |
| 88 | + ASSERT_EQ(accessor->readFile(CanonPath("links/foo")), "hello world"); |
| 89 | +}; |
| 90 | + |
| 91 | +TEST_F(GitUtilsTest, sink_hardlink) |
| 92 | +{ |
| 93 | + auto repo = openRepo(); |
| 94 | + auto sink = repo->getFileSystemObjectSink(); |
| 95 | + |
| 96 | + sink->createDirectory(CanonPath("foo-1.1")); |
| 97 | + |
| 98 | + sink->createRegularFile(CanonPath("foo-1.1/hello"), [](CreateRegularFileSink & fileSink) { |
| 99 | + writeString(fileSink, "hello world", false); |
| 100 | + }); |
| 101 | + |
| 102 | + try { |
| 103 | + sink->createHardlink(CanonPath("foo-1.1/link"), CanonPath("hello")); |
| 104 | + FAIL() << "Expected an exception"; |
| 105 | + } catch (const nix::Error & e) { |
| 106 | + ASSERT_THAT(e.msg(), testing::HasSubstr("invalid hard link target")); |
| 107 | + ASSERT_THAT(e.msg(), testing::HasSubstr("/hello")); |
| 108 | + ASSERT_THAT(e.msg(), testing::HasSubstr("foo-1.1/link")); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +} // namespace nix |
0 commit comments