Skip to content

fix flakey tests #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/cmd/match_criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// checks if a PR matches all filtering criteria
func PrMatchesCriteria(branch string, prLabels []string) bool {
// Check branch criteria if any are specified
if !branchMatchesCriteria(branch) {
if !branchMatchesCriteria(branch, combineBranchName, branchPrefix, branchSuffix, branchRegex) {
return false
}

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

// checks if a branch matches the branch filtering criteria
func branchMatchesCriteria(branch string) bool {
func branchMatchesCriteria(branch, combineBranchName, branchPrefix, branchSuffix, branchRegex string) bool {
Logger.Debug("Checking branch criteria", "branch", branch)
// Do not attempt to match on existing branches that were created by this CLI
if branch == combineBranchName {
Expand Down
47 changes: 12 additions & 35 deletions internal/cmd/match_criteria_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package cmd

import (
"sync"
"testing"
)

func TestLabelsMatch(t *testing.T) {
t.Parallel()

tests := []struct {
name string
prLabels []string
Expand Down Expand Up @@ -147,19 +146,10 @@ func TestLabelsMatch(t *testing.T) {
caseSensitive: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

// Save the original value of caseSensitiveLabels
originalCaseSensitive := caseSensitiveLabels
defer func() { caseSensitiveLabels = originalCaseSensitive }() // Restore after test

// Set caseSensitiveLabels for this test
caseSensitiveLabels = test.caseSensitive

// Run the function
// Run the function, passing the caseSensitive parameter directly
got := labelsMatch(test.prLabels, test.ignoreLabels, test.selectLabels, test.caseSensitive)
if got != test.want {
t.Errorf("Test %q failed: want %v, got %v", test.name, test.want, got)
Expand All @@ -168,8 +158,6 @@ func TestLabelsMatch(t *testing.T) {
}
}
func TestBranchMatchesCriteria(t *testing.T) {
// Remove t.Parallel() at the function level to avoid races on global variables

// Define test cases
tests := []struct {
name string
Expand Down Expand Up @@ -269,28 +257,8 @@ func TestBranchMatchesCriteria(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel() // Parallelize at the subtest level, each with their own local variables

// Save original values of global variables
origCombineBranchName := combineBranchName
origBranchPrefix := branchPrefix
origBranchSuffix := branchSuffix
origBranchRegex := branchRegex

// Restore original values after test
defer func() {
combineBranchName = origCombineBranchName
branchPrefix = origBranchPrefix
branchSuffix = origBranchSuffix
branchRegex = origBranchRegex
}()

// Set global variables for this specific test
combineBranchName = test.combineBranch
branchPrefix = test.prefix
branchSuffix = test.suffix
branchRegex = test.regex

// Run the function
got := branchMatchesCriteria(test.branch)
got := branchMatchesCriteria(test.branch, test.combineBranch, test.prefix, test.suffix, test.regex)

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

func TestPrMatchesCriteria(t *testing.T) {
// Since this test changes global state, don't use t.Parallel()

// Create mutex to protect access to global variables
var mutex sync.Mutex

// Save original values of global variables
mutex.Lock()
origIgnoreLabels := ignoreLabels
origSelectLabels := selectLabels
origCaseSensitiveLabels := caseSensitiveLabels
origCombineBranchName := combineBranchName
origBranchPrefix := branchPrefix
origBranchSuffix := branchSuffix
origBranchRegex := branchRegex
mutex.Unlock()

// Restore original values after test
defer func() {
mutex.Lock()
ignoreLabels = origIgnoreLabels
selectLabels = origSelectLabels
caseSensitiveLabels = origCaseSensitiveLabels
combineBranchName = origCombineBranchName
branchPrefix = origBranchPrefix
branchSuffix = origBranchSuffix
branchRegex = origBranchRegex
mutex.Unlock()
}()

// Test cases
Expand Down
1 change: 0 additions & 1 deletion internal/cmd/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func TestParseRepositoriesArgs(t *testing.T) {
}

func TestParseRepositoriesFile(t *testing.T) {
t.Parallel()
tests := []struct {
content string
want []github.Repo
Expand Down
6 changes: 4 additions & 2 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

set -e

count=10

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