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"
|
|
|
|
)
|
|
|
|
|
2022-09-06 13:48:25 +02:00
|
|
|
// value: "1"
|
|
|
|
const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
|
|
|
|
|
2023-03-20 21:44:36 +01:00
|
|
|
func setupTestFix(t *testing.T) []string {
|
|
|
|
t.Helper()
|
|
|
|
|
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
|
|
|
|
2022-09-06 13:48:25 +02:00
|
|
|
if os.Getenv(envKeepTempFiles) == "1" {
|
2019-09-10 16:56:44 +03:00
|
|
|
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)
|
|
|
|
|
2023-03-20 21:44:36 +01:00
|
|
|
return findSources(t, tmpDir, "in", "*.go")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFix(t *testing.T) {
|
|
|
|
sources := setupTestFix(t)
|
|
|
|
|
|
|
|
for _, input := range sources {
|
|
|
|
input := input
|
2023-08-09 12:09:20 +02:00
|
|
|
|
2023-03-20 21:44:36 +01:00
|
|
|
t.Run(filepath.Base(input), func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
rc := testshared.ParseTestDirectives(t, input)
|
|
|
|
if rc == nil {
|
|
|
|
t.Logf("Skipped: %s", input)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
testshared.NewRunnerBuilder(t).
|
|
|
|
WithArgs("--disable-all",
|
|
|
|
"--print-issued-lines=false",
|
|
|
|
"--print-linter-name=false",
|
|
|
|
"--out-format=line-number",
|
|
|
|
"--fix").
|
|
|
|
WithRunContext(rc).
|
|
|
|
WithTargetPath(input).
|
|
|
|
Runner().
|
|
|
|
Run().
|
|
|
|
ExpectExitCode(rc.ExitCode)
|
|
|
|
|
|
|
|
output, err := os.ReadFile(input)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, string(expectedOutput), string(output))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFix_pathPrefix(t *testing.T) {
|
|
|
|
sources := setupTestFix(t)
|
2022-08-20 18:53:45 +02:00
|
|
|
|
|
|
|
for _, input := range sources {
|
2019-02-16 21:23:57 +03:00
|
|
|
input := input
|
2023-08-09 12:09:20 +02:00
|
|
|
|
2019-02-16 21:23:57 +03:00
|
|
|
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).
|
2023-03-20 21:44:36 +01:00
|
|
|
WithArgs("--disable-all",
|
2022-08-15 21:56:32 +02:00
|
|
|
"--print-issued-lines=false",
|
|
|
|
"--print-linter-name=false",
|
|
|
|
"--out-format=line-number",
|
2022-08-20 18:53:45 +02:00
|
|
|
"--fix",
|
2023-03-20 21:44:36 +01:00
|
|
|
"--path-prefix=foobar/").
|
2022-08-20 18:53:45 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|