diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index 3a31a33e40b5..1f714beded77 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -56,7 +56,12 @@ func (f Fixer) Process(issues []result.Issue) []result.Issue { for file, issuesToFix := range issuesToFixPerFile { var err error f.sw.TrackStage("all", func() { - err = f.fixIssuesInFile(file, issuesToFix) + fileWithoutPathPrefix := file + + if f.cfg.Output.PathPrefix != "" { + fileWithoutPathPrefix = strings.TrimPrefix(fileWithoutPathPrefix, filepath.Clean(f.cfg.Output.PathPrefix)+string(filepath.Separator)) + } + err = f.fixIssuesInFile(fileWithoutPathPrefix, issuesToFix) }) if err != nil { f.log.Errorf("Failed to fix issues in file %s: %s", file, err) diff --git a/test/fix_test.go b/test/fix_test.go index bccf620f5989..cd9e8d997c11 100644 --- a/test/fix_test.go +++ b/test/fix_test.go @@ -1,6 +1,7 @@ package test import ( + "fmt" "os" "os/exec" "path/filepath" @@ -16,57 +17,68 @@ const envKeepTempFiles = "GL_KEEP_TEMP_FILES" func TestFix(t *testing.T) { testshared.SkipOnWindows(t) + testshared.InstallGolangciLint(t) + sourcesDir := filepath.Join(testdataDir, "fix") - tmpDir := filepath.Join(testdataDir, "fix.tmp") - _ = os.RemoveAll(tmpDir) // cleanup previous runs - - if os.Getenv(envKeepTempFiles) == "1" { - t.Logf("Temp dir for fix test: %s", tmpDir) - } else { - t.Cleanup(func() { _ = os.RemoveAll(tmpDir) }) + tests := []struct { + Args []string + DirSuffix string + }{ + {[]string{}, ""}, + {[]string{"--path-prefix=simple-prefix"}, "-simple-prefix"}, + {[]string{"--path-prefix=slash-prefix/"}, "-slash-prefix"}, } - sourcesDir := filepath.Join(testdataDir, "fix") + for _, test := range tests { + tmpDir := filepath.Join(testdataDir, fmt.Sprintf("fix%s.tmp", test.DirSuffix)) + _ = os.RemoveAll(tmpDir) // cleanup previous runs - err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run() - require.NoError(t, err) + if os.Getenv(envKeepTempFiles) == "1" { + t.Logf("Temp dir for fix with args %v test: %s", test.Args, tmpDir) + } else { + t.Cleanup(func() { _ = os.RemoveAll(tmpDir) }) + } - testshared.InstallGolangciLint(t) + err := exec.Command("cp", "-R", sourcesDir, tmpDir).Run() + require.NoError(t, err) - sources := findSources(t, tmpDir, "in", "*.go") + sources := findSources(t, tmpDir, "in", "*.go") - for _, input := range sources { - input := input - t.Run(filepath.Base(input), func(t *testing.T) { - t.Parallel() + for _, input := range sources { + input := input + t.Run(filepath.Base(input)+test.DirSuffix, func(t *testing.T) { + t.Parallel() - rc := testshared.ParseTestDirectives(t, input) - if rc == nil { - t.Logf("Skipped: %s", input) - return - } + rc := testshared.ParseTestDirectives(t, input) + if rc == nil { + t.Logf("Skipped: %s", input) + return + } - testshared.NewRunnerBuilder(t). - WithArgs( + args := []string{ "--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)) - }) + } + args = append(args, test.Args...) + testshared.NewRunnerBuilder(t). + WithArgs(args...). + 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)) + }) + } } }