-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitgrab_test.go
More file actions
394 lines (340 loc) · 12.2 KB
/
Copy pathgitgrab_test.go
File metadata and controls
394 lines (340 loc) · 12.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package gitgrab
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
type mockHTTPClient struct {
doFunc func(req *http.Request) (*http.Response, error)
}
func (m *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
return m.doFunc(req)
}
func TestNewGitHubClient(t *testing.T) {
token := GitHubToken("test-token")
client := NewGitHubClient(token)
if client.token != token {
t.Errorf("Expected token %s, got %s", token, client.token)
}
if client.client == nil {
t.Error("Expected HTTP client to be initialized")
}
}
func TestGitHubClient_makeRequest(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "token test-token" {
t.Errorf("Expected Authorization header 'token test-token', got '%s'", r.Header.Get("Authorization"))
}
if r.Header.Get("Accept") != "application/vnd.github.v3+json" {
t.Errorf("Expected Accept header 'application/vnd.github.v3+json', got '%s'", r.Header.Get("Accept"))
}
if r.Header.Get("User-Agent") != "GitHub-Repo-Cloner" {
t.Errorf("Expected User-Agent header 'GitHub-Repo-Cloner', got '%s'", r.Header.Get("User-Agent"))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"test": "response"}`))
}))
defer server.Close()
client := NewGitHubClient(GitHubToken("test-token"))
resp, err := client.makeRequest(server.URL)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
}
func TestGitHubClient_FetchAllRepos(t *testing.T) {
testRepos := []Repository{
{Name: RepositoryName("repo1"), CloneURL: HTTPURL("https://github.com/test/repo1.git"), SSHURL: SSHURL("git@github.com:test/repo1.git"), Private: false, DefaultBranch: BranchName("main")},
{Name: RepositoryName("repo2"), CloneURL: HTTPURL("https://github.com/test/repo2.git"), SSHURL: SSHURL("git@github.com:test/repo2.git"), Private: true, DefaultBranch: BranchName("master")},
}
callCount := 0
mockClient := &mockHTTPClient{
doFunc: func(req *http.Request) (*http.Response, error) {
callCount++
recorder := httptest.NewRecorder()
if callCount == 1 {
recorder.WriteHeader(http.StatusOK)
json.NewEncoder(recorder).Encode(testRepos)
} else {
recorder.WriteHeader(http.StatusOK)
json.NewEncoder(recorder).Encode([]Repository{})
}
return recorder.Result(), nil
},
}
client := NewGitHubClientWithHTTPClient(GitHubToken("test-token"), mockClient)
repos, err := client.FetchAllRepos(OrganizationName("testorg"))
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if len(repos) != 2 {
t.Errorf("Expected 2 repositories, got %d", len(repos))
}
if repos[0].Name != "repo1" {
t.Errorf("Expected first repo name 'repo1', got '%s'", repos[0].Name)
}
if repos[1].Private != true {
t.Errorf("Expected second repo to be private")
}
}
func TestGitHubClient_FetchAllRepos_APIError(t *testing.T) {
mockClient := &mockHTTPClient{
doFunc: func(req *http.Request) (*http.Response, error) {
recorder := httptest.NewRecorder()
recorder.WriteHeader(http.StatusUnauthorized)
recorder.Write([]byte(`{"message": "Bad credentials"}`))
return recorder.Result(), nil
},
}
client := NewGitHubClientWithHTTPClient(GitHubToken("invalid-token"), mockClient)
_, err := client.FetchAllRepos(OrganizationName("testorg"))
if err == nil {
t.Fatal("Expected error for API failure, got none")
}
if !strings.Contains(err.Error(), "API request failed") {
t.Errorf("Expected 'API request failed' in error message, got %v", err)
}
}
func TestCloneRepo_DirectoryExists_SkipUpdate(t *testing.T) {
// This test creates a directory but doesn't set up a proper git repo
// The function should attempt to update but may fail gracefully
tempDir := t.TempDir()
repo := Repository{
Name: RepositoryName("test-repo"),
CloneURL: HTTPURL("https://github.com/test/test-repo.git"),
SSHURL: SSHURL("git@github.com:test/test-repo.git"),
Private: false,
DefaultBranch: BranchName("main"),
}
repoDir := filepath.Join(tempDir, repo.Name.String())
err := os.MkdirAll(repoDir, 0755)
if err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}
// The function will try to update existing directories now
// It may fail due to API calls or git commands, which is expected in test environment
config := CloneConfig{
Repository: repo,
TargetDir: tempDir,
Token: GitHubToken("invalid-token"),
Organization: OrganizationName("testorg"),
Method: CloneMethodSSH,
}
err = CloneRepo(config)
// We expect either success (if git commands work) or specific failure messages
if err != nil {
// Check that we're getting expected error types (API or git failures)
if !strings.Contains(err.Error(), "failed to") {
t.Errorf("Expected git or API related error, got: %v", err)
}
}
}
func TestCloneRepo_URLGeneration_PrivateSSH(t *testing.T) {
repo := Repository{
Name: RepositoryName("private-repo"),
CloneURL: HTTPURL("https://github.com/test/private-repo.git"),
SSHURL: SSHURL("git@github.com:test/private-repo.git"),
Private: true,
DefaultBranch: BranchName("main"),
}
expectedURL := "git@github.com:test/private-repo.git"
if repo.Private && repo.SSHURL.String() != expectedURL {
t.Errorf("Expected private repo SSH URL %s, got %s", expectedURL, repo.SSHURL)
}
}
func TestCloneRepo_URLGeneration_PrivateHTTP(t *testing.T) {
repo := Repository{
Name: RepositoryName("private-repo"),
CloneURL: HTTPURL("https://github.com/test/private-repo.git"),
SSHURL: SSHURL("git@github.com:test/private-repo.git"),
Private: true,
DefaultBranch: BranchName("main"),
}
token := "token123"
orgName := "testorg"
expectedURL := "https://token123@github.com/testorg/private-repo.git"
if repo.Private {
actualURL := "https://" + token + "@github.com/" + orgName + "/" + repo.Name.String() + ".git"
if actualURL != expectedURL {
t.Errorf("Expected private repo HTTP URL %s, got %s", expectedURL, actualURL)
}
}
}
func TestCloneRepo_URLGeneration_Public(t *testing.T) {
repo := Repository{
Name: RepositoryName("public-repo"),
CloneURL: HTTPURL("https://github.com/test/public-repo.git"),
SSHURL: SSHURL("git@github.com:test/public-repo.git"),
Private: false,
DefaultBranch: BranchName("main"),
}
if repo.Private {
t.Error("Test repo should be public")
}
expectedURL := "https://github.com/test/public-repo.git"
if repo.CloneURL.String() != expectedURL {
t.Errorf("Expected public repo URL %s, got %s", expectedURL, repo.CloneURL)
}
}
func TestGetCurrentBranch(t *testing.T) {
// Create a temporary git repository for testing
tempDir := t.TempDir()
// Initialize git repo
cmd := exec.Command("git", "init", tempDir)
cmd.Stdout = nil
cmd.Stderr = nil
if err := cmd.Run(); err != nil {
t.Skip("git not available for testing")
}
// Configure git for the test
exec.Command("git", "-C", tempDir, "config", "user.email", "test@example.com").Run()
exec.Command("git", "-C", tempDir, "config", "user.name", "Test User").Run()
// Create a file and commit
testFile := filepath.Join(tempDir, "test.txt")
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
exec.Command("git", "-C", tempDir, "add", "test.txt").Run()
exec.Command("git", "-C", tempDir, "commit", "-m", "Initial commit").Run()
// Test getting current branch
branch, err := getCurrentBranch(tempDir)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
// The default branch could be "master" or "main" depending on git configuration
if branch != "master" && branch != "main" {
t.Errorf("Expected branch to be 'master' or 'main', got '%s'", branch)
}
}
func TestCloneRepo_ExistingDirectory_GitPullFetch(t *testing.T) {
tempDir := t.TempDir()
repo := Repository{
Name: RepositoryName("test-repo"),
CloneURL: HTTPURL("https://github.com/test/test-repo.git"),
SSHURL: SSHURL("git@github.com:test/test-repo.git"),
Private: false,
DefaultBranch: BranchName("main"),
}
repoDir := filepath.Join(tempDir, repo.Name.String())
// Create directory structure that mimics a git repository
err := os.MkdirAll(filepath.Join(repoDir, ".git"), 0755)
if err != nil {
t.Fatalf("Failed to create test directory: %v", err)
}
// Create a basic git config to make it look like a real repo
gitConfig := filepath.Join(repoDir, ".git", "config")
configContent := `[core]
repositoryformatversion = 0
filemode = true
bare = false
[remote "origin"]
url = https://github.com/test/test-repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main`
if err := os.WriteFile(gitConfig, []byte(configContent), 0644); err != nil {
t.Fatalf("Failed to create git config: %v", err)
}
// The function will try to call git commands, but we can't fully test them
// without a real git repository. We'll test that the function handles
// existing directories properly by checking it doesn't return a "directory exists" error
config := CloneConfig{
Repository: repo,
TargetDir: tempDir,
Token: GitHubToken("token"),
Organization: OrganizationName("testorg"),
Method: CloneMethodSSH,
}
err = CloneRepo(config)
// We expect this to not return a "directory exists" error since we handle that case
// It may fail on git commands, but that's expected in this test environment
if err != nil && !strings.Contains(err.Error(), "failed to") {
t.Errorf("Unexpected error type: %v", err)
}
}
func TestCloneRepo_CloneMethodSSH(t *testing.T) {
tests := []struct {
name string
repo Repository
cloneMethod string
expectedSSHURL bool
}{
{
name: "Private repo with SSH method",
repo: Repository{
Name: RepositoryName("private-repo"),
CloneURL: HTTPURL("https://github.com/test/private-repo.git"),
SSHURL: SSHURL("git@github.com:test/private-repo.git"),
Private: true,
DefaultBranch: BranchName("main"),
},
cloneMethod: "ssh",
expectedSSHURL: true,
},
{
name: "Private repo with HTTP method",
repo: Repository{
Name: RepositoryName("private-repo"),
CloneURL: HTTPURL("https://github.com/test/private-repo.git"),
SSHURL: SSHURL("git@github.com:test/private-repo.git"),
Private: true,
DefaultBranch: BranchName("main"),
},
cloneMethod: "http",
expectedSSHURL: false,
},
{
name: "Public repo with SSH method",
repo: Repository{
Name: RepositoryName("public-repo"),
CloneURL: HTTPURL("https://github.com/test/public-repo.git"),
SSHURL: SSHURL("git@github.com:test/public-repo.git"),
Private: false,
DefaultBranch: BranchName("main"),
},
cloneMethod: "ssh",
expectedSSHURL: true, // Public repos now respect method flag
},
{
name: "Public repo with HTTP method",
repo: Repository{
Name: RepositoryName("public-repo"),
CloneURL: HTTPURL("https://github.com/test/public-repo.git"),
SSHURL: SSHURL("git@github.com:test/public-repo.git"),
Private: false,
DefaultBranch: BranchName("main"),
},
cloneMethod: "http",
expectedSSHURL: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var expectedURL string
if tt.cloneMethod == "ssh" {
expectedURL = tt.repo.SSHURL.String()
} else {
if tt.repo.Private {
expectedURL = "https://token@github.com/testorg/" + tt.repo.Name.String() + ".git"
} else {
expectedURL = tt.repo.CloneURL.String()
}
}
// We can't actually test git clone without git being available,
// but we can verify the URL generation logic
if tt.cloneMethod == "ssh" && tt.repo.SSHURL.String() != expectedURL {
t.Errorf("Expected SSH URL %s, got %s", expectedURL, tt.repo.SSHURL)
} else if tt.cloneMethod == "http" && !tt.repo.Private && tt.repo.CloneURL.String() != expectedURL {
t.Errorf("Expected HTTP URL %s, got %s", expectedURL, tt.repo.CloneURL)
}
})
}
}