Add BONSAI_TEST_ARGS env-var fallback to runpytest.py

PowerShell and some wrapper scripts on Windows occasionally strip
or reorder the `--` separator before Blender sees it, dropping the
pytest args into Blender's positional file-load slot ("File format
is not supported"). The env var carries the same args via a
shell-evaluation-free channel. Default `--` path is byte-identical
to the pre-change behaviour.

Generated with the assistance of an AI coding tool.
This commit is contained in:
Gorgious56
2026-05-21 11:17:31 +02:00
parent 6caf94f1d3
commit 4943c77c5e
+31 -3
View File
@@ -17,18 +17,46 @@
# along with Bonsai. If not, see <http://www.gnu.org/licenses/>.
"""
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 :]