Skip to content

Commit

Permalink
feat: support fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jun 23, 2022
1 parent fa49f6b commit b73f59d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/bad/bad_test.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bad_test // want "package should be `bad_test` instead of `bad`"
18 changes: 17 additions & 1 deletion pkg/testpackage/testpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testpackage

import (
"flag"
"fmt"
"regexp"
"strings"

Expand Down Expand Up @@ -32,7 +33,22 @@ func processTestFile(pass *analysis.Pass, f *ast.File, allowedPackages []string)
}

if !strings.HasSuffix(packageName, "_test") {
pass.Reportf(f.Name.Pos(), "package should be `%s_test` instead of `%s`", packageName, packageName)
pass.Report(analysis.Diagnostic{
Pos: f.Name.Pos(),
Message: fmt.Sprintf("package should be `%s_test` instead of `%s`", packageName, packageName),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: fmt.Sprintf("replace `%s` with `%s_test`", packageName, packageName),
TextEdits: []analysis.TextEdit{
{
Pos: f.Name.Pos(),
End: f.Name.End(),
NewText: []byte(fmt.Sprintf("%s_test", packageName)),
},
},
},
},
})
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/testpackage/testpackage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func TestAnalyzer_Bad(t *testing.T) {
analysistest.Run(t, testdata, testpackage.NewAnalyzer())
}

func TestAnalyzer_Bad_Fixed(t *testing.T) {
testdata, err := filepath.Abs("testdata/bad")
if err != nil {
t.FailNow()
}

analysistest.RunWithSuggestedFixes(t, testdata, testpackage.NewAnalyzer())
}

func TestAnalyzer_Allowed(t *testing.T) {
analyzer := testpackage.NewAnalyzer()
err := analyzer.Flags.Set(testpackage.AllowPackagesFlagName, "allowed")
Expand Down

0 comments on commit b73f59d

Please sign in to comment.