-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_test.go
287 lines (270 loc) · 6.39 KB
/
git_test.go
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
// Copyright 2018 The gg Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package git
import (
"context"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"gg-scm.io/pkg/git/internal/filesystem"
"github.com/google/go-cmp/cmp"
)
var _ Piper = new(Local)
func TestLocalCommand(t *testing.T) {
gitPath, err := findGit()
if err != nil {
t.Skip("git not found:", err)
}
tests := []struct {
name string
env []string
wantEnv []string
}{
{
name: "NilEnv",
env: nil,
wantEnv: os.Environ(),
},
{
name: "EmptyEnv",
env: []string{},
wantEnv: []string{},
},
{
name: "FooEnv",
env: []string{"FOO=bar"},
wantEnv: []string{"FOO=bar"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
dir := t.TempDir()
var hookArgs []string
var env []string
if test.env != nil {
env = append([]string{}, test.env...)
}
l, err := NewLocal(Options{
GitExe: gitPath,
Env: env,
LogHook: func(_ context.Context, args []string) {
hookArgs = append([]string(nil), args...)
},
})
if err != nil {
t.Fatal(err)
}
c, err := l.command(ctx, &Invocation{
Dir: dir,
Args: []string{"commit", "-m", "Hello, World!"},
})
if err != nil {
t.Fatal(err)
}
if c.Path != gitPath {
t.Errorf("c.Path = %q; want %q", c.Path, gitPath)
}
if len(c.Args) == 0 {
t.Error("len(c.Args) = 0; want 4")
} else {
if got, want := filepath.Base(c.Args[0]), filepath.Base(gitPath); got != want {
t.Errorf("c.Args[0], filepath.Base(c.Args[0]) = %q, %q; want %q, %q", c.Args[0], got, gitPath, want)
}
if got, want := c.Args[1:], ([]string{"commit", "-m", "Hello, World!"}); !cmp.Equal(got, want) {
t.Errorf("c.Args[1:] = %q; want %q", got, want)
}
}
if !cmp.Equal(c.Env, test.wantEnv) {
t.Errorf("c.Env = %#v; want %#v", c.Env, test.wantEnv)
}
if c.Dir != dir {
t.Errorf("c.Dir = %q; want %q", c.Dir, dir)
}
if want := ([]string{"commit", "-m", "Hello, World!"}); !cmp.Equal(hookArgs, want) {
t.Errorf("log hook args = %q; want %q", hookArgs, want)
}
})
}
}
func TestRun(t *testing.T) {
gitPath, err := findGit()
if err != nil {
t.Skip("git not found:", err)
}
ctx := context.Background()
env, err := newTestEnv(ctx, gitPath)
if err != nil {
t.Fatal(err)
}
defer env.cleanup()
if err := env.g.Run(ctx, "init", "repo"); err != nil {
t.Error("Run:", err)
}
gitDir := env.root.FromSlash("repo/.git")
info, err := os.Stat(gitDir)
if err != nil {
t.Fatal(err)
}
if !info.IsDir() {
t.Errorf("%s is not a git directory", gitDir)
}
}
func TestOutput(t *testing.T) {
gitPath, err := findGit()
if err != nil {
t.Skip("git not found:", err)
}
ctx := context.Background()
env, err := newTestEnv(ctx, gitPath)
if err != nil {
t.Fatal(err)
}
defer env.cleanup()
if err := env.g.Run(ctx, "init"); err != nil {
t.Fatal(err)
}
out, err := env.g.Output(ctx, "config", "core.bare")
if out != "false\n" || err != nil {
t.Errorf("Output(ctx, \"config\", \"core.bare\") = %q, %v; want \"false\\n\", <nil>", out, err)
}
}
func TestIndexCommand(t *testing.T) {
tests := []struct {
args []string
want int
}{
{
args: nil,
want: 0,
},
{
args: []string{"diff"},
want: 0,
},
{
args: []string{"diff", "-p"},
want: 0,
},
{
args: []string{"-p", "diff", "-p"},
want: 1,
},
{
args: []string{"-C", "foo", "diff", "-p"},
want: 2,
},
{
args: []string{"-pC", "foo", "diff", "-p"},
want: 2,
},
{
args: []string{"--work-tree", "foo"},
want: 2,
},
{
args: []string{"--work-tree=foo", "foo"},
want: 1,
},
{
args: []string{"--work-treex", "foo"},
want: 1,
},
{
args: []string{"--work-treex", "foo", "bar"},
want: 1,
},
}
for _, test := range tests {
if got := indexCommand(test.args); got != test.want {
t.Errorf("indexCommand(%q) = %d; want %d", test.args, got, test.want)
}
}
}
type testEnv struct {
top filesystem.Dir
root filesystem.Dir
g *Git
}
func newTestEnv(ctx context.Context, gitExe string) (*testEnv, error) {
topPath, err := os.MkdirTemp("", "gg_git_test")
if err != nil {
return nil, err
}
// Always evaluate symlinks in the root directory path so as to make path
// comparisons easier (simple equality). This is mostly relevant on macOS.
topPath, err = filepath.EvalSymlinks(topPath)
if err != nil {
return nil, err
}
top := filesystem.Dir(topPath)
if err := top.Apply(filesystem.Mkdir("scratch")); err != nil {
os.RemoveAll(topPath)
return nil, err
}
root := filesystem.Dir(top.FromSlash("scratch"))
g, err := New(Options{
GitExe: gitExe,
Dir: root.String(),
Env: []string{
"GIT_CONFIG_NOSYSTEM=1",
"HOME=" + topPath,
"TERM=xterm-color", // stops git from assuming output is to a "dumb" terminal
},
})
if err != nil {
os.RemoveAll(topPath)
return nil, err
}
const miniConfig = "[user]\nname = User\nemail = [email protected]\n"
if err := top.Apply(filesystem.Write(".gitconfig", miniConfig)); err != nil {
os.RemoveAll(topPath)
return nil, err
}
return &testEnv{top: top, root: root, g: g}, nil
}
func (env *testEnv) cleanup() {
os.RemoveAll(env.top.String())
}
// prettyCommit annotates the hex-encoded hash with a name if present
// in the given map.
func prettyCommit(h Hash, names map[Hash]string) string {
n := names[h]
if n == "" {
return h.String()
}
return h.String() + " (" + n + ")"
}
var gitPathCache struct {
mu sync.Mutex
val string
}
func findGit() (string, error) {
defer gitPathCache.mu.Unlock()
gitPathCache.mu.Lock()
if gitPathCache.val != "" {
return gitPathCache.val, nil
}
path, err := exec.LookPath("git")
if err != nil {
return "", err
}
gitPathCache.val = path
return path, nil
}
// dummyContent is a non-empty string that is used in tests where the
// exact data is not relevant to the test.
const dummyContent = "Hello, World!\n"