Skip to content

Commit

Permalink
Merge pull request #1 from nmiyake/addBuildTagsTestCooperGoModules
Browse files Browse the repository at this point in the history
Add test for build tags
  • Loading branch information
coopernurse authored Jan 1, 2019
2 parents 4c22418 + 44a747d commit 5b0b0b9
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions internal/errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,112 @@ func TestAll(t *testing.T) {
test(t, CheckAsserts|CheckBlank)
}

func TestBuildTags(t *testing.T) {
const (
// uses "custom1" build tag and contains 1 unchecked error
testBuildCustom1Tag = `
` + `// +build custom1
package custom
import "fmt"
func Print1() {
// returns an error that is not checked
fmt.Fprintln(nil)
}`
// uses "custom2" build tag and contains 1 unchecked error
testBuildCustom2Tag = `
` + `// +build custom2
package custom
import "fmt"
func Print2() {
// returns an error that is not checked
fmt.Fprintln(nil)
}`
// included so that package is not empty when built without specifying tags
testDoc = `
// Package custom contains code for testing build tags.
package custom
`
)

tmpGopath, err := ioutil.TempDir("", "testbuildtags")
if err != nil {
t.Fatalf("unable to create testbuildtags directory: %v", err)
}
testBuildTagsDir := path.Join(tmpGopath, "src", "github.com/testbuildtags")
if err := os.MkdirAll(testBuildTagsDir, 0755); err != nil {
t.Fatalf("MkdirAll failed: %v", err)
}
defer func() {
os.RemoveAll(tmpGopath)
}()

if err := ioutil.WriteFile(path.Join(testBuildTagsDir, "custom1.go"), []byte(testBuildCustom1Tag), 0644); err != nil {
t.Fatalf("Failed to write testbuildtags custom1: %v", err)
}
if err := ioutil.WriteFile(path.Join(testBuildTagsDir, "custom2.go"), []byte(testBuildCustom2Tag), 0644); err != nil {
t.Fatalf("Failed to write testbuildtags custom2: %v", err)
}
if err := ioutil.WriteFile(path.Join(testBuildTagsDir, "doc.go"), []byte(testDoc), 0644); err != nil {
t.Fatalf("Failed to write testbuildtags doc: %v", err)
}

cases := []struct {
tags []string
numExpectedErrs int
}{
// with no tags specified, main is ignored and there are no errors
{
tags: nil,
numExpectedErrs: 0,
},
// specifying "custom1" tag includes file with 1 error
{
tags: []string{"custom1"},
numExpectedErrs: 1,
},
// specifying "custom1" and "custom2" tags includes 2 files with 1 error each
{
tags: []string{"custom1", "custom2"},
numExpectedErrs: 2,
},
}

for i, currCase := range cases {
checker := NewChecker()
checker.Tags = currCase.tags
loadPackages = func(cfg *packages.Config, paths ...string) ([]*packages.Package, error) {
cfg.Env = append(os.Environ(), "GOPATH="+tmpGopath)
cfg.Dir = testBuildTagsDir
pkgs, err := packages.Load(cfg, paths...)
return pkgs, err
}
err := checker.CheckPackages("github.com/testbuildtags")

if currCase.numExpectedErrs == 0 {
if err != nil {
t.Errorf("Case %d: expected no errors, but got: %v", i, err)
}
continue
}

uerr, ok := err.(*UncheckedErrors)
if !ok {
t.Errorf("Case %d: wrong error type returned: %v", i, err)
continue
}

if currCase.numExpectedErrs != len(uerr.Errors) {
t.Errorf("Case %d:\nExpected: %d errors\nActual: %d errors", i, currCase.numExpectedErrs, len(uerr.Errors))
}
}
}

func TestWhitelist(t *testing.T) {

}
Expand Down

0 comments on commit 5b0b0b9

Please sign in to comment.