Skip to content

Commit 5efdd32

Browse files
authored
Merge pull request #23 from github/flakey-test
bug: fix a flakey test
2 parents 6e20d07 + 7660e14 commit 5efdd32

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

internal/cmd/match_criteria_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestLabelsMatch(t *testing.T) {
166166
}
167167
}
168168
func TestBranchMatchesCriteria(t *testing.T) {
169-
t.Parallel()
169+
// Remove t.Parallel() at the function level to avoid races on global variables
170170

171171
// Define test cases
172172
tests := []struct {
@@ -263,10 +263,25 @@ func TestBranchMatchesCriteria(t *testing.T) {
263263
}
264264

265265
for _, test := range tests {
266+
test := test // Create a local copy of the test variable to use in the closure
266267
t.Run(test.name, func(t *testing.T) {
267-
t.Parallel()
268+
t.Parallel() // Parallelize at the subtest level, each with their own local variables
269+
270+
// Save original values of global variables
271+
origCombineBranchName := combineBranchName
272+
origBranchPrefix := branchPrefix
273+
origBranchSuffix := branchSuffix
274+
origBranchRegex := branchRegex
275+
276+
// Restore original values after test
277+
defer func() {
278+
combineBranchName = origCombineBranchName
279+
branchPrefix = origBranchPrefix
280+
branchSuffix = origBranchSuffix
281+
branchRegex = origBranchRegex
282+
}()
268283

269-
// Set global variables for the test
284+
// Set global variables for this specific test
270285
combineBranchName = test.combineBranch
271286
branchPrefix = test.prefix
272287
branchSuffix = test.suffix

0 commit comments

Comments
 (0)