Skip to content

Commit

Permalink
Change how the test script runs pytest and adjust.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Jul 25, 2023
1 parent ebe91d0 commit 2375b81
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 7 additions & 3 deletions pex/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,17 @@ def add_arguments(self, parser):
pass

@contextmanager
def parsed_command(self, args=None):
# type: (Optional[Sequence[str]]) -> Iterator[_C]
def parsed_command(
self,
args=None, # type: Optional[Sequence[str]]
rewrite_prog=True, # type: bool
):
# type: (...) -> Iterator[_C]
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)

# By default, let argparse derive prog from sys.argv[0].
prog = self._prog
if os.path.basename(sys.argv[0]) == "__main__.py":
if os.path.basename(sys.argv[0]) == "__main__.py" and rewrite_prog:
prog = "{python} -m {module}".format(
python=sys.executable, module=".".join(type(self).__module__.split(".")[:-1])
)
Expand Down
5 changes: 1 addition & 4 deletions testing/bin/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ def main():
print("No test control environment variables set.")

return subprocess.call(
args=["pytest"] + passthrough_args[1:],
args=[sys.executable, "-m", "pytest"] + passthrough_args[1:],
cwd=pex_project_dir(),
# N.B.: The pytest console script needs this to see the Pex project dir on the sys.path;
# cwd is not enough.
env=make_env(PYTHONPATH=pex_project_dir()),
)


Expand Down
9 changes: 8 additions & 1 deletion tests/commands/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import io
import sys
from argparse import Namespace
from textwrap import dedent

Expand Down Expand Up @@ -73,7 +74,13 @@ def assert_output(
assert isinstance(command, Command2)
assert_output()

cm = main.parsed_command([])
cm = main.parsed_command(
args=[],
# N.B.: Help output normally tries to re-write prog depending on sys.argv. Since we're
# executing Main in-process, sys.argv is ours and not its; so disable prog re-writing so
# that we can rely on the "test_main" prog name.
rewrite_prog=False,
)
with pytest.raises(SystemExit) as exc_info:
cm.__enter__()
assert 2 == exc_info.value.code
Expand Down

0 comments on commit 2375b81

Please sign in to comment.