diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-29 15:06:01 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-08-02 09:27:02 +0300 |
commit | 1e5e0c9bde504598c1203763c742e0f0743acfa1 (patch) | |
tree | 77d02c32f4ec28046083fcfda9701f8878654bb2 | |
parent | 6937fda9ca556e7efb56c83019045c1dfdb794e1 (diff) |
gittest: Create test data at runtime in the repository suite
The shared repository suite that is used to verify both the localrepo
and remoterepo implementations currently uses a toggle to tell the
implementation-specific setup function whether the repository should be
seeded or not. This doesn't make a whole lot of sense: setup of the test
data really should be handled by the suite itself.
Refactor the code and create the test data at runtime. This also enables
us to easily test against SHA256.
-rw-r--r-- | internal/git/gittest/repository_suite.go | 44 | ||||
-rw-r--r-- | internal/git/localrepo/repo_test.go | 15 | ||||
-rw-r--r-- | internal/git/remoterepo/repository_test.go | 13 |
3 files changed, 27 insertions, 45 deletions
diff --git a/internal/git/gittest/repository_suite.go b/internal/git/gittest/repository_suite.go index 676e17564..505279598 100644 --- a/internal/git/gittest/repository_suite.go +++ b/internal/git/gittest/repository_suite.go @@ -13,7 +13,7 @@ import ( // GetRepositoryFunc is used to get a clean test repository for the different implementations of the // Repository interface in the common test suite TestRepository. -type GetRepositoryFunc func(ctx context.Context, t testing.TB, seeded bool) (git.Repository, string) +type GetRepositoryFunc func(ctx context.Context, t testing.TB) (git.Repository, string) // TestRepository tests an implementation of Repository. func TestRepository(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFunc) { @@ -43,6 +43,12 @@ func TestRepository(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFun func testRepositoryResolveRevision(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFunc) { ctx := testhelper.Context(t) + repo, repoPath := getRepository(ctx, t) + + firstParentCommitID := WriteCommit(t, cfg, repoPath, WithMessage("first parent")) + secondParentCommitID := WriteCommit(t, cfg, repoPath, WithMessage("second parent")) + masterCommitID := WriteCommit(t, cfg, repoPath, WithBranch("master"), WithParents(firstParentCommitID, secondParentCommitID)) + for _, tc := range []struct { desc string revision string @@ -51,22 +57,22 @@ func testRepositoryResolveRevision(t *testing.T, cfg config.Cfg, getRepository G { desc: "unqualified master branch", revision: "master", - expected: "1e292f8fedd741b75372e19097c76d327140c312", + expected: masterCommitID, }, { desc: "fully qualified master branch", revision: "refs/heads/master", - expected: "1e292f8fedd741b75372e19097c76d327140c312", + expected: masterCommitID, }, { desc: "typed commit", revision: "refs/heads/master^{commit}", - expected: "1e292f8fedd741b75372e19097c76d327140c312", + expected: masterCommitID, }, { desc: "extended SHA notation", revision: "refs/heads/master^2", - expected: "c1c67abbaf91f624347bb3ae96eabe3a1b742478", + expected: secondParentCommitID, }, { desc: "nonexistent branch", @@ -78,7 +84,6 @@ func testRepositoryResolveRevision(t *testing.T, cfg config.Cfg, getRepository G }, } { t.Run(tc.desc, func(t *testing.T) { - repo, _ := getRepository(ctx, t, true) oid, err := repo.ResolveRevision(ctx, git.Revision(tc.revision)) if tc.expected == "" { require.Equal(t, err, git.ErrReferenceNotFound) @@ -94,7 +99,7 @@ func testRepositoryResolveRevision(t *testing.T, cfg config.Cfg, getRepository G func testRepositoryHasBranches(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFunc) { ctx := testhelper.Context(t) - repo, repoPath := getRepository(ctx, t, false) + repo, repoPath := getRepository(ctx, t) emptyCommit := text.ChompBytes(Exec(t, cfg, "-C", repoPath, "commit-tree", DefaultObjectHash.EmptyTreeOID.String())) @@ -112,7 +117,6 @@ func testRepositoryHasBranches(t *testing.T, cfg config.Cfg, getRepository GetRe } func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository GetRepositoryFunc) { - const testOID = "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863" ctx := testhelper.Context(t) for _, tc := range []struct { @@ -123,7 +127,7 @@ func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository { desc: "default ref", repo: func(t *testing.T) git.Repository { - repo, repoPath := getRepository(ctx, t, false) + repo, repoPath := getRepository(ctx, t) oid := WriteCommit(t, cfg, repoPath, WithBranch("apple")) WriteCommit(t, cfg, repoPath, WithParents(oid), WithBranch("main")) return repo @@ -133,7 +137,7 @@ func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository { desc: "legacy default ref", repo: func(t *testing.T) git.Repository { - repo, repoPath := getRepository(ctx, t, false) + repo, repoPath := getRepository(ctx, t) oid := WriteCommit(t, cfg, repoPath, WithBranch("apple")) WriteCommit(t, cfg, repoPath, WithParents(oid), WithBranch("master")) return repo @@ -143,14 +147,14 @@ func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository { desc: "no branches", repo: func(t *testing.T) git.Repository { - repo, _ := getRepository(ctx, t, false) + repo, _ := getRepository(ctx, t) return repo }, }, { desc: "one branch", repo: func(t *testing.T) git.Repository { - repo, repoPath := getRepository(ctx, t, false) + repo, repoPath := getRepository(ctx, t) WriteCommit(t, cfg, repoPath, WithBranch("apple")) return repo }, @@ -159,7 +163,7 @@ func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository { desc: "no default branches", repo: func(t *testing.T) git.Repository { - repo, repoPath := getRepository(ctx, t, false) + repo, repoPath := getRepository(ctx, t) oid := WriteCommit(t, cfg, repoPath, WithBranch("apple")) WriteCommit(t, cfg, repoPath, WithParents(oid), WithBranch("banana")) return repo @@ -167,19 +171,13 @@ func testRepositoryGetDefaultBranch(t *testing.T, cfg config.Cfg, getRepository expectedName: git.NewReferenceNameFromBranchName("apple"), }, { - desc: "test repo default", - repo: func(t *testing.T) git.Repository { - repo, _ := getRepository(ctx, t, true) - return repo - }, - expectedName: git.LegacyDefaultRef, - }, - { desc: "test repo HEAD set", repo: func(t *testing.T) git.Repository { - repo, repoPath := getRepository(ctx, t, true) - Exec(t, cfg, "-C", repoPath, "update-ref", "refs/heads/feature", testOID) + repo, repoPath := getRepository(ctx, t) + + WriteCommit(t, cfg, repoPath, WithBranch("feature")) Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/feature") + return repo }, expectedName: git.NewReferenceNameFromBranchName("feature"), diff --git a/internal/git/localrepo/repo_test.go b/internal/git/localrepo/repo_test.go index 8d68dc821..bdfa53ed7 100644 --- a/internal/git/localrepo/repo_test.go +++ b/internal/git/localrepo/repo_test.go @@ -24,24 +24,15 @@ import ( func TestRepo(t *testing.T) { cfg := testcfg.Build(t) - gittest.TestRepository(t, cfg, func(ctx context.Context, t testing.TB, seeded bool) (git.Repository, string) { + gittest.TestRepository(t, cfg, func(ctx context.Context, t testing.TB) (git.Repository, string) { t.Helper() - var ( - pbRepo *gitalypb.Repository - repoPath string - ) - - if seeded { - pbRepo, repoPath = gittest.CloneRepo(t, cfg, cfg.Storages[0]) - } else { - pbRepo, repoPath = gittest.InitRepo(t, cfg, cfg.Storages[0]) - } + repoProto, repoPath := gittest.InitRepo(t, cfg, cfg.Storages[0]) gitCmdFactory := gittest.NewCommandFactory(t, cfg) catfileCache := catfile.NewCache(cfg) t.Cleanup(catfileCache.Stop) - return New(config.NewLocator(cfg), gitCmdFactory, catfileCache, pbRepo), repoPath + return New(config.NewLocator(cfg), gitCmdFactory, catfileCache, repoProto), repoPath }) } diff --git a/internal/git/remoterepo/repository_test.go b/internal/git/remoterepo/repository_test.go index d99af35f3..7057718bb 100644 --- a/internal/git/remoterepo/repository_test.go +++ b/internal/git/remoterepo/repository_test.go @@ -55,22 +55,15 @@ func TestRepository(t *testing.T) { pool := client.NewPool() defer pool.Close() - gittest.TestRepository(t, cfg, func(ctx context.Context, t testing.TB, seeded bool) (git.Repository, string) { + gittest.TestRepository(t, cfg, func(ctx context.Context, t testing.TB) (git.Repository, string) { t.Helper() - seed := "" - if seeded { - seed = gittest.SeedGitLabTest - } - ctx, err := storage.InjectGitalyServers(ctx, "default", cfg.SocketPath, cfg.Auth.Token) require.NoError(t, err) - pbRepo, repoPath := gittest.CreateRepository(ctx, t, cfg, gittest.CreateRepositoryConfig{ - Seed: seed, - }) + repoProto, repoPath := gittest.CreateRepository(ctx, t, cfg) - repo, err := remoterepo.New(metadata.OutgoingToIncoming(ctx), pbRepo, pool) + repo, err := remoterepo.New(metadata.OutgoingToIncoming(ctx), repoProto, pool) require.NoError(t, err) return repo, repoPath }) |