Skip to content

Commit e64d835

Browse files
committed
dev: improve runner to run dir with go.mod
1 parent 970b0a5 commit e64d835

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

test/linters_test.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package test
22

33
import (
4+
"os"
45
"os/exec"
56
"path/filepath"
7+
"strings"
68
"testing"
9+
"time"
710

811
"github.com/stretchr/testify/assert"
912
"github.com/stretchr/testify/require"
1013

14+
"github.com/golangci/golangci-lint/pkg/logutils"
1115
"github.com/golangci/golangci-lint/test/testshared"
1216
)
1317

@@ -28,18 +32,32 @@ func testSourcesFromDir(t *testing.T, dir string) {
2832

2933
sources := findSources(t, dir, "*.go")
3034

31-
testshared.InstallGolangciLint(t)
35+
binPath := testshared.InstallGolangciLint(t)
3236

33-
for _, s := range sources {
34-
s := s
35-
t.Run(filepath.Base(s), func(subTest *testing.T) {
37+
cwd, err := os.Getwd()
38+
require.NoError(t, err)
39+
t.Cleanup(func() { _ = os.Chdir(cwd) })
40+
41+
err = os.Chdir(dir)
42+
require.NoError(t, err)
43+
44+
log := logutils.NewStderrLog("test")
45+
log.SetLevel(logutils.LogLevelInfo)
46+
47+
for _, source := range sources {
48+
source := source
49+
t.Run(filepath.Base(source), func(subTest *testing.T) {
3650
subTest.Parallel()
37-
testOneSource(subTest, s)
51+
52+
rel, err := filepath.Rel(dir, sources[0])
53+
require.NoError(t, err)
54+
55+
testOneSource(subTest, log, binPath, rel)
3856
})
3957
}
4058
}
4159

42-
func testOneSource(t *testing.T, sourcePath string) {
60+
func testOneSource(t *testing.T, log *logutils.StderrLog, binPath, sourcePath string) {
4361
t.Helper()
4462

4563
rc := testshared.ParseTestDirectives(t, sourcePath)
@@ -62,15 +80,20 @@ func testOneSource(t *testing.T, sourcePath string) {
6280
}
6381

6482
cmd := testshared.NewRunnerBuilder(t).
83+
WithBinPath(binPath).
6584
WithNoParallelRunners().
6685
WithArgs(caseArgs...).
6786
WithRunContext(rc).
6887
WithTargetPath(sourcePath).
6988
Runner().
7089
Command()
7190

91+
startedAt := time.Now()
92+
7293
output, err := cmd.CombinedOutput()
7394

95+
log.Infof("ran [%s] in %s", strings.Join(cmd.Args, " "), time.Since(startedAt))
96+
7497
// The returned error will be nil if the test file does not have any issues
7598
// and thus the linter exits with exit code 0.
7699
// So perform the additional assertions only if the error is non-nil.

test/testshared/analysis.go

-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"go/parser"
77
"go/token"
88
"os"
9-
"path/filepath"
109
"regexp"
1110
"sort"
1211
"strconv"
@@ -21,8 +20,6 @@ import (
2120

2221
const keyword = "want"
2322

24-
const testdataDir = "testdata"
25-
2623
type jsonResult struct {
2724
Issues []*result.Issue
2825
}
@@ -52,9 +49,6 @@ func Analyze(t *testing.T, sourcePath string, rawData []byte) {
5249
require.NoError(t, err)
5350

5451
for _, issue := range reportData.Issues {
55-
if !strings.HasPrefix(issue.Pos.Filename, testdataDir) {
56-
issue.Pos.Filename = filepath.Join(testdataDir, issue.Pos.Filename)
57-
}
5852
checkMessage(t, want, issue.Pos, "diagnostic", issue.FromLinter, issue.Text)
5953
}
6054

test/testshared/runner.go

+27-18
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ import (
1717
"github.com/golangci/golangci-lint/pkg/logutils"
1818
)
1919

20-
const binName = "../golangci-lint"
20+
const defaultBinPath = "../golangci-lint"
2121

2222
type RunnerBuilder struct {
2323
tb testing.TB
2424
log logutils.Log
2525

26+
binPath string
2627
command string
2728
env []string
2829

@@ -42,11 +43,18 @@ func NewRunnerBuilder(tb testing.TB) *RunnerBuilder {
4243
return &RunnerBuilder{
4344
tb: tb,
4445
log: log,
46+
binPath: defaultBinPath,
4547
command: "run",
4648
allowParallelRunners: true,
4749
}
4850
}
4951

52+
func (b *RunnerBuilder) WithBinPath(binPath string) *RunnerBuilder {
53+
b.binPath = binPath
54+
55+
return b
56+
}
57+
5058
func (b *RunnerBuilder) WithCommand(command string) *RunnerBuilder {
5159
b.command = command
5260

@@ -162,6 +170,7 @@ func (b *RunnerBuilder) Runner() *Runner {
162170
}
163171

164172
return &Runner{
173+
binPath: b.binPath,
165174
log: b.log,
166175
tb: b.tb,
167176
env: b.env,
@@ -174,6 +183,7 @@ type Runner struct {
174183
log logutils.Log
175184
tb testing.TB
176185

186+
binPath string
177187
env []string
178188
command string
179189
args []string
@@ -197,11 +207,10 @@ func (r *Runner) Run() *RunnerResult {
197207
runArgs := append([]string{r.command}, r.args...)
198208

199209
defer func(startedAt time.Time) {
200-
r.log.Infof("ran [%s %s] in %s", binName, strings.Join(runArgs, " "), time.Since(startedAt))
210+
r.log.Infof("ran [%s %s] in %s", r.binPath, strings.Join(runArgs, " "), time.Since(startedAt))
201211
}(time.Now())
202212

203-
cmd := exec.Command(binName, runArgs...)
204-
cmd.Env = append(os.Environ(), r.env...)
213+
cmd := r.Command()
205214

206215
out, err := cmd.CombinedOutput()
207216
if err != nil {
@@ -239,11 +248,8 @@ func (r *Runner) Command() *exec.Cmd {
239248

240249
runArgs := append([]string{r.command}, r.args...)
241250

242-
defer func(startedAt time.Time) {
243-
r.log.Infof("ran [../golangci-lint %s] in %s", strings.Join(runArgs, " "), time.Since(startedAt))
244-
}(time.Now())
245-
246-
cmd := exec.Command("../golangci-lint", runArgs...)
251+
//nolint:gosec
252+
cmd := exec.Command(r.binPath, runArgs...)
247253
cmd.Env = append(os.Environ(), r.env...)
248254

249255
return cmd
@@ -311,19 +317,22 @@ func (r *RunnerResult) ExpectHasIssue(issueText string) *RunnerResult {
311317
return r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputContains(issueText)
312318
}
313319

314-
func InstallGolangciLint(tb testing.TB) {
320+
func InstallGolangciLint(tb testing.TB) string {
315321
tb.Helper()
316322

317-
if os.Getenv("GOLANGCI_LINT_INSTALLED") == "true" {
318-
return
319-
}
323+
if os.Getenv("GOLANGCI_LINT_INSTALLED") != "true" {
324+
cmd := exec.Command("make", "-C", "..", "build")
320325

321-
cmd := exec.Command("make", "-C", "..", "build")
326+
output, err := cmd.CombinedOutput()
327+
if err != nil {
328+
tb.Log(string(output))
329+
}
322330

323-
output, err := cmd.CombinedOutput()
324-
if err != nil {
325-
tb.Log(string(output))
331+
require.NoError(tb, err, "Can't go install golangci-lint")
326332
}
327333

328-
require.NoError(tb, err, "Can't go install golangci-lint")
334+
abs, err := filepath.Abs(defaultBinPath)
335+
require.NoError(tb, err)
336+
337+
return abs
329338
}

0 commit comments

Comments
 (0)