github.com/yuqengo/golangci-lint@v0.0.2/test/fix_test.go (about) 1 package test 2 3 import ( 4 "os" 5 "os/exec" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 "gopkg.in/yaml.v3" 11 12 "github.com/golangci/golangci-lint/pkg/exitcodes" 13 "github.com/golangci/golangci-lint/test/testshared" 14 ) 15 16 func TestFix(t *testing.T) { 17 findSources := func(pathPatterns ...string) []string { 18 sources, err := filepath.Glob(filepath.Join(pathPatterns...)) 19 require.NoError(t, err) 20 require.NotEmpty(t, sources) 21 return sources 22 } 23 24 tmpDir := filepath.Join(testdataDir, "fix.tmp") 25 os.RemoveAll(tmpDir) // cleanup after previous runs 26 27 if os.Getenv("GL_KEEP_TEMP_FILES") == "1" { 28 t.Logf("Temp dir for fix test: %s", tmpDir) 29 } else { 30 t.Cleanup(func() { 31 os.RemoveAll(tmpDir) 32 }) 33 } 34 35 fixDir := filepath.Join(testdataDir, "fix") 36 err := exec.Command("cp", "-R", fixDir, tmpDir).Run() 37 require.NoError(t, err) 38 39 inputs := findSources(tmpDir, "in", "*.go") 40 for _, input := range inputs { 41 input := input 42 t.Run(filepath.Base(input), func(t *testing.T) { 43 t.Parallel() 44 45 args := []string{ 46 "--go=1.17", // TODO(ldez): we force to use an old version of Go for the CI and the tests. 47 "--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", 48 "--allow-parallel-runners", "--fix", 49 input, 50 } 51 rc := extractRunContextFromComments(t, input) 52 if rc == nil { 53 t.Logf("Skipped: %s", input) 54 return 55 } 56 57 args = append(args, rc.args...) 58 59 cfg, err := yaml.Marshal(rc.config) 60 require.NoError(t, err) 61 62 var runResult *testshared.RunResult 63 if rc.configPath != "" { 64 args = append(args, "-c", rc.configPath) 65 runResult = testshared.NewLintRunner(t).RunCommand("run", args...) 66 } else { 67 runResult = testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...) 68 } 69 70 // nolintlint test uses non existing linters (bob, alice) 71 if rc.expectedLinter != "nolintlint" { 72 runResult.ExpectExitCode(exitcodes.Success) 73 } 74 75 output, err := os.ReadFile(input) 76 require.NoError(t, err) 77 78 expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) 79 require.NoError(t, err) 80 81 require.Equal(t, string(expectedOutput), string(output)) 82 }) 83 } 84 }