@@ -21,17 +21,21 @@ const (
21
21
benchmarkReposDir = "benchmark/repos/"
22
22
)
23
23
24
- func cloneRepo (url , dir , name string ) (string , error ) {
25
- repoDir := filepath . Join ( dir , name )
26
- if _ , err := os . Stat ( repoDir ); err = = nil {
27
- return repoDir , nil
24
+ func cloneRepo (url , name string ) (string , error ) {
25
+ repoDir , err := os . MkdirTemp ( "" , name )
26
+ if err ! = nil {
27
+ return "" , err
28
28
}
29
- return repoDir , Clone (DefaultContext , url , repoDir , CloneRepoOptions {
29
+ if err := Clone (DefaultContext , url , repoDir , CloneRepoOptions {
30
30
Mirror : false ,
31
31
Bare : false ,
32
32
Quiet : true ,
33
33
Timeout : 5 * time .Minute ,
34
- })
34
+ }); err != nil {
35
+ _ = util .RemoveAll (repoDir )
36
+ return "" , err
37
+ }
38
+ return repoDir , nil
35
39
}
36
40
37
41
func testGetCommitsInfo (t * testing.T , repo1 * Repository ) {
@@ -61,20 +65,35 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
61
65
}
62
66
for _ , testCase := range testCases {
63
67
commit , err := repo1 .GetCommit (testCase .CommitID )
64
- assert .NoError (t , err )
68
+ if err != nil {
69
+ assert .NoError (t , err , "Unable to get commit: %s from testcase due to error: %v" , testCase .CommitID , err )
70
+ // no point trying to do anything else for this test.
71
+ continue
72
+ }
65
73
assert .NotNil (t , commit )
66
74
assert .NotNil (t , commit .Tree )
67
75
assert .NotNil (t , commit .Tree .repo )
68
76
69
77
tree , err := commit .Tree .SubTree (testCase .Path )
78
+ if err != nil {
79
+ assert .NoError (t , err , "Unable to get subtree: %s of commit: %s from testcase due to error: %v" , testCase .Path , testCase .CommitID , err )
80
+ // no point trying to do anything else for this test.
81
+ continue
82
+ }
83
+
70
84
assert .NotNil (t , tree , "tree is nil for testCase CommitID %s in Path %s" , testCase .CommitID , testCase .Path )
71
85
assert .NotNil (t , tree .repo , "repo is nil for testCase CommitID %s in Path %s" , testCase .CommitID , testCase .Path )
72
86
73
- assert .NoError (t , err )
74
87
entries , err := tree .ListEntries ()
75
- assert .NoError (t , err )
76
- commitsInfo , treeCommit , err := entries .GetCommitsInfo (context .Background (), commit , testCase .Path , nil )
77
- assert .NoError (t , err )
88
+ if err != nil {
89
+ 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 )
90
+ // no point trying to do anything else for this test.
91
+ continue
92
+ }
93
+
94
+ // FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
95
+ commitsInfo , treeCommit , err := entries .GetCommitsInfo (context .TODO (), commit , testCase .Path , nil )
96
+ 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 )
78
97
if err != nil {
79
98
t .FailNow ()
80
99
}
@@ -100,40 +119,52 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
100
119
101
120
testGetCommitsInfo (t , bareRepo1 )
102
121
103
- clonedPath , err := cloneRepo (bareRepo1Path , testReposDir , "repo1_TestEntries_GetCommitsInfo" )
104
- assert .NoError (t , err )
122
+ clonedPath , err := cloneRepo (bareRepo1Path , "repo1_TestEntries_GetCommitsInfo" )
123
+ if err != nil {
124
+ assert .NoError (t , err )
125
+ }
105
126
defer util .RemoveAll (clonedPath )
106
127
clonedRepo1 , err := OpenRepository (clonedPath )
107
- assert .NoError (t , err )
128
+ if err != nil {
129
+ assert .NoError (t , err )
130
+ }
108
131
defer clonedRepo1 .Close ()
109
132
110
133
testGetCommitsInfo (t , clonedRepo1 )
111
134
}
112
135
113
136
func BenchmarkEntries_GetCommitsInfo (b * testing.B ) {
114
- benchmarks := [] struct {
137
+ type benchmarkType struct {
115
138
url string
116
139
name string
117
- }{
140
+ }
141
+
142
+ benchmarks := []benchmarkType {
118
143
{url : "https://github.com/go-gitea/gitea.git" , name : "gitea" },
119
144
{url : "https://github.com/ethantkoenig/manyfiles.git" , name : "manyfiles" },
120
145
{url : "https://github.com/moby/moby.git" , name : "moby" },
121
146
{url : "https://github.com/golang/go.git" , name : "go" },
122
147
{url : "https://github.com/torvalds/linux.git" , name : "linux" },
123
148
}
124
- for _ , benchmark := range benchmarks {
149
+
150
+ doBenchmark := func (benchmark benchmarkType ) {
125
151
var commit * Commit
126
152
var entries Entries
127
153
var repo * Repository
128
- if repoPath , err := cloneRepo (benchmark .url , benchmarkReposDir , benchmark .name ); err != nil {
154
+ repoPath , err := cloneRepo (benchmark .url , benchmark .name )
155
+ if err != nil {
129
156
b .Fatal (err )
130
- } else if repo , err = OpenRepository (repoPath ); err != nil {
157
+ }
158
+ defer util .RemoveAll (repoPath )
159
+
160
+ if repo , err = OpenRepository (repoPath ); err != nil {
131
161
b .Fatal (err )
132
- } else if commit , err = repo .GetBranchCommit ("master" ); err != nil {
133
- repo .Close ()
162
+ }
163
+ defer repo .Close ()
164
+
165
+ if commit , err = repo .GetBranchCommit ("master" ); err != nil {
134
166
b .Fatal (err )
135
167
} else if entries , err = commit .Tree .ListEntries (); err != nil {
136
- repo .Close ()
137
168
b .Fatal (err )
138
169
}
139
170
entries .Sort ()
@@ -146,6 +177,9 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
146
177
}
147
178
}
148
179
})
149
- repo .Close ()
180
+ }
181
+
182
+ for _ , benchmark := range benchmarks {
183
+ doBenchmark (benchmark )
150
184
}
151
185
}
0 commit comments