github.com/nakabonne/golangci-lint@v1.26.1/test/fix_test.go (about) 1 package test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "testing" 9 10 assert "github.com/stretchr/testify/require" 11 yaml "gopkg.in/yaml.v2" 12 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 assert.NoError(t, err) 20 assert.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 defer os.RemoveAll(tmpDir) 31 } 32 33 fixDir := filepath.Join(testdataDir, "fix") 34 err := exec.Command("cp", "-R", fixDir, tmpDir).Run() 35 assert.NoError(t, err) 36 37 inputs := findSources(tmpDir, "in", "*.go") 38 for _, input := range inputs { 39 input := input 40 t.Run(filepath.Base(input), func(t *testing.T) { 41 args := []string{ 42 "--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", 43 "--fix", 44 input, 45 } 46 rc := extractRunContextFromComments(t, input) 47 args = append(args, rc.args...) 48 49 cfg, err := yaml.Marshal(rc.config) 50 assert.NoError(t, err) 51 52 testshared.NewLintRunner(t).RunWithYamlConfig(string(cfg), args...) 53 output, err := ioutil.ReadFile(input) 54 assert.NoError(t, err) 55 56 expectedOutput, err := ioutil.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input))) 57 assert.NoError(t, err) 58 59 assert.Equal(t, string(expectedOutput), string(output)) 60 }) 61 } 62 }