@@ -14,25 +14,22 @@ import (
14
14
"unsafe"
15
15
)
16
16
17
- var bucketIndex * int
18
- var bucketCount * int
19
- var directoriesToExclude * string
20
- var packagesToExclude * string
21
-
22
- var directoriesToExcludeList []string
23
- var packagesToExcludeList []string
24
-
25
17
// Buckets must be called to get the test bucket feature working.
26
18
// It will modify the tests present in the testing.M struct.
27
19
func Buckets (m * testing.M ) {
20
+ var bucketIndex int
21
+ var bucketCount int
22
+
23
+ var directoriesToExclude []string
24
+ var packagesToExclude []string
25
+
28
26
if v := os .Getenv ("BUCKET" ); v != "" {
29
27
//nolint: gomnd // use 64 bits for parsing
30
28
n , err := strconv .ParseInt (v , 0 , 64 )
31
29
if err != nil {
32
30
panic (fmt .Sprintf ("unable to parse BUCKET %s: %v" , v , err ))
33
31
}
34
- i := int (n )
35
- bucketIndex = & i
32
+ bucketIndex = int (n )
36
33
}
37
34
38
35
if v := os .Getenv ("TOTAL_BUCKETS" ); v != "" {
@@ -41,59 +38,33 @@ func Buckets(m *testing.M) {
41
38
if err != nil {
42
39
panic (fmt .Sprintf ("unable to parse BUCKET_COUNT %s: %v" , v , err ))
43
40
}
44
- i := int (n )
45
- bucketCount = & i
41
+ bucketCount = int (n )
46
42
}
47
43
48
44
if v := os .Getenv ("EXCLUDE_DIRECTORIES" ); v != "" {
49
- directoriesToExclude = & v
50
- }
51
-
52
- if v := os .Getenv ("EXCLUDE_PACKAGES" ); v != "" {
53
- packagesToExclude = & v
54
- }
55
-
56
- if directoriesToExclude != nil {
57
- directoriesToExcludeList = strings .FieldsFunc (* directoriesToExclude , func (r rune ) bool {
45
+ directoriesToExclude = strings .FieldsFunc (v , func (r rune ) bool {
58
46
return r == ',' || r == ';'
59
47
})
60
- for i := range directoriesToExcludeList {
61
- directoriesToExcludeList [i ] = filepath .ToSlash (directoriesToExcludeList [i ])
48
+ for i := range directoriesToExclude {
49
+ directoriesToExclude [i ] = filepath .ToSlash (directoriesToExclude [i ])
62
50
}
63
51
}
64
- if packagesToExclude != nil {
65
- packagesToExcludeList = strings .FieldsFunc (* packagesToExclude , func (r rune ) bool {
52
+
53
+ if v := os .Getenv ("EXCLUDE_PACKAGES" ); v != "" {
54
+ packagesToExclude = strings .FieldsFunc (v , func (r rune ) bool {
66
55
return r == ',' || r == ';'
67
56
})
68
57
}
69
58
70
- if ! isBucketFeatureEnabled ( ) && ! isDirectoriesToExcludeEnabled () && ! isPackagesToExcludeEnabled ( ) {
59
+ if ( bucketCount == 0 || bucketIndex >= bucketCount ) && ( len ( directoriesToExclude ) == 0 && len ( packagesToExclude ) == 0 ) {
71
60
return
72
61
}
73
62
74
63
v := reflect .ValueOf (m ).Elem ()
75
64
testsField := v .FieldByName ("tests" )
76
65
//nolint: gosec // allow the usage of unsafe so we can get the test slice.
77
66
ptr := unsafe .Pointer (testsField .UnsafeAddr ())
78
- filterTests ((* []testing.InternalTest )(ptr ))
79
- }
80
- func isBucketFeatureEnabled () bool {
81
- if bucketCount == nil || bucketIndex == nil {
82
- return false
83
- }
84
-
85
- if * bucketCount <= 0 || * bucketIndex < 0 || * bucketIndex >= * bucketCount {
86
- return false
87
- }
88
- return true
89
- }
90
-
91
- func isDirectoriesToExcludeEnabled () bool {
92
- return len (directoriesToExcludeList ) > 0
93
- }
94
-
95
- func isPackagesToExcludeEnabled () bool {
96
- return len (packagesToExcludeList ) > 0
67
+ filterTests ((* []testing.InternalTest )(ptr ), bucketIndex , bucketCount , directoriesToExclude , packagesToExclude )
97
68
}
98
69
99
70
func getSourceFile (f func (* testing.T )) string {
@@ -153,39 +124,40 @@ dirLoop:
153
124
return false
154
125
}
155
126
156
- func filterTests (tests * []testing.InternalTest ) {
157
- if isDirectoriesToExcludeEnabled () {
127
+ func filterTests (tests * []testing.InternalTest , bucketIndex , bucketCount int , directoriesToExclude , packagesToExclude [] string ) {
128
+ if len ( directoriesToExclude ) > 0 {
158
129
for i := len (* tests ) - 1 ; i >= 0 ; i -- {
159
130
file := getSourceFile ((* tests )[i ].F )
160
131
if file == "" {
161
132
fmt .Printf ("unable to find source of %s\n " , (* tests )[i ].Name )
162
133
continue
163
134
}
164
135
165
- if isFileInDir (filepath .ToSlash (file ), directoriesToExcludeList ... ) {
136
+ if isFileInDir (filepath .ToSlash (file ), directoriesToExclude ... ) {
166
137
* tests = append ((* tests )[:i ], (* tests )[i + 1 :]... )
167
138
}
168
139
}
169
140
}
170
- if isPackagesToExcludeEnabled () {
141
+ if len ( packagesToExclude ) > 0 {
171
142
for i := len (* tests ) - 1 ; i >= 0 ; i -- {
172
143
pkg := getPackageName ((* tests )[i ].F )
173
144
if pkg == "" {
174
145
fmt .Printf ("unable to find package of %s\n " , (* tests )[i ].Name )
175
146
continue
176
147
}
177
- if isFileInDir (pkg , packagesToExcludeList ... ) {
148
+ if isFileInDir (pkg , packagesToExclude ... ) {
178
149
* tests = append ((* tests )[:i ], (* tests )[i + 1 :]... )
179
150
}
180
151
}
181
152
}
182
- if isBucketFeatureEnabled () {
183
- perBucket := int (math .Ceil (float64 (len (* tests )) / float64 (* bucketCount )))
184
153
185
- from := * bucketIndex * perBucket
154
+ if bucketCount > 0 && bucketIndex >= 0 && bucketIndex < bucketCount {
155
+ perBucket := int (math .Ceil (float64 (len (* tests )) / float64 (bucketCount )))
156
+
157
+ from := bucketIndex * perBucket
186
158
to := from + perBucket
187
159
188
- if to > len (* tests ) {
160
+ if to > len (* tests )- 1 {
189
161
to = len (* tests )
190
162
}
191
163
0 commit comments