Skip to content

Commit a65fb8d

Browse files
chavacavachavacava
and
chavacava
authored
adds tests for specific go versions (#1043)
Co-authored-by: chavacava <salvador.cavadini@gmail.com>
1 parent 4ac5cb5 commit a65fb8d

15 files changed

+341
-15
lines changed

test/datarace_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ import (
99
func TestDatarace(t *testing.T) {
1010
testRule(t, "datarace", &rule.DataRaceRule{})
1111
}
12+
13+
func TestDataraceAfterGo1_22(t *testing.T) {
14+
testRule(t, "go1.22/datarace", &rule.DataRaceRule{})
15+
}

test/file-filter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestFileExcludeFilterAtRuleLevel(t *testing.T) {
3838

3939
t.Run("not called if exclude not match", func(t *testing.T) {
4040
rule := &TestFileFilterRule{}
41-
cfg := &lint.RuleConfig{Exclude: []string{"file-to-exclude.go"}}
41+
cfg := &lint.RuleConfig{Exclude: []string{"../testdata/file-to-exclude.go"}}
4242
cfg.Initialize()
4343
testRule(t, "file-to-exclude", rule, cfg)
4444
if rule.WasApplyed {

test/golint_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestAll(t *testing.T) {
6464
t.Fatalf("Failed reading %s: %v", fi.Name(), err)
6565
}
6666

67-
if err := assertFailures(t, baseDir, fileInfo, src, rules, map[string]lint.RuleConfig{}); err != nil {
67+
if err := assertFailures(t, path.Dir(baseDir), fileInfo, src, rules, map[string]lint.RuleConfig{}); err != nil {
6868
t.Errorf("Linting %s: %v", fi.Name(), err)
6969
}
7070
})

test/range-val-address_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ import (
1010
func TestRangeValAddress(t *testing.T) {
1111
testRule(t, "range-val-address", &rule.RangeValAddress{}, &lint.RuleConfig{})
1212
}
13+
14+
func TestRangeValAddressAfterGo1_22(t *testing.T) {
15+
testRule(t, "go1.22/range-val-address", &rule.RangeValAddress{}, &lint.RuleConfig{})
16+
}

test/range-val-in-closure_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ import (
1010
func TestRangeValInClosure(t *testing.T) {
1111
testRule(t, "range-val-in-closure", &rule.RangeValInClosureRule{}, &lint.RuleConfig{})
1212
}
13+
14+
func TestRangeValInClosureAfterGo1_22(t *testing.T) {
15+
testRule(t, "go1.22/range-val-in-closure", &rule.RangeValInClosureRule{}, &lint.RuleConfig{})
16+
}

test/redefines-builtin-id_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ import (
1010
func TestRedefinesBuiltinID(t *testing.T) {
1111
testRule(t, "redefines-builtin-id", &rule.RedefinesBuiltinIDRule{})
1212
}
13+
14+
func TestRedefinesBuiltinIDAfterGo1_21(t *testing.T) {
15+
testRule(t, "go1.21/redefines-builtin-id", &rule.RedefinesBuiltinIDRule{})
16+
}

test/utils_test.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"go/token"
1111
"go/types"
1212
"os"
13+
"path"
1314
"strconv"
1415
"strings"
1516
"testing"
@@ -18,21 +19,22 @@ import (
1819
)
1920

2021
func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.RuleConfig) {
21-
baseDir := "../testdata/"
22-
filename = filename + ".go"
23-
src, err := os.ReadFile(baseDir + filename)
22+
baseDir := path.Join("../testdata/" + path.Dir(filename))
23+
filename = path.Base(filename) + ".go"
24+
fullFilePath := path.Join(baseDir, filename)
25+
src, err := os.ReadFile(fullFilePath)
2426
if err != nil {
2527
t.Fatalf("Bad filename path in test for %s: %v", rule.Name(), err)
2628
}
27-
stat, err := os.Stat(baseDir + filename)
29+
stat, err := os.Stat(fullFilePath)
2830
if err != nil {
2931
t.Fatalf("Cannot get file info for %s: %v", rule.Name(), err)
3032
}
3133
c := map[string]lint.RuleConfig{}
3234
if config != nil {
3335
c[rule.Name()] = *config[0]
3436
}
35-
if parseInstructions(t, filename, src) == nil {
37+
if parseInstructions(t, fullFilePath, src) == nil {
3638
assertSuccess(t, baseDir, stat, []lint.Rule{rule}, c)
3739
return
3840
}
@@ -41,10 +43,11 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul
4143

4244
func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Rule, config map[string]lint.RuleConfig) error {
4345
l := lint.New(func(file string) ([]byte, error) {
44-
return os.ReadFile(baseDir + file)
46+
return os.ReadFile(file)
4547
}, 0)
4648

47-
ps, err := l.Lint([][]string{{fi.Name()}}, rules, lint.Config{
49+
filePath := path.Join(baseDir, fi.Name())
50+
ps, err := l.Lint([][]string{{filePath}}, rules, lint.Config{
4851
Rules: config,
4952
})
5053
if err != nil {
@@ -63,15 +66,15 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Ru
6366

6467
func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error {
6568
l := lint.New(func(file string) ([]byte, error) {
66-
return os.ReadFile(baseDir + file)
69+
return os.ReadFile(file)
6770
}, 0)
6871

69-
ins := parseInstructions(t, fi.Name(), src)
72+
ins := parseInstructions(t, path.Join(baseDir, fi.Name()), src)
7073
if ins == nil {
7174
return fmt.Errorf("Test file %v does not have instructions", fi.Name())
7275
}
7376

74-
ps, err := l.Lint([][]string{{fi.Name()}}, rules, lint.Config{
77+
ps, err := l.Lint([][]string{{path.Join(baseDir, fi.Name())}}, rules, lint.Config{
7578
Rules: config,
7679
})
7780
if err != nil {

testdata/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/mgechev/revive/testdata
2+
3+
// set the lowest go version
4+
// to trigger testing of all rules
5+
go 1.0

testdata/go1.21/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/mgechev/revive/testdata
2+
3+
go 1.21
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package fixtures
2+
3+
func (this data) vmethod() {
4+
nil := true // MATCH /assignment creates a shadow of built-in identifier nil/
5+
iota = 1 // MATCH /assignment modifies built-in identifier iota/
6+
}
7+
8+
func append(i, j int) { // MATCH /redefinition of the built-in function append/
9+
10+
}
11+
12+
type string int16 // MATCH /redefinition of the built-in type string/
13+
14+
func delete(set []int64, i int) (y []int64) { // MATCH /redefinition of the built-in function delete/
15+
for j, v := range set {
16+
if j != i {
17+
y = append(y, v)
18+
}
19+
}
20+
return
21+
}
22+
23+
type any int // MATCH /redefinition of the built-in type any/
24+
25+
func any() {} // MATCH /redefinition of the built-in type any/
26+
27+
var any int // MATCH /redefinition of the built-in type any/
28+
29+
const any = 1 // MATCH /redefinition of the built-in type any/
30+
31+
var i, copy int // MATCH /redefinition of the built-in function copy/
32+
33+
// issue #792
34+
type ()
35+
36+
func foo() {
37+
clear := 0 // MATCH /redefinition of the built-in function clear/
38+
max := 0 // MATCH /redefinition of the built-in function max/
39+
min := 0 // MATCH /redefinition of the built-in function min/
40+
_ = clear
41+
_ = max
42+
_ = min
43+
}
44+
45+
func foo1(new int) { // MATCH /redefinition of the built-in function new/
46+
_ = new
47+
}
48+
49+
func foo2() (new int) { // MATCH /redefinition of the built-in function new/
50+
return
51+
}
52+
53+
func foo3[new any]() { // MATCH /redefinition of the built-in function new/
54+
}

testdata/go1.22/datarace.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fixtures
2+
3+
func datarace() (r int, c char) {
4+
for _, p := range []int{1, 2} {
5+
go func() {
6+
print(r) // MATCH /potential datarace: return value r is captured (by-reference) in goroutine/
7+
print(p) // Shall not match /datarace: range value p is captured (by-reference) in goroutine/
8+
}()
9+
for i, p1 := range []int{1, 2} {
10+
a := p1
11+
go func() {
12+
print(r) // MATCH /potential datarace: return value r is captured (by-reference) in goroutine/
13+
print(p) // Shall not match /datarace: range value p is captured (by-reference) in goroutine/
14+
print(p1) // Shall not match /datarace: range value p1 is captured (by-reference) in goroutine/
15+
print(a)
16+
print(i) // Shall not match /datarace: range value i is captured (by-reference) in goroutine/
17+
}()
18+
print(i)
19+
print(p)
20+
go func() {
21+
_ = c // MATCH /potential datarace: return value c is captured (by-reference) in goroutine/
22+
}()
23+
}
24+
print(p1)
25+
}
26+
go func() {
27+
print(r) // MATCH /potential datarace: return value r is captured (by-reference) in goroutine/
28+
}()
29+
print(r)
30+
}

testdata/go1.22/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/mgechev/revive/testdata
2+
3+
go 1.22

testdata/go1.22/range-val-address.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package fixtures
2+
3+
func rangeValAddress() {
4+
m := map[string]*string{}
5+
6+
mySlice := []string{"A", "B", "C"}
7+
for _, value := range mySlice {
8+
m["address"] = &value // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
9+
}
10+
}
11+
12+
func rangeValAddress2() {
13+
m := map[string]*string{}
14+
15+
mySlice := []string{"A", "B", "C"}
16+
for i := range mySlice {
17+
m["address"] = &mySlice[i]
18+
}
19+
}
20+
21+
func rangeValAddress3() {
22+
m := map[string]*string{}
23+
24+
mySlice := []string{"A", "B", "C"}
25+
for _, value := range mySlice {
26+
a := &value // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
27+
m["address"] = a
28+
}
29+
}
30+
31+
func rangeValAddress4() {
32+
m := []*string{}
33+
34+
mySlice := []string{"A", "B", "C"}
35+
for _, value := range mySlice {
36+
m = append(m, &value) // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
37+
}
38+
}
39+
40+
func rangeValAddress5() {
41+
m := map[*string]string{}
42+
43+
mySlice := []string{"A", "B", "C"}
44+
for _, value := range mySlice {
45+
m[&value] = value // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
46+
}
47+
}
48+
49+
func rangeValAddress6() {
50+
type v struct {
51+
id string
52+
}
53+
m := []*string{}
54+
55+
mySlice := []v{{id: "A"}, {id: "B"}, {id: "C"}}
56+
for _, value := range mySlice {
57+
m = append(m, &value.id) // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
58+
}
59+
}
60+
61+
func rangeValAddress7() {
62+
type v struct {
63+
id string
64+
}
65+
m := []*string{}
66+
67+
for _, value := range []v{{id: "A"}, {id: "B"}, {id: "C"}} {
68+
m = append(m, &value.id) // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
69+
}
70+
}
71+
72+
func rangeValAddress8() {
73+
type v struct {
74+
id string
75+
}
76+
m := []*string{}
77+
78+
mySlice := []*v{{id: "A"}, {id: "B"}, {id: "C"}}
79+
for _, value := range mySlice {
80+
m = append(m, &value.id)
81+
}
82+
}
83+
84+
func rangeValAddress9() {
85+
type v struct {
86+
id string
87+
}
88+
m := []*string{}
89+
90+
mySlice := map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}}
91+
for _, value := range mySlice {
92+
m = append(m, &value.id)
93+
}
94+
}
95+
96+
func rangeValAddress10() {
97+
type v struct {
98+
id string
99+
}
100+
m := []*string{}
101+
102+
for _, value := range map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}} {
103+
m = append(m, &value.id)
104+
}
105+
}
106+
107+
func rangeValAddress11() {
108+
type v struct {
109+
id string
110+
}
111+
m := map[string]*string{}
112+
113+
for key, value := range map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}} {
114+
m[key] = &value.id
115+
}
116+
}
117+
118+
func rangeValAddress12() {
119+
type v struct {
120+
id string
121+
}
122+
m := map[string]*string{}
123+
124+
for key, value := range map[string]v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}} {
125+
m[key] = &value.id // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
126+
}
127+
}
128+
129+
func rangeValAddress13() {
130+
type v struct {
131+
id string
132+
}
133+
m := []*string{}
134+
135+
otherSlice := map[string]*v{"a": {id: "A"}, "b": {id: "B"}, "c": {id: "C"}}
136+
mySlice := otherSlice
137+
for _, value := range mySlice {
138+
m = append(m, &value.id)
139+
}
140+
}
141+
142+
func rangeValAddress14() {
143+
type v struct {
144+
id *string
145+
}
146+
147+
m := []v{}
148+
for _, value := range []string{"A", "B", "C"} {
149+
a := v{id: &value} // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
150+
m = append(m, a)
151+
}
152+
}
153+
154+
func rangeValAddress15() {
155+
type v struct {
156+
id *string
157+
}
158+
159+
m := []v{}
160+
for _, value := range []string{"A", "B", "C"} {
161+
m = append(m, v{id: &value}) // Shall not match /suspicious assignment of 'value'. range-loop variables always have the same address/
162+
}
163+
}

0 commit comments

Comments
 (0)