-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestSquashMerge.bash
executable file
·294 lines (244 loc) · 11.6 KB
/
testSquashMerge.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
# Enhanced PR Test Script for testing PR generation with edge cases
# Set GPG_TTY
GPG_TTY=$(tty)
export GPG_TTY
# Valid types and scopes for conventional commits
VALID_TYPES=("build" "chore" "ci" "docs" "feat" "fix" "perf" "refactor" "style" "test")
VALID_SCOPES=("dev" "comp" "lib" "fd2" "examples" "school" "none")
COAUTHORS=(
"Grant Braught <braught@dickinson.edu>"
"John MacCormick <jmac@dickinson.edu>"
"William Goble <goblew@dickinson.edu>"
"Matt Ferland <ferlandm@dickinson.edu>"
"Boosung Kim <kimbo@dickinson.edu>"
"Ty Chermsirivatana <chermsit@dickinson.edu>"
)
# At the beginning of the script
STASH_MARKER="test_squash_merge_temporary_stash"
# Helper functions
get_random_element() {
local -n array=$1
echo "${array[RANDOM % ${#array[@]}]}"
}
get_random_invalid() {
local invalid_values=("invalid" "unknown" "test123" "custom" "random" "xyz")
echo "${invalid_values[RANDOM % ${#invalid_values[@]}]}"
}
generate_title() {
local title="$1"
local rand_types="$2"
local invalid_type="$3"
local invalid_scope="$4"
local no_type="$5"
local no_scope="$6"
if [ "$no_type" = true ]; then
echo "$title"
return
fi
if [ "$rand_types" = true ]; then
type=$(get_random_invalid)
scope=$(get_random_invalid)
else
if [ "$invalid_type" = true ]; then
type=$(get_random_invalid)
else
type=$(get_random_element VALID_TYPES)
fi
if [ "$invalid_scope" = true ]; then
scope=$(get_random_invalid)
else
scope=$(get_random_element VALID_SCOPES)
fi
fi
if [ "$no_scope" != true ]; then
scope="($scope)"
else
scope=""
fi
echo "${type}${scope}: ${title}"
}
create_pr() {
local case="$1"
local title="$2"
local body="$3"
local rand_coauthors="$4"
local rand_types="$5"
local invalid_type="${6:-false}"
local invalid_scope="${7:-false}"
local no_type="${8:-false}"
local no_scope="${9:-false}"
local branch="test-case-${case}-$(date +%s)"
local pr_title=$(generate_title "$title" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
# Case-specific modifications for "BREAKING CHANGE" or co-authors
case "$case" in
1) # Breaking change in PR description only
body="$body
BREAKING CHANGE: This is a breaking change in the PR description."
;;
2) # Breaking change in commit only - no PR description breaking change
;;
3) # Breaking changes in both PR and commits
body="$body
BREAKING CHANGE: This is a breaking change in the PR description."
;;
4) # Co-author in PR description only
if [ "$rand_coauthors" = true ]; then
random_coauthor=$(get_random_element COAUTHORS)
body="${body}
Co-authored-by: ${random_coauthor}"
else
body="${body}
Co-authored-by: ${COAUTHORS[0]}"
fi
;;
5) # Co-author in commit only - no PR description co-author
;;
6) # Co-authors in both PR and commits
if [ "$rand_coauthors" = true ]; then
random_coauthor=$(get_random_element COAUTHORS)
body="${body}
Co-authored-by: ${random_coauthor}"
else
body="${body}
Co-authored-by: ${COAUTHORS[1]}"
fi
;;
esac
echo "Creating PR test case ${case}: ${pr_title}"
echo "Purpose: ${body}"
git checkout main && git pull && git checkout -b "$branch"
mkdir -p "test_case_${case}" && echo "Change for: $pr_title" >> "test_case_${case}/testfile.txt"
# First commit with specific message based on case
case "$case" in
2|3) # Cases with breaking changes in commits
git add "test_case_${case}" && git commit -m "$(generate_title "Initial commit" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
Initial commit with breaking changes.
BREAKING CHANGE: Initial breaking change that affects backward compatibility."
;;
5|6) # Cases with co-authors in commits
if [ "$rand_coauthors" = true ]; then
random_coauthor=$(get_random_element COAUTHORS)
git add "test_case_${case}" && git commit -m "$(generate_title "Initial commit" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
Initial collaborative commit.
Co-authored-by: ${random_coauthor}"
else
coauthor_index=0
[ "$case" = "6" ] && coauthor_index=1
git add "test_case_${case}" && git commit -m "$(generate_title "Initial commit" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
Initial collaborative commit.
Co-authored-by: ${COAUTHORS[$coauthor_index]}"
fi
;;
*) # Default case - no special handling needed
git add "test_case_${case}" && git commit -m "$(generate_title "Initial commit" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")"
;;
esac
# Second commit with additional changes and specific metadata
echo "Additional change for test case ${case}" >> "test_case_${case}/testfile.txt"
case "$case" in
2|3) # Cases with breaking changes in commits
git add "test_case_${case}" && git commit -m "$(generate_title "Additional changes" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
Further implementation with breaking changes.
BREAKING CHANGE: Additional breaking changes that affect the API and existing functionality."
;;
5|6) # Cases with co-authors in commits
if [ "$rand_coauthors" = true ]; then
random_coauthor=$(get_random_element COAUTHORS)
git add "test_case_${case}" && git commit -m "$(generate_title "Additional changes" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
Additional collaborative changes.
Co-authored-by: ${random_coauthor}"
else
coauthor_index=0
[ "$case" = "6" ] && coauthor_index=1
git add "test_case_${case}" && git commit -m "$(generate_title "Additional changes" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")
Additional collaborative changes.
Co-authored-by: ${COAUTHORS[$coauthor_index]}"
fi
;;
*) # Default case - simple commit message
git add "test_case_${case}" && git commit -m "$(generate_title "Additional changes" "$rand_types" "$invalid_type" "$invalid_scope" "$no_type" "$no_scope")"
;;
esac
# Create PR and cleanup
git push -u origin "$branch" && pr_url=$(gh pr create --title "$pr_title" --body "$body")
echo "Created PR: $pr_url"
git checkout main
}
run_selected_tests() {
local selected_cases=(${1//,/ })
local random_coauthors="$2"
local random_types="$3"
for case in "${selected_cases[@]}"; do
case "$case" in
1) create_pr "1" "Breaking change in PR description only" "Testing breaking change in PR description only." "$random_coauthors" "$random_types" false false false false ;;
2) create_pr "2" "Breaking change only in commit message" "Testing breaking change in commit message only." "$random_coauthors" "$random_types" false false false false ;;
3) create_pr "3" "Breaking changes in both PR and commits" "Testing breaking changes in both locations." "$random_coauthors" "$random_types" false false false false ;;
4) create_pr "4" "Co-author only in PR description" "Testing co-author in PR description only." "$random_coauthors" "$random_types" false false false false ;;
5) create_pr "5" "Co-author only in commit message" "Testing co-author in commit message only." "$random_coauthors" "$random_types" false false false false ;;
6) create_pr "6" "Co-authors in both PR and commits" "Testing co-authors in both locations." "$random_coauthors" "$random_types" false false false false ;;
7) create_pr "7" "Invalid type and scope test" "Testing invalid type and scope." "$random_coauthors" "$random_types" true true false false ;;
8) create_pr "8" "Missing type and scope test" "Testing missing type and scope." "$random_coauthors" "$random_types" false false true true ;;
9) create_pr "9" "Test invalid type" "This PR has an invalid type." "$random_coauthors" "$random_types" true false false false ;;
10) create_pr "10" "Test invalid scope" "This PR has an invalid scope." "$random_coauthors" "$random_types" false true false false ;;
11) create_pr "11" "Add new feature without type" "Testing missing type in commit title." "$random_coauthors" "$random_types" false false true false ;;
12) create_pr "12" "Add feature without scope" "Testing missing scope in commit title." "$random_coauthors" "$random_types" false false false true ;;
13) create_pr "13" "Empty title test" "" "This PR has no description in the title." "$random_coauthors" "$random_types" false false false false ;;
14) create_pr "14" "Very long title exceeding usual length" "Testing long title in PR." "$random_coauthors" "$random_types" false false false false ;;
15) create_pr "15" "Special characters (!@#$%^&*())" "Testing special characters in PR title." "$random_coauthors" "$random_types" false false false false ;;
*) echo "Invalid test case number: $case" ;;
esac
done
}
reset_all_test_cases() {
echo "Resetting all test cases..."
# Switch to main branch and pull latest changes
git checkout main || { echo "Failed to switch to main branch"; return; }
git pull
# Delete local branches matching pattern "test-case-*"
for branch in $(git branch --list "test-case-*"); do
git branch -D "$branch" || true
done
# Delete remote branches matching pattern "test-case-*"
for branch in $(git branch -r | grep "origin/test-case-" | sed 's/origin\///'); do
git push origin --delete "$branch" || true
done
# Remove test case directories
rm -rf test_case_* || true
}
display_help() {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -h, --help Display help"
echo " -r, --reset Reset all test cases"
echo " -a, --all Run all test cases"
echo " -s, --select CASES Run specific test cases (comma-separated list, e.g., 1 or 2,3,4)"
echo " -R, --random Run with random co-authors"
echo " -T, --types Run with random types/scopes"
echo " --full-random Run with random co-authors and types"
}
main() {
[[ $# -eq 0 ]] && display_help && exit 1
local random_coauthors=false
local random_types=false
local selected_cases="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help) display_help; restore_current_state $had_changes; exit 0 ;;
-r|--reset) reset_all_test_cases; restore_current_state $had_changes; exit 0 ;;
-a|--all) selected_cases="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15" ;;
-s|--select) selected_cases="$2"; shift ;;
-R|--random) random_coauthors=true ;;
-T|--types) random_types=true ;;
--full-random) random_coauthors=true; random_types=true ;;
*) echo "Unknown option: $1"; display_help; exit 1 ;;
esac
shift
done
if [ -n "$selected_cases" ]; then
run_selected_tests "$selected_cases" "$random_coauthors" "$random_types"
else
echo "No test cases selected. Use -a for all or -s to specify cases."
fi
}
main "$@"