Skip to content

Commit

Permalink
Merge pull request #168 from LukeShu/for-upstream-go112
Browse files Browse the repository at this point in the history
Fix CI, get the tests passing with GO111MODULE=on
  • Loading branch information
kisielk authored Mar 27, 2020
2 parents e14f8d5 + dd5e04e commit f02139a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 32 deletions.
29 changes: 29 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,36 @@ matrix:
include:
- go: "1.9"
- go: "1.10"
- go: "1.11"
env: {GO111MODULE: "off"}
- go: "1.11"
env: {GO111MODULE: "on"}
- go: "1.12"
env: {GO111MODULE: "off"}
- go: "1.12"
env: {GO111MODULE: "on"}
- go: "1.13"
env: {GO111MODULE: "off"}
- go: "1.13"
env: {GO111MODULE: "on"}
- go: "1.14"
env: {GO111MODULE: "off"}
- go: "1.14"
env: {GO111MODULE: "on"}
- go: "tip"
env: {GO111MODULE: "off"}
- go: "tip"
env: {GO111MODULE: "on"}

install: |
if test -z "$(go env GOMOD)"; then
go get -d -t ./... &&
ver=$(sed -n '/^require/s/.*[ -]//p' go.mod) &&
(
cd "$(go env GOPATH)/src/golang.org/x/tools" &&
git checkout "$ver"
)
fi
script:
- go test -race ./...
51 changes: 35 additions & 16 deletions internal/errcheck/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go/token"
"go/types"
"os"
"os/exec"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -217,6 +218,20 @@ func (c *Checker) CheckPackages(paths ...string) error {
}
}

gomod, err := exec.Command("go", "env", "GOMOD").Output()
go111module := (err == nil) && strings.TrimSpace(string(gomod)) != ""
ignore := c.Ignore
if go111module {
ignore = make(map[string]*regexp.Regexp)
for pkg, re := range c.Ignore {
if nonVendoredPkg, ok := nonVendoredPkgPath(pkg); ok {
ignore[nonVendoredPkg] = re
} else {
ignore[pkg] = re
}
}
}

var wg sync.WaitGroup
u := &UncheckedErrors{}
for _, pkg := range pkgs {
Expand All @@ -227,13 +242,14 @@ func (c *Checker) CheckPackages(paths ...string) error {
c.logf("Checking %s", pkg.Types.Path())

v := &visitor{
pkg: pkg,
ignore: c.Ignore,
blank: c.Blank,
asserts: c.Asserts,
lines: make(map[string][]string),
exclude: c.exclude,
errors: []UncheckedError{},
pkg: pkg,
ignore: ignore,
blank: c.Blank,
asserts: c.Asserts,
lines: make(map[string][]string),
exclude: c.exclude,
go111module: go111module,
errors: []UncheckedError{},
}

for _, astFile := range v.pkg.Syntax {
Expand Down Expand Up @@ -265,12 +281,13 @@ func (c *Checker) CheckPackages(paths ...string) error {

// visitor implements the errcheck algorithm
type visitor struct {
pkg *packages.Package
ignore map[string]*regexp.Regexp
blank bool
asserts bool
lines map[string][]string
exclude map[string]bool
pkg *packages.Package
ignore map[string]*regexp.Regexp
blank bool
asserts bool
lines map[string][]string
exclude map[string]bool
go111module bool

errors []UncheckedError
}
Expand Down Expand Up @@ -439,9 +456,11 @@ func (v *visitor) ignoreCall(call *ast.CallExpr) bool {

// if current package being considered is vendored, check to see if it should be ignored based
// on the unvendored path.
if nonVendoredPkg, ok := nonVendoredPkgPath(pkg.Path()); ok {
if re, ok := v.ignore[nonVendoredPkg]; ok {
return re.MatchString(id.Name)
if !v.go111module {
if nonVendoredPkg, ok := nonVendoredPkgPath(pkg.Path()); ok {
if re, ok := v.ignore[nonVendoredPkg]; ok {
return re.MatchString(id.Name)
}
}
}
}
Expand Down
42 changes: 26 additions & 16 deletions internal/errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"path"
"regexp"
"runtime"
"strings"
"testing"

"golang.org/x/tools/go/packages"
Expand Down Expand Up @@ -45,7 +43,7 @@ func init() {
}
pkgs, err := packages.Load(cfg, testPackage)
if err != nil {
panic("failed to import test package")
panic(fmt.Errorf("failed to import test package: %v", err))
}
for _, pkg := range pkgs {
for _, file := range pkg.Syntax {
Expand Down Expand Up @@ -133,6 +131,9 @@ package custom
os.RemoveAll(tmpGopath)
}()

if err := ioutil.WriteFile(path.Join(testBuildTagsDir, "go.mod"), []byte("module github.com/testbuildtags"), 0644); err != nil {
t.Fatalf("Failed to write testbuildtags go.mod: %v", err)
}
if err := ioutil.WriteFile(path.Join(testBuildTagsDir, "custom1.go"), []byte(testBuildCustom1Tag), 0644); err != nil {
t.Fatalf("Failed to write testbuildtags custom1: %v", err)
}
Expand Down Expand Up @@ -169,7 +170,8 @@ package custom
checker.Tags = currCase.tags

loadPackages = func(cfg *packages.Config, paths ...string) ([]*packages.Package, error) {
cfg.Env = append(os.Environ(), "GOPATH="+tmpGopath)
cfg.Env = append(os.Environ(),
"GOPATH="+tmpGopath)
cfg.Dir = testBuildTagsDir
pkgs, err := packages.Load(cfg, paths...)
return pkgs, err
Expand Down Expand Up @@ -200,6 +202,10 @@ func TestWhitelist(t *testing.T) {
}

func TestIgnore(t *testing.T) {
const testVendorGoMod = `module github.com/testvendor
require github.com/testlog v0.0.0
`
const testVendorMain = `
package main
Expand All @@ -216,11 +222,6 @@ func TestIgnore(t *testing.T) {
return nil
}`

if strings.HasPrefix(runtime.Version(), "go1.5") && os.Getenv("GO15VENDOREXPERIMENT") != "1" {
// skip tests if running in go1.5 and vendoring is not enabled
t.SkipNow()
}

// copy testvendor directory into directory for test
tmpGopath, err := ioutil.TempDir("", "testvendor")
if err != nil {
Expand All @@ -234,6 +235,9 @@ func TestIgnore(t *testing.T) {
os.RemoveAll(tmpGopath)
}()

if err := ioutil.WriteFile(path.Join(testVendorDir, "go.mod"), []byte(testVendorGoMod), 0755); err != nil {
t.Fatalf("Failed to write testvendor go.mod: %v", err)
}
if err := ioutil.WriteFile(path.Join(testVendorDir, "main.go"), []byte(testVendorMain), 0755); err != nil {
t.Fatalf("Failed to write testvendor main: %v", err)
}
Expand Down Expand Up @@ -271,7 +275,9 @@ func TestIgnore(t *testing.T) {
checker := NewChecker()
checker.Ignore = currCase.ignore
loadPackages = func(cfg *packages.Config, paths ...string) ([]*packages.Package, error) {
cfg.Env = append(os.Environ(), "GOPATH="+tmpGopath)
cfg.Env = append(os.Environ(),
"GOPATH="+tmpGopath,
"GOFLAGS=-mod=vendor")
cfg.Dir = testVendorDir
pkgs, err := packages.Load(cfg, paths...)
return pkgs, err
Expand All @@ -298,6 +304,10 @@ func TestIgnore(t *testing.T) {
}

func TestWithoutGeneratedCode(t *testing.T) {
const testVendorGoMod = `module github.com/testvendor
require github.com/testlog v0.0.0
`
const testVendorMain = `
// Code generated by protoc-gen-go. DO NOT EDIT.
package main
Expand All @@ -315,11 +325,6 @@ func TestWithoutGeneratedCode(t *testing.T) {
return nil
}`

if strings.HasPrefix(runtime.Version(), "go1.5") && os.Getenv("GO15VENDOREXPERIMENT") != "1" {
// skip tests if running in go1.5 and vendoring is not enabled
t.SkipNow()
}

// copy testvendor directory into directory for test
tmpGopath, err := ioutil.TempDir("", "testvendor")
if err != nil {
Expand All @@ -333,6 +338,9 @@ func TestWithoutGeneratedCode(t *testing.T) {
os.RemoveAll(tmpGopath)
}()

if err := ioutil.WriteFile(path.Join(testVendorDir, "go.mod"), []byte(testVendorGoMod), 0755); err != nil {
t.Fatalf("Failed to write testvendor go.mod: %v", err)
}
if err := ioutil.WriteFile(path.Join(testVendorDir, "main.go"), []byte(testVendorMain), 0755); err != nil {
t.Fatalf("Failed to write testvendor main: %v", err)
}
Expand Down Expand Up @@ -363,7 +371,9 @@ func TestWithoutGeneratedCode(t *testing.T) {
checker := NewChecker()
checker.WithoutGeneratedCode = currCase.withoutGeneratedCode
loadPackages = func(cfg *packages.Config, paths ...string) ([]*packages.Package, error) {
cfg.Env = append(os.Environ(), "GOPATH="+tmpGopath)
cfg.Env = append(os.Environ(),
"GOPATH="+tmpGopath,
"GOFLAGS=-mod=vendor")
cfg.Dir = testVendorDir
pkgs, err := packages.Load(cfg, paths...)
return pkgs, err
Expand Down

0 comments on commit f02139a

Please sign in to comment.