1
1
package test
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"os/exec"
6
7
"path/filepath"
@@ -16,58 +17,68 @@ const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
16
17
17
18
func TestFix (t * testing.T ) {
18
19
testshared .SkipOnWindows (t )
20
+ testshared .InstallGolangciLint (t )
21
+ sourcesDir := filepath .Join (testdataDir , "fix" )
19
22
20
- tmpDir := filepath . Join ( testdataDir , "fix.tmp" )
21
- _ = os . RemoveAll ( tmpDir ) // cleanup previous runs
22
-
23
- if os . Getenv ( envKeepTempFiles ) == "1" {
24
- t . Logf ( "Temp dir for fix test: %s" , tmpDir )
25
- } else {
26
- t . Cleanup ( func () { _ = os . RemoveAll ( tmpDir ) })
23
+ tests := [] struct {
24
+ Args [] string
25
+ DirSuffix string
26
+ } {
27
+ {[] string {}, "" },
28
+ {[] string { "--path-prefix=simple-prefix" }, "-simple-prefix" },
29
+ {[] string { "--path-prefix=slash-prefix/" }, "-slash-prefix" },
27
30
}
28
31
29
- sourcesDir := filepath .Join (testdataDir , "fix" )
32
+ for _ , test := range tests {
33
+ tmpDir := filepath .Join (testdataDir , fmt .Sprintf ("fix%s.tmp" , test .DirSuffix ))
34
+ _ = os .RemoveAll (tmpDir ) // cleanup previous runs
30
35
31
- err := exec .Command ("cp" , "-R" , sourcesDir , tmpDir ).Run ()
32
- require .NoError (t , err )
36
+ if os .Getenv (envKeepTempFiles ) == "1" {
37
+ t .Logf ("Temp dir for fix with args %v test: %s" , test .Args , tmpDir )
38
+ } else {
39
+ t .Cleanup (func () { _ = os .RemoveAll (tmpDir ) })
40
+ }
33
41
34
- testshared .InstallGolangciLint (t )
42
+ err := exec .Command ("cp" , "-R" , sourcesDir , tmpDir ).Run ()
43
+ require .NoError (t , err )
35
44
36
- sources := findSources (t , tmpDir , "in" , "*.go" )
45
+ sources := findSources (t , tmpDir , "in" , "*.go" )
37
46
38
- for _ , input := range sources {
39
- input := input
40
- t .Run (filepath .Base (input ), func (t * testing.T ) {
41
- t .Parallel ()
47
+ for _ , input := range sources {
48
+ input := input
49
+ t .Run (filepath .Base (input )+ test . DirSuffix , func (t * testing.T ) {
50
+ t .Parallel ()
42
51
43
- rc := testshared .ParseTestDirectives (t , input )
44
- if rc == nil {
45
- t .Logf ("Skipped: %s" , input )
46
- return
47
- }
52
+ rc := testshared .ParseTestDirectives (t , input )
53
+ if rc == nil {
54
+ t .Logf ("Skipped: %s" , input )
55
+ return
56
+ }
48
57
49
- testshared .NewRunnerBuilder (t ).
50
- WithArgs (
58
+ args := []string {
51
59
"--disable-all" ,
52
60
"--print-issued-lines=false" ,
53
61
"--print-linter-name=false" ,
54
62
"--out-format=line-number" ,
55
63
"--fix" ,
56
- "--path-prefix=mock" ,
57
- ).
58
- WithRunContext (rc ).
59
- WithTargetPath (input ).
60
- Runner ().
61
- Run ().
62
- ExpectExitCode (rc .ExitCode )
63
-
64
- output , err := os .ReadFile (input )
65
- require .NoError (t , err )
66
-
67
- expectedOutput , err := os .ReadFile (filepath .Join (testdataDir , "fix" , "out" , filepath .Base (input )))
68
- require .NoError (t , err )
69
-
70
- require .Equal (t , string (expectedOutput ), string (output ))
71
- })
64
+ }
65
+ args = append (args , test .Args ... )
66
+ testshared .NewRunnerBuilder (t ).
67
+ WithArgs (args ... ).
68
+ WithRunContext (rc ).
69
+ WithTargetPath (input ).
70
+ Runner ().
71
+ Run ().
72
+ ExpectExitCode (rc .ExitCode )
73
+
74
+ output , err := os .ReadFile (input )
75
+ require .NoError (t , err )
76
+
77
+ expectedOutput , err := os .ReadFile (filepath .Join (testdataDir , "fix" , "out" , filepath .Base (input )))
78
+ require .NoError (t , err )
79
+
80
+ require .Equal (t , string (expectedOutput ), string (output ))
81
+ })
82
+ }
72
83
}
73
84
}
0 commit comments