Skip to content

Commit d339008

Browse files
committed
protect against race and flakey tests going forward
1 parent f64338f commit d339008

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

internal/cmd/match_criteria_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package cmd
22

33
import (
4+
"sync"
45
"testing"
56
)
67

78
func TestLabelsMatch(t *testing.T) {
8-
t.Parallel()
9-
109
tests := []struct {
1110
name string
1211
prLabels []string
@@ -147,19 +146,10 @@ func TestLabelsMatch(t *testing.T) {
147146
caseSensitive: true,
148147
},
149148
}
150-
151149
for _, test := range tests {
152150
t.Run(test.name, func(t *testing.T) {
153-
t.Parallel()
154-
155-
// Save the original value of caseSensitiveLabels
156-
originalCaseSensitive := caseSensitiveLabels
157-
defer func() { caseSensitiveLabels = originalCaseSensitive }() // Restore after test
158151

159-
// Set caseSensitiveLabels for this test
160-
caseSensitiveLabels = test.caseSensitive
161-
162-
// Run the function
152+
// Run the function, passing the caseSensitive parameter directly
163153
got := labelsMatch(test.prLabels, test.ignoreLabels, test.selectLabels, test.caseSensitive)
164154
if got != test.want {
165155
t.Errorf("Test %q failed: want %v, got %v", test.name, test.want, got)
@@ -356,24 +346,33 @@ func TestPrMatchesCriteriaWithMocks(t *testing.T) {
356346
}
357347

358348
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+
359354
// Save original values of global variables
355+
mutex.Lock()
360356
origIgnoreLabels := ignoreLabels
361357
origSelectLabels := selectLabels
362358
origCaseSensitiveLabels := caseSensitiveLabels
363359
origCombineBranchName := combineBranchName
364360
origBranchPrefix := branchPrefix
365361
origBranchSuffix := branchSuffix
366362
origBranchRegex := branchRegex
363+
mutex.Unlock()
367364

368365
// Restore original values after test
369366
defer func() {
367+
mutex.Lock()
370368
ignoreLabels = origIgnoreLabels
371369
selectLabels = origSelectLabels
372370
caseSensitiveLabels = origCaseSensitiveLabels
373371
combineBranchName = origCombineBranchName
374372
branchPrefix = origBranchPrefix
375373
branchSuffix = origBranchSuffix
376374
branchRegex = origBranchRegex
375+
mutex.Unlock()
377376
}()
378377

379378
// Test cases

internal/cmd/repos_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func TestParseRepositoriesArgs(t *testing.T) {
7171
}
7272

7373
func TestParseRepositoriesFile(t *testing.T) {
74-
t.Parallel()
7574
tests := []struct {
7675
content string
7776
want []github.Repo

script/test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
set -e
44

5+
count=10
6+
57
# if the tparse binary is not found, don't use it
68
if ! command -v tparse &> /dev/null; then
7-
go test -v -cover -coverprofile=coverage.out ./...
9+
go test -race -count $count -v -cover -coverprofile=coverage.out ./...
810
else
9-
set -o pipefail && go test -cover -coverprofile=coverage.out -json ./... | tparse -smallscreen -all -trimpath github.com/github/
11+
set -o pipefail && go test -race -count $count -cover -coverprofile=coverage.out -json ./... | tparse -smallscreen -all -trimpath github.com/github/
1012
fi

0 commit comments

Comments
 (0)