From 3959cccd7b4622b2a0d70f8d58cef4fa3af9d6ca Mon Sep 17 00:00:00 2001 From: Matthias Bach Date: Mon, 8 May 2023 10:33:01 +0200 Subject: [PATCH] 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) ============ --- .../test_subcommands/test_tutorial001.py | 5 ++++- .../test_subcommands/test_tutorial003.py | 5 ++++- tests/test_types_file.py | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/test_tutorial/test_subcommands/test_tutorial001.py b/tests/test_tutorial/test_subcommands/test_tutorial001.py index 6b228bd788..b5a208e29b 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 import items, users env = os.environ.copy() - env["PYTHONPATH"] = ":".join(list(tutorial001.__path__)) + env["PYTHONPATH"] = ":".join( + list(tutorial001.__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 631cc0cd13..a95fcb0ba2 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.__path__)) + env["PYTHONPATH"] = ":".join( + list(tutorial003.__path__) + + ([env["PYTHONPATH"]] if "PYTHONPATH" in env else []) + ) for module in [mod, items, lands, reigns, towns, users]: result = subprocess.run( diff --git a/tests/test_types_file.py b/tests/test_types_file.py index 49dfcd6226..77658c3ae6 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_macos, 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"