Visitar URL original
Added a test updater tool by terryluan12 · Pull Request #6709 · RustPython/RustPython · GitHub
Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added a semaphore + limited number of jobs at the same time
  • Loading branch information
terryluan12 committed Jan 11, 2026
commit 28ef3b46b94c22ef6b60fede8ca77c270ed197f6
28 changes: 21 additions & 7 deletions scripts/update_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Options:
-s/--check-skipped Check existing skipped tests (must be run separate from updating the tests)
-t/--timeout Set a timeout for a test
-a/--annotate While copying tests, run them and annotate failures dynamically
-j/--jobs How many libraries can be processed at a time
-h/--help Show this help message and exit

Example Usage:
Expand All @@ -34,6 +35,7 @@ copy_untracked=false
annotate=false
timeout=300
check_skip_flag=false
num_jobs=5
libraries=()

while [[ $# -gt 0 ]]; do
Expand Down Expand Up @@ -66,6 +68,10 @@ while [[ $# -gt 0 ]]; do
annotate=true
shift
;;
-j|--jobs)
num_jobs="$2"
shift 2
;;
*)
libraries+=("$1")
shift
Expand All @@ -81,6 +87,7 @@ update_tests() {
local libraries=("$@")
for lib in "${libraries[@]}"
do
sem
update_test "$lib" &
done
wait
Expand All @@ -91,7 +98,7 @@ update_test() {
local clib_path="$cpython_path/$lib"
local rlib_path="$rpython_path/$lib"

if files_equal "$clib_path" "$rlib_path"; then
if [[ -f "$clib_path" && -f "$rlib_path" ]] && files_equal "$clib_path" "$rlib_path"; then
echo "No changes in $lib. Skipping..."
return
fi
Expand All @@ -117,6 +124,7 @@ check_skips() {
local libraries=("$@")
for lib in "${libraries[@]}"
do
sem
check_skip "$lib" &
done
wait
Expand All @@ -132,7 +140,7 @@ check_skip() {
}

annotate_lib() {
local lib=$(echo "$1" | sed 's/\//./g')
local lib=${1//\//.}
local rlib_path=$2
local output=$(rustpython $lib 2>&1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Declare and assign separately to capture exit status.

If rustpython fails (e.g., cargo run fails), the exit status is masked by the assignment. This could lead to processing invalid output.

Suggested fix
-    local output=$(rustpython $lib 2>&1)
+    local output
+    output=$(rustpython "$lib" 2>&1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
local output=$(rustpython $lib 2>&1)
local output
output=$(rustpython "$lib" 2>&1)
🧰 Tools
🪛 Shellcheck (0.11.0)

[warning] 159-159: Declare and assign separately to avoid masking return values.

(SC2155)

🤖 Prompt for AI Agents
In @scripts/update_tests.sh at line 159, Replace the combined
declaration+assignment "local output=$(rustpython $lib 2>&1)" with a separate
declaration and capture so the command's exit status isn't masked: first declare
"local output" (and optionally "local rc"), then run "output=$(rustpython $lib
2>&1)" and immediately save the exit code with "rc=$?" (or check $?) before any
other commands; use the saved rc when deciding failure handling for the
rustpython invocation.


Expand All @@ -147,7 +155,7 @@ annotate_lib() {
while ! grep -q "Tests result: SUCCESS" <<< "$output"; do
((attempts++))
echo "$lib failing, annotating..."
readarray -t failed_tests <<< $(echo "$output" | awk '/^(FAIL:|ERROR:)/ {print $2}' | sort -u)
readarray -t failed_tests <<< "$(echo "$output" | awk '/^(FAIL:|ERROR:)/ {print $2}' | sort -u)"

# If the test fails/errors, then expectedFailure it
for test in "${failed_tests[@]}"
Expand All @@ -159,28 +167,34 @@ annotate_lib() {
if grep -q "\.\.\.$" <<< "$output"; then
crashing_test=$(echo "$output" | grep '\.\.\.$' | head -n 1 | awk '{print $1}')
if grep -q "Timeout" <<< "$output"; then
message="; hanging"
message=" hanging"
fi
add_above_test $rlib_path $crashing_test "@unittest.skip('TODO: RUSTPYTHON$message')"
add_above_test $rlib_path $crashing_test "@unittest.skip('TODO: RUSTPYTHON;$message')"
fi

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

if [[ attempts -gt 10 ]]; then
echo "Issue annotating $lib"
echo "Issue annotating $lib" >&2
break;
fi
done
}

files_equal() {
[[ -f "$1" && -f "$2" ]] && cmp --silent "$1" "$2"
cmp --silent "$1" "$2"
}

rustpython() {
cargo run --release --features encodings,sqlite -- -m test -j 1 -u all --fail-env-changed --timeout "$timeout" -v "$@"
}
Comment on lines +201 to +250

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Declare and assign separately to avoid masking return values.

The combined declaration and assignment on line 204 masks the exit status of rustpython. If rustpython fails catastrophically, you won't detect it.

Also, the 15-attempt limit is reasonable but the logic could still get stuck if tests alternate between different failure modes.

Suggested fix for SC2155
 annotate_lib() {
     local lib=${1//\//.}
     local rlib_path=$2
-    local output=$(rustpython $lib 2>&1)
+    local output
+    output=$(rustpython "$lib" 2>&1)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
annotate_lib() {
local lib=${1//\//.}
local rlib_path=$2
local output=$(rustpython $lib 2>&1)
if grep -q "NO TESTS RAN" <<< "$output"; then
echo "No tests ran in $lib. skipping annotation"
return
fi
echo "Annotating $lib"
local attempts=0
while ! grep -q "Tests result: SUCCESS" <<< "$output"; do
((attempts++))
# echo "$lib failing, annotating..."
readarray -t failed_tests <<< "$(echo "$output" | awk '/^(FAIL:|ERROR:)/ {print $2}' | sort -u)"
# If the test fails/errors, then expectedFailure it
for test in "${failed_tests[@]}"
do
if already_failed $rlib_path $test; then
replace_expected_with_skip $rlib_path $test
else
add_above_test $rlib_path $test "$RUSTPYTHON_CANONICAL_EX_FAILURE"
fi
done
# If the test crashes/hangs, then skip it
if grep -q "\.\.\.$" <<< "$output"; then
crashing_test=$(echo "$output" | grep '\.\.\.$' | head -n 1 | awk '{print $1}')
if grep -q "Timeout" <<< "$output"; then
hanging=true
else
hanging=false
fi
apply_skip "$rlib_path" "$crashing_test" $hanging
fi
output=$(rustpython $lib 2>&1)
if [[ $attempts -gt 15 ]]; then
echo "Issue annotating $lib" >&2
return;
fi
done
echo "Successfully updated $lib"
unset SKIP_BACKUP
}
annotate_lib() {
local lib=${1//\//.}
local rlib_path=$2
local output
output=$(rustpython "$lib" 2>&1)
if grep -q "NO TESTS RAN" <<< "$output"; then
echo "No tests ran in $lib. skipping annotation"
return
fi
echo "Annotating $lib"
local attempts=0
while ! grep -q "Tests result: SUCCESS" <<< "$output"; do
((attempts++))
# echo "$lib failing, annotating..."
readarray -t failed_tests <<< "$(echo "$output" | awk '/^(FAIL:|ERROR:)/ {print $2}' | sort -u)"
# If the test fails/errors, then expectedFailure it
for test in "${failed_tests[@]}"
do
if already_failed $rlib_path $test; then
replace_expected_with_skip $rlib_path $test
else
add_above_test $rlib_path $test "$RUSTPYTHON_CANONICAL_EX_FAILURE"
fi
done
# If the test crashes/hangs, then skip it
if grep -q "\.\.\.$" <<< "$output"; then
crashing_test=$(echo "$output" | grep '\.\.\.$' | head -n 1 | awk '{print $1}')
if grep -q "Timeout" <<< "$output"; then
hanging=true
else
hanging=false
fi
apply_skip "$rlib_path" "$crashing_test" $hanging
fi
output=$(rustpython $lib 2>&1)
if [[ $attempts -gt 15 ]]; then
echo "Issue annotating $lib" >&2
return;
fi
done
echo "Successfully updated $lib"
unset SKIP_BACKUP
}
🧰 Tools
🪛 Shellcheck (0.11.0)

[warning] 204-204: Declare and assign separately to avoid masking return values.

(SC2155)

🤖 Prompt for AI Agents
In @scripts/update_tests.sh around lines 201 - 250, In annotate_lib, avoid
combining declaration and assignment for output (currently local
output=$(rustpython ...)) so the rustpython exit code isn’t masked; instead
declare local output first, run output=$(rustpython $lib 2>&1) and capture its
exit status ($?) immediately, logging and returning on a non-zero catastrophic
failure; also harden the retry loop around attempts by detecting unchanged or
oscillating output (e.g., keep previous_output and if output == previous_output
or it alternates between two states for multiple iterations then abort with an
error) so the while loop can’t hang indefinitely even within the 15-attempt cap.


sem() {
while (( $(jobs -rp | wc -l) >= $num_jobs )); do
sleep 0.1 # brief pause before checking again
done
}

add_above_test() {
local file=$1
local test=$2
Expand Down