X Tutup
Skip to content

Commit db005d4

Browse files
committed
Rearranged the update_tests.sh to be easier to read + fixed minor bugs/formatting issues
1 parent 5b9ae5c commit db005d4

File tree

1 file changed

+86
-69
lines changed

1 file changed

+86
-69
lines changed

scripts/update_tests.sh

Lines changed: 86 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Options:
1919
-h/--help Show this help message and exit
2020
2121
Example Usage:
22-
$0 -c ~/cpython -r - . -t 300 # Updates all non-updated tests with a timeout value of 300 seconds
22+
$0 -c ~/cpython -r . -t 300 # Updates all non-updated tests with a timeout value of 300 seconds
2323
$0 -c ~/cpython -r . -u -j 5 # Updates all non-updated tests + copies files not in cpython into rpython, with maximum 5 processes active at a time
2424
$0 -c ~/cpython -r . -a # Updates all non-updated tests + annotates with @unittest.expectedFailure/@unittest.skip
2525
$0 -r . -s # For all current tests, check if @unittest.skip can be downgraded to @unittest.expectedFailure
@@ -56,11 +56,11 @@ ignored_libraries=("multiprocessing" "concurrent")
5656
while [[ $# -gt 0 ]]; do
5757
case "$1" in
5858
-c|--cpython-path)
59-
cpython_path="$2"
59+
cpython_path="$2/Lib/test"
6060
shift 2
6161
;;
6262
-r|--rpython-path)
63-
rpython_path="$2"
63+
rpython_path="$2/Lib/test"
6464
shift 2
6565
;;
6666
-u|--copy-untracked)
@@ -93,10 +93,13 @@ while [[ $# -gt 0 ]]; do
9393
;;
9494
esac
9595
done
96+
# -------------------------------------- Constants ------------------------------------- #
97+
RUSTPYTHON_POSSIBLE_SKIP_RE="@unittest.skip.*\([\"']TODO:\s*RUSTPYTHON.*[\"']\)"
98+
RUSTPYTHON_CANONICAL_SKIP_RE="@unittest.skip\('TODO: RUSTPYTHON; .*'\)"
99+
RUSTPYTHON_CANONICAL_EX_FAILURE="@unittest.expectedFailure # TODO: RUSTPYTHON"
100+
RUSTPYTHON_CANONICAL_EX_FAILURE_RE="\s@unittest\.expectedFailure # TODO: RUSTPYTHON.*"
96101

97-
cpython_path="$cpython_path/Lib/test"
98-
rpython_path="$rpython_path/Lib/test"
99-
102+
# --------------------------------- Updating functions --------------------------------- #
100103

101104
update_tests() {
102105
local libraries=("$@")
@@ -135,11 +138,13 @@ update_test() {
135138
fi
136139
}
137140

141+
# --------------------------------- Downgrade Skips functions --------------------------------- #
142+
138143
check_skips() {
139144
local libraries=("$@")
140145
for lib in "${libraries[@]}"
141146
do
142-
if grep -qiE "@unittest.skip.*\('TODO:\s*RUSTPYTHON.*'\)" "$rpython_path/$lib"; then
147+
if grep -qiE "$RUSTPYTHON_POSSIBLE_SKIP_RE" "$rpython_path/$lib"; then
143148
sem
144149
check_skip "$lib" &
145150
else
@@ -158,6 +163,41 @@ check_skip() {
158163
annotate_lib $lib $rlib_path
159164
}
160165

166+
apply_skip() {
167+
local rlib_path=$1
168+
local test_name=$2
169+
local hanging=$3
170+
message="unknown"
171+
172+
# Check if the test has a backup skip
173+
if [[ -n "${SKIP_BACKUP[$test_name]}" ]]; then
174+
message="${SKIP_BACKUP[$test_name]//\'/\"}"
175+
elif $hanging; then
176+
message="hanging"
177+
fi
178+
179+
add_above_test "$rlib_path" "$test_name" "@unittest.skip('TODO: RUSTPYTHON; $message')"
180+
}
181+
182+
backup_skips() {
183+
local rlib_path=$1
184+
declare -gA SKIP_BACKUP=() # global associative array
185+
readarray -t skips < <(grep -E -n "^[[:space:]]*@unittest\.skip.*TODO\s?:\s?RUSTPYTHON" "$rlib_path" | sort -u)
186+
187+
for line in "${skips[@]}"; do
188+
line_num="${line%%:*}"
189+
line_text=$(echo "$line" | grep -oPi "(?<=RUSTPYTHON)\s*[;:]\s*\K(.*)?(?=[\"'])")
190+
next_line=$(sed -n "$((line_num + 1))p" "$rlib_path")
191+
192+
if [[ "$next_line" =~ def[[:space:]]+([a-zA-Z0-9_]+)\( ]]; then
193+
test_name="${BASH_REMATCH[1]}"
194+
SKIP_BACKUP[$test_name]="$line_text"
195+
fi
196+
done
197+
}
198+
199+
# --------------------------------- General functions --------------------------------- #
200+
161201
annotate_lib() {
162202
local lib=${1//\//.}
163203
local rlib_path=$2
@@ -182,7 +222,7 @@ annotate_lib() {
182222
if already_failed $rlib_path $test; then
183223
replace_expected_with_skip $rlib_path $test
184224
else
185-
add_above_test $rlib_path $test "@unittest.expectedFailure # TODO: RUSTPYTHON"
225+
add_above_test $rlib_path $test "$RUSTPYTHON_CANONICAL_EX_FAILURE"
186226
fi
187227
done
188228

@@ -199,7 +239,7 @@ annotate_lib() {
199239

200240
output=$(rustpython $lib 2>&1)
201241

202-
if [[ attempts -gt 10 ]]; then
242+
if [[ $attempts -gt 15 ]]; then
203243
echo "Issue annotating $lib" >&2
204244
return;
205245
fi
@@ -209,38 +249,41 @@ annotate_lib() {
209249
unset SKIP_BACKUP
210250
}
211251

252+
sem() {
253+
while (( $(jobs -rp | wc -l) >= $num_jobs )); do
254+
sleep 0.1 # brief pause before checking again
255+
done
256+
}
257+
258+
add_above_test() {
259+
local file=$1
260+
local test=$2
261+
local line=$3
262+
sed -i "s/^\([[:space:]]*\)def $test(/\1$line\n\1def $test(/" "$file"
263+
}
264+
265+
# --------------------------------- Utility functions --------------------------------- #
266+
267+
rustpython() {
268+
cargo run --release --features encodings,sqlite -- -m test -j 1 -u all --fail-env-changed --timeout "$timeout" -v "$@"
269+
}
270+
212271
replace_expected_with_skip() {
213272
file=$1
214273
test_name=$2
215-
sed -E "/^\s*@unittest\.expectedFailure\s+# TODO: RUSTPYTHON/ { N; /\n\s*def $test_name/ { s/^(\s*)@unittest\.expectedFailure\s+# TODO: RUSTPYTHON/\1@unittest.skip\('TODO: RUSTPYTHON'\)/ } }" -i $file
274+
sed -E "/$RUSTPYTHON_CANONICAL_EX_FAILURE_RE/ { N; /\n\s*def $test_name/ { s/^(\s*)@unittest\.expectedFailure\s+# TODO: RUSTPYTHON/\1@unittest.skip\('TODO: RUSTPYTHON'\)/ } }" -i $file
216275
}
217276

218277
already_failed() {
219278
file=$1
220279
test_name=$2
221-
grep -qPz "\s*@unittest\.expectedFailure # TODO: RUSTPYTHON\n\s*def\s+${test_name}\(" $file
280+
grep -qPz "$RUSTPYTHON_CANONICAL_EX_FAILURE_RE\n\s*def\s+${test_name}\(" $file
222281
}
223282

224283
files_equal() {
225284
cmp --silent "$1" "$2"
226285
}
227286

228-
rustpython() {
229-
cargo run --release --features encodings,sqlite -- -m test -j 1 -u all --fail-env-changed --timeout "$timeout" -v "$@"
230-
}
231-
232-
sem() {
233-
while (( $(jobs -rp | wc -l) >= $num_jobs )); do
234-
sleep 0.1 # brief pause before checking again
235-
done
236-
}
237-
238-
add_above_test() {
239-
local file=$1
240-
local test=$2
241-
local line=$3
242-
sed -i "s/^\([[:space:]]*\)def $test(/\1$line\n\1def $test(/" "$file"
243-
}
244287

245288
remove_skips() {
246289
local rlib_path=$1
@@ -252,51 +295,25 @@ remove_skips() {
252295
sed -i -E '/^[[:space:]]*@unittest\.skip.*\(["'\'']TODO\s?:\s?RUSTPYTHON.*["'\'']\)/Id' $rlib_path
253296
}
254297

255-
apply_skip() {
256-
local rlib_path=$1
257-
local test_name=$2
258-
local hanging=$3
259-
message="unknown"
260-
261-
# Check if the test has a backup skip
262-
if [[ -n "${SKIP_BACKUP[$test_name]}" ]]; then
263-
message="${SKIP_BACKUP[$test_name]//\'/\"}"
264-
elif $hanging; then
265-
message="hanging"
266-
fi
267-
268-
add_above_test "$rlib_path" "$test_name" "@unittest.skip('TODO: RUSTPYTHON; $message')"
269-
}
270-
271-
backup_skips() {
272-
local rlib_path=$1
273-
declare -gA SKIP_BACKUP=() # global associative array
274-
readarray -t skips < <(grep -E -n "^[[:space:]]*@unittest\.skip.*TODO\s?:\s?RUSTPYTHON" "$rlib_path" | sort -u)
298+
main() {
299+
if ! $check_skip_flag; then
300+
echo "Updating Tests"
275301

276-
for line in "${skips[@]}"; do
277-
line_num="${line%%:*}"
278-
line_text=$(echo "$line" | grep -oPi "(?<=RUSTPYTHON)\s*[;:]\s*\K(.*)?(?=[\"'])")
279-
next_line=$(sed -n "$((line_num + 1))p" "$rlib_path")
302+
# If libraries are not specified, then update all tests
303+
if [[ "${#libraries[@]}" -eq 0 ]]; then
304+
readarray -t libraries <<< $(find ${cpython_path} -type f -printf "%P\n" | grep -vE "$(IFS=\|; echo "${ignored_libraries[*]}")")
305+
fi
306+
update_tests "${libraries[@]}"
307+
else
308+
echo "Checking Skips"
280309

281-
if [[ "$next_line" =~ def[[:space:]]+([a-zA-Z0-9_]+)\( ]]; then
282-
test_name="${BASH_REMATCH[1]}"
283-
SKIP_BACKUP[$test_name]="$line_text"
310+
# If libraries are not specified, then check all tests
311+
if [[ ${#libraries[@]} -eq 0 ]]; then
312+
readarray -t libraries <<< $(find ${rpython_path} -iname "test_*.py" -type f -printf "%P\n" | grep -vE "$(IFS=\|; echo "${ignored_libraries[*]}")")
284313
fi
285-
done
314+
check_skips "${libraries[@]}"
315+
fi
286316
}
287317

288-
if ! $check_skip_flag; then
289-
echo "Updating Tests"
290-
291-
if [[ ${#libraries[@]} -eq 0 ]]; then
292-
readarray -t libraries <<< $(find ${cpython_path} -type f -printf "%P\n" | grep -vE "$(IFS=\|; echo "${ignored_libraries[*]}")")
293-
fi
294-
update_tests "${libraries[@]}"
295-
else
296-
echo "Checking Skips"
297318

298-
if [[ ${#libraries[@]} -eq 0 ]]; then
299-
readarray -t libraries <<< $(find ${rpython_path} -iname "test_*.py" -type f -printf "%P\n" | grep -vE "$(IFS=\|; echo "${ignored_libraries[*]}")")
300-
fi
301-
check_skips "${libraries[@]}"
302-
fi
319+
main

0 commit comments

Comments
 (0)
X Tutup