-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitfake_test.go
More file actions
175 lines (160 loc) · 4.86 KB
/
gitfake_test.go
File metadata and controls
175 lines (160 loc) · 4.86 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
package main
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
type fakeGitRepo struct {
changes []testFileChange
revisions map[string]map[string]string
diffOutputs map[string]string
mergeBases map[string]string
headSHA string
failures map[string]gitResponse
}
type gitResponse struct {
result gitResult
err error
}
func (repo fakeGitRepo) runner(t *testing.T) gitRunner {
t.Helper()
return func(_ context.Context, _ string, args ...string) (gitResult, error) {
t.Helper()
if response, ok := repo.failures[gitKey(args...)]; ok {
return response.result, response.err
}
switch args[0] {
case "diff":
return repo.diffResponse(t, args)
case "cat-file":
return repo.catFileResponse(t, args)
case "show":
return repo.showResponse(t, args)
case "ls-tree":
return repo.lsTreeResponse(t, args)
case "merge-base":
return repo.mergeBaseResponse(t, args)
case "rev-parse":
return repo.revParseResponse(t, args)
default:
t.Fatalf("unexpected git command: %v", args)
return gitResult{}, nil
}
}
}
func (repo fakeGitRepo) diffResponse(t *testing.T, args []string) (gitResult, error) {
t.Helper()
if len(args) >= 2 && args[1] == "--name-status" {
return gitResult{Stdout: repo.nameStatusOutput()}, nil
}
separator := slices.Index(args, "--")
require.NotEqual(t, -1, separator)
paths := args[separator+1:]
output, ok := repo.diffOutputs[strings.Join(paths, "\x00")]
if !ok {
t.Fatalf("unexpected diff paths %q", strings.Join(paths, "\x00"))
}
return gitResult{Stdout: output}, nil
}
func (repo fakeGitRepo) nameStatusOutput() string {
parts := make([]string, 0, len(repo.changes)*3)
for _, change := range repo.changes {
switch change.Kind {
case changeRenamed:
parts = append(parts, "R100", change.OldPath, change.NewPath)
case changeAdded:
parts = append(parts, string(change.Kind), change.NewPath)
case changeDeleted:
parts = append(parts, string(change.Kind), change.OldPath)
default:
parts = append(parts, string(change.Kind), change.displayPath())
}
}
return strings.Join(parts, "\x00") + "\x00"
}
func (repo fakeGitRepo) catFileResponse(t *testing.T, args []string) (gitResult, error) {
t.Helper()
require.Len(t, args, 3)
require.Equal(t, "-e", args[1])
spec := args[2]
if revision, ok := strings.CutSuffix(spec, "^{commit}"); ok {
if _, ok := repo.revisions[revision]; ok {
return gitResult{}, nil
}
return gitFailure(fmt.Sprintf("fatal: bad revision %q", revision))
}
revision, path := splitRevisionPath(t, spec)
if _, ok := repo.revisions[revision][path]; ok {
return gitResult{}, nil
}
return gitFailure(fmt.Sprintf("fatal: path %q does not exist in %q", path, revision))
}
func (repo fakeGitRepo) showResponse(t *testing.T, args []string) (gitResult, error) {
t.Helper()
require.Len(t, args, 2)
revision, path := splitRevisionPath(t, args[1])
content, ok := repo.revisions[revision][path]
if !ok {
return gitFailure(fmt.Sprintf("fatal: path %q does not exist in %q", path, revision))
}
return gitResult{Stdout: content}, nil
}
func (repo fakeGitRepo) lsTreeResponse(t *testing.T, args []string) (gitResult, error) {
t.Helper()
separator := slices.Index(args, "--")
require.Greater(t, separator, 1)
require.Less(t, separator+1, len(args))
revision := args[separator-1]
pathspec := cleanGitPath(args[separator+1])
files := make([]string, 0)
for filePath := range repo.revisions[revision] {
cleanPath := cleanGitPath(filePath)
if pathspec != "." && !strings.HasPrefix(cleanPath, pathspec+"/") && cleanPath != pathspec {
continue
}
files = append(files, cleanPath)
}
slices.Sort(files)
return gitResult{Stdout: strings.Join(files, "\x00") + "\x00"}, nil
}
func (repo fakeGitRepo) mergeBaseResponse(t *testing.T, args []string) (gitResult, error) {
t.Helper()
require.Len(t, args, 3)
key := gitKey(args...)
if repo.mergeBases != nil {
if base, ok := repo.mergeBases[key]; ok {
return gitResult{Stdout: base + "\n"}, nil
}
}
left := args[1]
if _, ok := repo.revisions[left]; ok {
return gitResult{Stdout: left + "\n"}, nil
}
return gitFailure(fmt.Sprintf("fatal: no merge base for %s and %s", args[1], args[2]))
}
func (repo fakeGitRepo) revParseResponse(t *testing.T, args []string) (gitResult, error) {
t.Helper()
require.Equal(t, []string{"rev-parse", "HEAD"}, args)
head := repo.headSHA
if head == "" {
head = "head"
}
return gitResult{Stdout: head + "\n"}, nil
}
func splitRevisionPath(t *testing.T, spec string) (revision string, path string) {
t.Helper()
revision, path, ok := strings.Cut(spec, ":")
require.True(t, ok)
return revision, cleanGitPath(path)
}
func gitFailure(stderr string) (gitResult, error) {
return gitResult{}, errors.New(stderr)
}
func gitKey(args ...string) string {
// NUL is a stable separator because git diff pathspecs can contain spaces.
return strings.Join(args, "\x00")
}