-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_test.go
More file actions
374 lines (321 loc) · 10.2 KB
/
Copy pathgithub_test.go
File metadata and controls
374 lines (321 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
// --- parseGitHubURL tests ---
func TestParseGitHubURL_Valid(t *testing.T) {
tests := []struct {
input string
wantOwner string
wantRepo string
}{
{"github.com/user/repo", "user", "repo"},
{"github.com/some-org/my-repo", "some-org", "my-repo"},
{"github.com/user/repo/", "user", "repo"},
{"github.com/UPPER/Case", "UPPER", "Case"},
{"github.com/user123/repo456", "user123", "repo456"},
{"github.com/a/b", "a", "b"},
{"github.com/user/repo.with.dots", "user", "repo.with.dots"},
{"github.com/user/repo-with-dashes", "user", "repo-with-dashes"},
{"github.com/user/repo_with_underscores", "user", "repo_with_underscores"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
owner, repo, err := parseGitHubURL(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if owner != tt.wantOwner {
t.Errorf("owner = %q, want %q", owner, tt.wantOwner)
}
if repo != tt.wantRepo {
t.Errorf("repo = %q, want %q", repo, tt.wantRepo)
}
})
}
}
func TestParseGitHubURL_Invalid(t *testing.T) {
tests := []string{
"",
"github.com",
"github.com/",
"github.com/user",
"github.com/user/",
"gitlab.com/user/repo",
"https://github.com/user/repo",
"github.com/user/repo/extra",
"github.com/user/repo/extra/path",
"not-a-url",
}
for _, input := range tests {
t.Run(input, func(t *testing.T) {
_, _, err := parseGitHubURL(input)
if err == nil {
t.Errorf("expected error for input %q, got nil", input)
}
})
}
}
// --- parseGitHubSpec tests ---
func TestParseGitHubSpec_Valid(t *testing.T) {
tests := []struct {
input string
wantURL string
wantRef string
}{
{"github.com/user/repo", "github.com/user/repo", ""},
{"github.com/user/repo@main", "github.com/user/repo", "main"},
{"github.com/user/repo@v1.0.0", "github.com/user/repo", "v1.0.0"},
{"github.com/user/repo@abc123", "github.com/user/repo", "abc123"},
{"github.com/user/repo@feature/branch", "github.com/user/repo", "feature/branch"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
url, ref, err := parseGitHubSpec(tt.input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if url != tt.wantURL {
t.Errorf("url = %q, want %q", url, tt.wantURL)
}
if ref != tt.wantRef {
t.Errorf("ref = %q, want %q", ref, tt.wantRef)
}
})
}
}
func TestParseGitHubSpec_TooManyAtSigns(t *testing.T) {
_, _, err := parseGitHubSpec("github.com/user/repo@v1@extra")
if err == nil {
t.Error("expected error for multiple @ signs, got nil")
}
}
// --- resolveRef tests (SHA detection, no network) ---
func TestResolveRef_FullSHA(t *testing.T) {
sha := "abcdef1234567890abcdef1234567890abcdef12"
gotSHA, gotRef, err := resolveRef("owner", "repo", sha)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotSHA != sha {
t.Errorf("sha = %q, want %q", gotSHA, sha)
}
if gotRef != sha {
t.Errorf("ref = %q, want %q", gotRef, sha)
}
}
func TestResolveRef_FullSHA_AllDigits(t *testing.T) {
sha := "0123456789012345678901234567890123456789"
gotSHA, gotRef, err := resolveRef("owner", "repo", sha)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotSHA != sha {
t.Errorf("sha = %q, want %q", gotSHA, sha)
}
if gotRef != sha {
t.Errorf("ref = %q, want %q", gotRef, sha)
}
}
// --- HTTP-based GitHub API function tests ---
// testGitHubServer creates a mock GitHub API server and configures the package
// globals to use it. Returns a cleanup function that must be deferred.
func testGitHubServer(t *testing.T, handler http.Handler) func() {
t.Helper()
srv := httptest.NewServer(handler)
origClient := httpClient
origBase := githubAPIBaseURL
httpClient = srv.Client()
githubAPIBaseURL = srv.URL
return func() {
srv.Close()
httpClient = origClient
githubAPIBaseURL = origBase
}
}
func TestGetLatestCommitSHA(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubRepo{DefaultBranch: "main"})
})
mux.HandleFunc("/repos/testowner/testrepo/branches/main", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubBranch{
Commit: GitHubCommit{SHA: "abc123def456abc123def456abc123def456abc1"},
})
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
sha, branch, err := getLatestCommitSHA("testowner", "testrepo")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if branch != "main" {
t.Errorf("branch = %q, want %q", branch, "main")
}
if sha != "abc123def456abc123def456abc123def456abc1" {
t.Errorf("sha = %q, want %q", sha, "abc123def456abc123def456abc123def456abc1")
}
}
func TestGetLatestCommitSHA_RepoNotFound(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
_, _, err := getLatestCommitSHA("testowner", "testrepo")
if err == nil {
t.Error("expected error for 404 response, got nil")
}
}
func TestGetBranchCommitSHA(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo/branches/develop", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubBranch{
Commit: GitHubCommit{SHA: "deadbeef12345678deadbeef12345678deadbeef"},
})
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
sha, ref, err := getBranchCommitSHA("testowner", "testrepo", "develop")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ref != "develop" {
t.Errorf("ref = %q, want %q", ref, "develop")
}
if sha != "deadbeef12345678deadbeef12345678deadbeef" {
t.Errorf("sha = %q, want %q", sha, "deadbeef12345678deadbeef12345678deadbeef")
}
}
func TestGetBranchCommitSHA_NotFound(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo/branches/nope", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
_, _, err := getBranchCommitSHA("testowner", "testrepo", "nope")
if err == nil {
t.Error("expected error for missing branch, got nil")
}
}
func TestGetTagCommitSHA(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo/git/refs/tags/v1.0.0", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubRef{
Object: struct {
SHA string `json:"sha"`
}{SHA: "caffee12345678caffee12345678caffee123456"},
})
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
sha, err := getTagCommitSHA("testowner", "testrepo", "v1.0.0")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if sha != "caffee12345678caffee12345678caffee123456" {
t.Errorf("sha = %q, want %q", sha, "caffee12345678caffee12345678caffee123456")
}
}
func TestGetTagCommitSHA_NotFound(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo/git/refs/tags/v999", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
_, err := getTagCommitSHA("testowner", "testrepo", "v999")
if err == nil {
t.Error("expected error for missing tag, got nil")
}
}
func TestResolveRef_EmptyRef_UsesDefaultBranch(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubRepo{DefaultBranch: "main"})
})
mux.HandleFunc("/repos/testowner/testrepo/branches/main", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubBranch{
Commit: GitHubCommit{SHA: "aabbccdd11223344aabbccdd11223344aabbccdd"},
})
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
sha, ref, err := resolveRef("testowner", "testrepo", "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ref != "main" {
t.Errorf("ref = %q, want %q", ref, "main")
}
if sha != "aabbccdd11223344aabbccdd11223344aabbccdd" {
t.Errorf("sha = %q, want %q", sha, "aabbccdd11223344aabbccdd11223344aabbccdd")
}
}
func TestResolveRef_BranchRef(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo/branches/develop", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubBranch{
Commit: GitHubCommit{SHA: "1111222233334444555566667777888899990000"},
})
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
sha, ref, err := resolveRef("testowner", "testrepo", "develop")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ref != "develop" {
t.Errorf("ref = %q, want %q", ref, "develop")
}
if sha != "1111222233334444555566667777888899990000" {
t.Errorf("sha = %q, want %q", sha, "1111222233334444555566667777888899990000")
}
}
func TestResolveRef_TagFallback(t *testing.T) {
mux := http.NewServeMux()
// Branch lookup returns 404
mux.HandleFunc("/repos/testowner/testrepo/branches/v1.0.0", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
// Tag lookup succeeds
mux.HandleFunc("/repos/testowner/testrepo/git/refs/tags/v1.0.0", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(GitHubRef{
Object: struct {
SHA string `json:"sha"`
}{SHA: "aabbccdd00112233aabbccdd00112233aabbccdd"},
})
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
sha, ref, err := resolveRef("testowner", "testrepo", "v1.0.0")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ref != "v1.0.0" {
t.Errorf("ref = %q, want %q", ref, "v1.0.0")
}
if sha != "aabbccdd00112233aabbccdd00112233aabbccdd" {
t.Errorf("sha = %q, want %q", sha, "aabbccdd00112233aabbccdd00112233aabbccdd")
}
}
func TestResolveRef_NeitherBranchNorTag(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/testowner/testrepo/branches/nonexistent", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
mux.HandleFunc("/repos/testowner/testrepo/git/refs/tags/nonexistent", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
})
cleanup := testGitHubServer(t, mux)
defer cleanup()
_, _, err := resolveRef("testowner", "testrepo", "nonexistent")
if err == nil {
t.Error("expected error when ref is neither branch nor tag, got nil")
}
}