diff --git a/src/libfetchers-tests/git-utils.cc b/src/libfetchers-tests/git-utils.cc index 10e98141f..51622a955 100644 --- a/src/libfetchers-tests/git-utils.cc +++ b/src/libfetchers-tests/git-utils.cc @@ -11,10 +11,14 @@ namespace nix { +namespace fs { +using namespace std::filesystem; +} + class GitUtilsTest : public ::testing::Test { // We use a single repository for all tests. - Path tmpDir; + fs::path tmpDir; std::unique_ptr delTmpDir; public: @@ -42,6 +46,11 @@ public: { return GitRepo::openRepo(tmpDir, true, false); } + + std::string getRepoName() const + { + return tmpDir.filename(); + } }; void writeString(CreateRegularFileSink & fileSink, std::string contents, bool executable) @@ -79,7 +88,7 @@ TEST_F(GitUtilsTest, sink_basic) // sink->createHardlink("foo-1.1/links/foo-2", CanonPath("foo-1.1/hello")); auto result = repo->dereferenceSingletonDirectory(sink->flush()); - auto accessor = repo->getAccessor(result, false); + auto accessor = repo->getAccessor(result, false, getRepoName()); auto entries = accessor->readDirectory(CanonPath::root); ASSERT_EQ(entries.size(), 5); ASSERT_EQ(accessor->readFile(CanonPath("hello")), "hello world"); @@ -110,131 +119,4 @@ TEST_F(GitUtilsTest, sink_hardlink) } }; -namespace lfs { - -TEST_F(GitUtilsTest, parseGitRemoteUrl) -{ - { - GitUrl result = parseGitUrl("git@example.com:path/repo.git"); - EXPECT_EQ(result.protocol, "ssh"); - EXPECT_EQ(result.user, "git"); - EXPECT_EQ(result.host, "example.com"); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, "path/repo.git"); - } - - { - GitUrl result = parseGitUrl("example.com:/path/repo.git"); - EXPECT_EQ(result.protocol, "ssh"); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, "example.com"); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, "/path/repo.git"); - } - - { - GitUrl result = parseGitUrl("example.com:path/repo.git"); - EXPECT_EQ(result.protocol, "ssh"); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, "example.com"); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, "path/repo.git"); - } - - { - GitUrl result = parseGitUrl("https://example.com/path/repo.git"); - EXPECT_EQ(result.protocol, "https"); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, "example.com"); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, "path/repo.git"); - } - - { - GitUrl result = parseGitUrl("ssh://git@example.com/path/repo.git"); - EXPECT_EQ(result.protocol, "ssh"); - EXPECT_EQ(result.user, "git"); - EXPECT_EQ(result.host, "example.com"); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, "path/repo.git"); - } - - { - GitUrl result = parseGitUrl("ssh://example/path/repo.git"); - EXPECT_EQ(result.protocol, "ssh"); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, "example"); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, "path/repo.git"); - } - - { - GitUrl result = parseGitUrl("http://example.com:8080/path/repo.git"); - EXPECT_EQ(result.protocol, "http"); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, "example.com"); - EXPECT_EQ(result.port, "8080"); - EXPECT_EQ(result.path, "path/repo.git"); - } - - { - GitUrl result = parseGitUrl("invalid-url"); - EXPECT_EQ(result.protocol, ""); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, ""); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, ""); - } - - { - GitUrl result = parseGitUrl(""); - EXPECT_EQ(result.protocol, ""); - EXPECT_EQ(result.user, ""); - EXPECT_EQ(result.host, ""); - EXPECT_EQ(result.port, ""); - EXPECT_EQ(result.path, ""); - } -} -TEST_F(GitUtilsTest, gitUrlToHttp) -{ - { - const GitUrl url = parseGitUrl("git@github.com:user/repo.git"); - EXPECT_EQ(url.toHttp(), "https://github.com/user/repo.git"); - } - { - const GitUrl url = parseGitUrl("https://github.com/user/repo.git"); - EXPECT_EQ(url.toHttp(), "https://github.com/user/repo.git"); - } - { - const GitUrl url = parseGitUrl("http://github.com/user/repo.git"); - EXPECT_EQ(url.toHttp(), "http://github.com/user/repo.git"); - } - { - const GitUrl url = parseGitUrl("ssh://git@github.com:22/user/repo.git"); - EXPECT_EQ(url.toHttp(), "https://github.com:22/user/repo.git"); - } - { - const GitUrl url = parseGitUrl("invalid-url"); - EXPECT_EQ(url.toHttp(), ""); - } -} - -TEST_F(GitUtilsTest, gitUrlToSsh) -{ - { - const GitUrl url = parseGitUrl("https://example.com/user/repo.git"); - const auto [host, path] = url.toSsh(); - EXPECT_EQ(host, "example.com"); - EXPECT_EQ(path, "user/repo.git"); - } - { - const GitUrl url = parseGitUrl("git@example.com:user/repo.git"); - const auto [host, path] = url.toSsh(); - EXPECT_EQ(host, "git@example.com"); - EXPECT_EQ(path, "user/repo.git"); - } -} - -} // namespace lfs - } // namespace nix diff --git a/src/libfetchers-tests/meson.build b/src/libfetchers-tests/meson.build index 739435501..11a65a66a 100644 --- a/src/libfetchers-tests/meson.build +++ b/src/libfetchers-tests/meson.build @@ -31,6 +31,9 @@ deps_private += rapidcheck gtest = dependency('gtest', main : true) deps_private += gtest +libgit2 = dependency('libgit2') +deps_private += libgit2 + add_project_arguments( # TODO(Qyriad): Yes this is how the autoconf+Make system did it. # It would be nice for our headers to be idempotent instead. @@ -44,6 +47,7 @@ subdir('nix-meson-build-support/common') sources = files( 'public-key.cc', + 'git-utils.cc' ) include_dirs = [include_directories('.')] diff --git a/src/libfetchers-tests/package.nix b/src/libfetchers-tests/package.nix index 1e379fc5a..6e3581183 100644 --- a/src/libfetchers-tests/package.nix +++ b/src/libfetchers-tests/package.nix @@ -7,6 +7,7 @@ nix-fetchers, nix-store-test-support, + libgit2, rapidcheck, gtest, runCommand, @@ -42,6 +43,7 @@ mkMesonExecutable (finalAttrs: { nix-store-test-support rapidcheck gtest + libgit2 ]; mesonFlags = [