Skip to content

Commit 45a012c

Browse files
youknowdotcodex
andauthored
Drive WASI snippets through the existing pytest harness (#9753)
Use the host CPython harness for fixture setup and teardown and a Wasmer launcher for RustPython execution. Preserve the exact exclusion set without pytest node-prefix deselection, and cover selection behavior with regression tests. Assisted-by: OpenAI Codex:version-unavailable Co-authored-by: Codex <[email protected]>
1 parent 7231f8a commit 45a012c

4 files changed

Lines changed: 69 additions & 12 deletions

File tree

‎.github/workflows/ci.yaml‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ jobs:
7979
':benches/**' \
8080
':example_projects/**' \
8181
':extra_tests/snippets/**' \
82+
':extra_tests/test_snippets.py' \
83+
':extra_tests/rustpython_wasi.sh' \
84+
':extra_tests/requirements.txt' \
8285
':.github/workflows/ci.yaml' \
8386
; then
8487
echo "changed=false" >> "$GITHUB_OUTPUT"
@@ -1055,7 +1058,6 @@ jobs:
10551058
builtin_memoryview
10561059
builtin_open
10571060
builtin_posixshmem
1058-
builtin_slice
10591061
builtin_str
10601062
builtin_thread
10611063
cli_int_max_str_digits
@@ -1179,19 +1181,23 @@ jobs:
11791181
- name: build rustpython
11801182
if: env.RUN_WASM_WASI == 'true'
11811183
run: cargo build --locked --profile wasm-release --target wasm32-wasip1 --no-default-features --features freeze-stdlib,stdlib,stdio,importlib,host_env,doc,ucd_3_2_0 --verbose
1184+
- name: Setup Python for the snippet harness
1185+
if: env.RUN_WASM_WASI == 'true'
1186+
uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
1187+
with:
1188+
python-version-file: .python-version
1189+
1190+
- name: Install snippet harness dependencies
1191+
if: env.RUN_WASM_WASI == 'true'
1192+
run: python -m pip install -r extra_tests/requirements.txt
1193+
11821194
- name: run snippets
11831195
if: env.RUN_WASM_WASI == 'true'
1184-
run: |
1185-
exclude=" $WASM_WASI_SNIPPET_EXCLUDE "
1186-
for path in extra_tests/snippets/*.py; do
1187-
name=$(basename "$path" .py)
1188-
case "$exclude" in
1189-
*" $name "*) continue ;;
1190-
esac
1191-
echo "::group::${path}"
1192-
wasmer run --volume "$(pwd)" target/wasm32-wasip1/wasm-release/rustpython.wasm -- "$(pwd)/${path}"
1193-
echo "::endgroup::"
1194-
done
1196+
working-directory: extra_tests
1197+
env:
1198+
RUSTPYTHON: ${{ github.workspace }}/extra_tests/rustpython_wasi.sh
1199+
RUSTPYTHON_SNIPPET_EXCLUDE: ${{ env.WASM_WASI_SNIPPET_EXCLUDE }}
1200+
run: pytest -v test_snippets.py -k test_rustpython_
11951201
- name: run cpython unittest
11961202
if: env.RUN_WASM_WASI == 'true'
11971203
run: |

‎extra_tests/rustpython_wasi.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
repo_root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
5+
exec wasmer run --volume "$repo_root" \
6+
--env "RUSTPYTHONPATH=$repo_root/Lib" \
7+
"$repo_root/target/wasm32-wasip1/wasm-release/rustpython.wasm" -- "$@"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
from unittest.mock import patch
3+
4+
import test_snippets
5+
6+
7+
class SnippetSelectionTests(unittest.TestCase):
8+
def make_test(self, method, name):
9+
class Tests:
10+
pass
11+
12+
test_snippets.create_test_function(Tests, f"snippets/{name}.py", method, None)
13+
return getattr(Tests, f"test_{method}_{name}")
14+
15+
def test_exclusion_matches_the_whole_snippet_name(self):
16+
with patch.object(test_snippets, "RUSTPYTHON_SNIPPET_EXCLUDE", {"builtin_str"}):
17+
excluded = self.make_test("rustpython", "builtin_str")
18+
sibling = self.make_test("rustpython", "builtin_str_add")
19+
20+
self.assertTrue(excluded.__unittest_skip__)
21+
self.assertFalse(getattr(sibling, "__unittest_skip__", False))
22+
23+
def test_exclusion_does_not_change_cpython_reference_tests(self):
24+
with patch.object(test_snippets, "RUSTPYTHON_SNIPPET_EXCLUDE", {"builtin_str"}):
25+
reference = self.make_test("cpython", "builtin_str")
26+
27+
self.assertFalse(getattr(reference, "__unittest_skip__", False))
28+
29+
def test_no_exclusions_keeps_the_native_default(self):
30+
with patch.object(test_snippets, "RUSTPYTHON_SNIPPET_EXCLUDE", set()):
31+
test = self.make_test("rustpython", "builtin_str")
32+
33+
self.assertFalse(getattr(test, "__unittest_skip__", False))

‎extra_tests/test_snippets.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def run_via_cpython(filename):
5151
ROOT_DIR, "target/release/rustpython"
5252
)
5353
RUSTPYTHON_BINARY = os.path.abspath(RUSTPYTHON_BINARY)
54+
RUSTPYTHON_SNIPPET_EXCLUDE = set(
55+
os.environ.get("RUSTPYTHON_SNIPPET_EXCLUDE", "").split()
56+
)
5457

5558

5659
def run_via_rustpython(filename, test_type):
@@ -71,6 +74,14 @@ def create_test_function(cls, filename, method, test_type):
7174
def test_function(self):
7275
perform_test(filename, method, test_type)
7376

77+
if (
78+
method == "rustpython"
79+
and os.path.splitext(snippet_filename)[0] in RUSTPYTHON_SNIPPET_EXCLUDE
80+
):
81+
test_function = unittest.skip("excluded by RUSTPYTHON_SNIPPET_EXCLUDE")(
82+
test_function
83+
)
84+
7485
if hasattr(cls, test_function_name):
7586
raise ValueError("Duplicate test case {}".format(test_function_name))
7687
setattr(cls, test_function_name, test_function)

0 commit comments

Comments
 (0)