diff --git a/src/bonsai/runpytest.py b/src/bonsai/runpytest.py index 9a00ea69b0..88e1472095 100755 --- a/src/bonsai/runpytest.py +++ b/src/bonsai/runpytest.py @@ -17,18 +17,46 @@ # along with Bonsai. If not, see . """ -Requires pytest installed under blender +Requires pytest installed under blender. -Usage: `blender -b -P runpytest.py -- ARGS` +Usage: + blender -b -P runpytest.py -- ARGS + +Alternative (when the calling shell strips or reorders the ``--`` separator +before it reaches Blender — observed with some PowerShell / wrapper-script +invocations on Windows): pass the same pytest args via the +``BONSAI_TEST_ARGS`` environment variable as a single shell-quoted string +and invoke without ``--``:: + + $env:BONSAI_TEST_ARGS = "test/bim/ -x -q" + blender -b -P runpytest.py """ +import os +import shlex import sys import pytest argv = [__file__] -if "--" in sys.argv: +env_args = os.environ.get("BONSAI_TEST_ARGS", "") +if env_args: + # POSIX-style quoting works on all three OSes — env var values are + # literal strings (no shell evaluation when Python reads them), and + # POSIX quoting (``'foo "bar baz" qux'`` → three tokens, quotes stripped) + # matches what most docs and examples use. + argv += shlex.split(env_args) + # On the env-var path the args never appear in Blender's argv at all, + # so any pytest plugin that reads ``sys.argv`` directly (instead of + # going through pytest's API) would otherwise see only Blender's own + # ``-b -P runpytest.py`` and miss the test args entirely. Shadow argv + # so those plugins see the pytest-shaped view they expect. + sys.argv = list(argv) +elif "--" in sys.argv: + # The traditional path: Blender forwards everything after ``--`` to the + # script via ``sys.argv``. ``sys.argv`` is deliberately left as Blender + # set it — pre-existing behavior, preserved. i = sys.argv.index("--") argv += sys.argv[i + 1 :]