Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/result/processors/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
88 changes: 50 additions & 38 deletions test/fix_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -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))
})
}
}
}