From 34fdabc50f355b8dc0eafaf4c92787627874150b Mon Sep 17 00:00:00 2001 From: dibrinsofor Date: Sun, 16 Oct 2022 11:21:18 +0100 Subject: [PATCH 1/3] extended test cases: included nested functions --- nakedret_test.go | 59 +++++++++++++++++++++++++++++++++------------- testdata/nested.go | 18 ++++++++++++++ 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/nakedret_test.go b/nakedret_test.go index 1533e76..698a2d9 100644 --- a/nakedret_test.go +++ b/nakedret_test.go @@ -2,11 +2,43 @@ package main import ( "bytes" + "fmt" "log" "os" "testing" ) +type testParams struct { + filename string + maxLength uint +} + +var testcases = []struct { + name string + expected string + params testParams +}{ + {"return in block", `testdata/ret-in-block.go:9: Dummy naked returns on 8 line function +`, testParams{ + filename: "testdata/ret-in-block.go", + maxLength: 0, + }}, + {"ignore short functions", ``, testParams{ + filename: "testdata/ret-in-block.go", + maxLength: 10, + }}, + {"nested function literals", `testdata/nested.go:16: Bad naked returns on 6 line function +testdata/nested.go:21: naked returns on 2 line function +testdata/nested.go:28: naked returns on 2 line function +testdata/nested.go:32: naked returns on 2 line function +testdata/nested.go:36: naked returns on 2 line function +testdata/nested.go:40: naked returns on 2 line function +`, testParams{ + filename: "testdata/nested.go", + maxLength: 0, + }}, +} + func runNakedret(t *testing.T, filename string, maxLength uint, expected string) { t.Helper() defer func() { @@ -21,25 +53,18 @@ func runNakedret(t *testing.T, filename string, maxLength uint, expected string) if err := checkNakedReturns([]string{filename}, &maxLength, false); err != nil { t.Fatal(err) } - actual := string(logBuf.Bytes()) + actual := logBuf.String() + fmt.Print(actual) if expected != actual { - t.Errorf("Unexpected output:\n-----\n%s\n-----\n", actual) + // t.Errorf("Unexpected output:\n-----\ngot: \n%s\nexpected: \n%v\n-----\n", actual, expected) + t.Errorf("Unexpected output:\n-----\n%s\n-----", actual) } } -func TestReturnInBlock(t *testing.T) { - expected := `testdata/ret-in-block.go:9: Dummy naked returns on 8 line function -` - runNakedret(t, "testdata/ret-in-block.go", 0, expected) -} - -func TestIgnoreShortFunctions(t *testing.T) { - runNakedret(t, "testdata/ret-in-block.go", 10, "") -} - -func TestNestedFunctionLiterals(t *testing.T) { - expected := `testdata/nested.go:16: Bad naked returns on 6 line function -testdata/nested.go:21: naked returns on 2 line function -` - runNakedret(t, "testdata/nested.go", 0, expected) +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) + }) + } } diff --git a/testdata/nested.go b/testdata/nested.go index 71b9b88..fac4778 100644 --- a/testdata/nested.go +++ b/testdata/nested.go @@ -22,3 +22,21 @@ func BadNested() { } return } + +func MoreBad() { + var _ = func() (err error) { + return + } + + func() (err error) { + return + }() + + defer func() (err error) { + return + }() + + go func() (err error) { + return + }() +} From 33b64a48da57194794af0cf1f328e9f3f43c329d Mon Sep 17 00:00:00 2001 From: dibrinsofor Date: Sun, 16 Oct 2022 11:24:03 +0100 Subject: [PATCH 2/3] modified error message --- nakedret.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nakedret.go b/nakedret.go index 667a14e..7787b0b 100644 --- a/nakedret.go +++ b/nakedret.go @@ -64,7 +64,7 @@ func checkNakedReturns(args []string, maxLength *uint, setExitStatus bool) error files, err := parseInput(args, fset) if err != nil { - return fmt.Errorf("could not parse input %v", err) + return fmt.Errorf("could not parse input: %v", err) } if maxLength == nil { From 5ace50e8f8d5fe09961dfabebaa8121529329445 Mon Sep 17 00:00:00 2001 From: dibrinsofor Date: Tue, 18 Oct 2022 14:06:19 +0100 Subject: [PATCH 3/3] removed trailing print statements and comments --- nakedret_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/nakedret_test.go b/nakedret_test.go index 698a2d9..5d0908a 100644 --- a/nakedret_test.go +++ b/nakedret_test.go @@ -2,7 +2,6 @@ package main import ( "bytes" - "fmt" "log" "os" "testing" @@ -54,10 +53,8 @@ func runNakedret(t *testing.T, filename string, maxLength uint, expected string) t.Fatal(err) } actual := logBuf.String() - fmt.Print(actual) if expected != actual { - // t.Errorf("Unexpected output:\n-----\ngot: \n%s\nexpected: \n%v\n-----\n", actual, expected) - t.Errorf("Unexpected output:\n-----\n%s\n-----", actual) + t.Errorf("Unexpected output:\n-----\ngot: \n%s\nexpected: \n%v\n-----\n", actual, expected) } }