-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit_test.go
More file actions
244 lines (204 loc) · 6.51 KB
/
Copy pathgit_test.go
File metadata and controls
244 lines (204 loc) · 6.51 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
package getit //nolint:testpackage
import (
"context"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/alecthomas/assert/v2"
)
func TestGitMatch(t *testing.T) {
tests := []struct {
name string
scheme string
expected bool
}{
{name: "GitHTTPS", scheme: "git+https", expected: true},
{name: "GitSSH", scheme: "git+ssh", expected: true},
{name: "Git", scheme: "git", expected: true},
{name: "HTTPS", scheme: "https", expected: false},
{name: "HTTP", scheme: "http", expected: false},
{name: "SSH", scheme: "ssh", expected: false},
{name: "File", scheme: "file", expected: false},
{name: "Empty", scheme: "", expected: false},
}
git := NewGit()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &url.URL{Scheme: tt.scheme, Host: "github.com", Path: "/user/repo"}
result := git.Match(u)
assert.Equal(t, tt.expected, result)
})
}
}
func TestConvertGitURL(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "GitHTTPS",
input: "git+https://github.com/user/repo",
expected: "https://github.com/user/repo",
},
{
name: "GitSSH",
input: "git+ssh://github.com/user/repo",
expected: "git@github.com:user/repo",
},
{
name: "Git",
input: "git://github.com/user/repo",
expected: "git://github.com/user/repo",
},
{
name: "WithQueryParams",
input: "git+https://github.com/user/repo?ref=main&depth=1",
expected: "https://github.com/user/repo",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := url.Parse(tt.input)
assert.NoError(t, err)
result := convertGitURL(u)
assert.Equal(t, tt.expected, result)
})
}
}
// createTestRepo creates a git repository with test files and returns its path
// along with a helper function for running git commands in that repo.
func createTestRepo(t *testing.T) (repoDir string, runGit func(args ...string)) {
t.Helper()
repoDir = t.TempDir()
runGit = func(args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repoDir
cmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=Test",
"GIT_AUTHOR_EMAIL=test@test.com",
"GIT_COMMITTER_NAME=Test",
"GIT_COMMITTER_EMAIL=test@test.com",
)
output, err := cmd.CombinedOutput()
assert.NoError(t, err, "git %v failed: %s", args, output)
}
runGit("init", "--initial-branch=master")
runGit("config", "user.email", "test@test.com")
runGit("config", "user.name", "Test")
err := os.WriteFile(filepath.Join(repoDir, "file.txt"), []byte("hello from test\n"), 0o644)
assert.NoError(t, err)
err = os.WriteFile(filepath.Join(repoDir, "nested.txt"), []byte("nested content\n"), 0o644)
assert.NoError(t, err)
runGit("add", ".")
runGit("commit", "-m", "Initial commit")
return repoDir, runGit
}
func TestGitFetch(t *testing.T) {
repoDir, _ := createTestRepo(t)
u, err := url.Parse("git+file://" + repoDir)
assert.NoError(t, err)
dest := t.TempDir()
git := NewGit()
err = git.Fetch(context.Background(), Source{URL: u}, dest)
assert.NoError(t, err)
content, err := os.ReadFile(filepath.Join(dest, "file.txt"))
assert.NoError(t, err)
assert.Equal(t, "hello from test\n", string(content))
content, err = os.ReadFile(filepath.Join(dest, "nested.txt"))
assert.NoError(t, err)
assert.Equal(t, "nested content\n", string(content))
// Verify it's a git repo
_, err = os.Stat(filepath.Join(dest, ".git"))
assert.NoError(t, err)
}
func TestGitFetchWithRef(t *testing.T) {
repoDir, runGit := createTestRepo(t)
// Create a branch with different content
runGit("checkout", "-b", "feature-branch")
err := os.WriteFile(filepath.Join(repoDir, "file.txt"), []byte("feature branch content\n"), 0o644)
assert.NoError(t, err)
runGit("add", ".")
runGit("commit", "-m", "Feature commit")
runGit("checkout", "master")
// Clone the feature branch
u, err := url.Parse("git+file://" + repoDir + "?ref=feature-branch")
assert.NoError(t, err)
dest := t.TempDir()
git := NewGit()
err = git.Fetch(context.Background(), Source{URL: u}, dest)
assert.NoError(t, err)
content, err := os.ReadFile(filepath.Join(dest, "file.txt"))
assert.NoError(t, err)
assert.Equal(t, "feature branch content\n", string(content))
}
func TestGitFetchWithDepth(t *testing.T) {
repoDir, runGit := createTestRepo(t)
// Add more commits
for i := range 5 {
err := os.WriteFile(filepath.Join(repoDir, "file.txt"), []byte("commit "+string(rune('A'+i))+"\n"), 0o644)
assert.NoError(t, err)
runGit("add", ".")
runGit("commit", "-m", "Commit "+string(rune('A'+i)))
}
u, err := url.Parse("git+file://" + repoDir + "?depth=1")
assert.NoError(t, err)
dest := t.TempDir()
git := NewGit()
err = git.Fetch(context.Background(), Source{URL: u}, dest)
assert.NoError(t, err)
// Verify shallow clone by checking commit count
cmd := exec.Command("git", "rev-list", "--count", "HEAD")
cmd.Dir = dest
output, err := cmd.Output()
assert.NoError(t, err)
assert.Equal(t, "1\n", string(output))
}
func TestGitFetchCancelledContext(t *testing.T) {
repoDir, _ := createTestRepo(t)
u, err := url.Parse("git+file://" + repoDir)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
cancel()
dest := t.TempDir()
git := NewGit()
err = git.Fetch(ctx, Source{URL: u}, dest)
assert.Error(t, err)
}
func TestGitFetchCancelKillsProcessGroup(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("requires sh")
}
// Fake git that spawns a grandchild holding the output pipe open. If
// cancellation only kills the direct child, Fetch blocks until the
// grandchild exits.
binDir := t.TempDir()
script := "#!/bin/sh\nsleep 60 &\nwait\n"
assert.NoError(t, os.WriteFile(filepath.Join(binDir, "git"), []byte(script), 0o755)) //nolint:gosec
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(200 * time.Millisecond)
cancel()
}()
u, err := url.Parse("git+https://example.com/user/repo")
assert.NoError(t, err)
start := time.Now()
err = NewGit().Fetch(ctx, Source{URL: u}, t.TempDir())
assert.Error(t, err)
assert.True(t, time.Since(start) < 5*time.Second, "Fetch blocked on an orphaned grandchild for %s", time.Since(start))
}
func TestGitFetchInvalidRepo(t *testing.T) {
u, err := url.Parse("git+file:///nonexistent/repo/path")
assert.NoError(t, err)
dest := t.TempDir()
git := NewGit()
err = git.Fetch(context.Background(), Source{URL: u}, dest)
assert.Error(t, err)
assert.Contains(t, err.Error(), "git clone failed")
}