-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixtures.go
More file actions
390 lines (340 loc) · 10.3 KB
/
fixtures.go
File metadata and controls
390 lines (340 loc) · 10.3 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
package testutil
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// CreateTestRepo creates a real git repository in a temporary directory
// with optional configuration for testing different scenarios
type RepoOptions struct {
// Name of the repo (used for directory name)
Name string
// Add uncommitted files (makes repo "dirty")
Dirty bool
// Files to create and commit
Files map[string]string
// Remotes to configure (name -> URL)
Remotes map[string]string
// Branches to create
Branches []string
// Initial commit message
InitialCommit string
}
// CreateTestRepo creates a test git repository
func CreateTestRepo(t *testing.T, opts RepoOptions) string {
t.Helper()
repoPath, err := CreateTestRepoInDir(t.TempDir(), opts)
if err != nil {
t.Fatalf("Failed to create test repo: %v", err)
}
return repoPath
}
// CreateTestRepoInDir creates a test repository under the provided base directory.
func CreateTestRepoInDir(baseDir string, opts RepoOptions) (string, error) {
repoName, err := validateSimpleName(opts.Name)
if err != nil {
return "", fmt.Errorf("invalid repo name %q: %w", opts.Name, err)
}
repoPath := filepath.Join(baseDir, repoName)
if err := os.MkdirAll(repoPath, 0755); err != nil {
return "", fmt.Errorf("failed to create repo directory: %w", err)
}
if _, err := RunGitCmdE(repoPath, "init"); err != nil {
return "", err
}
if _, err := RunGitCmdE(repoPath, "config", "user.email", "test@example.com"); err != nil {
return "", err
}
if _, err := RunGitCmdE(repoPath, "config", "user.name", "Test User"); err != nil {
return "", err
}
commitMsg := opts.InitialCommit
if commitMsg == "" {
commitMsg = "Initial commit"
}
initialFile := filepath.Join(repoPath, "README.md")
if err := os.WriteFile(initialFile, []byte("# Test Repo\n"), 0644); err != nil {
return "", fmt.Errorf("failed to create README: %w", err)
}
if _, err := RunGitCmdE(repoPath, "add", "README.md"); err != nil {
return "", err
}
if _, err := RunGitCmdE(repoPath, "commit", "-m", commitMsg); err != nil {
return "", err
}
originalBranch, err := RunGitCmdE(repoPath, "rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return "", err
}
for filename, content := range opts.Files {
relPath, err := validateFixturePath(filename)
if err != nil {
return "", fmt.Errorf("invalid file path %q: %w", filename, err)
}
filePath := filepath.Join(repoPath, relPath)
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
return "", fmt.Errorf("failed to create directory for %s: %w", filename, err)
}
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
return "", fmt.Errorf("failed to create file %s: %w", filename, err)
}
if _, err := RunGitCmdE(repoPath, "add", "--", filepath.ToSlash(relPath)); err != nil {
return "", err
}
if _, err := RunGitCmdE(repoPath, "commit", "-m", "Add "+filename); err != nil {
return "", err
}
}
for name, url := range opts.Remotes {
if _, err := RunGitCmdE(repoPath, "remote", "add", name, url); err != nil {
return "", err
}
}
for _, branch := range opts.Branches {
if branch == originalBranch {
continue
}
if _, err := RunGitCmdE(repoPath, "checkout", "-b", branch); err != nil {
return "", err
}
}
if len(opts.Branches) > 0 {
if _, err := RunGitCmdE(repoPath, "checkout", originalBranch); err != nil {
return "", err
}
}
if opts.Dirty {
dirtyFile := filepath.Join(repoPath, "uncommitted.txt")
if err := os.WriteFile(dirtyFile, []byte("uncommitted changes\n"), 0644); err != nil {
return "", fmt.Errorf("failed to create dirty file: %w", err)
}
}
return repoPath, nil
}
// CreateBareRemote creates a bare git repository to use as a remote
func CreateBareRemote(t *testing.T, name string) string {
t.Helper()
remotePath, err := CreateBareRemoteInDir(t.TempDir(), name)
if err != nil {
t.Fatalf("Failed to create bare remote: %v", err)
}
return remotePath
}
// CreateBareRemoteInDir creates a bare remote repository under the provided base directory.
func CreateBareRemoteInDir(baseDir, name string) (string, error) {
remoteName, err := validateSimpleName(name)
if err != nil {
return "", fmt.Errorf("invalid remote name %q: %w", name, err)
}
remotePath := filepath.Join(baseDir, remoteName+".git")
if err := os.MkdirAll(remotePath, 0755); err != nil {
return "", fmt.Errorf("failed to create bare repo directory: %w", err)
}
if _, err := RunGitCmdE(remotePath, "init", "--bare"); err != nil {
return "", err
}
return remotePath, nil
}
// SetupFakeFilesystem creates a fake filesystem structure for scanning tests
func SetupFakeFilesystem(t *testing.T) string {
t.Helper()
root, err := SetupFakeFilesystemInDir(t.TempDir())
if err != nil {
t.Fatalf("Failed to setup fake filesystem: %v", err)
}
return root
}
// SetupFakeFilesystemInDir creates a deterministic fake filesystem tree under baseDir.
func SetupFakeFilesystemInDir(baseDir string) (string, error) {
dirs := []string{
"home/testuser/projects",
"home/testuser/src",
"home/testuser/.cache",
"home/testuser/node_modules",
"root/sys",
"root/proc",
}
for _, dir := range dirs {
path := filepath.Join(baseDir, dir)
if err := os.MkdirAll(path, 0755); err != nil {
return "", fmt.Errorf("failed to create directory %s: %w", dir, err)
}
}
return baseDir, nil
}
// runGit is a helper to run git commands in a specific directory
func runGit(t *testing.T, dir string, args ...string) {
t.Helper()
_, err := RunGitCmdE(dir, args...)
if err != nil {
t.Fatalf("%v", err)
}
}
// IsDirty checks if a git repo has uncommitted changes
func IsDirty(t *testing.T, repoPath string) bool {
t.Helper()
dirty, err := IsDirtyE(repoPath)
if err != nil {
t.Fatalf("Failed to check git status: %v", err)
}
return dirty
}
// GetRemotes returns the configured remotes for a repo
func GetRemotes(t *testing.T, repoPath string) map[string]string {
t.Helper()
remotes, err := GetRemotesE(repoPath)
if err != nil {
t.Fatalf("Failed to get remotes: %v", err)
}
return remotes
}
// RunGitCmd runs a git command and fails the test if it errors
// Exported version of runGit for use in other test packages
func RunGitCmd(t *testing.T, dir string, args ...string) {
t.Helper()
runGit(t, dir, args...)
}
// RunGitCmdE runs git command in dir and returns trimmed command output.
func RunGitCmdE(dir string, args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Dir = dir
var stderr bytes.Buffer
cmd.Stderr = &stderr
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf(
"git command failed: git %v\nStdout: %s\nStderr: %s\nError: %w",
args,
strings.TrimSpace(string(output)),
strings.TrimSpace(stderr.String()),
err,
)
}
return strings.TrimSpace(string(output)), nil
}
func validateSimpleName(name string) (string, error) {
trimmed := strings.TrimSpace(name)
if trimmed == "" {
return "", fmt.Errorf("name cannot be empty")
}
if filepath.IsAbs(trimmed) {
return "", fmt.Errorf("absolute paths are not allowed")
}
if trimmed == "." || trimmed == ".." {
return "", fmt.Errorf("relative traversal segments are not allowed")
}
if strings.ContainsAny(trimmed, `/\`) {
return "", fmt.Errorf("path separators are not allowed")
}
return trimmed, nil
}
func validateFixturePath(name string) (string, error) {
trimmed := strings.TrimSpace(name)
if trimmed == "" {
return "", fmt.Errorf("path cannot be empty")
}
clean := filepath.Clean(trimmed)
if filepath.IsAbs(clean) {
return "", fmt.Errorf("absolute paths are not allowed")
}
if clean == "." || clean == ".." || strings.HasPrefix(clean, ".."+string(filepath.Separator)) {
return "", fmt.Errorf("path traversal is not allowed")
}
parts := strings.Split(clean, string(filepath.Separator))
if len(parts) > 0 && strings.EqualFold(parts[0], ".git") {
return "", fmt.Errorf(".git paths are not allowed")
}
return clean, nil
}
// GetCurrentSHA returns the current commit SHA
func GetCurrentSHA(t *testing.T, repoPath string) string {
t.Helper()
sha, err := GetCurrentSHAE(repoPath)
if err != nil {
t.Fatalf("Failed to get current SHA: %v", err)
}
return sha
}
// GetBranches returns all branches in the repo
func GetBranches(t *testing.T, repoPath string) []string {
t.Helper()
branches, err := GetBranchesE(repoPath)
if err != nil {
t.Fatalf("Failed to get branches: %v", err)
}
return branches
}
// IsDirtyE checks if a git repo has uncommitted changes.
func IsDirtyE(repoPath string) (bool, error) {
output, err := RunGitCmdE(repoPath, "status", "--porcelain")
if err != nil {
return false, err
}
return len(output) > 0, nil
}
// GetRemotesE returns configured remotes for a repo.
func GetRemotesE(repoPath string) (map[string]string, error) {
output, err := RunGitCmdE(repoPath, "remote", "-v")
if err != nil {
return nil, err
}
return parseRemotesOutput(output), nil
}
func parseRemotesOutput(output string) map[string]string {
remotes := make(map[string]string)
lines := strings.TrimSpace(output)
if lines == "" {
return remotes
}
for _, line := range strings.Split(lines, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
name, remainder, ok := strings.Cut(line, "\t")
if !ok {
idx := strings.IndexAny(line, " \t")
if idx == -1 {
continue
}
name = strings.TrimSpace(line[:idx])
remainder = strings.TrimSpace(line[idx+1:])
} else {
name = strings.TrimSpace(name)
remainder = strings.TrimSpace(remainder)
}
if strings.HasSuffix(remainder, " (fetch)") {
remainder = strings.TrimSuffix(remainder, " (fetch)")
} else if strings.HasSuffix(remainder, " (push)") {
remainder = strings.TrimSuffix(remainder, " (push)")
}
if name != "" && remainder != "" {
remotes[name] = remainder
}
}
return remotes
}
// GetCurrentSHAE returns the current commit SHA.
func GetCurrentSHAE(repoPath string) (string, error) {
return RunGitCmdE(repoPath, "rev-parse", "HEAD")
}
// GetBranchesE returns all branches in a repo.
func GetBranchesE(repoPath string) ([]string, error) {
output, err := RunGitCmdE(repoPath, "branch", "--format=%(refname:short)")
if err != nil {
return nil, err
}
branches := strings.Split(strings.TrimSpace(output), "\n")
// Filter out empty lines
var result []string
for _, b := range branches {
if b != "" {
result = append(result, b)
}
}
return result, nil
}