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