Skip to content

Commit 3aa455c

Browse files
committed
test(fix): test various path prefix cases separately
1 parent f0211f6 commit 3aa455c

File tree

1 file changed

+50
-39
lines changed

1 file changed

+50
-39
lines changed

test/fix_test.go

+50-39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67
"path/filepath"
@@ -16,58 +17,68 @@ const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
1617

1718
func TestFix(t *testing.T) {
1819
testshared.SkipOnWindows(t)
20+
testshared.InstallGolangciLint(t)
21+
sourcesDir := filepath.Join(testdataDir, "fix")
1922

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"},
2730
}
2831

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
3035

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+
}
3341

34-
testshared.InstallGolangciLint(t)
42+
err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run()
43+
require.NoError(t, err)
3544

36-
sources := findSources(t, tmpDir, "in", "*.go")
45+
sources := findSources(t, tmpDir, "in", "*.go")
3746

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()
4251

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+
}
4857

49-
testshared.NewRunnerBuilder(t).
50-
WithArgs(
58+
args := []string{
5159
"--disable-all",
5260
"--print-issued-lines=false",
5361
"--print-linter-name=false",
5462
"--out-format=line-number",
5563
"--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+
}
7283
}
7384
}

0 commit comments

Comments
 (0)