dev: improve runner to run dir with go.mod (#3124)
This commit is contained in:
parent
970b0a5bd7
commit
e1afce4433
@ -1,13 +1,17 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
"github.com/golangci/golangci-lint/test/testshared"
|
"github.com/golangci/golangci-lint/test/testshared"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,18 +32,32 @@ func testSourcesFromDir(t *testing.T, dir string) {
|
|||||||
|
|
||||||
sources := findSources(t, dir, "*.go")
|
sources := findSources(t, dir, "*.go")
|
||||||
|
|
||||||
testshared.InstallGolangciLint(t)
|
binPath := testshared.InstallGolangciLint(t)
|
||||||
|
|
||||||
for _, s := range sources {
|
cwd, err := os.Getwd()
|
||||||
s := s
|
require.NoError(t, err)
|
||||||
t.Run(filepath.Base(s), func(subTest *testing.T) {
|
t.Cleanup(func() { _ = os.Chdir(cwd) })
|
||||||
|
|
||||||
|
err = os.Chdir(dir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
log := logutils.NewStderrLog("test")
|
||||||
|
log.SetLevel(logutils.LogLevelInfo)
|
||||||
|
|
||||||
|
for _, source := range sources {
|
||||||
|
source := source
|
||||||
|
t.Run(filepath.Base(source), func(subTest *testing.T) {
|
||||||
subTest.Parallel()
|
subTest.Parallel()
|
||||||
testOneSource(subTest, s)
|
|
||||||
|
rel, err := filepath.Rel(dir, sources[0])
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
testOneSource(subTest, log, binPath, rel)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testOneSource(t *testing.T, sourcePath string) {
|
func testOneSource(t *testing.T, log *logutils.StderrLog, binPath, sourcePath string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
rc := testshared.ParseTestDirectives(t, sourcePath)
|
rc := testshared.ParseTestDirectives(t, sourcePath)
|
||||||
@ -62,6 +80,7 @@ func testOneSource(t *testing.T, sourcePath string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd := testshared.NewRunnerBuilder(t).
|
cmd := testshared.NewRunnerBuilder(t).
|
||||||
|
WithBinPath(binPath).
|
||||||
WithNoParallelRunners().
|
WithNoParallelRunners().
|
||||||
WithArgs(caseArgs...).
|
WithArgs(caseArgs...).
|
||||||
WithRunContext(rc).
|
WithRunContext(rc).
|
||||||
@ -69,8 +88,12 @@ func testOneSource(t *testing.T, sourcePath string) {
|
|||||||
Runner().
|
Runner().
|
||||||
Command()
|
Command()
|
||||||
|
|
||||||
|
startedAt := time.Now()
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
log.Infof("ran [%s] in %s", strings.Join(cmd.Args, " "), time.Since(startedAt))
|
||||||
|
|
||||||
// The returned error will be nil if the test file does not have any issues
|
// The returned error will be nil if the test file does not have any issues
|
||||||
// and thus the linter exits with exit code 0.
|
// and thus the linter exits with exit code 0.
|
||||||
// So perform the additional assertions only if the error is non-nil.
|
// So perform the additional assertions only if the error is non-nil.
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -21,8 +20,6 @@ import (
|
|||||||
|
|
||||||
const keyword = "want"
|
const keyword = "want"
|
||||||
|
|
||||||
const testdataDir = "testdata"
|
|
||||||
|
|
||||||
type jsonResult struct {
|
type jsonResult struct {
|
||||||
Issues []*result.Issue
|
Issues []*result.Issue
|
||||||
}
|
}
|
||||||
@ -52,9 +49,6 @@ func Analyze(t *testing.T, sourcePath string, rawData []byte) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, issue := range reportData.Issues {
|
for _, issue := range reportData.Issues {
|
||||||
if !strings.HasPrefix(issue.Pos.Filename, testdataDir) {
|
|
||||||
issue.Pos.Filename = filepath.Join(testdataDir, issue.Pos.Filename)
|
|
||||||
}
|
|
||||||
checkMessage(t, want, issue.Pos, "diagnostic", issue.FromLinter, issue.Text)
|
checkMessage(t, want, issue.Pos, "diagnostic", issue.FromLinter, issue.Text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,12 +17,13 @@ import (
|
|||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const binName = "../golangci-lint"
|
const defaultBinPath = "../golangci-lint"
|
||||||
|
|
||||||
type RunnerBuilder struct {
|
type RunnerBuilder struct {
|
||||||
tb testing.TB
|
tb testing.TB
|
||||||
log logutils.Log
|
log logutils.Log
|
||||||
|
|
||||||
|
binPath string
|
||||||
command string
|
command string
|
||||||
env []string
|
env []string
|
||||||
|
|
||||||
@ -42,11 +43,18 @@ func NewRunnerBuilder(tb testing.TB) *RunnerBuilder {
|
|||||||
return &RunnerBuilder{
|
return &RunnerBuilder{
|
||||||
tb: tb,
|
tb: tb,
|
||||||
log: log,
|
log: log,
|
||||||
|
binPath: defaultBinPath,
|
||||||
command: "run",
|
command: "run",
|
||||||
allowParallelRunners: true,
|
allowParallelRunners: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *RunnerBuilder) WithBinPath(binPath string) *RunnerBuilder {
|
||||||
|
b.binPath = binPath
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
func (b *RunnerBuilder) WithCommand(command string) *RunnerBuilder {
|
func (b *RunnerBuilder) WithCommand(command string) *RunnerBuilder {
|
||||||
b.command = command
|
b.command = command
|
||||||
|
|
||||||
@ -162,6 +170,7 @@ func (b *RunnerBuilder) Runner() *Runner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Runner{
|
return &Runner{
|
||||||
|
binPath: b.binPath,
|
||||||
log: b.log,
|
log: b.log,
|
||||||
tb: b.tb,
|
tb: b.tb,
|
||||||
env: b.env,
|
env: b.env,
|
||||||
@ -174,6 +183,7 @@ type Runner struct {
|
|||||||
log logutils.Log
|
log logutils.Log
|
||||||
tb testing.TB
|
tb testing.TB
|
||||||
|
|
||||||
|
binPath string
|
||||||
env []string
|
env []string
|
||||||
command string
|
command string
|
||||||
args []string
|
args []string
|
||||||
@ -197,11 +207,10 @@ func (r *Runner) Run() *RunnerResult {
|
|||||||
runArgs := append([]string{r.command}, r.args...)
|
runArgs := append([]string{r.command}, r.args...)
|
||||||
|
|
||||||
defer func(startedAt time.Time) {
|
defer func(startedAt time.Time) {
|
||||||
r.log.Infof("ran [%s %s] in %s", binName, strings.Join(runArgs, " "), time.Since(startedAt))
|
r.log.Infof("ran [%s %s] in %s", r.binPath, strings.Join(runArgs, " "), time.Since(startedAt))
|
||||||
}(time.Now())
|
}(time.Now())
|
||||||
|
|
||||||
cmd := exec.Command(binName, runArgs...)
|
cmd := r.Command()
|
||||||
cmd.Env = append(os.Environ(), r.env...)
|
|
||||||
|
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -239,11 +248,8 @@ func (r *Runner) Command() *exec.Cmd {
|
|||||||
|
|
||||||
runArgs := append([]string{r.command}, r.args...)
|
runArgs := append([]string{r.command}, r.args...)
|
||||||
|
|
||||||
defer func(startedAt time.Time) {
|
//nolint:gosec
|
||||||
r.log.Infof("ran [../golangci-lint %s] in %s", strings.Join(runArgs, " "), time.Since(startedAt))
|
cmd := exec.Command(r.binPath, runArgs...)
|
||||||
}(time.Now())
|
|
||||||
|
|
||||||
cmd := exec.Command("../golangci-lint", runArgs...)
|
|
||||||
cmd.Env = append(os.Environ(), r.env...)
|
cmd.Env = append(os.Environ(), r.env...)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
@ -311,13 +317,10 @@ func (r *RunnerResult) ExpectHasIssue(issueText string) *RunnerResult {
|
|||||||
return r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputContains(issueText)
|
return r.ExpectExitCode(exitcodes.IssuesFound).ExpectOutputContains(issueText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InstallGolangciLint(tb testing.TB) {
|
func InstallGolangciLint(tb testing.TB) string {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
|
||||||
if os.Getenv("GOLANGCI_LINT_INSTALLED") == "true" {
|
if os.Getenv("GOLANGCI_LINT_INSTALLED") != "true" {
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := exec.Command("make", "-C", "..", "build")
|
cmd := exec.Command("make", "-C", "..", "build")
|
||||||
|
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
@ -327,3 +330,9 @@ func InstallGolangciLint(tb testing.TB) {
|
|||||||
|
|
||||||
require.NoError(tb, err, "Can't go install golangci-lint")
|
require.NoError(tb, err, "Can't go install golangci-lint")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
abs, err := filepath.Abs(defaultBinPath)
|
||||||
|
require.NoError(tb, err)
|
||||||
|
|
||||||
|
return abs
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user