Skip to content

refactor: test utils functions by removing redundant test instructions parsing and simplifying asserting functions #1374

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

Merged
merged 1 commit into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 2 additions & 5 deletions test/golint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ func TestAll(t *testing.T) {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}

fileInfo, err := os.Stat(filePath)
if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
}
ins := parseInstructions(t, filePath, src)

if err := assertFailures(t, filepath.Dir(baseDir), fileInfo, src, rules, map[string]lint.RuleConfig{}); err != nil {
if err := assertFailures(t, filePath, rules, map[string]lint.RuleConfig{}, ins); err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err)
}
})
Expand Down
29 changes: 12 additions & 17 deletions test/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul
if err != nil {
t.Fatalf("Bad filename path in test for %s: %v", rule.Name(), err)
}
stat, err := os.Stat(fullFilePath)
if err != nil {
t.Fatalf("Cannot get file info for %s: %v", rule.Name(), err)
}

var ruleConfig lint.RuleConfig
c := map[string]lint.RuleConfig{}
if len(config) > 0 {
Expand All @@ -54,19 +51,19 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul
}
configureRule(t, rule, ruleConfig.Arguments)

if parseInstructions(t, fullFilePath, src) == nil {
assertSuccess(t, baseDir, stat, []lint.Rule{rule}, c)
ins := parseInstructions(t, fullFilePath, src)
if ins == nil {
assertSuccess(t, fullFilePath, []lint.Rule{rule}, c)
return
}
assertFailures(t, baseDir, stat, src, []lint.Rule{rule}, c)
assertFailures(t, fullFilePath, []lint.Rule{rule}, c, ins)
}

func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Rule, config map[string]lint.RuleConfig) error {
func assertSuccess(t *testing.T, filePath string, rules []lint.Rule, config map[string]lint.RuleConfig) error {
t.Helper()

l := lint.New(os.ReadFile, 0)

filePath := filepath.Join(baseDir, fi.Name())
ps, err := l.Lint([][]string{{filePath}}, rules, lint.Config{
Rules: config,
})
Expand All @@ -84,14 +81,12 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Ru
return nil
}

func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error {
func assertFailures(t *testing.T, filePath string, rules []lint.Rule, config map[string]lint.RuleConfig, ins []instruction) error {
t.Helper()

l := lint.New(os.ReadFile, 0)

ins := parseInstructions(t, filepath.Join(baseDir, fi.Name()), src)

ps, err := l.Lint([][]string{{filepath.Join(baseDir, fi.Name())}}, rules, lint.Config{
ps, err := l.Lint([][]string{{filePath}}, rules, lint.Config{
Rules: config,
})
if err != nil {
Expand Down Expand Up @@ -119,13 +114,13 @@ func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, ru
r = r[:i]
}
if r != in.Replacement {
t.Errorf("Lint failed at %s:%d; got replacement %q, want %q", fi.Name(), in.Line, r, in.Replacement)
t.Errorf("Lint failed at %s:%d; got replacement %q, want %q", filePath, in.Line, r, in.Replacement)
}
}

if in.Confidence > 0 {
if in.Confidence != p.Confidence {
t.Errorf("Lint failed at %s:%d; got confidence %f, want %f", fi.Name(), in.Line, p.Confidence, in.Confidence)
t.Errorf("Lint failed at %s:%d; got confidence %f, want %f", filePath, in.Line, p.Confidence, in.Confidence)
}
}

Expand All @@ -138,11 +133,11 @@ func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, ru
}
}
if !ok {
t.Errorf("Lint failed at %s:%d; /%v/ did not match", fi.Name(), in.Line, in.Match)
t.Errorf("Lint failed at %s:%d; /%v/ did not match", filePath, in.Line, in.Match)
}
}
for _, p := range failures {
t.Errorf("Unexpected problem at %s:%d: %v", fi.Name(), p.Position.Start.Line, p.Failure)
t.Errorf("Unexpected problem at %s:%d: %v", filePath, p.Position.Start.Line, p.Failure)
}
return nil
}
Expand Down