Skip to content

Commit 5ab5f7b

Browse files
committed
review: tests, filecache, and minor changes
1 parent 9639df6 commit 5ab5f7b

File tree

5 files changed

+60
-34
lines changed

5 files changed

+60
-34
lines changed

pkg/commands/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss
359359
lintCtx.Log = e.log.Child(logutils.DebugKeyLintersContext)
360360

361361
runner, err := lint.NewRunner(e.cfg, e.log.Child(logutils.DebugKeyRunner),
362-
e.goenv, e.EnabledLintersSet, e.lineCache, e.DBManager, lintCtx.Packages)
362+
e.goenv, e.EnabledLintersSet, e.lineCache, e.fileCache, e.DBManager, lintCtx.Packages)
363363
if err != nil {
364364
return nil, err
365365
}

pkg/fsutils/linecache.go

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ func NewLineCache(fc *FileCache) *LineCache {
1919
}
2020
}
2121

22-
func (lc *LineCache) GetFileCache() *FileCache { return lc.fileCache }
23-
2422
// GetLine returns the index1-th (1-based index) line from the file on filePath
2523
func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) {
2624
if index1 == 0 { // some linters, e.g. gosec can do it: it really means first line

pkg/lint/runner.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ type Runner struct {
2727
Log logutils.Log
2828
}
2929

30-
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lintersdb.EnabledSet,
31-
lineCache *fsutils.LineCache, dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
30+
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
31+
es *lintersdb.EnabledSet,
32+
lineCache *fsutils.LineCache, fileCache *fsutils.FileCache,
33+
dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
3234
// Beware that some processors need to add the path prefix when working with paths
3335
// because they get invoked before the path prefixer (exclude and severity rules)
3436
// or process other paths (skip files).
@@ -98,8 +100,10 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
98100
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
99101
processors.NewPathShortener(),
100102
getSeverityRulesProcessor(&cfg.Severity, log, files),
103+
101104
// The fixer still needs to see paths for the issues that are relative to the current directory.
102-
processors.NewFixer(cfg, log, lineCache.GetFileCache()),
105+
processors.NewFixer(cfg, log, fileCache),
106+
103107
// Now we can modify the issues for output.
104108
processors.NewPathPrefixer(cfg.Output.PathPrefix),
105109
processors.NewSortResults(cfg),

pkg/result/processors/fixer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/golangci/golangci-lint/pkg/timeutils"
1717
)
1818

19+
var _ Processor = Fixer{}
20+
1921
type Fixer struct {
2022
cfg *config.Config
2123
log logutils.Log
@@ -76,8 +78,6 @@ func (f Fixer) Name() string {
7678

7779
func (f Fixer) Finish() {}
7880

79-
var _ Processor = Fixer{}
80-
8181
func (f Fixer) fixIssuesInFile(filePath string, issues []result.Issue) error {
8282
// TODO: don't read the whole file into memory: read line by line;
8383
// can't just use bufio.scanner: it has a line length limit

test/fix_test.go

+50-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"os"
55
"os/exec"
66
"path/filepath"
7-
"sort"
87
"testing"
98

109
"github.com/stretchr/testify/require"
@@ -15,7 +14,9 @@ import (
1514
// value: "1"
1615
const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
1716

18-
func TestFix(t *testing.T) {
17+
func setupTestFix(t *testing.T) []string {
18+
t.Helper()
19+
1920
testshared.SkipOnWindows(t)
2021

2122
tmpDir := filepath.Join(testdataDir, "fix.tmp")
@@ -34,44 +35,67 @@ func TestFix(t *testing.T) {
3435

3536
testshared.InstallGolangciLint(t)
3637

37-
sources := findSources(t, tmpDir, "in", "*.go")
38+
return findSources(t, tmpDir, "in", "*.go")
39+
}
3840

39-
// The combination with --path gets tested for the first test case.
40-
// This is arbitrary. It could also be tested for all of them,
41-
// but then each test would have to run twice (with and without).
42-
// To make this determinstic, the sources get sorted by name.
43-
sort.Strings(sources)
41+
func TestFix(t *testing.T) {
42+
sources := setupTestFix(t)
4443

45-
for i, input := range sources {
46-
withPathPrefix := i == 0
44+
for _, input := range sources {
4745
input := input
4846
t.Run(filepath.Base(input), func(t *testing.T) {
4947
t.Parallel()
5048

5149
rc := testshared.ParseTestDirectives(t, input)
5250
if rc == nil {
53-
if withPathPrefix {
54-
t.Errorf("The testcase %s should not get skipped, it's used for testing --path.", input)
55-
return
56-
}
5751
t.Logf("Skipped: %s", input)
5852
return
5953
}
6054

61-
args := []string{
62-
"--disable-all",
63-
"--print-issued-lines=false",
64-
"--print-linter-name=false",
65-
"--out-format=line-number",
66-
"--fix",
67-
}
68-
if withPathPrefix {
69-
t.Log("Testing with --path-prefix.")
70-
// This must not break fixing...
71-
args = append(args, "--path-prefix=foobar/")
55+
testshared.NewRunnerBuilder(t).
56+
WithArgs("--disable-all",
57+
"--print-issued-lines=false",
58+
"--print-linter-name=false",
59+
"--out-format=line-number",
60+
"--fix").
61+
WithRunContext(rc).
62+
WithTargetPath(input).
63+
Runner().
64+
Run().
65+
ExpectExitCode(rc.ExitCode)
66+
67+
output, err := os.ReadFile(input)
68+
require.NoError(t, err)
69+
70+
expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
71+
require.NoError(t, err)
72+
73+
require.Equal(t, string(expectedOutput), string(output))
74+
})
75+
}
76+
}
77+
78+
func TestFix_pathPrefix(t *testing.T) {
79+
sources := setupTestFix(t)
80+
81+
for _, input := range sources {
82+
input := input
83+
t.Run(filepath.Base(input), func(t *testing.T) {
84+
t.Parallel()
85+
86+
rc := testshared.ParseTestDirectives(t, input)
87+
if rc == nil {
88+
t.Logf("Skipped: %s", input)
89+
return
7290
}
91+
7392
testshared.NewRunnerBuilder(t).
74-
WithArgs(args...).
93+
WithArgs("--disable-all",
94+
"--print-issued-lines=false",
95+
"--print-linter-name=false",
96+
"--out-format=line-number",
97+
"--fix",
98+
"--path-prefix=foobar/").
7599
WithRunContext(rc).
76100
WithTargetPath(input).
77101
Runner().

0 commit comments

Comments
 (0)