44 "os"
55 "os/exec"
66 "path/filepath"
7- "sort"
87 "testing"
98
109 "github.com/stretchr/testify/require"
@@ -15,7 +14,9 @@ import (
1514// value: "1"
1615const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
1716
18- func TestFix (t * testing.T ) {
17+ func setupTestFix (t * testing.T ) []string {
18+ t .Helper ()
19+
1920 testshared .SkipOnWindows (t )
2021
2122 tmpDir := filepath .Join (testdataDir , "fix.tmp" )
@@ -34,44 +35,67 @@ func TestFix(t *testing.T) {
3435
3536 testshared .InstallGolangciLint (t )
3637
37- sources := findSources (t , tmpDir , "in" , "*.go" )
38+ return findSources (t , tmpDir , "in" , "*.go" )
39+ }
3840
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 )
4443
45- for i , input := range sources {
46- withPathPrefix := i == 0
44+ for _ , input := range sources {
4745 input := input
4846 t .Run (filepath .Base (input ), func (t * testing.T ) {
4947 t .Parallel ()
5048
5149 rc := testshared .ParseTestDirectives (t , input )
5250 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- }
5751 t .Logf ("Skipped: %s" , input )
5852 return
5953 }
6054
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
7290 }
91+
7392 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/" ).
7599 WithRunContext (rc ).
76100 WithTargetPath (input ).
77101 Runner ().
0 commit comments