Skip to content

Commit

Permalink
Merge pull request kisielk#160 from nmiyake/addBuildTagsTestMaster
Browse files Browse the repository at this point in the history
Add test for build tags
  • Loading branch information
kisielk authored Dec 30, 2018
2 parents 9e64295 + 8e9c2a7 commit 3a04621
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions internal/errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,100 @@ 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
`
)

testBuildTagsDir, err := ioutil.TempDir(".", "testbuildtags")
if err != nil {
t.Fatalf("unable to create testbuildtags directory: %v", err)
}
defer os.RemoveAll(testBuildTagsDir)

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
err := checker.CheckPackages(path.Join("github.com/kisielk/errcheck/internal/errcheck", testBuildTagsDir))

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 3a04621

Please sign in to comment.