Repository navigation
Expand file tree
/
Copy pathsparse_test.go
More file actions
276 lines (252 loc) · 8.85 KB
/
Copy pathsparse_test.go
File metadata and controls
276 lines (252 loc) · 8.85 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
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/urfave/cli/v3"
)
func TestSparseCheckoutCommand(t *testing.T) {
cmd := sparseCheckout([]string{"src/backend", "dir with spaces", "-leading"})
want := []string{"git", "sparse-checkout", "set", "--cone", "--", "src/backend", "dir with spaces", "-leading"}
if !reflect.DeepEqual(cmd.Args, want) {
t.Fatalf("args = %#v, want %#v", cmd.Args, want)
}
}
func TestNormalizeSparseEntries(t *testing.T) {
if got := normalizeSparseEntries([]string{"src", "", "src"}); !reflect.DeepEqual(got, []string{"src", "src"}) {
t.Fatalf("normalization changed order or duplicates: %#v", got)
}
}
func TestSparseFlags(t *testing.T) {
t.Setenv("PLUGIN_SPARSE", "src/backend,shared")
var paths []string
command := &cli.Command{
Flags: globalFlags,
Action: func(_ context.Context, command *cli.Command) error {
paths = command.StringSlice("sparse")
return nil
},
}
if err := command.Run(context.Background(), []string{"plugin-git"}); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(paths, []string{"src/backend", "shared"}) {
t.Fatalf("sparse paths = %#v", paths)
}
}
func TestSparseCheckoutLocalRepository(t *testing.T) {
remote, commit := sparseFixture(t)
home := t.TempDir()
workspace := filepath.Join(t.TempDir(), "workspace")
plugin := sparseFixturePlugin(remote, commit, home, workspace)
plugin.Config.Sparse = []string{"src/backend", "shared"}
if err := plugin.Exec(); err != nil {
t.Fatal(err)
}
assertPathExists(t, workspace, "README.md", true)
assertPathExists(t, workspace, "Directory.Build.props", true)
assertPathExists(t, workspace, "src/backend/app.go", true)
assertPathExists(t, workspace, "shared/common.go", true)
assertPathExists(t, workspace, "src/frontend/app.ts", false)
assertPathExists(t, workspace, "docs/guide.md", false)
assertGitClean(t, workspace)
// Reusing the same checkout with a different cone replaces the old paths.
plugin.Config.Sparse = []string{"docs"}
if err := plugin.Exec(); err != nil {
t.Fatal(err)
}
assertPathExists(t, workspace, "docs/guide.md", true)
assertPathExists(t, workspace, "src/backend/app.go", false)
assertGitClean(t, workspace)
// Omitting sparse checkout restores a complete cached workspace.
plugin.Config.Sparse = nil
if err := plugin.Exec(); err != nil {
t.Fatal(err)
}
assertPathExists(t, workspace, "src/backend/app.go", true)
assertPathExists(t, workspace, "src/frontend/app.ts", true)
assertPathExists(t, workspace, "docs/guide.md", true)
assertGitClean(t, workspace)
if enabled, err := isSparseCheckoutEnabled(workspace, os.Environ()); err != nil || enabled {
t.Fatalf("sparse checkout still enabled after full checkout: enabled=%v err=%v", enabled, err)
}
}
func sparseFixturePlugin(remote, commit, home, workspace string) Plugin {
return Plugin{
Repo: Repo{Clone: remote},
Pipeline: Pipeline{
Path: workspace,
Commit: commit,
Event: "push",
},
Config: Config{
Branch: "main",
Home: home,
Lfs: false,
Partial: false,
Recursive: false,
},
}
}
func sparseFixture(t *testing.T) (string, string) {
t.Helper()
root := t.TempDir()
source := filepath.Join(root, "source")
remote := filepath.Join(root, "remote.git")
if err := os.MkdirAll(source, 0o755); err != nil {
t.Fatal(err)
}
runGit(t, source, "init", "-b", "main")
runGit(t, source, "config", "user.name", "Sparse Test")
runGit(t, source, "config", "user.email", "sparse@example.invalid")
files := map[string]string{
"README.md": "root\n",
"Directory.Build.props": "props\n",
"src/backend/app.go": "package backend\n",
"src/backend/nested/config.json": "{}\n",
"src/frontend/app.ts": "export {}\n",
"shared/common.go": "package shared\n",
"docs/guide.md": "guide\n",
"dir with spaces/file.txt": "spaces\n",
}
for name, contents := range files {
path := filepath.Join(source, name)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
t.Fatal(err)
}
}
runGit(t, source, "add", ".")
runGit(t, source, "commit", "-m", "fixture")
commit := gitOutput(t, source, "rev-parse", "HEAD")
runGit(t, root, "clone", "--bare", source, remote)
return filepath.ToSlash(remote), commit
}
func runGit(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1")
if output, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %s failed: %v\n%s", strings.Join(args, " "), err, output)
}
}
func gitOutput(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_CONFIG_NOSYSTEM=1")
output, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %s failed: %v\n%s", strings.Join(args, " "), err, output)
}
return strings.TrimSpace(string(output))
}
func assertPathExists(t *testing.T, root, path string, want bool) {
t.Helper()
_, err := os.Stat(filepath.Join(root, filepath.FromSlash(path)))
got := err == nil
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
if got != want {
t.Fatalf("path %q exists = %v, want %v", path, got, want)
}
}
func assertGitClean(t *testing.T, workspace string) {
t.Helper()
if got := gitOutput(t, workspace, "status", "--porcelain"); got != "" {
t.Fatalf("working tree is not clean:\n%s", got)
}
}
// lfsMergeFixture creates a remote where "main" gains an LFS file under
// src/backend after the PR branch "feature" diverged from it.
func lfsMergeFixture(t *testing.T) (remote, prCommit string) {
t.Helper()
root := t.TempDir()
source := filepath.Join(root, "source")
remote = filepath.Join(root, "remote.git")
write := func(name, contents string) {
path := filepath.Join(source, name)
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
t.Fatal(err)
}
}
runGit(t, root, "init", "--bare", "-b", "main", remote)
if err := os.MkdirAll(source, 0o755); err != nil {
t.Fatal(err)
}
runGit(t, source, "init", "-b", "main")
runGit(t, source, "config", "user.name", "Sparse Test")
runGit(t, source, "config", "user.email", "sparse@example.invalid")
runGit(t, source, "lfs", "install", "--local")
runGit(t, source, "remote", "add", "origin", remote)
write(".gitattributes", "*.bin filter=lfs diff=lfs merge=lfs -text\n")
write("src/backend/app.go", "package backend\n")
write("src/backend/base.bin", "base lfs content\n")
write("src/frontend/app.ts", "export {}\n")
runGit(t, source, "add", ".")
runGit(t, source, "commit", "-m", "base")
runGit(t, source, "switch", "-c", "feature")
write("src/backend/feature.go", "package backend\n")
runGit(t, source, "add", ".")
runGit(t, source, "commit", "-m", "feature")
prCommit = gitOutput(t, source, "rev-parse", "HEAD")
runGit(t, source, "switch", "main")
write("src/backend/merged.bin", "lfs content from target branch\n")
runGit(t, source, "add", ".")
runGit(t, source, "commit", "-m", "target adds lfs file")
runGit(t, source, "push", "origin", "main", "feature")
return filepath.ToSlash(remote), prCommit
}
func runMergePR(t *testing.T, sparse []string) string {
remote, commit := lfsMergeFixture(t)
home := t.TempDir()
workspace := filepath.Join(t.TempDir(), "workspace")
plugin := sparseFixturePlugin(remote, commit, home, workspace)
plugin.Pipeline.Event = "pull_request"
plugin.Config.Event = "pull_request"
plugin.Config.Branch = "feature"
plugin.Config.Lfs = true
plugin.Config.MergePullRequest = true
plugin.Config.TargetBranch = "main"
plugin.Config.GitUserName = "CI"
plugin.Config.GitUserEmail = "ci@example.invalid"
plugin.Config.Sparse = sparse
if err := plugin.Exec(); err != nil {
t.Fatal(err)
}
return workspace
}
func assertLFSMaterialized(t *testing.T, workspace, path, want string) {
t.Helper()
got, err := os.ReadFile(filepath.Join(workspace, filepath.FromSlash(path)))
if err != nil {
t.Fatal(err)
}
if strings.HasPrefix(string(got), "version https://git-lfs") {
t.Errorf("%s is still an LFS pointer:\n%s", path, got)
} else if string(got) != want {
t.Errorf("%s = %q, want %q", path, got, want)
}
}
func TestLFSMergePullRequestFullCheckout(t *testing.T) {
ws := runMergePR(t, nil)
assertLFSMaterialized(t, ws, "src/backend/base.bin", "base lfs content\n")
assertLFSMaterialized(t, ws, "src/backend/merged.bin", "lfs content from target branch\n")
}
func TestLFSMergePullRequestSparseCheckout(t *testing.T) {
ws := runMergePR(t, []string{"src/backend"})
assertPathExists(t, ws, "src/frontend/app.ts", false)
assertLFSMaterialized(t, ws, "src/backend/base.bin", "base lfs content\n")
assertLFSMaterialized(t, ws, "src/backend/merged.bin", "lfs content from target branch\n")
}