Skip to content

Commit

Permalink
Collect subprocess coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ncoghlan committed Oct 28, 2024
1 parent c0ba107 commit 38f44c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
20 changes: 18 additions & 2 deletions tests/test_cli_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ def test_implicit_help(self, mocked_runner: MockedRunner) -> None:
assert result.exception is None, report_traceback(result.exception)
assert result.exit_code == 0

# TODO: Collect test coverage stats for this entry point invocation
@requires_venv("Entry point test")
def test_entry_point_help_raw(self) -> None:
expected_entry_point = Path(sys.executable).parent / "venvstacks"
Expand All @@ -169,7 +168,6 @@ def test_entry_point_help_raw(self) -> None:
assert result.returncode == 0
assert result.stdout is not None

# TODO: Collect test coverage stats for this entry point invocation
@requires_venv("Entry point test")
def test_entry_point_help(self) -> None:
expected_entry_point = Path(sys.executable).parent / "venvstacks"
Expand All @@ -188,6 +186,24 @@ def test_entry_point_help(self) -> None:
assert result.returncode == 0
assert result.stdout is not None

def test_module_execution(self) -> None:
# TODO: `coverage.py` isn't picking this up as executing `venvstacks/__main__.py`
# (even an indirect invocation via the runpy module doesn't get detected)
command = [sys.executable, "-m", "venvstacks", "--help"]
result = run_python_command_unchecked(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if result.stdout is not None:
# Usage message should suggest indirect execution
assert "Usage: python -m venvstacks [" in result.stdout
# Top-level callback docstring is used as the overall CLI help text
cli_help = cli.handle_app_options.__doc__
assert cli_help is not None
assert cli_help.strip() in result.stdout
# Check operation result last to ensure test results are as informative as possible
assert result.returncode == 0
assert result.stdout is not None


EXPECTED_USAGE_PREFIX = "Usage: python -m venvstacks "
EXPECTED_SUBCOMMANDS = ["lock", "build", "local-export", "publish"]
Expand Down
13 changes: 6 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ labels =
static = lint,typecheck

[testenv]
# Multi-env performance tweak based on https://hynek.me/articles/turbo-charge-tox/
package = wheel
wheel_build_env = .pkg
groups = dev
allowlist_externals = pytest
passenv =
Expand All @@ -19,28 +22,24 @@ commands =
pytest {posargs:-m "not slow"} tests/

[testenv:coverage]
groups = dev
# Subprocess coverage based on https://hynek.me/articles/turbo-charge-tox/
allowlist_externals = coverage
passenv =
CI
VENVSTACKS_*
set_env = COVERAGE_PROCESS_START={toxinidir}/pyproject.toml
commands_pre = python -c 'import pathlib; pathlib.Path("{env_site_packages_dir}/cov.pth").write_text("import coverage; coverage.process_startup()")'
commands =
coverage run --parallel -m pytest {posargs} tests/

[testenv:format]
groups = dev
allowlist_externals = ruff
commands =
ruff format {posargs} src/ tests/ misc/

[testenv:lint]
groups = dev
allowlist_externals = ruff
commands =
ruff check --exclude 'tests/sample_project' {posargs} src/ tests/ misc/

[testenv:typecheck]
groups = dev
allowlist_externals = mypy
commands =
mypy --strict --exclude 'tests/sample_project' {posargs} src/ tests/ misc/
Expand Down

0 comments on commit 38f44c2

Please sign in to comment.