-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
404 lines (342 loc) · 12.4 KB
/
main_test.go
File metadata and controls
404 lines (342 loc) · 12.4 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
395
396
397
398
399
400
401
402
403
404
package main
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
func TestHelp(t *testing.T) {
code, stdout, stderr := runCLI(t, "--help")
if code != exitUsage {
t.Fatalf("expected exit %d, got %d", exitUsage, code)
}
if !strings.Contains(stdout, "usage: git clone [<options>] [--] <repo> [<dir>]") {
t.Fatalf("expected usage in stdout, got %q", stdout)
}
if strings.Contains(stdout, "--filter") || strings.Contains(stdout, "--reject-shallow") || strings.Contains(stdout, "--bundle-uri") {
t.Fatalf("expected unsupported flags to be omitted from help, got %q", stdout)
}
if stderr != "" {
t.Fatalf("expected empty stderr, got %q", stderr)
}
}
func TestUnknownSwitch(t *testing.T) {
code, _, stderr := runCLI(t, "-r", "repo")
if code != exitUsage {
t.Fatalf("expected exit %d, got %d", exitUsage, code)
}
if !strings.Contains(stderr, "error: unknown switch `r'") {
t.Fatalf("expected unknown switch error, got %q", stderr)
}
}
func TestMissingRepository(t *testing.T) {
code, _, stderr := runCLI(t)
if code != exitUsage {
t.Fatalf("expected exit %d, got %d", exitUsage, code)
}
if !strings.Contains(stderr, "fatal: You must specify a repository to clone.") {
t.Fatalf("expected missing repo error, got %q", stderr)
}
}
func TestUnsupportedFlag(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "--filter=blob:none", remote, destination)
if code != exitUsage {
t.Fatalf("expected exit %d, got %d", exitUsage, code)
}
if !strings.Contains(stderr, "error: option `filter' is not supported by this build of git-clone") {
t.Fatalf("expected unsupported flag error, got %q", stderr)
}
if _, err := os.Stat(destination); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected destination to remain absent, stat err=%v", err)
}
}
func TestDepthZeroFails(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "--depth=0", remote, destination)
if code != exitFatal {
t.Fatalf("expected exit %d, got %d", exitFatal, code)
}
if !strings.Contains(stderr, "fatal: depth 0 is not a positive number") {
t.Fatalf("expected depth error, got %q", stderr)
}
}
func TestCloneIntoEmptyExistingDirectory(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
if err := os.Mkdir(destination, 0o755); err != nil {
t.Fatal(err)
}
code, _, stderr := runCLI(t, remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
assertFileExists(t, filepath.Join(destination, "file.txt"))
}
func TestExistingNonEmptyDirectoryFails(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
if err := os.Mkdir(destination, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(destination, "existing.txt"), []byte("x\n"), 0o644); err != nil {
t.Fatal(err)
}
code, _, stderr := runCLI(t, remote, destination)
if code != exitFatal {
t.Fatalf("expected exit %d, got %d", exitFatal, code)
}
if !strings.Contains(stderr, "fatal: destination path '") || !strings.Contains(stderr, "already exists and is not an empty directory.") {
t.Fatalf("expected destination exists error, got %q", stderr)
}
}
func TestBranchBeatsTagWithSameName(t *testing.T) {
remote := createBranchTagCollisionRemote(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "-b", "same", remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
repo, err := git.PlainOpen(destination)
if err != nil {
t.Fatal(err)
}
head, err := repo.Head()
if err != nil {
t.Fatal(err)
}
if head.Name() != plumbing.NewBranchReferenceName("same") {
t.Fatalf("expected branch checkout, got %s", head.Name())
}
assertFileExists(t, filepath.Join(destination, "branch.txt"))
}
func TestTagCheckoutDetachedHead(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "-b", "v1.0.0", remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
repo, err := git.PlainOpen(destination)
if err != nil {
t.Fatal(err)
}
head, err := repo.Head()
if err != nil {
t.Fatal(err)
}
if head.Name().IsBranch() {
t.Fatalf("expected detached head, got branch %s", head.Name())
}
}
func TestLegacyTagPrefixRejected(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "-b", "tags/v1.0.0", remote, destination)
if code != exitFatal {
t.Fatalf("expected exit %d, got %d stderr=%q", exitFatal, code, stderr)
}
if !strings.Contains(stderr, "fatal: Remote branch tags/v1.0.0 not found in upstream origin") {
t.Fatalf("expected missing remote branch error, got %q", stderr)
}
}
func TestNoTags(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "--no-tags", remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
repo, err := git.PlainOpen(destination)
if err != nil {
t.Fatal(err)
}
if _, err := repo.Tag("v1.0.0"); err == nil {
t.Fatal("expected tag v1.0.0 to be absent")
}
}
func TestNoCheckout(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "--no-checkout", remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
assertPathAbsent(t, filepath.Join(destination, "file.txt"))
assertFileExists(t, filepath.Join(destination, ".git", "config"))
}
func TestConfigEntriesWritten(t *testing.T) {
remote := createBasicRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "-c", "core.filemode=false", "-c", "user.name=FromFlag", remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
configBytes, err := os.ReadFile(filepath.Join(destination, ".git", "config"))
if err != nil {
t.Fatal(err)
}
configText := string(configBytes)
if !strings.Contains(configText, "filemode = false") {
t.Fatalf("expected core.filemode override in config, got %q", configText)
}
if !strings.Contains(configText, "name = FromFlag") {
t.Fatalf("expected user.name override in config, got %q", configText)
}
}
func TestPullExtension(t *testing.T) {
remoteInfo := createBasicRemoteRepoDetails(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, remoteInfo.Remote, destination)
if code != exitOK {
t.Fatalf("initial clone failed: code=%d stderr=%q", code, stderr)
}
if err := os.WriteFile(filepath.Join(remoteInfo.Source, "new.txt"), []byte("new\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, remoteInfo.Source, "git", "add", "new.txt")
runCmd(t, remoteInfo.Source, "git", "commit", "-m", "new commit")
runCmd(t, remoteInfo.Source, "git", "push", remoteInfo.Remote, "HEAD:main")
code, _, stderr = runCLI(t, "--pull", remoteInfo.Remote, destination)
if code != exitOK {
t.Fatalf("pull failed: code=%d stderr=%q", code, stderr)
}
assertFileExists(t, filepath.Join(destination, "new.txt"))
}
func TestRecursiveClone(t *testing.T) {
remote := createSubmoduleRemoteRepo(t)
destination := filepath.Join(t.TempDir(), "clone")
code, _, stderr := runCLI(t, "--recursive", remote, destination)
if code != exitOK {
t.Fatalf("expected exit %d, got %d stderr=%q", exitOK, code, stderr)
}
assertFileExists(t, filepath.Join(destination, "modules", "submodule.txt"))
}
func runCLI(t *testing.T, args ...string) (int, string, string) {
t.Helper()
var stdout bytes.Buffer
var stderr bytes.Buffer
code := run(args, &stdout, &stderr)
return code, stdout.String(), stderr.String()
}
type remoteInfo struct {
Source string
Remote string
}
func createBasicRemoteRepo(t *testing.T) string {
t.Helper()
return createBasicRemoteRepoDetails(t).Remote
}
func createBasicRemoteRepoDetails(t *testing.T) remoteInfo {
t.Helper()
base := t.TempDir()
source := filepath.Join(base, "src")
remote := filepath.Join(base, "remote.git")
runCmd(t, base, "git", "init", "-b", "main", "src")
runCmd(t, source, "git", "config", "user.name", "Test User")
runCmd(t, source, "git", "config", "user.email", "test@example.com")
if err := os.WriteFile(filepath.Join(source, "file.txt"), []byte("v1\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, source, "git", "add", "file.txt")
runCmd(t, source, "git", "commit", "-m", "initial")
runCmd(t, source, "git", "tag", "v1.0.0")
runCmd(t, source, "git", "checkout", "-b", "feature")
if err := os.WriteFile(filepath.Join(source, "feature.txt"), []byte("feature\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, source, "git", "add", "feature.txt")
runCmd(t, source, "git", "commit", "-m", "feature commit")
runCmd(t, source, "git", "checkout", "main")
runCmd(t, base, "git", "clone", "--bare", source, remote)
return remoteInfo{
Source: source,
Remote: remote,
}
}
func createBranchTagCollisionRemote(t *testing.T) string {
t.Helper()
base := t.TempDir()
source := filepath.Join(base, "src")
remote := filepath.Join(base, "remote.git")
runCmd(t, base, "git", "init", "-b", "main", "src")
runCmd(t, source, "git", "config", "user.name", "Test User")
runCmd(t, source, "git", "config", "user.email", "test@example.com")
if err := os.WriteFile(filepath.Join(source, "file.txt"), []byte("base\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, source, "git", "add", "file.txt")
runCmd(t, source, "git", "commit", "-m", "base")
runCmd(t, source, "git", "checkout", "-b", "same")
if err := os.WriteFile(filepath.Join(source, "branch.txt"), []byte("branch\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, source, "git", "add", "branch.txt")
runCmd(t, source, "git", "commit", "-m", "branch")
runCmd(t, source, "git", "checkout", "main")
runCmd(t, source, "git", "tag", "same")
runCmd(t, base, "git", "clone", "--bare", source, remote)
return remote
}
func createSubmoduleRemoteRepo(t *testing.T) string {
t.Helper()
base := t.TempDir()
subSource := filepath.Join(base, "sub-src")
subRemote := filepath.Join(base, "sub-remote.git")
mainSource := filepath.Join(base, "main-src")
mainRemote := filepath.Join(base, "main-remote.git")
runCmd(t, base, "git", "init", "-b", "main", "sub-src")
runCmd(t, subSource, "git", "config", "user.name", "Test User")
runCmd(t, subSource, "git", "config", "user.email", "test@example.com")
if err := os.WriteFile(filepath.Join(subSource, "submodule.txt"), []byte("submodule\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, subSource, "git", "add", "submodule.txt")
runCmd(t, subSource, "git", "commit", "-m", "submodule init")
runCmd(t, base, "git", "clone", "--bare", subSource, subRemote)
runCmd(t, base, "git", "init", "-b", "main", "main-src")
runCmd(t, mainSource, "git", "config", "user.name", "Test User")
runCmd(t, mainSource, "git", "config", "user.email", "test@example.com")
if err := os.WriteFile(filepath.Join(mainSource, "main.txt"), []byte("main\n"), 0o644); err != nil {
t.Fatal(err)
}
runCmd(t, mainSource, "git", "add", "main.txt")
runCmd(t, mainSource, "git", "commit", "-m", "main init")
runCmd(t, mainSource, "git", "-c", "protocol.file.allow=always", "submodule", "add", subRemote, "modules")
runCmd(t, mainSource, "git", "commit", "-m", "add submodule")
runCmd(t, base, "git", "clone", "--bare", mainSource, mainRemote)
return mainRemote
}
func runCmd(t *testing.T, dir string, name string, args ...string) string {
t.Helper()
cmd := exec.Command(name, args...)
cmd.Dir = dir
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("command failed: %s %s\n%s", name, strings.Join(args, " "), string(output))
}
return string(output)
}
func assertFileExists(t *testing.T, path string) {
t.Helper()
info, err := os.Stat(path)
if err != nil {
t.Fatalf("expected %s to exist: %v", path, err)
}
if info.IsDir() {
t.Fatalf("expected %s to be a file", path)
}
}
func assertPathAbsent(t *testing.T, path string) {
t.Helper()
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("expected %s to be absent, err=%v", path, err)
}
}