4
4
"os"
5
5
"os/exec"
6
6
"path/filepath"
7
- "sort"
8
7
"testing"
9
8
10
9
"github.com/stretchr/testify/require"
@@ -15,7 +14,9 @@ import (
15
14
// value: "1"
16
15
const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
17
16
18
- func TestFix (t * testing.T ) {
17
+ func setupTestFix (t * testing.T ) []string {
18
+ t .Helper ()
19
+
19
20
testshared .SkipOnWindows (t )
20
21
21
22
tmpDir := filepath .Join (testdataDir , "fix.tmp" )
@@ -34,44 +35,67 @@ func TestFix(t *testing.T) {
34
35
35
36
testshared .InstallGolangciLint (t )
36
37
37
- sources := findSources (t , tmpDir , "in" , "*.go" )
38
+ return findSources (t , tmpDir , "in" , "*.go" )
39
+ }
38
40
39
- // The combination with --path gets tested for the first test case.
40
- // This is arbitrary. It could also be tested for all of them,
41
- // but then each test would have to run twice (with and without).
42
- // To make this determinstic, the sources get sorted by name.
43
- sort .Strings (sources )
41
+ func TestFix (t * testing.T ) {
42
+ sources := setupTestFix (t )
44
43
45
- for i , input := range sources {
46
- withPathPrefix := i == 0
44
+ for _ , input := range sources {
47
45
input := input
48
46
t .Run (filepath .Base (input ), func (t * testing.T ) {
49
47
t .Parallel ()
50
48
51
49
rc := testshared .ParseTestDirectives (t , input )
52
50
if rc == nil {
53
- if withPathPrefix {
54
- t .Errorf ("The testcase %s should not get skipped, it's used for testing --path." , input )
55
- return
56
- }
57
51
t .Logf ("Skipped: %s" , input )
58
52
return
59
53
}
60
54
61
- args := []string {
62
- "--disable-all" ,
63
- "--print-issued-lines=false" ,
64
- "--print-linter-name=false" ,
65
- "--out-format=line-number" ,
66
- "--fix" ,
67
- }
68
- if withPathPrefix {
69
- t .Log ("Testing with --path-prefix." )
70
- // This must not break fixing...
71
- args = append (args , "--path-prefix=foobar/" )
55
+ testshared .NewRunnerBuilder (t ).
56
+ WithArgs ("--disable-all" ,
57
+ "--print-issued-lines=false" ,
58
+ "--print-linter-name=false" ,
59
+ "--out-format=line-number" ,
60
+ "--fix" ).
61
+ WithRunContext (rc ).
62
+ WithTargetPath (input ).
63
+ Runner ().
64
+ Run ().
65
+ ExpectExitCode (rc .ExitCode )
66
+
67
+ output , err := os .ReadFile (input )
68
+ require .NoError (t , err )
69
+
70
+ expectedOutput , err := os .ReadFile (filepath .Join (testdataDir , "fix" , "out" , filepath .Base (input )))
71
+ require .NoError (t , err )
72
+
73
+ require .Equal (t , string (expectedOutput ), string (output ))
74
+ })
75
+ }
76
+ }
77
+
78
+ func TestFix_pathPrefix (t * testing.T ) {
79
+ sources := setupTestFix (t )
80
+
81
+ for _ , input := range sources {
82
+ input := input
83
+ t .Run (filepath .Base (input ), func (t * testing.T ) {
84
+ t .Parallel ()
85
+
86
+ rc := testshared .ParseTestDirectives (t , input )
87
+ if rc == nil {
88
+ t .Logf ("Skipped: %s" , input )
89
+ return
72
90
}
91
+
73
92
testshared .NewRunnerBuilder (t ).
74
- WithArgs (args ... ).
93
+ WithArgs ("--disable-all" ,
94
+ "--print-issued-lines=false" ,
95
+ "--print-linter-name=false" ,
96
+ "--out-format=line-number" ,
97
+ "--fix" ,
98
+ "--path-prefix=foobar/" ).
75
99
WithRunContext (rc ).
76
100
WithTargetPath (input ).
77
101
Runner ().
0 commit comments