Skip to content

Commit

Permalink
test flags of linters
Browse files Browse the repository at this point in the history
  • Loading branch information
sridharv committed Nov 12, 2016
1 parent 20f3408 commit 68397cb
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 10 deletions.
17 changes: 17 additions & 0 deletions dupl/dupl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,20 @@ const expectedUnformatted = `File not formatted: diff GOFMT_TMP_FOLDER
+ A string
+ Long string
}`

func TestSkipTwo(t *testing.T) {
testutil.TestSkips(t, []testutil.SkipTest{
{S: dupl.SkipTwo, Line: "some line", Skip: false},
{S: dupl.SkipTwo, Line: "found 2 clones: here", Skip: true},
{S: dupl.SkipTwo, Line: "checker: found 2 clones: here", Skip: false},
{S: dupl.SkipTwo, Line: "dupl.Check: found 2 clones: here", Skip: true},
})
}

func TestSkip(t *testing.T) {
testutil.TestSkips(t, []testutil.SkipTest{
{S: dupl.Skip("lint.go:1,12"), Line: "some line", Skip: false},
{S: dupl.Skip("lint.go:1,12"), Line: "lint.go:1,12", Skip: false},
{S: dupl.Skip("lint.go:1,12"), Line: "dupl.Check: found 2 clones: here\nlint.go:1,12", Skip: true},
})
}
7 changes: 6 additions & 1 deletion errcheck/errcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type Check struct {

// Check runs errcheck and returns any errors found.
func (c Check) Check(pkgs ...string) error {
return checkers.Lint("errcheck", "github.com/kisielk/errcheck", pkgs, c.Args()...)
}

// Args returns command line arguments used for errcheck
func (c Check) Args() []string {
var args []string
if c.Blank {
args = append(args, "-blank")
Expand All @@ -25,5 +30,5 @@ func (c Check) Check(pkgs ...string) error {
if c.Tags != "" {
args = append(args, "-tags", c.Tags)
}
return checkers.Lint("errcheck", "github.com/kisielk/errcheck", pkgs, args...)
return args
}
10 changes: 10 additions & 0 deletions errcheck/errcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ func TestFunc() {
},
)
}

func TestArgs(t *testing.T) {
testutil.TestArgs(t, []testutil.ArgTest{
{A: errcheck.Check{}, Expected: nil},
{A: errcheck.Check{Blank: true}, Expected: []string{"-blank"}},
{A: errcheck.Check{Assert: true}, Expected: []string{"-assert"}},
{A: errcheck.Check{Tags: "test"}, Expected: []string{"-tags", "test"}},
{A: errcheck.Check{Blank: true, Assert: true}, Expected: []string{"-blank", "-assert"}},
})
}
2 changes: 1 addition & 1 deletion govet/govet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"strings"

"github.com/surullabs/lint/testutil"
"github.com/surullabs/lint/govet"
"github.com/surullabs/lint/testutil"
)

func testVetError(err error) error {
Expand Down
13 changes: 9 additions & 4 deletions structcheck/structcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ type Check struct {

// Check runs structcheck and returns any errors found.
func (c Check) Check(pkgs ...string) error {
if _, err := checkers.InstallMissing("structcheck", "github.com/opennota/check", "github.com/opennota/check/cmd/structcheck"); err != nil {
return err
}
return checkers.Lint("structcheck", "github.com/opennota/check/cmd/structcheck", pkgs, c.Args()...)
}

// Args returns command line flags for structcheck
func (c Check) Args() []string {
var args []string
if c.ReportExported {
args = append(args, "-e")
Expand All @@ -25,8 +33,5 @@ func (c Check) Check(pkgs ...string) error {
if c.IncludeTests {
args = append(args, "-t")
}
if _, err := checkers.InstallMissing("structcheck", "github.com/opennota/check", "github.com/opennota/check/cmd/structcheck"); err != nil {
return err
}
return checkers.Lint("structcheck", "github.com/opennota/check/cmd/structcheck", pkgs, args...)
return args
}
10 changes: 10 additions & 0 deletions structcheck/structcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ type s struct {
},
)
}

func TestArgs(t *testing.T) {
testutil.TestArgs(t, []testutil.ArgTest{
{A: structcheck.Check{}, Expected: nil},
{A: structcheck.Check{IncludeTests: true}, Expected: []string{"-t"}},
{A: structcheck.Check{OnlyCountAssignments: true}, Expected: []string{"-a"}},
{A: structcheck.Check{ReportExported: true}, Expected: []string{"-e"}},
{A: structcheck.Check{IncludeTests: true, ReportExported: true}, Expected: []string{"-e", "-t"}},
})
}
58 changes: 58 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"regexp"

"reflect"

"github.com/sridharv/fakegopath"
"github.com/surullabs/lint"
"github.com/surullabs/lint/checkers"
Expand Down Expand Up @@ -111,3 +113,59 @@ func Contains(str string) func(err error) error {
return nil
}
}

// Arger holds the Args method that returns a list of command line arguments
type Arger interface {
Args() []string
}

// ArgTest verifies that the expected command line arguments are returned
type ArgTest struct {
A Arger
Expected []string
}

// Test runs ArgTest and returns an error if the arguments generated by a.A do not
// match a.Expected
func (a ArgTest) Test() error {
args := a.A.Args()
if !reflect.DeepEqual(args, a.Expected) {
return fmt.Errorf("Expected: %v, got %v", a.Expected, args)
}
return nil
}

// TestArgs runs the provided ArgTests. Errors are reported using
// Errorer.
func TestArgs(t Errorer, tests []ArgTest) {
for i, test := range tests {
if err := test.Test(); err != nil {
t.Error("Args", i, err)
}
}
}

// SkipTest is a table driven test for skippers
type SkipTest struct {
S lint.Skipper
Line string
Skip bool
}

// Test returns an error if the s.S.Skip(s.Line) != s.Skip
func (s SkipTest) Test() error {
if s.S.Skip(s.Line) != s.Skip {
return fmt.Errorf("expected %v for %s", s.Skip, s.Line)
}
return nil
}

// TestSkips runs the provided SkipTests. Errors are reported using
// Errorer.
func TestSkips(t Errorer, tests []SkipTest) {
for i, test := range tests {
if err := test.Test(); err != nil {
t.Error("Skips", i, err)
}
}
}
13 changes: 9 additions & 4 deletions varcheck/varcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ type Check struct {

// Check runs varcheck and returns any errors found.
func (c Check) Check(pkgs ...string) error {
if _, err := checkers.InstallMissing("varcheck", "github.com/opennota/check", "github.com/opennota/check/cmd/varcheck"); err != nil {
return err
}
return checkers.Lint("varcheck", "github.com/opennota/check/cmd/varcheck", pkgs, c.Args()...)
}

// Args returns all args passed to varcheck
func (c Check) Args() []string {
var args []string
if c.ReportExported {
args = append(args, "-e")
}
if _, err := checkers.InstallMissing("varcheck", "github.com/opennota/check", "github.com/opennota/check/cmd/varcheck"); err != nil {
return err
}
return checkers.Lint("varcheck", "github.com/opennota/check/cmd/varcheck", pkgs, args...)
return args
}
7 changes: 7 additions & 0 deletions varcheck/varcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ var unused bool
},
)
}

func TestArgs(t *testing.T) {
testutil.TestArgs(t, []testutil.ArgTest{
{A: varcheck.Check{}, Expected: nil},
{A: varcheck.Check{ReportExported: true}, Expected: []string{"-e"}},
})
}

0 comments on commit 68397cb

Please sign in to comment.