1
1
package cmd
2
2
3
3
import (
4
+ "sync"
4
5
"testing"
5
6
)
6
7
7
8
func TestLabelsMatch (t * testing.T ) {
8
- t .Parallel ()
9
-
10
9
tests := []struct {
11
10
name string
12
11
prLabels []string
@@ -147,19 +146,10 @@ func TestLabelsMatch(t *testing.T) {
147
146
caseSensitive : true ,
148
147
},
149
148
}
150
-
151
149
for _ , test := range tests {
152
150
t .Run (test .name , func (t * testing.T ) {
153
- t .Parallel ()
154
151
155
- // Save the original value of caseSensitiveLabels
156
- originalCaseSensitive := caseSensitiveLabels
157
- defer func () { caseSensitiveLabels = originalCaseSensitive }() // Restore after test
158
-
159
- // Set caseSensitiveLabels for this test
160
- caseSensitiveLabels = test .caseSensitive
161
-
162
- // Run the function
152
+ // Run the function, passing the caseSensitive parameter directly
163
153
got := labelsMatch (test .prLabels , test .ignoreLabels , test .selectLabels , test .caseSensitive )
164
154
if got != test .want {
165
155
t .Errorf ("Test %q failed: want %v, got %v" , test .name , test .want , got )
@@ -168,8 +158,6 @@ func TestLabelsMatch(t *testing.T) {
168
158
}
169
159
}
170
160
func TestBranchMatchesCriteria (t * testing.T ) {
171
- // Remove t.Parallel() at the function level to avoid races on global variables
172
-
173
161
// Define test cases
174
162
tests := []struct {
175
163
name string
@@ -269,28 +257,8 @@ func TestBranchMatchesCriteria(t *testing.T) {
269
257
t .Run (test .name , func (t * testing.T ) {
270
258
t .Parallel () // Parallelize at the subtest level, each with their own local variables
271
259
272
- // Save original values of global variables
273
- origCombineBranchName := combineBranchName
274
- origBranchPrefix := branchPrefix
275
- origBranchSuffix := branchSuffix
276
- origBranchRegex := branchRegex
277
-
278
- // Restore original values after test
279
- defer func () {
280
- combineBranchName = origCombineBranchName
281
- branchPrefix = origBranchPrefix
282
- branchSuffix = origBranchSuffix
283
- branchRegex = origBranchRegex
284
- }()
285
-
286
- // Set global variables for this specific test
287
- combineBranchName = test .combineBranch
288
- branchPrefix = test .prefix
289
- branchSuffix = test .suffix
290
- branchRegex = test .regex
291
-
292
260
// Run the function
293
- got := branchMatchesCriteria (test .branch )
261
+ got := branchMatchesCriteria (test .branch , test . combineBranch , test . prefix , test . suffix , test . regex )
294
262
295
263
// Check the result
296
264
if got != test .want {
@@ -378,24 +346,33 @@ func TestPrMatchesCriteriaWithMocks(t *testing.T) {
378
346
}
379
347
380
348
func TestPrMatchesCriteria (t * testing.T ) {
349
+ // Since this test changes global state, don't use t.Parallel()
350
+
351
+ // Create mutex to protect access to global variables
352
+ var mutex sync.Mutex
353
+
381
354
// Save original values of global variables
355
+ mutex .Lock ()
382
356
origIgnoreLabels := ignoreLabels
383
357
origSelectLabels := selectLabels
384
358
origCaseSensitiveLabels := caseSensitiveLabels
385
359
origCombineBranchName := combineBranchName
386
360
origBranchPrefix := branchPrefix
387
361
origBranchSuffix := branchSuffix
388
362
origBranchRegex := branchRegex
363
+ mutex .Unlock ()
389
364
390
365
// Restore original values after test
391
366
defer func () {
367
+ mutex .Lock ()
392
368
ignoreLabels = origIgnoreLabels
393
369
selectLabels = origSelectLabels
394
370
caseSensitiveLabels = origCaseSensitiveLabels
395
371
combineBranchName = origCombineBranchName
396
372
branchPrefix = origBranchPrefix
397
373
branchSuffix = origBranchSuffix
398
374
branchRegex = origBranchRegex
375
+ mutex .Unlock ()
399
376
}()
400
377
401
378
// Test cases
0 commit comments