-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_test.go
436 lines (386 loc) · 8.56 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package main
import (
"context"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/kr/pretty"
)
// test data
var maingo = []byte(`
package main
var a, b int = 10, 20
func main() {
fmt.Printf("%+v\n", add(a, b))
}
func add(a, b int) {
return a + b
}
`)
var mathgo = []byte(`
package main
const PI = 3.14
func sub(a, b) int {
return a - b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
`)
var mathgo_add_func = []byte(`
package main
const PI = 3.14
func sub(a, b) int {
return a - b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if min(a, b) == a {
return b
}
return a
}
`)
var mathgo_update_min_func = []byte(`
package main
const PI = 3.14
func sub(a, b) int {
return a - b
}
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func max(a, b int) int {
if min(a, b) == a {
return b
}
return a
}
`)
var mathgo_update_pkg_lvl_var_add_comment_change_func = []byte(`
package main
const PI = 3.14159
// sub func subtracts b from a
func sub(a, b) int {
return a - b
}
func min(a, b int) int {
if a < b {
return a
} else {
return b
}
}
func max(a, b int) int {
if b > a {
return b
}
return a
}
`)
var math_test_go = []byte(`
package main
func TestMin(t *testing.T) {
res := min(10, 20)
if res != 10 {
t.Errorf("expected 10, got %v", res)
}
}
`)
var math_test_go_test_max = []byte(`
package main
func TestMin(t *testing.T) {
res := min(10, 20)
if res != 10 {
t.Errorf("expected 10, got %v", res)
}
}
func TestMax(t *testing.T) {
res := max(10, 20)
if res != 20 {
t.Errorf("expected 20, got %v", res)
}
}
`)
var geogo = []byte(`
package main
func Perimeter(d, h int) int {
return 2*d + 2*h
}
`)
var geo_add_area = []byte(`
package main
func Perimeter(d, h int) int {
return 2*d + 2*h
}
func Area(d, h int) int {
return d * h
}
`)
var geo_area_func_rename = []byte(`
package main
func Perimeter(d, h int) int {
return 2*d + 2*h
}
func AreaRect(d, h int) int {
return d * h
}
`)
func TestGetDiff(t *testing.T) {
testDir := filepath.Join(os.TempDir(), "test_get_diff")
filePath := func(fname string) string {
return filepath.Join(testDir, fname)
}
files := map[string][]byte{
"main.go": maingo,
}
gitCmdRun := NewGitCmd(testDir)
setup := func() {
setupTestGitDir(t,
testDir, files,
[]string{"main.go"},
)
}
tearDown := func() {
if !t.Failed() {
// clean tmp dir on test success
_ = os.RemoveAll(testDir)
}
}
// prepare
setup()
defer tearDown()
// cases
cases := []struct {
desc string
setup, tearDown func() error
output []Change
expectedErr error
}{
{
desc: "Add new file, math.go, geo.go, math_test.go",
setup: func() error {
err := ioutil.WriteFile(filePath("math.go"), mathgo, 0600)
if err != nil {
return err
}
err = ioutil.WriteFile(filePath("geo.go"), geogo, 0600)
if err != nil {
return err
}
return ioutil.WriteFile(filePath("math_test.go"), math_test_go, 0600)
},
tearDown: func() error {
err := gitCmdRun("add", "math.go")
if err != nil {
return err
}
err = gitCmdRun("add", "math_test.go")
if err != nil {
return err
}
return gitCmdRun("commit", "-m", "add files")
},
output: []Change{
{"geo.go", "geo.go", 0, 0},
{"math.go", "math.go", 0, 0},
{"math_test.go", "math_test.go", 0, 0}},
},
{
desc: "Delete old file main.go",
setup: func() error {
return os.Remove(filePath("main.go"))
},
tearDown: func() error {
return gitCmdRun("commit", "-am", "changes")
},
output: []Change{{"geo.go", "geo.go", 0, 0}},
},
{
desc: "Change untracked file geo.go, add func Area",
setup: func() error {
return ioutil.WriteFile(filePath("geo.go"), geo_add_area, 0600)
},
output: []Change{{"geo.go", "geo.go", 0, 0}},
},
{
desc: "Commit untracked file geo.go",
setup: func() error {
err := gitCmdRun("add", "geo.go")
if err != nil {
return err
}
return gitCmdRun("commit", "-m", "add geo.go")
},
tearDown: nil,
output: nil,
},
{
desc: "Update file math.go with new func max with test",
setup: func() error {
err := ioutil.WriteFile(filePath("math.go"), mathgo_add_func, 0600)
if err != nil {
return err
}
return ioutil.WriteFile(filePath("math_test.go"), math_test_go_test_max, 0600)
},
tearDown: func() error {
return gitCmdRun("commit", "-am", "changes")
},
output: []Change{{"math.go", "math.go", 15, 7},
{"math_test.go", "math_test.go", 10, 7}},
},
{
desc: "Update file math.go, update func min",
setup: func() error {
return ioutil.WriteFile(filePath("math.go"), mathgo_update_min_func, 0600)
},
tearDown: func() error {
return gitCmdRun("commit", "-am", "changes")
},
output: []Change{{"math.go", "math.go", 5, 0},
{"math.go", "math.go", 13, 2},
{"math.go", "math.go", 15, 0},
{"math.go", "math.go", 18, 0},
},
},
{
desc: "Multiple updates to file math.go",
setup: func() error {
return ioutil.WriteFile(filePath("math.go"),
mathgo_update_pkg_lvl_var_add_comment_change_func, 0600)
},
tearDown: func() error {
return gitCmdRun("commit", "-am", "changes")
},
output: []Change{{"math.go", "math.go", 4, 0},
{"math.go", "math.go", 6, 0},
{"math.go", "math.go", 18, 0},
{"math.go", "math.go", 20, 0},
},
},
{
desc: "Change func name in file geo.go",
setup: func() error {
return ioutil.WriteFile(filePath("geo.go"), geo_area_func_rename, 0600)
},
output: []Change{{"geo.go", "geo.go", 8, 0}},
},
}
gitcmd := &GitCMD{testDir}
for i, tc := range cases {
// setup()
execTestHelper(t, i, tc.desc, tc.setup)
// should get line numbers by file and namespace
output, err := gitcmd.Diff(context.Background())
// teardown()
execTestHelper(t, i, tc.desc, tc.tearDown)
if isUnexpectedErr(t, i, tc.desc, tc.expectedErr, err) {
continue
}
diffs := pretty.Diff(tc.output, output)
if len(diffs) > 0 {
t.Errorf("case [%d] %s\nexpected %# v\ngot %# v", i, tc.desc, tc.output, output)
}
}
}
func TestCommitChangesTask(t *testing.T) {
testDir := filepath.Join(os.TempDir(), "test_commit_changes_task")
filePath := func(fname string) string {
return filepath.Join(testDir, fname)
}
files := map[string][]byte{
"main.go": maingo,
}
gitCmdRun := NewGitCmd(testDir)
setup := func() {
setupTestGitDir(t,
testDir, files,
[]string{"main.go"},
)
}
tearDown := func() {
if !t.Failed() {
// clean tmp dir on test success
_ = os.RemoveAll(testDir)
}
}
// prepare
setup()
defer tearDown()
// cases
cases := []struct {
desc string
ctx context.Context
in string
cmdErr error
cmdSuccess bool
setup, tearDown func() error
commitCmdLine string
output string
expectedErr error
}{
{
desc: "Add new file, math.go, geo.go, math_test.go",
ctx: context.Background(),
in: "Tests PASS: TestA$",
cmdErr: nil, cmdSuccess: true,
setup: func() error {
_ = ioutil.WriteFile(filePath("math.go"), mathgo, 0600)
_ = ioutil.WriteFile(filePath("geo.go"), geogo, 0600)
return ioutil.WriteFile(filePath("math_test.go"), math_test_go, 0600)
},
tearDown: func() error {
_ = gitCmdRun("add", "math.go", "math_test.go")
return gitCmdRun("commit", "-m", "add files")
},
commitCmdLine: "git -C /tmp/test_commit_changes_task commit -m 'auto_commit! Perimeter TestMin min sub'",
output: "'auto_commit! Perimeter TestMin min sub'",
expectedErr: nil,
},
// TODO add more test case
}
logger := log.New(os.Stdout, "gtr-test:", log.Ltime)
for i, tc := range cases {
// setup()
execTestHelper(t, i, tc.desc, tc.setup)
tc.ctx = context.WithValue(tc.ctx, prevTaskOutputKey, tc.in)
cmd := NewMockCommand(tc.cmdErr, tc.cmdSuccess)
output, err := CommitChanges(testDir, cmd.New)(logger, tc.ctx)
// teardown()
execTestHelper(t, i, tc.desc, tc.tearDown)
if isUnexpectedErr(t, i, tc.desc, tc.expectedErr, err) {
continue
}
cmdLineStr := strings.Join(cmd.GetArgs(), " ")
if tc.commitCmdLine != cmdLineStr {
t.Errorf("case [%d] %s\nexpected %# v\ngot %# v", i, tc.desc, tc.commitCmdLine, cmdLineStr)
}
if tc.output != output {
t.Errorf("case [%d] %s\nexpected %# v\ngot %# v", i, tc.desc, tc.output, output)
}
}
}
func NewGitCmd(workDir string) func(args ...string) error {
return func(args ...string) error {
gitCmd := exec.Command("git", "-C", workDir)
gitCmd.Args = append(gitCmd.Args, args...)
return gitCmd.Run()
}
}