Skip to content

Commit 816aa53

Browse files
ccoVeillecatenacyber
authored andcommitted
fix: refactor unit tests so they don't depend on previous tests
The fact a flag can deactivate some other flags must be done after the flags are overloaded in unit tests. Some unit tests were incorrectly written to fulfill this behavior, they are now failing.
1 parent eff1a84 commit 816aa53

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

analyzer/analyzer.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,15 @@ func New() *analysis.Analyzer {
7474
r.Flags.BoolVar(&n.strFormat.strconcat, "strconcat", true, "optimizes into strings concatenation")
7575
r.Flags.BoolVar(&n.fiximports, "fiximports", true, "fix needed imports from other fixes")
7676

77-
if !n.intFormat.enabled {
78-
n.intFormat.intConv = false
79-
}
80-
81-
if !n.errFormat.enabled {
82-
n.errFormat.errError = false
83-
n.errFormat.errorf = false
84-
}
85-
if !n.strFormat.enabled {
86-
n.strFormat.sprintf1 = false
87-
n.strFormat.strconcat = false
88-
}
89-
9077
return r
9178
}
9279

9380
// true if verb is a format string that could be replaced with concatenation.
9481
func isConcatable(verb string) bool {
95-
hasPrefix :=
96-
(strings.HasPrefix(verb, "%s") && !strings.Contains(verb, "%[1]s")) ||
97-
(strings.HasPrefix(verb, "%[1]s") && !strings.Contains(verb, "%s"))
98-
hasSuffix :=
99-
(strings.HasSuffix(verb, "%s") && !strings.Contains(verb, "%[1]s")) ||
100-
(strings.HasSuffix(verb, "%[1]s") && !strings.Contains(verb, "%s"))
82+
hasPrefix := (strings.HasPrefix(verb, "%s") && !strings.Contains(verb, "%[1]s")) ||
83+
(strings.HasPrefix(verb, "%[1]s") && !strings.Contains(verb, "%s"))
84+
hasSuffix := (strings.HasSuffix(verb, "%s") && !strings.Contains(verb, "%[1]s")) ||
85+
(strings.HasSuffix(verb, "%[1]s") && !strings.Contains(verb, "%s"))
10186

10287
if strings.Count(verb, "%[1]s") > 1 {
10388
return false
@@ -106,6 +91,18 @@ func isConcatable(verb string) bool {
10691
}
10792

10893
func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
94+
if !n.intFormat.enabled {
95+
n.intFormat.intConv = false
96+
}
97+
if !n.errFormat.enabled {
98+
n.errFormat.errError = false
99+
n.errFormat.errorf = false
100+
}
101+
if !n.strFormat.enabled {
102+
n.strFormat.sprintf1 = false
103+
n.strFormat.strconcat = false
104+
}
105+
109106
var fmtSprintObj, fmtSprintfObj, fmtErrorfObj types.Object
110107
for _, pkg := range pass.Pkg.Imports() {
111108
if pkg.Path() == "fmt" {

analyzer/analyzer_test.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,38 @@ import (
1414

1515
func TestAnalyzer(t *testing.T) {
1616
t.Parallel()
17-
a := analyzer.New()
18-
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, "default")
19-
a.Flags.VisitAll(func(f *flag.Flag) {
17+
18+
t.Run("default", func(t *testing.T) {
19+
a := analyzer.New()
20+
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, "default")
21+
})
22+
23+
defaultAnalyzer := analyzer.New()
24+
defaultAnalyzer.Flags.VisitAll(func(f *flag.Flag) {
2025
if f.Name == "fiximports" {
26+
// fiximports is a special case, let's skip it
2127
return
2228
}
29+
30+
var changedVal string
31+
switch f.DefValue {
32+
case "false":
33+
changedVal = "true"
34+
case "true":
35+
changedVal = "false"
36+
default:
37+
t.Fatalf("default value neither false or true")
38+
}
39+
2340
t.Run(f.Name, func(t *testing.T) {
24-
changedVal := "false"
25-
if f.DefValue == "false" {
26-
changedVal = "true"
27-
} else if f.DefValue != "true" {
28-
t.Fatalf("default value neither false or true")
29-
}
41+
a := analyzer.New()
3042
err := a.Flags.Set(f.Name, changedVal)
3143
if err != nil {
3244
t.Fatalf("failed to set %q flag", f.Name)
3345
}
3446
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), a, f.Name)
35-
err = a.Flags.Set(f.Name, f.DefValue)
36-
if err != nil {
37-
t.Fatalf("failed to set %q flag", f.Name)
38-
}
3947
})
4048
})
41-
4249
}
4350

4451
func TestReplacements(t *testing.T) {

0 commit comments

Comments
 (0)