Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to skip test files #35

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/nakedret/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

const (
DefaultLines = 5
DefaultLines = 5
DefaultSkipTestFiles = false
)

func init() {
Expand All @@ -18,6 +19,6 @@ func init() {
}

func main() {
analyzer := nakedret.NakedReturnAnalyzer(DefaultLines)
analyzer := nakedret.NakedReturnAnalyzer(DefaultLines, DefaultSkipTestFiles)
singlechecker.Main(analyzer)
}
27 changes: 17 additions & 10 deletions nakedret.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (

const pwd = "./"

func NakedReturnAnalyzer(defaultLines uint) *analysis.Analyzer {
func NakedReturnAnalyzer(defaultLines uint, skipTestFiles bool) *analysis.Analyzer {
nakedRet := &NakedReturnRunner{}
flags := flag.NewFlagSet("nakedret", flag.ExitOnError)
flags.UintVar(&nakedRet.MaxLength, "l", defaultLines, "maximum number of lines for a naked return function")
flags.BoolVar(&nakedRet.SkipTestFiles, "skip-test-files", skipTestFiles, "skipping test files")
alexkohler marked this conversation as resolved.
Show resolved Hide resolved
var analyzer = &analysis.Analyzer{
Name: "nakedret",
Doc: "Checks that functions with naked returns are not longer than a maximum size (can be zero).",
Expand All @@ -37,7 +38,8 @@ func NakedReturnAnalyzer(defaultLines uint) *analysis.Analyzer {
}

type NakedReturnRunner struct {
MaxLength uint
MaxLength uint
SkipTestFiles bool
}

func (n *NakedReturnRunner) run(pass *analysis.Pass) (any, error) {
Expand All @@ -49,18 +51,20 @@ func (n *NakedReturnRunner) run(pass *analysis.Pass) (any, error) {
(*ast.ReturnStmt)(nil),
}
retVis := &returnsVisitor{
pass: pass,
f: pass.Fset,
maxLength: n.MaxLength,
pass: pass,
f: pass.Fset,
maxLength: n.MaxLength,
skipTestFiles: n.SkipTestFiles,
}
inspector.Nodes(nodeFilter, retVis.NodesVisit)
return nil, nil
}

type returnsVisitor struct {
pass *analysis.Pass
f *token.FileSet
maxLength uint
pass *analysis.Pass
f *token.FileSet
maxLength uint
skipTestFiles bool

// functions contains funcInfo for each nested function definition encountered while visiting the AST.
functions []funcInfo
Expand All @@ -74,7 +78,7 @@ type funcInfo struct {
reportNaked bool
}

func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error {
func checkNakedReturns(args []string, maxLength *uint, skipTestFiles bool, setExitStatus bool) error {

fset := token.NewFileSet()

Expand All @@ -87,7 +91,7 @@ func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error
return errors.New("max length nil")
}

analyzer := NakedReturnAnalyzer(*maxLength)
analyzer := NakedReturnAnalyzer(*maxLength, skipTestFiles)
pass := &analysis.Pass{
Analyzer: analyzer,
Fset: fset,
Expand Down Expand Up @@ -292,6 +296,9 @@ func (v *returnsVisitor) NodesVisit(node ast.Node, push bool) bool {
if push && funcType != nil {
// Push function info to track returns for this function
file := v.f.File(node.Pos())
if v.skipTestFiles && strings.HasSuffix(file.Name(), "_test.go") {
return false
}
length := file.Position(node.End()).Line - file.Position(node.Pos()).Line
if length == 0 {
// consider functions that finish on the same line as they start as single line functions, not zero lines!
Expand Down
28 changes: 21 additions & 7 deletions nakedret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
)

type testParams struct {
filename string
maxLength uint
filename string
maxLength uint
skipTestFiles bool
}

var testcases = []struct {
Expand Down Expand Up @@ -58,9 +59,22 @@ var testcases = []struct {
filename: "testdata/src/x/nested.go",
maxLength: 0,
}},
{"failing on test files",
"testdata/src/x/example_test.go:11: naked return in func `SomeTestHelperFunction` with 3 lines of code\n",
testParams{
filename: "testdata/src/x/example_test.go",
maxLength: 0,
}},
{"skipping test files",
"",
testParams{
filename: "testdata/src/x/example_test.go",
maxLength: 0,
skipTestFiles: true,
}},
}

func runNakedret(t *testing.T, filename string, maxLength uint, expected string) {
func runNakedret(t *testing.T, filename string, maxLength uint, skipTestFiles bool, expected string) {
t.Helper()
defer func() {
// Reset logging
Expand All @@ -71,7 +85,7 @@ func runNakedret(t *testing.T, filename string, maxLength uint, expected string)
log.SetOutput(&logBuf)
log.SetFlags(0)

if err := checkNakedReturns([]string{filename}, &maxLength, false); err != nil {
if err := checkNakedReturns([]string{filename}, &maxLength, skipTestFiles, false); err != nil {
t.Fatal(err)
}
actual := logBuf.String()
Expand All @@ -83,7 +97,7 @@ func runNakedret(t *testing.T, filename string, maxLength uint, expected string)
func TestCheckNakedReturns(t *testing.T) {
for _, tt := range testcases {
t.Run(tt.name, func(t *testing.T) {
runNakedret(t, tt.params.filename, tt.params.maxLength, tt.expected)
runNakedret(t, tt.params.filename, tt.params.maxLength, tt.params.skipTestFiles, tt.expected)
})
}
}
Expand All @@ -95,7 +109,7 @@ func TestAll(t *testing.T) {
}

testdata := filepath.Join(wd, "testdata")
analysistest.Run(t, testdata, NakedReturnAnalyzer(0), "x")
analysistest.Run(t, testdata, NakedReturnAnalyzer(0, true), "x")
}

func TestAllFixes(t *testing.T) {
Expand All @@ -105,5 +119,5 @@ func TestAllFixes(t *testing.T) {
}

testdata := filepath.Join(wd, "testdata")
analysistest.RunWithSuggestedFixes(t, testdata, NakedReturnAnalyzer(0), "x")
analysistest.RunWithSuggestedFixes(t, testdata, NakedReturnAnalyzer(0, true), "x")
}
12 changes: 12 additions & 0 deletions testdata/src/x/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package x

import "testing"

func TestCheckNakedReturns(t *testing.T) {
SomeTestHelperFunction()
}

func SomeTestHelperFunction() (res string) {
res = "res"
return
}