From a753a06f50e55618c8aeb3a65dd31866433f07f7 Mon Sep 17 00:00:00 2001 From: Matthias Bach Date: Mon, 8 May 2023 10:33:01 +0200 Subject: [PATCH 1/2] # Das ist eine Kombination aus 2 Commits. # Das ist die erste Commit-Beschreibung: Ensure the PYTHONPATH is set properly when testing the tutorial scripts There seems to be some weird behaviour when testing for Python 3.11 that typer is not found in subprocesses because of the wrangling we do to the systempath. [ 193s] =================================== FAILURES =================================== [ 193s] _________________________________ test_scripts _________________________________ [ 193s] [ 193s] mod = [ 193s] [ 193s] def test_scripts(mod): [ 193s] from docs_src.subcommands.tutorial001 import items, users [ 193s] [ 193s] env = os.environ.copy() [ 193s] env["PYTHONPATH"] = ":".join(list(tutorial001.__path__)) [ 193s] [ 193s] for module in [mod, items, users]: [ 193s] result = subprocess.run( [ 193s] [sys.executable, "-m", "coverage", "run", module.__file__, "--help"], [ 193s] stdout=subprocess.PIPE, [ 193s] stderr=subprocess.PIPE, [ 193s] encoding="utf-8", [ 193s] env=env, [ 193s] ) [ 193s] > assert "Usage" in result.stdout [ 193s] E assert 'Usage' in '' [ 193s] E + where '' = CompletedProcess(args=['/usr/bin/python3.11', '-m', 'coverage', 'run', '/home/abuild/rpmbuild/BUILD/typer-0.9.0/docs_src/subcommands/tutorial001/main.py', '--help'], returncode=1, stdout='', stderr='Traceback (most recent call last):\n File "/home/abuild/rpmbuild/BUILD/typer-0.9.0/docs_src/subcommands/tutorial001/main.py", line 1, in \n import typer\nModuleNotFoundError: No module named \'typer\'\n').stdout [ 193s] [ 193s] tests/test_tutorial/test_subcommands/test_tutorial001.py:100: AssertionError [ 193s] _________________________________ test_scripts _________________________________ [ 193s] [ 193s] mod = [ 193s] [ 193s] def test_scripts(mod): [ 193s] from docs_src.subcommands.tutorial003 import items, lands, reigns, towns, users [ 193s] [ 193s] env = os.environ.copy() [ 193s] env["PYTHONPATH"] = ":".join(list(tutorial003.__path__)) [ 193s] [ 193s] for module in [mod, items, lands, reigns, towns, users]: [ 193s] result = subprocess.run( [ 193s] [sys.executable, "-m", "coverage", "run", module.__file__, "--help"], [ 193s] stdout=subprocess.PIPE, [ 193s] stderr=subprocess.PIPE, [ 193s] encoding="utf-8", [ 193s] env=env, [ 193s] ) [ 193s] > assert "Usage" in result.stdout [ 193s] E assert 'Usage' in '' [ 193s] E + where '' = CompletedProcess(args=['/usr/bin/python3.11', '-m', 'coverage', 'run', '/home/abuild/rpmbuild/BUILD/typer-0.9.0/docs_src/subcommands/tutorial003/main.py', '--help'], returncode=1, stdout='', stderr='Traceback (most recent call last):\n File "/home/abuild/rpmbuild/BUILD/typer-0.9.0/docs_src/subcommands/tutorial003/main.py", line 1, in \n import typer\nModuleNotFoundError: No module named \'typer\'\n').stdout [ 193s] [ 193s] tests/test_tutorial/test_subcommands/test_tutorial003.py:173: AssertionError [ 193s] =========================== short test summary info ============================ [ 193s] FAILED tests/test_tutorial/test_subcommands/test_tutorial001.py::test_scripts [ 193s] FAILED tests/test_tutorial/test_subcommands/test_tutorial003.py::test_scripts [ 193s] ============ 2 failed, 878 passed, 2 deselected in 82.15s (0:01:22) ============ # Die Commit-Beschreibung #2 wird ausgelassen: # fixup! Ensure the PYTHONPATH is set properly when testing the tutorial scripts --- tests/test_tutorial/test_subcommands/test_tutorial001.py | 5 ++++- tests/test_tutorial/test_subcommands/test_tutorial003.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/test_tutorial/test_subcommands/test_tutorial001.py b/tests/test_tutorial/test_subcommands/test_tutorial001.py index 4e8b23ec30..0b40ab7f68 100644 --- a/tests/test_tutorial/test_subcommands/test_tutorial001.py +++ b/tests/test_tutorial/test_subcommands/test_tutorial001.py @@ -87,7 +87,10 @@ def test_scripts(mod): from docs_src.subcommands.tutorial001_py310 import items, users env = os.environ.copy() - env["PYTHONPATH"] = ":".join(list(tutorial001_py310.__path__)) + env["PYTHONPATH"] = ":".join( + list(tutorial001_py310.__path__) + + ([env["PYTHONPATH"]] if "PYTHONPATH" in env else []) + ) for module in [mod, items, users]: result = subprocess.run( diff --git a/tests/test_tutorial/test_subcommands/test_tutorial003.py b/tests/test_tutorial/test_subcommands/test_tutorial003.py index 30b43526fd..756e582bd2 100644 --- a/tests/test_tutorial/test_subcommands/test_tutorial003.py +++ b/tests/test_tutorial/test_subcommands/test_tutorial003.py @@ -166,7 +166,10 @@ def test_scripts(mod): ) env = os.environ.copy() - env["PYTHONPATH"] = ":".join(list(tutorial003_py310.__path__)) + env["PYTHONPATH"] = ":".join( + list(tutorial003_py310.__path__) + + ([env["PYTHONPATH"]] if "PYTHONPATH" in env else []) + ) for module in [mod, items, lands, reigns, towns, users]: result = subprocess.run( From 7a6d2689ae9aa094b0882ac4d24f17add337a452 Mon Sep 17 00:00:00 2001 From: Matthias Bach Date: Wed, 3 Jun 2026 21:27:26 +0200 Subject: [PATCH 2/2] Ensure PYTHONPATH is passed in file argument tests --- tests/test_types_file.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_types_file.py b/tests/test_types_file.py index 3fb7d82f69..734867d373 100644 --- a/tests/test_types_file.py +++ b/tests/test_types_file.py @@ -1,3 +1,4 @@ +import os import subprocess import sys from io import BytesIO, StringIO @@ -9,6 +10,7 @@ from typer._click.utils import PacifyFlushWrapper from typer.testing import CliRunner +import tests from tests.utils import needs_linux, needs_windows app = typer.Typer() @@ -136,6 +138,17 @@ def test_binary_dash() -> None: def test_binary_stderr() -> None: + env = os.environ.copy() + env.update( + { + "PYTHONPATH": ":".join( + [str(Path(tests.__path__[0]).parent)] + [env["PYTHONPATH"]] + ) + } + if "PYTHONPATH" in env + else {} + ) + result = subprocess.run( [ sys.executable, @@ -146,6 +159,7 @@ def test_binary_stderr() -> None: "write-binary-stderr", ], capture_output=True, + env=env, ) assert result.returncode == 0 assert result.stderr == b"binary-stderr\n"