Skip to content

Commit 395c1d2

Browse files
authored
fix flakey tests (#35)
* fix flakey tests * lint * protect against race and flakey tests going forward
1 parent 2257a80 commit 395c1d2

File tree

4 files changed

+18
-40
lines changed

4 files changed

+18
-40
lines changed

internal/cmd/match_criteria.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// checks if a PR matches all filtering criteria
1616
func PrMatchesCriteria(branch string, prLabels []string) bool {
1717
// Check branch criteria if any are specified
18-
if !branchMatchesCriteria(branch) {
18+
if !branchMatchesCriteria(branch, combineBranchName, branchPrefix, branchSuffix, branchRegex) {
1919
return false
2020
}
2121

@@ -28,7 +28,7 @@ func PrMatchesCriteria(branch string, prLabels []string) bool {
2828
}
2929

3030
// checks if a branch matches the branch filtering criteria
31-
func branchMatchesCriteria(branch string) bool {
31+
func branchMatchesCriteria(branch, combineBranchName, branchPrefix, branchSuffix, branchRegex string) bool {
3232
Logger.Debug("Checking branch criteria", "branch", branch)
3333
// Do not attempt to match on existing branches that were created by this CLI
3434
if branch == combineBranchName {

internal/cmd/match_criteria_test.go

Lines changed: 12 additions & 35 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()
154151

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
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)
@@ -168,8 +158,6 @@ func TestLabelsMatch(t *testing.T) {
168158
}
169159
}
170160
func TestBranchMatchesCriteria(t *testing.T) {
171-
// Remove t.Parallel() at the function level to avoid races on global variables
172-
173161
// Define test cases
174162
tests := []struct {
175163
name string
@@ -269,28 +257,8 @@ func TestBranchMatchesCriteria(t *testing.T) {
269257
t.Run(test.name, func(t *testing.T) {
270258
t.Parallel() // Parallelize at the subtest level, each with their own local variables
271259

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-
292260
// Run the function
293-
got := branchMatchesCriteria(test.branch)
261+
got := branchMatchesCriteria(test.branch, test.combineBranch, test.prefix, test.suffix, test.regex)
294262

295263
// Check the result
296264
if got != test.want {
@@ -378,24 +346,33 @@ func TestPrMatchesCriteriaWithMocks(t *testing.T) {
378346
}
379347

380348
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+
381354
// Save original values of global variables
355+
mutex.Lock()
382356
origIgnoreLabels := ignoreLabels
383357
origSelectLabels := selectLabels
384358
origCaseSensitiveLabels := caseSensitiveLabels
385359
origCombineBranchName := combineBranchName
386360
origBranchPrefix := branchPrefix
387361
origBranchSuffix := branchSuffix
388362
origBranchRegex := branchRegex
363+
mutex.Unlock()
389364

390365
// Restore original values after test
391366
defer func() {
367+
mutex.Lock()
392368
ignoreLabels = origIgnoreLabels
393369
selectLabels = origSelectLabels
394370
caseSensitiveLabels = origCaseSensitiveLabels
395371
combineBranchName = origCombineBranchName
396372
branchPrefix = origBranchPrefix
397373
branchSuffix = origBranchSuffix
398374
branchRegex = origBranchRegex
375+
mutex.Unlock()
399376
}()
400377

401378
// 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)