diff options
author | John Cai <jcai@gitlab.com> | 2022-05-04 23:49:37 +0300 |
---|---|---|
committer | John Cai <jcai@gitlab.com> | 2022-05-04 23:49:37 +0300 |
commit | 372599313791cb92e579e0ff02279f33cbcd71b5 (patch) | |
tree | a2f40ff53b8bf8c811c07ecab1549adceeed378a | |
parent | 9b6153d614bd9b9e70625697ca0e74c7c5f0686d (diff) | |
parent | 38320ca5545d2f224e3010ae57a1b99b2938ef13 (diff) |
Merge branch 'pks-services-reduce-usage-of-worktrees' into 'master'
tests: Reduce usage of Git worktrees in service tests
See merge request gitlab-org/gitaly!4519
28 files changed, 250 insertions, 240 deletions
diff --git a/internal/git/gittest/commit.go b/internal/git/gittest/commit.go index 50ab3aa2c..712a7b351 100644 --- a/internal/git/gittest/commit.go +++ b/internal/git/gittest/commit.go @@ -25,6 +25,7 @@ type writeCommitConfig struct { committerName string message string treeEntries []TreeEntry + treeID git.ObjectID alternateObjectDir string } @@ -67,6 +68,15 @@ func WithTreeEntries(entries ...TreeEntry) WriteCommitOption { } } +// WithTree is an option for WriteCommit which will cause it to use the given object ID as the root +// tree of the resulting commit. +// as root tree of the resulting commit. +func WithTree(treeID git.ObjectID) WriteCommitOption { + return func(cfg *writeCommitConfig) { + cfg.treeID = treeID + } +} + // WithCommitterName is an option for WriteCommit which will set the committer name. func WithCommitterName(name string) WriteCommitOption { return func(cfg *writeCommitConfig) { @@ -104,9 +114,15 @@ func WriteCommit(t testing.TB, cfg config.Cfg, repoPath string, opts ...WriteCom parents = writeCommitConfig.parents } + if len(writeCommitConfig.treeEntries) > 0 && writeCommitConfig.treeID != "" { + require.FailNow(t, "cannot set tree entries and tree ID at the same time") + } + var tree string if len(writeCommitConfig.treeEntries) > 0 { tree = WriteTree(t, cfg, repoPath, writeCommitConfig.treeEntries).String() + } else if writeCommitConfig.treeID != "" { + tree = writeCommitConfig.treeID.String() } else if len(parents) == 0 { // If there are no parents, then we set the root tree to the empty tree. tree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" diff --git a/internal/git/gittest/commit_test.go b/internal/git/gittest/commit_test.go index 3494ad382..3b19127c4 100644 --- a/internal/git/gittest/commit_test.go +++ b/internal/git/gittest/commit_test.go @@ -153,6 +153,35 @@ func TestWriteCommit(t *testing.T) { }, }, }, + { + desc: "with tree", + opts: []gittest.WriteCommitOption{ + gittest.WithTree(gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + { + Content: "something", + Mode: "100644", + Path: "file", + }, + })), + }, + expectedCommit: &gitalypb.GitCommit{ + Author: defaultCommitter, + Committer: defaultCommitter, + Subject: []byte("message"), + Body: []byte("message"), + Id: "fc157fcabd57d95752ade820a791899f9891b984", + ParentIds: []string{ + defaultParentID, + }, + }, + expectedTreeEntries: []gittest.TreeEntry{ + { + Content: "something", + Mode: "100644", + Path: "file", + }, + }, + }, } { t.Run(tc.desc, func(t *testing.T) { oid := gittest.WriteCommit(t, cfg, repoPath, tc.opts...) diff --git a/internal/gitaly/service/commit/commit_messages_test.go b/internal/gitaly/service/commit/commit_messages_test.go index 7e19673cb..43d162ecf 100644 --- a/internal/gitaly/service/commit/commit_messages_test.go +++ b/internal/gitaly/service/commit/commit_messages_test.go @@ -17,7 +17,7 @@ func TestSuccessfulGetCommitMessagesRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) message1 := strings.Repeat("a\n", helper.MaxCommitOrTagMessageSize*2) message2 := strings.Repeat("b\n", helper.MaxCommitOrTagMessageSize*2) @@ -59,7 +59,7 @@ func TestFailedGetCommitMessagesRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, _, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, _, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { desc string diff --git a/internal/gitaly/service/commit/commit_signatures_test.go b/internal/gitaly/service/commit/commit_signatures_test.go index 7481f3633..90fa72c7e 100644 --- a/internal/gitaly/service/commit/commit_signatures_test.go +++ b/internal/gitaly/service/commit/commit_signatures_test.go @@ -18,7 +18,7 @@ func TestSuccessfulGetCommitSignaturesRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) commitData := testhelper.MustReadFile(t, "testdata/dc00eb001f41dfac08192ead79c2377c588b82ee.commit") commit := text.ChompBytes(gittest.ExecOpts(t, cfg, gittest.ExecConfig{Stdin: bytes.NewReader(commitData)}, @@ -87,7 +87,7 @@ func TestFailedGetCommitSignaturesRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { desc string diff --git a/internal/gitaly/service/commit/commits_by_message_test.go b/internal/gitaly/service/commit/commits_by_message_test.go index eeab11601..1974a342a 100644 --- a/internal/gitaly/service/commit/commits_by_message_test.go +++ b/internal/gitaly/service/commit/commits_by_message_test.go @@ -38,7 +38,7 @@ func TestSuccessfulCommitsByMessageRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) commits := []*gitalypb.GitCommit{ { @@ -159,7 +159,7 @@ func TestFailedCommitsByMessageRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"} diff --git a/internal/gitaly/service/commit/count_commits_test.go b/internal/gitaly/service/commit/count_commits_test.go index 1697ea87e..085a83090 100644 --- a/internal/gitaly/service/commit/count_commits_test.go +++ b/internal/gitaly/service/commit/count_commits_test.go @@ -17,7 +17,7 @@ func TestSuccessfulCountCommitsRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo1, _, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo1, _, client := setupCommitServiceWithRepo(ctx, t) repo2, repo2Path := gittest.CreateRepository(ctx, t, cfg) @@ -158,7 +158,7 @@ func TestFailedCountCommitsRequestDueToValidationError(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) revision := []byte("d42783470dc29fde2cf459eb3199ee1d7e3f3a72") diff --git a/internal/gitaly/service/commit/count_diverging_commits_test.go b/internal/gitaly/service/commit/count_diverging_commits_test.go index b90fa15bb..e882c0ec5 100644 --- a/internal/gitaly/service/commit/count_diverging_commits_test.go +++ b/internal/gitaly/service/commit/count_diverging_commits_test.go @@ -159,7 +159,7 @@ func TestFailedCountDivergentCommitsRequestDueToValidationError(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) revision := []byte("d42783470dc29fde2cf459eb3199ee1d7e3f3a72") diff --git a/internal/gitaly/service/commit/filter_shas_with_signatures_test.go b/internal/gitaly/service/commit/filter_shas_with_signatures_test.go index 785a54161..100b2a4da 100644 --- a/internal/gitaly/service/commit/filter_shas_with_signatures_test.go +++ b/internal/gitaly/service/commit/filter_shas_with_signatures_test.go @@ -13,7 +13,7 @@ func TestFilterShasWithSignaturesSuccessful(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) type testCase struct { desc string diff --git a/internal/gitaly/service/commit/find_all_commits_test.go b/internal/gitaly/service/commit/find_all_commits_test.go index 13e4cb379..8ce96855e 100644 --- a/internal/gitaly/service/commit/find_all_commits_test.go +++ b/internal/gitaly/service/commit/find_all_commits_test.go @@ -16,7 +16,7 @@ func TestSuccessfulFindAllCommitsRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repoProto, _, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repoProto, _, client := setupCommitServiceWithRepo(ctx, t) repo := localrepo.NewTestRepo(t, cfg, repoProto) refs, err := repo.GetReferences(ctx, "refs/") @@ -159,7 +159,7 @@ func TestFailedFindAllCommitsRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"} diff --git a/internal/gitaly/service/commit/find_commit_test.go b/internal/gitaly/service/commit/find_commit_test.go index 54b2ad459..4eaf718ea 100644 --- a/internal/gitaly/service/commit/find_commit_test.go +++ b/internal/gitaly/service/commit/find_commit_test.go @@ -23,7 +23,7 @@ func TestSuccessfulFindCommitRequest(t *testing.T) { windows1251Message := testhelper.MustReadFile(t, "testdata/commit-c809470461118b7bcab850f6e9a7ca97ac42f8ea-message.txt") ctx := testhelper.Context(t) - cfg, repoProto, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repoProto, repoPath, client := setupCommitServiceWithRepo(ctx, t) repo := localrepo.NewTestRepo(t, cfg, repoProto) @@ -255,7 +255,7 @@ func TestFailedFindCommitRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"} @@ -294,7 +294,7 @@ func BenchmarkFindCommitWithCache(b *testing.B) { func benchmarkFindCommit(withCache bool, b *testing.B) { ctx := testhelper.Context(b) - cfg, repo, _, client := setupCommitServiceWithRepo(ctx, b, false) + cfg, repo, _, client := setupCommitServiceWithRepo(ctx, b) // get a list of revisions gitCmdFactory := gittest.NewCommandFactory(b, cfg) @@ -332,7 +332,7 @@ func TestFindCommitWithCache(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, _, client := setupCommitServiceWithRepo(ctx, t) // get a list of revisions diff --git a/internal/gitaly/service/commit/find_commits_test.go b/internal/gitaly/service/commit/find_commits_test.go index 0252e9c45..957e21252 100644 --- a/internal/gitaly/service/commit/find_commits_test.go +++ b/internal/gitaly/service/commit/find_commits_test.go @@ -21,7 +21,7 @@ func TestFindCommitsFields(t *testing.T) { windows1251Message := testhelper.MustReadFile(t, "testdata/commit-c809470461118b7bcab850f6e9a7ca97ac42f8ea-message.txt") ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { id string @@ -177,7 +177,7 @@ func TestSuccessfulFindCommitsRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { desc string @@ -435,7 +435,7 @@ func TestSuccessfulFindCommitsRequestWithAltGitObjectDirs(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) altObjectsDir := "./alt-objects" commitID := gittest.WriteCommit(t, cfg, repoPath, @@ -482,7 +482,7 @@ func TestSuccessfulFindCommitsRequestWithAmbiguousRef(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, false) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) // These are arbitrary SHAs in the repository. The important part is // that we create a branch using one of them with a different SHA so @@ -490,7 +490,7 @@ func TestSuccessfulFindCommitsRequestWithAmbiguousRef(t *testing.T) { branchName := "1e292f8fedd741b75372e19097c76d327140c312" commitSha := "6907208d755b60ebeacb2e9dfea74c92c3449a1f" - gittest.Exec(t, cfg, "-C", repoPath, "checkout", "-b", branchName, commitSha) + gittest.Exec(t, cfg, "-C", repoPath, "branch", branchName, commitSha) request := &gitalypb.FindCommitsRequest{ Repository: repo, @@ -510,7 +510,7 @@ func TestFailureFindCommitsRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { desc string @@ -554,7 +554,7 @@ func TestFindCommitsRequestWithFollowAndOffset(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) request := &gitalypb.FindCommitsRequest{ Repository: repo, @@ -581,7 +581,7 @@ func TestFindCommitsWithExceedingOffset(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) stream, err := client.FindCommits(ctx, &gitalypb.FindCommitsRequest{ Repository: repo, diff --git a/internal/gitaly/service/commit/isancestor_test.go b/internal/gitaly/service/commit/isancestor_test.go index f850af9dd..355f5256f 100644 --- a/internal/gitaly/service/commit/isancestor_test.go +++ b/internal/gitaly/service/commit/isancestor_test.go @@ -19,7 +19,7 @@ func TestCommitIsAncestorFailure(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) queries := []struct { Request *gitalypb.CommitIsAncestorRequest @@ -79,7 +79,7 @@ func TestCommitIsAncestorSuccess(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) queries := []struct { Request *gitalypb.CommitIsAncestorRequest @@ -166,7 +166,7 @@ func TestSuccessfulIsAncestorRequestWithAltGitObjectDirs(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) parentCommitID := git.ObjectID(text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "--verify", "HEAD"))) diff --git a/internal/gitaly/service/commit/languages_test.go b/internal/gitaly/service/commit/languages_test.go index 82a676cd2..7ca44f17f 100644 --- a/internal/gitaly/service/commit/languages_test.go +++ b/internal/gitaly/service/commit/languages_test.go @@ -56,7 +56,7 @@ func TestFileCountIsZeroWhenFeatureIsDisabled(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) request := &gitalypb.CommitLanguagesRequest{ Repository: repo, @@ -87,7 +87,7 @@ func TestLanguagesEmptyRevision(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) request := &gitalypb.CommitLanguagesRequest{ Repository: repo, @@ -110,7 +110,7 @@ func TestInvalidCommitLanguagesRequestRevision(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) _, err := client.CommitLanguages(ctx, &gitalypb.CommitLanguagesRequest{ Repository: repo, @@ -123,7 +123,7 @@ func TestAmbiguousRefCommitLanguagesRequestRevision(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) // gitlab-test repo has both a branch and a tag named 'v1.1.0' // b83d6e391c22777fca1ed3012fce84f633d7fed0 refs/heads/v1.1.0 diff --git a/internal/gitaly/service/commit/last_commit_for_path_test.go b/internal/gitaly/service/commit/last_commit_for_path_test.go index fa040279c..9f61eeb39 100644 --- a/internal/gitaly/service/commit/last_commit_for_path_test.go +++ b/internal/gitaly/service/commit/last_commit_for_path_test.go @@ -15,7 +15,7 @@ func TestSuccessfulLastCommitForPathRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) commit := testhelper.GitLabTestCommit("570e7b2abdd848b95f2f578043fc23bd6f6fd24d") @@ -75,7 +75,7 @@ func TestFailedLastCommitForPathRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"} @@ -120,7 +120,7 @@ func TestSuccessfulLastCommitWithGlobCharacters(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) // This is an arbitrary blob known to exist in the test repository const blobID = "c60514b6d3d6bf4bec1030f70026e34dfbd69ad5" diff --git a/internal/gitaly/service/commit/list_all_commits_test.go b/internal/gitaly/service/commit/list_all_commits_test.go index e6ceb9463..e568436c3 100644 --- a/internal/gitaly/service/commit/list_all_commits_test.go +++ b/internal/gitaly/service/commit/list_all_commits_test.go @@ -47,7 +47,7 @@ func TestListAllCommits(t *testing.T) { }) t.Run("normal repo", func(t *testing.T) { - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) stream, err := client.ListAllCommits(ctx, &gitalypb.ListAllCommitsRequest{ Repository: repo, @@ -78,7 +78,7 @@ func TestListAllCommits(t *testing.T) { }) t.Run("pagination", func(t *testing.T) { - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) stream, err := client.ListAllCommits(ctx, &gitalypb.ListAllCommitsRequest{ Repository: repo, @@ -95,7 +95,7 @@ func TestListAllCommits(t *testing.T) { }) t.Run("quarantine directory", func(t *testing.T) { - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) quarantineDir := filepath.Join("objects", "incoming-123456") require.NoError(t, os.Mkdir(filepath.Join(repoPath, quarantineDir), 0o777)) @@ -150,7 +150,7 @@ func BenchmarkListAllCommits(b *testing.B) { b.StopTimer() ctx := testhelper.Context(b) - _, repo, _, client := setupCommitServiceWithRepo(ctx, b, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, b) b.Run("ListAllCommits", func(b *testing.B) { b.ReportAllocs() diff --git a/internal/gitaly/service/commit/list_commits_by_oid_test.go b/internal/gitaly/service/commit/list_commits_by_oid_test.go index 100f2eb22..b80904cdb 100644 --- a/internal/gitaly/service/commit/list_commits_by_oid_test.go +++ b/internal/gitaly/service/commit/list_commits_by_oid_test.go @@ -12,7 +12,7 @@ func TestSuccessfulListCommitsByOidRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) commits := []*gitalypb.GitCommit{ { @@ -154,7 +154,7 @@ func TestSuccessfulListCommitsByOidLargeRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) req := &gitalypb.ListCommitsByOidRequest{ Oid: masterCommitids, diff --git a/internal/gitaly/service/commit/list_commits_by_ref_name_test.go b/internal/gitaly/service/commit/list_commits_by_ref_name_test.go index 0fce1f01d..cb31f0224 100644 --- a/internal/gitaly/service/commit/list_commits_by_ref_name_test.go +++ b/internal/gitaly/service/commit/list_commits_by_ref_name_test.go @@ -13,7 +13,7 @@ func TestSuccessfulListCommitsByRefNameRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { desc string @@ -169,7 +169,7 @@ func TestSuccessfulListCommitsByRefNameLargeRequest(t *testing.T) { } ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) refNames := [][]byte{} for _, refName := range repositoryRefNames { diff --git a/internal/gitaly/service/commit/list_commits_test.go b/internal/gitaly/service/commit/list_commits_test.go index 75a59a6ac..bd2812459 100644 --- a/internal/gitaly/service/commit/list_commits_test.go +++ b/internal/gitaly/service/commit/list_commits_test.go @@ -14,7 +14,7 @@ import ( func TestListCommits(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) for _, tc := range []struct { desc string diff --git a/internal/gitaly/service/commit/list_files_test.go b/internal/gitaly/service/commit/list_files_test.go index cb4857b4d..c6f27d1eb 100644 --- a/internal/gitaly/service/commit/list_files_test.go +++ b/internal/gitaly/service/commit/list_files_test.go @@ -38,7 +38,7 @@ var defaultFiles = [][]byte{ func TestListFiles_success(t *testing.T) { ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) gittest.Exec(t, cfg, "-C", repoPath, "symbolic-ref", "HEAD", "refs/heads/test-do-not-touch") @@ -125,7 +125,7 @@ func TestListFiles_unbornBranch(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, _, _, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, _, _, client := setupCommitServiceWithRepo(ctx, t) repo, _ := gittest.CreateRepository(ctx, t, cfg) tests := []struct { @@ -199,7 +199,7 @@ func TestListFiles_failure(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, _, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, _, _, client := setupCommitServiceWithRepo(ctx, t) tests := []struct { desc string @@ -253,7 +253,7 @@ func TestListFiles_invalidRevision(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) stream, err := client.ListFiles(ctx, &gitalypb.ListFilesRequest{ Repository: repo, diff --git a/internal/gitaly/service/commit/list_last_commits_for_tree_test.go b/internal/gitaly/service/commit/list_last_commits_for_tree_test.go index f848e48e6..f1edfbe09 100644 --- a/internal/gitaly/service/commit/list_last_commits_for_tree_test.go +++ b/internal/gitaly/service/commit/list_last_commits_for_tree_test.go @@ -1,17 +1,13 @@ package commit import ( - "bytes" "io" - "os" - "path/filepath" "testing" "unicode/utf8" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" - "gitlab.com/gitlab-org/gitaly/v14/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -26,7 +22,7 @@ func TestSuccessfulListLastCommitsForTreeRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { desc string @@ -215,7 +211,7 @@ func TestFailedListLastCommitsForTreeRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) invalidRepo := &gitalypb.Repository{StorageName: "broken", RelativePath: "path"} @@ -323,7 +319,7 @@ func TestNonUtf8ListLastCommitsForTreeRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) // This is an arbitrary blob known to exist in the test repository const blobID = "c60514b6d3d6bf4bec1030f70026e34dfbd69ad5" @@ -354,55 +350,66 @@ func TestSuccessfulListLastCommitsForTreeRequestWithGlobCharacters(t *testing.T) t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, false) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) - path := ":wq" - err := os.Mkdir(filepath.Join(repoPath, path), 0o755) - require.NoError(t, err) + commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries(gittest.TreeEntry{ + Path: ":wq", Mode: "040000", OID: gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + {Path: "README.md", Mode: "100644", Content: "something"}, + }), + }), gittest.WithParents()) + + t.Run("with literal pathspecs", func(t *testing.T) { + stream, err := client.ListLastCommitsForTree(ctx, &gitalypb.ListLastCommitsForTreeRequest{ + Repository: repo, + Revision: commitID.String(), + Path: []byte(":wq"), + GlobalOptions: &gitalypb.GlobalOptions{LiteralPathspecs: true}, + Limit: 100, + }) + require.NoError(t, err) + require.Equal(t, []string{":wq"}, fetchCommitPaths(t, stream)) + }) + + t.Run("without literal pathspecs", func(t *testing.T) { + stream, err := client.ListLastCommitsForTree(ctx, &gitalypb.ListLastCommitsForTreeRequest{ + Repository: repo, + Revision: commitID.String(), + Path: []byte(":wq"), + GlobalOptions: &gitalypb.GlobalOptions{LiteralPathspecs: false}, + Limit: 100, + }) + require.NoError(t, err) + require.Nil(t, fetchCommitPaths(t, stream)) + }) +} - gittest.Exec(t, cfg, "-C", repoPath, "mv", "README.md", path) - gittest.Exec(t, cfg, "-C", repoPath, "commit", "-a", "-m", "renamed test file") - commitID := text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "HEAD")) +func fileExistsInCommits(t *testing.T, stream gitalypb.CommitService_ListLastCommitsForTreeClient, path string) bool { + t.Helper() - request := &gitalypb.ListLastCommitsForTreeRequest{ - Repository: repo, - Revision: commitID, - Path: []byte(path), - GlobalOptions: &gitalypb.GlobalOptions{LiteralPathspecs: true}, - Limit: 100, - Offset: 0, + for _, commitPath := range fetchCommitPaths(t, stream) { + if commitPath == path { + return true + } } - stream, err := client.ListLastCommitsForTree(ctx, request) - require.NoError(t, err) - assert.True(t, fileExistsInCommits(t, stream, path)) - - request.GlobalOptions = &gitalypb.GlobalOptions{LiteralPathspecs: false} - stream, err = client.ListLastCommitsForTree(ctx, request) - require.NoError(t, err) - assert.False(t, fileExistsInCommits(t, stream, path)) + return false } -func fileExistsInCommits(t *testing.T, stream gitalypb.CommitService_ListLastCommitsForTreeClient, path string) bool { +func fetchCommitPaths(t *testing.T, stream gitalypb.CommitService_ListLastCommitsForTreeClient) []string { t.Helper() - var filenameFound bool + var files []string for { - fetchedCommits, err := stream.Recv() + response, err := stream.Recv() if err == io.EOF { break } - require.NoError(t, err) - commits := fetchedCommits.GetCommits() - - for _, fetchedCommit := range commits { - if bytes.Equal(fetchedCommit.PathBytes, []byte(path)) { - filenameFound = true - } + for _, commit := range response.GetCommits() { + files = append(files, string(commit.PathBytes)) } } - return filenameFound + return files } diff --git a/internal/gitaly/service/commit/raw_blame_test.go b/internal/gitaly/service/commit/raw_blame_test.go index 5de729b04..2313c29f1 100644 --- a/internal/gitaly/service/commit/raw_blame_test.go +++ b/internal/gitaly/service/commit/raw_blame_test.go @@ -16,7 +16,7 @@ func TestSuccessfulRawBlameRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { revision, path, data, blameRange []byte @@ -75,7 +75,7 @@ func TestFailedRawBlameRequest(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) invalidRepo := &gitalypb.Repository{StorageName: "fake", RelativePath: "path"} diff --git a/internal/gitaly/service/commit/stats_test.go b/internal/gitaly/service/commit/stats_test.go index 2305eb13d..3237eff4f 100644 --- a/internal/gitaly/service/commit/stats_test.go +++ b/internal/gitaly/service/commit/stats_test.go @@ -14,7 +14,7 @@ func TestCommitStatsSuccess(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) tests := []struct { desc string @@ -78,7 +78,7 @@ func TestCommitStatsFailure(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) tests := []struct { desc string diff --git a/internal/gitaly/service/commit/testhelper_test.go b/internal/gitaly/service/commit/testhelper_test.go index b1b7daf03..8da0178aa 100644 --- a/internal/gitaly/service/commit/testhelper_test.go +++ b/internal/gitaly/service/commit/testhelper_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -35,19 +34,12 @@ func setupCommitService(ctx context.Context, t testing.TB) (config.Cfg, gitalypb // setupCommitServiceWithRepo makes a basic configuration, creates a test repository and starts the service with the client. func setupCommitServiceWithRepo( - ctx context.Context, t testing.TB, bare bool, + ctx context.Context, t testing.TB, ) (config.Cfg, *gitalypb.Repository, string, gitalypb.CommitServiceClient) { return setupCommitServiceCreateRepo(ctx, t, func(ctx context.Context, tb testing.TB, cfg config.Cfg) (*gitalypb.Repository, string) { repo, repoPath := gittest.CreateRepository(ctx, tb, cfg, gittest.CreateRepositoryConfig{ Seed: gittest.SeedGitLabTest, }) - - if !bare { - gittest.AddWorktree(t, cfg, repoPath, "worktree") - repoPath = filepath.Join(repoPath, "worktree") - gittest.Exec(t, cfg, "-C", repoPath, "checkout", "master") - } - return repo, repoPath }) } diff --git a/internal/gitaly/service/commit/tree_entries_test.go b/internal/gitaly/service/commit/tree_entries_test.go index e27e2286b..eccd6841a 100644 --- a/internal/gitaly/service/commit/tree_entries_test.go +++ b/internal/gitaly/service/commit/tree_entries_test.go @@ -4,14 +4,12 @@ import ( "errors" "fmt" "io" - "os" - "path/filepath" - "strings" + "strconv" "testing" "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/v14/internal/git" "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" - "gitlab.com/gitlab-org/gitaly/v14/internal/helper/text" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -22,50 +20,49 @@ func TestGetTreeEntries_curlyBraces(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, false) - - normalFolderName := "issue-46261/folder" - curlyFolderName := "issue-46261/{{curly}}" - normalFolder := filepath.Join(repoPath, normalFolderName) - curlyFolder := filepath.Join(repoPath, curlyFolderName) - - require.NoError(t, os.MkdirAll(normalFolder, 0o755)) - require.NoError(t, os.MkdirAll(curlyFolder, 0o755)) - - testhelper.MustRunCommand(t, nil, "touch", filepath.Join(normalFolder, "/test1.txt")) - testhelper.MustRunCommand(t, nil, "touch", filepath.Join(curlyFolder, "/test2.txt")) - - gittest.Exec(t, cfg, "-C", repoPath, "add", "--all") - gittest.Exec(t, cfg, "-C", repoPath, "commit", "-m", "Test commit") + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) + + commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(), gittest.WithTreeEntries(gittest.TreeEntry{ + Path: "issue-46261", Mode: "040000", OID: gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + { + Path: "folder", Mode: "040000", OID: gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + {Path: "test1.txt", Mode: "100644", Content: "test1"}, + }), + }, + { + Path: "{{curly}}", Mode: "040000", OID: gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{ + {Path: "test2.txt", Mode: "100644", Content: "test2"}, + }), + }, + }), + })) - testCases := []struct { - description string - revision []byte - path []byte - recursive bool - filename []byte + for _, tc := range []struct { + desc string + revision []byte + path []byte + recursive bool + filename []byte }{ { - description: "with a normal folder", - revision: []byte("master"), - path: []byte(normalFolderName), - filename: []byte("issue-46261/folder/test1.txt"), + desc: "with a normal folder", + revision: []byte("master"), + path: []byte("issue-46261/folder"), + filename: []byte("issue-46261/folder/test1.txt"), }, { - description: "with a folder with curly braces", - revision: []byte("master"), - path: []byte(curlyFolderName), - filename: []byte("issue-46261/{{curly}}/test2.txt"), + desc: "with a folder with curly braces", + revision: []byte("master"), + path: []byte("issue-46261/{{curly}}"), + filename: []byte("issue-46261/{{curly}}/test2.txt"), }, - } - - for _, testCase := range testCases { - t.Run(testCase.description, func(t *testing.T) { + } { + t.Run(tc.desc, func(t *testing.T) { request := &gitalypb.GetTreeEntriesRequest{ Repository: repo, - Revision: []byte("HEAD"), - Path: testCase.path, - Recursive: testCase.recursive, + Revision: []byte(commitID.String()), + Path: tc.path, + Recursive: tc.recursive, } c, err := client.GetTreeEntries(ctx, request) @@ -73,7 +70,7 @@ func TestGetTreeEntries_curlyBraces(t *testing.T) { fetchedEntries, _ := getTreeEntriesFromTreeEntryClient(t, c, nil) require.Equal(t, 1, len(fetchedEntries)) - require.Equal(t, testCase.filename, fetchedEntries[0].FlatPath) + require.Equal(t, tc.filename, fetchedEntries[0].FlatPath) }) } } @@ -85,7 +82,7 @@ func TestGetTreeEntries_successful(t *testing.T) { commitID := "d25b6d94034242f3930dfcfeb6d8d9aac3583992" rootOid := "21bdc8af908562ae485ed46d71dd5426c08b084a" - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) rootEntries := []*gitalypb.TreeEntry{ { @@ -488,7 +485,7 @@ func TestGetTreeEntries_unsuccessful(t *testing.T) { commitID := "d25b6d94034242f3930dfcfeb6d8d9aac3583992" - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { description string @@ -535,53 +532,58 @@ func TestGetTreeEntries_deepFlatpath(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, false) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) - folderName := "1/2/3/4/5/6/7/8/9/10/11/12" - require.GreaterOrEqual(t, strings.Count(strings.Trim(folderName, "/"), "/"), defaultFlatTreeRecursion, "sanity check: construct folder deeper than default recursion value") + nestingLevel := 12 + require.Greater(t, nestingLevel, defaultFlatTreeRecursion, "sanity check: construct folder deeper than default recursion value") - nestedFolder := filepath.Join(repoPath, folderName) - require.NoError(t, os.MkdirAll(nestedFolder, 0o755)) - // put single file into the deepest directory - testhelper.MustRunCommand(t, nil, "touch", filepath.Join(nestedFolder, ".gitkeep")) - gittest.Exec(t, cfg, "-C", repoPath, "add", "--all") - gittest.Exec(t, cfg, "-C", repoPath, "commit", "-m", "Deep folder struct") + // We create a tree structure that is one deeper than the flat-tree recursion limit. + var treeID git.ObjectID + for i := nestingLevel; i >= 0; i-- { + var treeEntry gittest.TreeEntry + if treeID == "" { + treeEntry = gittest.TreeEntry{Path: ".gitkeep", Mode: "100644", Content: "something"} + } else { + // We use a numbered directory name to make it easier to see when things get + // truncated. + treeEntry = gittest.TreeEntry{Path: strconv.Itoa(i), Mode: "040000", OID: treeID} + } - commitID := text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "HEAD")) - rootOid := text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "HEAD^{tree}")) + treeID = gittest.WriteTree(t, cfg, repoPath, []gittest.TreeEntry{treeEntry}) + } + commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(), gittest.WithTree(treeID)) - // make request to folder that contains nothing except one folder - request := &gitalypb.GetTreeEntriesRequest{ + // We make a non-recursive request which tries to fetch tree entrie for the tree structure + // we have created above. This should return a single entry, which is the directory we're + // requesting. + stream, err := client.GetTreeEntries(ctx, &gitalypb.GetTreeEntriesRequest{ Repository: repo, Revision: []byte(commitID), - Path: []byte("1"), + Path: []byte("0"), Recursive: false, - } - - // request entries of the tree with single-folder structure on each level - entriesClient, err := client.GetTreeEntries(ctx, request) + }) require.NoError(t, err) + treeEntries, _ := getTreeEntriesFromTreeEntryClient(t, stream, nil) - fetchedEntries, _ := getTreeEntriesFromTreeEntryClient(t, entriesClient, nil) - // We know that there is a directory "1/2/3/4/5/6/7/8/9/10/11/12" - // but here we only get back "1/2/3/4/5/6/7/8/9/10/11". - // This proves that FlatPath recursion is bounded, which is the point of this test. + // We know that there is a directory "1/2/3/4/5/6/7/8/9/10/11/12", but here we only get + // "1/2/3/4/5/6/7/8/9/10/11" as flat path. This proves that FlatPath recursion is bounded, + // which is the point of this test. require.Equal(t, []*gitalypb.TreeEntry{{ - Oid: "c836b95b37958e7179f5a42a32b7197b5dec7321", - RootOid: rootOid, - Path: []byte("1/2"), - FlatPath: []byte("1/2/3/4/5/6/7/8/9/10/11"), + Oid: "ba0cae41e396836584a4114feac0b943faf786da", + RootOid: treeID.String(), + Path: []byte("0/1"), + FlatPath: []byte("0/1/2/3/4/5/6/7/8/9/10"), Type: gitalypb.TreeEntry_TREE, Mode: 0o40000, - CommitOid: commitID, - }}, fetchedEntries) + CommitOid: commitID.String(), + }}, treeEntries) } func TestGetTreeEntries_file(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t, true) + cfg, repo, repoPath, client := setupCommitServiceWithRepo(ctx, t) commitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries(gittest.TreeEntry{ @@ -611,7 +613,7 @@ func TestGetTreeEntries_validation(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) revision := []byte("d42783470dc29fde2cf459eb3199ee1d7e3f3a72") path := []byte("a/b/c") diff --git a/internal/gitaly/service/commit/tree_entry_test.go b/internal/gitaly/service/commit/tree_entry_test.go index 08ef53116..706be6173 100644 --- a/internal/gitaly/service/commit/tree_entry_test.go +++ b/internal/gitaly/service/commit/tree_entry_test.go @@ -24,7 +24,7 @@ func TestSuccessfulTreeEntry(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) testCases := []struct { revision []byte @@ -153,7 +153,7 @@ func TestFailedTreeEntry(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - _, repo, _, client := setupCommitServiceWithRepo(ctx, t, true) + _, repo, _, client := setupCommitServiceWithRepo(ctx, t) revision := []byte("d42783470dc29fde2cf459eb3199ee1d7e3f3a72") path := []byte("a/b/c") diff --git a/internal/gitaly/service/conflicts/list_conflict_files_test.go b/internal/gitaly/service/conflicts/list_conflict_files_test.go index bfe08d35f..b0c502408 100644 --- a/internal/gitaly/service/conflicts/list_conflict_files_test.go +++ b/internal/gitaly/service/conflicts/list_conflict_files_test.go @@ -1,18 +1,12 @@ package conflicts import ( - "bytes" - "context" "io" - "os" - "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/v14/internal/git" "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" - "gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo" - "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config" "gitlab.com/gitlab-org/gitaly/v14/internal/testhelper" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" "google.golang.org/grpc/codes" @@ -26,7 +20,7 @@ type conflictFile struct { func TestSuccessfulListConflictFilesRequest(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(ctx, t, false, nil) + _, repo, _, client := setupConflictsService(ctx, t, nil) ourCommitOid := "1a35b5a77cf6af7edf6703f88e82f6aff613666f" theirCommitOid := "8309e68585b28d61eb85b7e2834849dda6bf1733" @@ -92,7 +86,7 @@ end func TestSuccessfulListConflictFilesRequestWithAncestor(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(ctx, t, true, nil) + _, repo, _, client := setupConflictsService(ctx, t, nil) ourCommitOid := "824be604a34828eb682305f0d963056cfac87b2d" theirCommitOid := "1450cd639e0bc6721eb02800169e464f212cde06" @@ -138,67 +132,46 @@ func TestSuccessfulListConflictFilesRequestWithAncestor(t *testing.T) { func TestListConflictFilesHugeDiff(t *testing.T) { ctx := testhelper.Context(t) - cfg, repo, repoPath, client := SetupConflictsService(ctx, t, false, nil) + cfg, repo, repoPath, client := setupConflictsService(ctx, t, nil) - our := buildCommit(t, ctx, cfg, repo, repoPath, map[string][]byte{ - "a": bytes.Repeat([]byte("a\n"), 128*1024), - "b": bytes.Repeat([]byte("b\n"), 128*1024), - }) - - their := buildCommit(t, ctx, cfg, repo, repoPath, map[string][]byte{ - "a": bytes.Repeat([]byte("x\n"), 128*1024), - "b": bytes.Repeat([]byte("y\n"), 128*1024), - }) + ourCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries( + gittest.TreeEntry{Path: "a", Mode: "100644", Content: strings.Repeat("a\n", 128*1024)}, + gittest.TreeEntry{Path: "b", Mode: "100644", Content: strings.Repeat("b\n", 128*1024)}, + )) + theirCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries( + gittest.TreeEntry{Path: "a", Mode: "100644", Content: strings.Repeat("x\n", 128*1024)}, + gittest.TreeEntry{Path: "b", Mode: "100644", Content: strings.Repeat("y\n", 128*1024)}, + )) request := &gitalypb.ListConflictFilesRequest{ Repository: repo, - OurCommitOid: our, - TheirCommitOid: their, + OurCommitOid: ourCommitID.String(), + TheirCommitOid: theirCommitID.String(), } c, err := client.ListConflictFiles(ctx, request) require.NoError(t, err) receivedFiles := getConflictFiles(t, c) - require.Len(t, receivedFiles, 2) testhelper.ProtoEqual(t, &gitalypb.ConflictFileHeader{ - CommitOid: our, + CommitOid: ourCommitID.String(), OurMode: int32(0o100644), OurPath: []byte("a"), TheirPath: []byte("a"), }, receivedFiles[0].Header) testhelper.ProtoEqual(t, &gitalypb.ConflictFileHeader{ - CommitOid: our, + CommitOid: ourCommitID.String(), OurMode: int32(0o100644), OurPath: []byte("b"), TheirPath: []byte("b"), }, receivedFiles[1].Header) } -func buildCommit(t *testing.T, ctx context.Context, cfg config.Cfg, repo *gitalypb.Repository, repoPath string, files map[string][]byte) string { - t.Helper() - - for file, contents := range files { - filePath := filepath.Join(repoPath, file) - require.NoError(t, os.WriteFile(filePath, contents, 0o666)) - gittest.Exec(t, cfg, "-C", repoPath, "add", filePath) - } - - gittest.Exec(t, cfg, "-C", repoPath, "commit", "-m", "message") - - oid, err := localrepo.NewTestRepo(t, cfg, repo).ResolveRevision(ctx, git.Revision("HEAD")) - require.NoError(t, err) - - gittest.Exec(t, cfg, "-C", repoPath, "reset", "--hard", "HEAD~") - - return oid.String() -} - func TestListConflictFilesFailedPrecondition(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(ctx, t, true, nil) + _, repo, _, client := setupConflictsService(ctx, t, nil) testCases := []struct { desc string @@ -255,7 +228,7 @@ func TestListConflictFilesFailedPrecondition(t *testing.T) { func TestListConflictFilesAllowTreeConflicts(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(ctx, t, true, nil) + _, repo, _, client := setupConflictsService(ctx, t, nil) ourCommitOid := "eb227b3e214624708c474bdab7bde7afc17cefcc" theirCommitOid := "824be604a34828eb682305f0d963056cfac87b2d" @@ -347,7 +320,7 @@ end func TestFailedListConflictFilesRequestDueToValidation(t *testing.T) { ctx := testhelper.Context(t) - _, repo, _, client := SetupConflictsService(ctx, t, true, nil) + _, repo, _, client := setupConflictsService(ctx, t, nil) ourCommitOid := "0b4bc9a49b562e85de7cc9e834518ea6828729b9" theirCommitOid := "bb5206fee213d983da88c47f9cf4cc6caf9c66dc" diff --git a/internal/gitaly/service/conflicts/resolve_conflicts_test.go b/internal/gitaly/service/conflicts/resolve_conflicts_test.go index 0cc778069..68840f987 100644 --- a/internal/gitaly/service/conflicts/resolve_conflicts_test.go +++ b/internal/gitaly/service/conflicts/resolve_conflicts_test.go @@ -67,7 +67,7 @@ func TestSuccessfulResolveConflictsRequestHelper(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, verifyFuncProxy, verifyFuncProxy, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, repoPath, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repoProto, repoPath, client := setupConflictsService(ctx, t, hookManager) repo := localrepo.NewTestRepo(t, cfg, repoProto) @@ -201,7 +201,7 @@ func TestResolveConflictsWithRemoteRepo(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, sourceRepo, sourceRepoPath, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, sourceRepo, sourceRepoPath, client := setupConflictsService(ctx, t, hookManager) testcfg.BuildGitalySSH(t, cfg) testcfg.BuildGitalyHooks(t, cfg) @@ -271,7 +271,7 @@ func TestResolveConflictsWithRemoteRepo(t *testing.T) { func TestResolveConflictsLineEndings(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repo, repoPath, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repo, repoPath, client := setupConflictsService(ctx, t, hookManager) ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg)) @@ -391,7 +391,7 @@ func TestResolveConflictsLineEndings(t *testing.T) { func TestResolveConflictsNonOIDRequests(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, _, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repoProto, _, client := setupConflictsService(ctx, t, hookManager) ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg)) @@ -429,7 +429,7 @@ func TestResolveConflictsIdenticalContent(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, repoPath, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repoProto, repoPath, client := setupConflictsService(ctx, t, hookManager) repo := localrepo.NewTestRepo(t, cfg, repoProto) @@ -526,7 +526,7 @@ func TestResolveConflictsStableID(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repoProto, _, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repoProto, _, client := setupConflictsService(ctx, t, hookManager) repo := localrepo.NewTestRepo(t, cfg, repoProto) @@ -594,7 +594,7 @@ func TestResolveConflictsStableID(t *testing.T) { func TestFailedResolveConflictsRequestDueToResolutionError(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repo, _, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repo, _, client := setupConflictsService(ctx, t, hookManager) mdGS := testcfg.GitalyServersMetadataFromCfg(t, cfg) mdFF, _ := metadata.FromOutgoingContext(ctx) @@ -650,7 +650,7 @@ func TestFailedResolveConflictsRequestDueToResolutionError(t *testing.T) { func TestFailedResolveConflictsRequestDueToValidation(t *testing.T) { ctx := testhelper.Context(t) hookManager := hook.NewMockManager(t, hook.NopPreReceive, hook.NopPostReceive, hook.NopUpdate, hook.NopReferenceTransaction) - cfg, repo, _, client := SetupConflictsService(ctx, t, true, hookManager) + cfg, repo, _, client := setupConflictsService(ctx, t, hookManager) mdGS := testcfg.GitalyServersMetadataFromCfg(t, cfg) ourCommitOid := "1450cd639e0bc6721eb02800169e464f212cde06" @@ -816,7 +816,7 @@ func TestResolveConflictsQuarantine(t *testing.T) { t.Parallel() ctx := testhelper.Context(t) - cfg, sourceRepoProto, sourceRepoPath, client := SetupConflictsService(ctx, t, true, nil) + cfg, sourceRepoProto, sourceRepoPath, client := setupConflictsService(ctx, t, nil) testcfg.BuildGitalySSH(t, cfg) testcfg.BuildGitalyHooks(t, cfg) diff --git a/internal/gitaly/service/conflicts/testhelper_test.go b/internal/gitaly/service/conflicts/testhelper_test.go index 853a0581e..60f59c18b 100644 --- a/internal/gitaly/service/conflicts/testhelper_test.go +++ b/internal/gitaly/service/conflicts/testhelper_test.go @@ -2,7 +2,6 @@ package conflicts import ( "context" - "path/filepath" "testing" "gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest" @@ -24,7 +23,7 @@ func TestMain(m *testing.M) { testhelper.Run(m) } -func SetupConflictsService(ctx context.Context, t testing.TB, bare bool, hookManager hook.Manager) (config.Cfg, *gitalypb.Repository, string, gitalypb.ConflictsServiceClient) { +func setupConflictsService(ctx context.Context, t testing.TB, hookManager hook.Manager) (config.Cfg, *gitalypb.Repository, string, gitalypb.ConflictsServiceClient) { cfg := testcfg.Build(t) testcfg.BuildGitalyGit2Go(t, cfg) @@ -39,14 +38,6 @@ func SetupConflictsService(ctx context.Context, t testing.TB, bare bool, hookMan Seed: gittest.SeedGitLabTest, }) - if !bare { - gittest.AddWorktree(t, cfg, repoPath, "worktree") - repoPath = filepath.Join(repoPath, "worktree") - // AddWorktree creates a detached worktree. Checkout master here so the - // branch pointer moves as we later commit. - gittest.Exec(t, cfg, "-C", repoPath, "checkout", "master") - } - return cfg, repo, repoPath, client } |