forked from uber-go/gopatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_test.go
407 lines (349 loc) · 11.7 KB
/
e2e_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
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"github.com/rogpeppe/go-internal/txtar"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
)
func TestIntegration(t *testing.T) {
const testdata = "testdata"
infos, err := os.ReadDir(testdata)
require.NoErrorf(t, err, "failed to ls %q", testdata)
// Disable Go modules so that we don't try to fetch against tests.
oldModule := os.Getenv("GO111MODULE")
os.Setenv("GO111MODULE", "off")
defer os.Setenv("GO111MODULE", oldModule)
for _, info := range infos {
if info.IsDir() || info.Name() == "README.md" {
continue
}
t.Run(info.Name(), func(t *testing.T) {
runIntegrationTest(t, filepath.Join(testdata, info.Name()))
})
}
}
func resolvePatchPath(t *testing.T, path string) string {
// If a patch file takes the form,
// => another/file.patch
// Replace its path with the provided path.
const linkPrefix = "=>"
f, err := os.Open(path)
require.NoError(t, err, "open patch %q", path)
defer f.Close()
scanner := bufio.NewScanner(f)
if !scanner.Scan() {
return path
}
line := scanner.Text()
// Not a link to anothe rfile. Return the original path
// unchanged.
if !strings.HasPrefix(line, linkPrefix) {
return path
}
return strings.TrimSpace(strings.TrimPrefix(line, linkPrefix))
}
func runIntegrationTest(t *testing.T, testFile string) {
ta, err := loadTestArchive(testFile)
require.NoError(t, err, "failed to load tests from txtar")
dir := t.TempDir()
require.NoErrorf(t, txtar.Write(ta.Archive, dir),
"could not write archive to %q", dir)
var (
args []string // args for run()
stdin []byte
)
// If there's only one patch, use stdin. Otherwise, use "-p".
if ps := ta.Patches; len(ps) == 1 {
path := resolvePatchPath(t, filepath.Join(dir, ps[0]))
stdin, err = os.ReadFile(path)
require.NoError(t, err, "failed to read patch file %q", ps)
} else {
for _, path := range ps {
path = resolvePatchPath(t, filepath.Join(dir, path))
args = append(args, "-p", path)
}
}
run := func(extraArgs ...string) (stdout, stderr string, err error) {
var stdoutbuf, stderrbuf bytes.Buffer
cmd := mainCmd{
Stdin: bytes.NewReader(stdin),
Stdout: &stdoutbuf,
Stderr: &stderrbuf,
Getwd: func() (string, error) {
// cwd is always the directory inside which we
// extracted the txtar.
return dir, nil
},
}
err = cmd.Run(append(extraArgs, args...))
return stdoutbuf.String(), stderrbuf.String(), err
}
for _, tt := range ta.Files {
filePath := tt.Give
t.Run(tt.Name, func(t *testing.T) {
t.Run("skipImportProcessing", func(t *testing.T) {
if includeSkipImportTest(testFile, tt.Name) {
stdout, stderr, err := run("--skip-import-processing", "-d", filePath)
require.NoError(t, err, "could not run patch")
assert.Equal(t,
string(tt.WantSkipImportProcessing),
stdout,
"output of --skip-import-processing did not match")
assert.Equal(t, string(tt.WantComment), stderr)
}
})
if skipTest(testFile, tt.Name) {
t.Skipf("skipping unfixed test case %v/%v", testFile, tt.Name)
}
t.Run("diff", func(t *testing.T) {
stdout, stderr, err := run("-d", filePath)
require.NoError(t, err, "could not run patch")
assert.Equal(t,
string(tt.WantDiff),
stdout,
"output of --diff did not match")
assert.Equal(t, string(tt.WantComment), stderr)
})
t.Run("print", func(t *testing.T) {
stdout, stderr, err := run("--print-only", filePath)
require.NoError(t, err, "could not run patch")
assert.Equal(t, string(tt.Want), stdout)
assert.Equal(t, string(tt.WantComment), stderr)
})
_, _, err := run(filePath)
require.NoError(t, err, "could not run patch")
got, err := os.ReadFile(filepath.Join(dir, filePath))
require.NoError(t, err, "failed to read %q", filePath)
assert.Equal(t, string(tt.Want), string(got))
})
}
}
func TestGeneratedE2e(t *testing.T) {
patch := "testdata/patch/time.patch"
t.Parallel()
tests := []struct {
name string
path string
want string
}{
{
name: "go special comment for generated code",
path: "testdata/test_files/skip_generated_files/special_notation_generated.go",
},
{
name: "@generated",
path: "testdata/test_files/skip_generated_files/simple_generated.go",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
want, err := os.ReadFile(tt.path)
require.NoError(t, err)
var stdoutbuf bytes.Buffer
cmd := mainCmd{
Stdout: &stdoutbuf,
Getwd: os.Getwd,
}
err = cmd.Run([]string{"--skip-generated", "-p", patch, tt.path})
require.NoError(t, err, "could not run patch")
assert.Empty(t, stdoutbuf)
// verify the target file's content didn't modify.
got, err := os.ReadFile(tt.path)
require.NoError(t, err)
assert.Equal(t, want, got)
})
}
}
// includeSkipImportTest returns whether we should include a testfile for
// the skipImportProcessing flag testing
func includeSkipImportTest(testFile, testName string) bool {
fullName := filepath.Join(filepath.Base(testFile), testName)
_, enable := groupImportTestFiles[fullName]
return enable
}
// skipTest returns whether we should skip a testfile
func skipTest(testFile, testName string) bool {
fullName := filepath.Join(filepath.Base(testFile), testName)
_, skip := testsToSkip[fullName]
return skip
}
// testsToSkip is a set of integration tests that do not yet pass, but
// eventually should. It is essentially a todo list of bugs to be fixed.
// For now, skip these tests.
var testsToSkip = map[string]struct{}{
"add_error_param/a": {}, // https://github.com/uber-go/gopatch/issues/6
"func_within_a_func/two_dots": {},
"mismatched_dots/unnamed": {}, // https://github.com/uber-go/gopatch/issues/9
"switch_elision/body": {},
"select_elision/example": {},
"case_elision/a": {},
"httpclient_use_ctx/one_return": {},
"const_to_var/single_top_level": {},
"struct_field_list/zero": {},
"struct_field_list/middle": {},
"value_group_elision/func": {}, // https://github.com/uber-go/gopatch/issues/6
"value_list_elision/foo": {}, // https://github.com/uber-go/gopatch/issues/6
"range_value_elision/no_params": {},
"range_value_elision/one_param": {},
"range_value_elision/two_params": {},
}
// groupImportTestFiles is a set of integration tests that test the
// --skip-import-processing flag
var groupImportTestFiles = map[string]struct{}{
"noop_import/remove_some": {},
}
const (
_patch = ".patch"
_in = ".in.go"
_out = ".out.go"
_go = ".go"
_diff = ".diff"
_skipImportProcessing = ".groupimports"
_stderr = ".diff.stderr"
)
type testArchive struct {
Archive *txtar.Archive
// Names of files in the archive that are patches.
Patches []string
// Integration test files found in the patch.
Files []*testFile
}
type testFile struct {
// Name of the test file.
//
// Specified in the name of the go file before the .in.go/.out.go.
Name string
// Path to the input Go file.
Give string
// Expected contents of the Go file after the patches have been
// applied.
Want []byte
// Expected diff after the patches have been applied with
// import processing skipped.
WantSkipImportProcessing []byte
// Expected diff if tool run in --diff mode
WantDiff []byte
// Expected comment if tool run in --diff mode
WantComment []byte
}
// Loads a test archive in-memory.
func loadTestArchive(path string) (*testArchive, error) {
// test case name -> test case
index := make(map[string]*testFile)
// Retrieves test cases by name, adding to the map if needed.
getTestFile := func(name string) *testFile {
tf := index[name]
if tf == nil {
tf = &testFile{Name: name}
index[name] = tf
}
return tf
}
archive, err := txtar.ParseFile(path)
if err != nil {
return nil, fmt.Errorf("failed to open %q: %v", path, err)
}
// Rather than reproducing the entire contents of the txtar, we only
// want the patches and input files. The contents of the output files
// are needed in-memory.
var patches []string // names of patch files
newFiles := archive.Files[:0] // zero-alloc filtering
for _, f := range archive.Files {
switch {
case strings.HasSuffix(f.Name, _patch):
patches = append(patches, f.Name)
case strings.HasSuffix(f.Name, _in):
name := strings.TrimSuffix(f.Name, _in) // foo.in.go => foo
// Replace the .in.go suffix with just .go so that
// test cases have more control over the name of the
// file when it affects the behavior of `go list`
// (test files, for example).
f.Name = name + _go // foo.in.go => foo.go
f.Data = singleTrailingNewline(f.Data)
getTestFile(name).Give = f.Name
case strings.HasSuffix(f.Name, _diff):
name := strings.TrimSuffix(f.Name, _diff) // foo.diff => foo
getTestFile(name).WantDiff = singleTrailingNewline(f.Data)
case strings.HasSuffix(f.Name, _skipImportProcessing):
name := strings.TrimSuffix(f.Name, _skipImportProcessing) // foo.groupimports => foo
getTestFile(name).WantSkipImportProcessing = singleTrailingNewline(f.Data)
case strings.HasSuffix(f.Name, _stderr):
name := strings.TrimSuffix(f.Name, _stderr)
getTestFile(name).WantComment = singleTrailingNewline(f.Data)
case strings.HasSuffix(f.Name, _out):
name := strings.TrimSuffix(f.Name, _out) // foo.out.go => foo
getTestFile(name).Want = singleTrailingNewline(f.Data)
// Don't include this file in the list of files
// reproduced by the archive.
continue
default:
err = multierr.Append(err,
fmt.Errorf("unknown file %q found in %q", f.Name, path))
}
newFiles = append(newFiles, f)
}
archive.Files = newFiles
if len(patches) == 0 {
err = multierr.Append(err, fmt.Errorf("no patches found in %q", path))
}
if len(index) == 0 {
err = multierr.Append(err, fmt.Errorf("no Go files found in %q", path))
}
files := make([]*testFile, 0, len(index))
for _, tt := range index {
if len(tt.Give) == 0 {
err = multierr.Append(err, fmt.Errorf(
"test %q of %q does not have an input file", tt.Name, path))
}
if len(tt.Want) == 0 {
err = multierr.Append(err, fmt.Errorf(
"test %q of %q does not have an output file", tt.Name, path))
}
files = append(files, tt)
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name < files[j].Name
})
return &testArchive{
Archive: archive,
Patches: patches,
Files: files,
}, nil
}
// Removes all but the last trailing newline from a slice.
//
// Makes for easier to read test cases.
func singleTrailingNewline(bs []byte) []byte {
i := len(bs) - 1
for i > 0 && bs[i] == '\n' && bs[i-1] == '\n' {
i--
}
return bs[:i+1]
}