Skip to content

Commit

Permalink
dev: support build tags about go version in linter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored and kulti committed Apr 6, 2022
1 parent bb7731f commit 999c664
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
5 changes: 5 additions & 0 deletions test/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func TestFix(t *testing.T) {
input,
}
rc := extractRunContextFromComments(t, input)
if rc == nil {
t.Logf("Skipped: %s", input)
return
}

args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
Expand Down
73 changes: 65 additions & 8 deletions test/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package test
import (
"bufio"
"fmt"
"go/build/constraint"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"

hcversion "github.com/hashicorp/go-version"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -73,7 +76,10 @@ func TestGoimportsLocal(t *testing.T) {
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
Expand All @@ -89,7 +95,10 @@ func TestGciLocal(t *testing.T) {
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := os.ReadFile(rc.configPath)
Expand All @@ -105,7 +114,10 @@ func TestMultipleOutputs(t *testing.T) {
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number,json:stdout",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := os.ReadFile(rc.configPath)
Expand All @@ -122,7 +134,10 @@ func TestStderrOutput(t *testing.T) {
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number,json:stderr",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := os.ReadFile(rc.configPath)
Expand All @@ -142,7 +157,10 @@ func TestFileOutput(t *testing.T) {
fmt.Sprintf("--out-format=json:%s,line-number", resultPath),
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := os.ReadFile(rc.configPath)
Expand Down Expand Up @@ -188,8 +206,11 @@ func testOneSource(t *testing.T, sourcePath string) {
}

rc := extractRunContextFromComments(t, sourcePath)
var cfgPath string
if rc == nil {
t.Skipf("Skipped: %s", sourcePath)
}

var cfgPath string
if rc.config != nil {
p, finish := saveConfig(t, rc.config)
defer finish()
Expand Down Expand Up @@ -254,6 +275,7 @@ func skipMultilineComment(scanner *bufio.Scanner) {
}
}

//nolint:gocyclo,funlen
func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext {
f, err := os.Open(sourcePath)
require.NoError(t, err)
Expand All @@ -275,6 +297,17 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
break
}

if strings.HasPrefix(line, "//go:build") || strings.HasPrefix(line, "// +build") {
parse, err := constraint.Parse(line)
require.NoError(t, err)

if !parse.Eval(buildTagGoVersion) {
return nil
}

continue
}

line = strings.TrimLeft(strings.TrimPrefix(line, "//"), " ")
if strings.HasPrefix(line, "args: ") {
require.Nil(t, rc.args)
Expand Down Expand Up @@ -315,10 +348,7 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
if rc.expectedLinter == "" {
for _, arg := range rc.args {
if strings.HasPrefix(arg, "-E") && !strings.Contains(arg, ",") {
if rc.expectedLinter != "" {
require.Fail(t, "could not infer expected linter for errors because multiple linters are enabled. Please use the `expected_linter: ` directive in your test to indicate the linter-under-test.") //nolint:lll
break
}
require.Empty(t, rc.expectedLinter, "could not infer expected linter for errors because multiple linters are enabled. Please use the `expected_linter: ` directive in your test to indicate the linter-under-test.") //nolint:lll
rc.expectedLinter = arg[2:]
}
}
Expand All @@ -327,19 +357,38 @@ func extractRunContextFromComments(t *testing.T, sourcePath string) *runContext
return rc
}

func buildTagGoVersion(tag string) bool {
vRuntime, err := hcversion.NewVersion(strings.TrimPrefix(runtime.Version(), "go"))
if err != nil {
return false
}

vTag, err := hcversion.NewVersion(strings.TrimPrefix(tag, "go"))
if err != nil {
return false
}

return vRuntime.GreaterThanOrEqual(vTag)
}

func TestExtractRunContextFromComments(t *testing.T) {
rc := extractRunContextFromComments(t, filepath.Join(testdataDir, "goimports", "goimports.go"))
require.NotNil(t, rc)
require.Equal(t, []string{"-Egoimports"}, rc.args)
}

func TestTparallel(t *testing.T) {
t.Run("should fail on missing top-level Parallel()", func(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "tparallel", "missing_toplevel_test.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
"--disable-all", "--enable", "tparallel",
"--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
Expand All @@ -354,10 +403,14 @@ func TestTparallel(t *testing.T) {
t.Run("should fail on missing subtest Parallel()", func(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "tparallel", "missing_subtest_test.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
"--disable-all", "--enable", "tparallel",
"--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
Expand All @@ -372,10 +425,14 @@ func TestTparallel(t *testing.T) {
t.Run("should pass on parallel test with no subtests", func(t *testing.T) {
sourcePath := filepath.Join(testdataDir, "tparallel", "happy_path_test.go")
args := []string{
"--disable-all", "--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number", "--enable", "tparallel",
"--disable-all", "--enable", "tparallel",
"--print-issued-lines=false", "--print-linter-name=false", "--out-format=line-number",
sourcePath,
}

rc := extractRunContextFromComments(t, sourcePath)
require.NotNil(t, rc)

args = append(args, rc.args...)

cfg, err := yaml.Marshal(rc.config)
Expand Down
19 changes: 19 additions & 0 deletions test/testdata/tenv_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build go1.18
// +build go1.18

// args: -Etenv
package testdata

import (
"os"
"testing"
)

func FuzzF(f *testing.F) {
os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `f\\.Setenv\\(\\)` in FuzzF"
err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `f\\.Setenv\\(\\)` in FuzzF"
_ = err
if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `f\\.Setenv\\(\\)` in FuzzF"
_ = err
}
}

0 comments on commit 999c664

Please sign in to comment.