Skip to content

Commit

Permalink
Fix bug in logic for ignoring test files
Browse files Browse the repository at this point in the history
Only ignore test files if we're not checking them instead of ignoring
them all the time. Add tests to make sure this doesn't regress in the
future.
  • Loading branch information
ashmrtn committed Jun 19, 2023
1 parent d5686d0 commit e727f32
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/commentmimic/commentmimic.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (m mimic) checkFuncDecl(pass *analysis.Pass, fun *ast.FuncDecl) {
commentExported := m.commentExportedFuncs
commentAllExported := m.commentAllExportedFuncs

if isTestFunc(pass.Fset, fun.Pos(), fun.Name.Name) {
if !m.commentTests && isTestFunc(pass.Fset, fun.Pos(), fun.Name.Name) {
commentExported = false
commentAllExported = false
}
Expand Down
56 changes: 56 additions & 0 deletions pkg/commentmimic/commentmimic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,62 @@ func (s *CommentMimicSuite) TestSkipTestComments() {
analysistest.Run(t, dir, mimic, "./...")
}

func (s *CommentMimicSuite) TestTestComments() {
table := []struct {
name string
input string
flags map[string]bool
}{
{
name: "DontRequireComments",
input: testdata.SkipTestComments,
flags: map[string]bool{
commentmimic.CommentExportedFuncsFlag: true,
commentmimic.CommentAllExportedFuncsFlag: true,
commentmimic.CommentInterfacesFlag: true,
commentmimic.CommentStructsFlag: true,
commentmimic.CommentTestsFlag: false,
},
},
{
name: "RequireComments",
input: testdata.TestComments,
flags: map[string]bool{
commentmimic.CommentExportedFuncsFlag: true,
commentmimic.CommentAllExportedFuncsFlag: true,
commentmimic.CommentInterfacesFlag: true,
commentmimic.CommentStructsFlag: true,
commentmimic.CommentTestsFlag: true,
},
},
}

for _, test := range table {
test := test

s.T().Run(test.name, func(t *testing.T) {
t.Parallel()

fileMap := map[string]string{
"a/a_test.go": test.input,
}

dir, cleanup, err := analysistest.WriteFiles(fileMap)
require.NoError(t, err)

defer cleanup()

mimic := commentmimic.New()

for flag, value := range test.flags {
require.NoError(t, mimic.Flags.Set(flag, strconv.FormatBool(value)))
}

analysistest.Run(t, dir, mimic, "./...")
})
}
}

func (s *CommentMimicSuite) TestFuncCommentErrors() {
element := "element"
base := generateCommentMimicCases(element)
Expand Down
27 changes: 27 additions & 0 deletions pkg/commentmimic/testdata/out_of_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,31 @@ type testIface interface {
func TestDoesntNeedComment(t *testing.T) {}
`

TestComments = `package a_test
import (
"testing"
)
type BenchmarkInterface interface {} // want "exported element 'BenchmarkInterface' should be commented"
type ExampleInterface interface {} // want "exported element 'ExampleInterface' should be commented"
type FuzzInterface interface {} // want "exported element 'FuzzInterface' should be commented"
type TestInterface interface {} // want "exported element 'TestInterface' should be commented"
func BenchmarkNeedsComment(b *testing.B) {} // want "exported element 'BenchmarkNeedsComment' should be commented"
func ExportedElement() bool { // want "exported element 'ExportedElement' should be commented"
return false
}
func ExampleNeedsComment() {} // want "exported element 'ExampleNeedsComment' should be commented"
func FuzzNeedsComment(f *testing.F) {} // want "exported element 'FuzzNeedsComment' should be commented"
func TestNeedsComment(t *testing.T) {} // want "exported element 'TestNeedsComment' should be commented"
`
)

0 comments on commit e727f32

Please sign in to comment.