2019-02-16 21:23:57 +03:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-03-13 04:45:32 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-16 21:23:57 +03:00
|
|
|
|
|
|
|
"github.com/golangci/golangci-lint/test/testshared"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFix(t *testing.T) {
|
2022-08-24 22:10:51 +02:00
|
|
|
testshared.SkipOnWindows(t)
|
|
|
|
|
2019-02-16 21:23:57 +03:00
|
|
|
tmpDir := filepath.Join(testdataDir, "fix.tmp")
|
2022-08-20 18:53:45 +02:00
|
|
|
_ = os.RemoveAll(tmpDir) // cleanup previous runs
|
2019-02-16 21:23:57 +03:00
|
|
|
|
2019-09-10 16:56:44 +03:00
|
|
|
if os.Getenv("GL_KEEP_TEMP_FILES") == "1" {
|
|
|
|
t.Logf("Temp dir for fix test: %s", tmpDir)
|
|
|
|
} else {
|
2022-08-20 18:53:45 +02:00
|
|
|
t.Cleanup(func() { _ = os.RemoveAll(tmpDir) })
|
2019-02-16 21:23:57 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:53:45 +02:00
|
|
|
sourcesDir := filepath.Join(testdataDir, "fix")
|
|
|
|
|
|
|
|
err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run()
|
2021-03-13 04:45:32 +01:00
|
|
|
require.NoError(t, err)
|
2019-02-16 21:23:57 +03:00
|
|
|
|
2022-08-15 21:56:32 +02:00
|
|
|
testshared.InstallGolangciLint(t)
|
|
|
|
|
2022-08-20 18:53:45 +02:00
|
|
|
sources := findSources(t, tmpDir, "in", "*.go")
|
|
|
|
|
|
|
|
for _, input := range sources {
|
2019-02-16 21:23:57 +03:00
|
|
|
input := input
|
|
|
|
t.Run(filepath.Base(input), func(t *testing.T) {
|
2021-02-20 07:38:39 -08:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-08-15 21:56:32 +02:00
|
|
|
rc := testshared.ParseTestDirectives(t, input)
|
2022-04-06 10:44:20 +02:00
|
|
|
if rc == nil {
|
|
|
|
t.Logf("Skipped: %s", input)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-20 18:53:45 +02:00
|
|
|
testshared.NewRunnerBuilder(t).
|
2022-08-15 21:56:32 +02:00
|
|
|
WithArgs(
|
|
|
|
"--disable-all",
|
|
|
|
"--print-issued-lines=false",
|
|
|
|
"--print-linter-name=false",
|
|
|
|
"--out-format=line-number",
|
2022-08-20 18:53:45 +02:00
|
|
|
"--fix",
|
|
|
|
).
|
|
|
|
WithRunContext(rc).
|
|
|
|
WithTargetPath(input).
|
2022-08-15 21:56:32 +02:00
|
|
|
Runner().
|
2022-08-20 18:53:45 +02:00
|
|
|
Run().
|
|
|
|
ExpectExitCode(rc.ExitCode)
|
2022-01-04 07:10:25 +01:00
|
|
|
|
2021-11-02 03:21:26 +08:00
|
|
|
output, err := os.ReadFile(input)
|
2021-03-13 04:45:32 +01:00
|
|
|
require.NoError(t, err)
|
2019-02-16 21:23:57 +03:00
|
|
|
|
2021-11-02 03:21:26 +08:00
|
|
|
expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
|
2021-03-13 04:45:32 +01:00
|
|
|
require.NoError(t, err)
|
2019-02-16 21:23:57 +03:00
|
|
|
|
2021-03-13 04:45:32 +01:00
|
|
|
require.Equal(t, string(expectedOutput), string(output))
|
2019-02-16 21:23:57 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|