diff options
Diffstat (limited to 'modules/git/commit_info_test.go')
-rw-r--r-- | modules/git/commit_info_test.go | 83 |
1 files changed, 58 insertions, 25 deletions
diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go index 4cc207de64..42bed75a3d 100644 --- a/modules/git/commit_info_test.go +++ b/modules/git/commit_info_test.go @@ -17,21 +17,24 @@ import ( ) const ( - testReposDir = "tests/repos/" - benchmarkReposDir = "benchmark/repos/" + testReposDir = "tests/repos/" ) -func cloneRepo(url, dir, name string) (string, error) { - repoDir := filepath.Join(dir, name) - if _, err := os.Stat(repoDir); err == nil { - return repoDir, nil +func cloneRepo(url, name string) (string, error) { + repoDir, err := os.MkdirTemp("", name) + if err != nil { + return "", err } - return repoDir, Clone(DefaultContext, url, repoDir, CloneRepoOptions{ + if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{ Mirror: false, Bare: false, Quiet: true, Timeout: 5 * time.Minute, - }) + }); err != nil { + _ = util.RemoveAll(repoDir) + return "", err + } + return repoDir, nil } func testGetCommitsInfo(t *testing.T, repo1 *Repository) { @@ -61,20 +64,35 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) { } for _, testCase := range testCases { commit, err := repo1.GetCommit(testCase.CommitID) - assert.NoError(t, err) + if err != nil { + assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err) + // no point trying to do anything else for this test. + continue + } assert.NotNil(t, commit) assert.NotNil(t, commit.Tree) assert.NotNil(t, commit.Tree.repo) tree, err := commit.Tree.SubTree(testCase.Path) + if err != nil { + assert.NoError(t, err, "Unable to get subtree: %s of commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err) + // no point trying to do anything else for this test. + continue + } + assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path) assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path) - assert.NoError(t, err) entries, err := tree.ListEntries() - assert.NoError(t, err) - commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.Background(), commit, testCase.Path, nil) - assert.NoError(t, err) + if err != nil { + assert.NoError(t, err, "Unable to get entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err) + // no point trying to do anything else for this test. + continue + } + + // FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain. + commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path, nil) + assert.NoError(t, err, "Unable to get commit information for entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err) if err != nil { t.FailNow() } @@ -100,40 +118,52 @@ func TestEntries_GetCommitsInfo(t *testing.T) { testGetCommitsInfo(t, bareRepo1) - clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo") - assert.NoError(t, err) + clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo") + if err != nil { + assert.NoError(t, err) + } defer util.RemoveAll(clonedPath) clonedRepo1, err := OpenRepository(clonedPath) - assert.NoError(t, err) + if err != nil { + assert.NoError(t, err) + } defer clonedRepo1.Close() testGetCommitsInfo(t, clonedRepo1) } func BenchmarkEntries_GetCommitsInfo(b *testing.B) { - benchmarks := []struct { + type benchmarkType struct { url string name string - }{ + } + + benchmarks := []benchmarkType{ {url: "https://github.com/go-gitea/gitea.git", name: "gitea"}, {url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"}, {url: "https://github.com/moby/moby.git", name: "moby"}, {url: "https://github.com/golang/go.git", name: "go"}, {url: "https://github.com/torvalds/linux.git", name: "linux"}, } - for _, benchmark := range benchmarks { + + doBenchmark := func(benchmark benchmarkType) { var commit *Commit var entries Entries var repo *Repository - if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil { + repoPath, err := cloneRepo(benchmark.url, benchmark.name) + if err != nil { b.Fatal(err) - } else if repo, err = OpenRepository(repoPath); err != nil { + } + defer util.RemoveAll(repoPath) + + if repo, err = OpenRepository(repoPath); err != nil { b.Fatal(err) - } else if commit, err = repo.GetBranchCommit("master"); err != nil { - repo.Close() + } + defer repo.Close() + + if commit, err = repo.GetBranchCommit("master"); err != nil { b.Fatal(err) } else if entries, err = commit.Tree.ListEntries(); err != nil { - repo.Close() b.Fatal(err) } entries.Sort() @@ -146,6 +176,9 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) { } } }) - repo.Close() + } + + for _, benchmark := range benchmarks { + doBenchmark(benchmark) } } |