Skip to content

Commit 88f9593

Browse files
committed
adding branch match tests
1 parent 185d4e8 commit 88f9593

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

internal/cmd/match_criteria_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,120 @@ func TestLabelsMatch(t *testing.T) {
111111
})
112112
}
113113
}
114+
func TestBranchMatchesCriteria(t *testing.T) {
115+
t.Parallel()
116+
117+
// Define test cases
118+
tests := []struct {
119+
name string
120+
branch string
121+
combineBranch string
122+
prefix string
123+
suffix string
124+
regex string
125+
want bool
126+
}{
127+
{
128+
name: "Branch matches all criteria",
129+
branch: "feature/test",
130+
combineBranch: "combined-prs",
131+
prefix: "feature/",
132+
suffix: "/test",
133+
regex: `^feature/.*$`,
134+
want: true,
135+
},
136+
{
137+
name: "Branch is the combine branch",
138+
branch: "combined-prs",
139+
combineBranch: "combined-prs",
140+
want: false,
141+
},
142+
{
143+
name: "Branch ends with the combine branch",
144+
branch: "fix-combined-prs",
145+
combineBranch: "combined-prs",
146+
want: true,
147+
},
148+
{
149+
name: "No filters specified",
150+
branch: "any-branch",
151+
combineBranch: "combined-prs",
152+
want: true,
153+
},
154+
{
155+
name: "No filters specified and partial match on combine branch name",
156+
branch: "bug/combined-prs-fix",
157+
combineBranch: "combined-prs",
158+
want: true,
159+
},
160+
{
161+
name: "Prefix does not match",
162+
branch: "test/feature",
163+
combineBranch: "combined-prs",
164+
prefix: "feature/",
165+
want: false,
166+
},
167+
{
168+
name: "Suffix does not match",
169+
branch: "feature/test",
170+
combineBranch: "combined-prs",
171+
suffix: "/feature",
172+
want: false,
173+
},
174+
{
175+
name: "Regex does not match",
176+
branch: "test/feature",
177+
combineBranch: "combined-prs",
178+
regex: `^feature/.*`,
179+
want: false,
180+
},
181+
{
182+
name: "Invalid regex pattern",
183+
branch: "feature/test",
184+
combineBranch: "combined-prs",
185+
regex: `^(feature/.*$`,
186+
want: false,
187+
},
188+
{
189+
name: "Branch matches prefix only",
190+
branch: "feature/test",
191+
combineBranch: "combined-prs",
192+
prefix: "feature/",
193+
want: true,
194+
},
195+
{
196+
name: "Branch matches suffix only",
197+
branch: "test/feature",
198+
combineBranch: "combined-prs",
199+
suffix: "/feature",
200+
want: true,
201+
},
202+
{
203+
name: "Branch matches regex only",
204+
branch: "feature/test",
205+
combineBranch: "combined-prs",
206+
regex: `^feature/.*$`,
207+
want: true,
208+
},
209+
}
210+
211+
for _, test := range tests {
212+
t.Run(test.name, func(t *testing.T) {
213+
t.Parallel()
214+
215+
// Set global variables for the test
216+
combineBranchName = test.combineBranch
217+
branchPrefix = test.prefix
218+
branchSuffix = test.suffix
219+
branchRegex = test.regex
220+
221+
// Run the function
222+
got := branchMatchesCriteria(test.branch)
223+
224+
// Check the result
225+
if got != test.want {
226+
t.Errorf("branchMatchesCriteria(%q) = %v; want %v", test.branch, got, test.want)
227+
}
228+
})
229+
}
230+
}

0 commit comments

Comments
 (0)