From 57bd7bfe25c158e05c1157da44133b8416f61bd8 Mon Sep 17 00:00:00 2001 From: Matias Lahti Date: Thu, 14 Oct 2021 12:45:43 +0300 Subject: [PATCH 1/4] fix: make --fix work (naively) with --prefix-path --- pkg/result/processors/fixer.go | 7 ++++++- test/fix_test.go | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index 3a31a33e40b5..81b53a00b805 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.Replace(fileWithoutPathPrefix, f.cfg.Output.PathPrefix+string(filepath.Separator), "", 1) + } + 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..ed510a52823d 100644 --- a/test/fix_test.go +++ b/test/fix_test.go @@ -53,6 +53,7 @@ func TestFix(t *testing.T) { "--print-linter-name=false", "--out-format=line-number", "--fix", + "--path-prefix=mock", ). WithRunContext(rc). WithTargetPath(input). From 21b03586c580519f5aa526f7f967d496caa16679 Mon Sep 17 00:00:00 2001 From: Matias Lahti Date: Tue, 9 Nov 2021 15:52:33 +0200 Subject: [PATCH 2/4] feat: use TrimPrefix instead of strings.Replace Co-authored-by: Oleksandr Redko --- pkg/result/processors/fixer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index 81b53a00b805..aa1c08b37e89 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -59,7 +59,7 @@ func (f Fixer) Process(issues []result.Issue) []result.Issue { fileWithoutPathPrefix := file if f.cfg.Output.PathPrefix != "" { - fileWithoutPathPrefix = strings.Replace(fileWithoutPathPrefix, f.cfg.Output.PathPrefix+string(filepath.Separator), "", 1) + fileWithoutPathPrefix = strings.TrimPrefix(fileWithoutPathPrefix, f.cfg.Output.PathPrefix+string(filepath.Separator)) } err = f.fixIssuesInFile(fileWithoutPathPrefix, issuesToFix) }) From 0155202a71eeea9e4a7bb1da99f16e0c4ffb73c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 22 Feb 2023 20:51:42 +0200 Subject: [PATCH 3/4] fix: clean given path prefix to trim --- pkg/result/processors/fixer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index aa1c08b37e89..1f714beded77 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -59,7 +59,7 @@ func (f Fixer) Process(issues []result.Issue) []result.Issue { fileWithoutPathPrefix := file if f.cfg.Output.PathPrefix != "" { - fileWithoutPathPrefix = strings.TrimPrefix(fileWithoutPathPrefix, f.cfg.Output.PathPrefix+string(filepath.Separator)) + fileWithoutPathPrefix = strings.TrimPrefix(fileWithoutPathPrefix, filepath.Clean(f.cfg.Output.PathPrefix)+string(filepath.Separator)) } err = f.fixIssuesInFile(fileWithoutPathPrefix, issuesToFix) }) From 4e70b7157dfe105b1a90e07dcb9f65bd19a5a3b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 22 Feb 2023 20:52:19 +0200 Subject: [PATCH 4/4] test(fix): test various path prefix cases separately --- test/fix_test.go | 89 +++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/test/fix_test.go b/test/fix_test.go index ed510a52823d..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,58 +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", - "--path-prefix=mock", - ). - 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)) + }) + } } }