From 6b680df400c3d2cdc692dd173f7aafad0afaf200 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 28 Jan 2025 17:17:25 +0000 Subject: [PATCH] Make testing easier Use a run wrapper to avoid passing extra variables --- .pre-commit-config.yaml | 2 - pyproject.toml | 2 +- tests/conftest.py | 40 +++++++++++++-- tests/integration/test_basic.py | 67 +++++++++++-------------- tests/integration/test_user_provided.py | 17 +++---- 5 files changed, 70 insertions(+), 58 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b4e772d..56446a2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -23,8 +23,6 @@ repos: rev: v3.1.0 hooks: - id: add-trailing-comma - args: - - --py36-plus - repo: https://github.com/Lucas-C/pre-commit-hooks.git rev: v1.5.5 diff --git a/pyproject.toml b/pyproject.toml index e564165..571dd43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,7 +72,7 @@ allow-init-docstring = true arg-type-hints-in-docstring = false baseline = ".config/pydoclint-baseline.txt" check-return-types = false -exclude = '\.cache|\.eggs|\.git|\.mypy_cache|\.tox|build|dist|out|venv' +exclude = '\.cache|\.eggs|\.git|\.mypy_cache|\.tox|build|dist|out|site|\.?venv' should-document-private-class-attributes = true show-filenames-in-every-violation-message = true skip-checking-short-docstrings = false diff --git a/tests/conftest.py b/tests/conftest.py index 472e85a..5823bc0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,11 +33,44 @@ if TYPE_CHECKING: + from collections.abc import Sequence + from _pytest.python import Metafunc GH_MATRIX_LENGTH = 45 +def run( + args: Sequence[str | Path] | str | Path, + *, + cwd: Path, + check: bool = False, + shell: bool = True, + env: subprocess._ENV | None = None, +) -> subprocess.CompletedProcess[str]: + """Utility function to run a command. + + Args: + args: The command to run + cwd: The current working directory + check: Whether to raise an exception if the command fails + shell: Whether to run the command in a shell + env: The environment to run the command in + + Returns: + A CompletedProcess with the result of the command + """ + return subprocess.run( + args=args, + capture_output=True, + check=check, + cwd=str(cwd), + shell=shell, + text=True, + env=env, + ) + + @pytest.fixture(scope="session") def tox_bin() -> Path: """Provide the path to the tox binary. @@ -117,14 +150,11 @@ def pytest_generate_tests(metafunc: Metafunc) -> None: env.pop("TOX_ENV_NAME", None) env.pop("TOX_WORK_DIR", None) - proc = subprocess.run( + proc = run( args=cmd, - capture_output=True, check=True, - cwd=str(basic_dir), + cwd=basic_dir, env=env, - shell=True, - text=True, ) except subprocess.CalledProcessError as exc: print(exc.stdout) diff --git a/tests/integration/test_basic.py b/tests/integration/test_basic.py index 4fb252f..3e6302b 100644 --- a/tests/integration/test_basic.py +++ b/tests/integration/test_basic.py @@ -9,6 +9,8 @@ import pytest +from tests.conftest import run + if TYPE_CHECKING: from pathlib import Path @@ -23,15 +25,9 @@ def test_ansible_environments(module_fixture_dir: Path, tox_bin: Path) -> None: module_fixture_dir: pytest fixture to get the fixtures directory tox_bin: pytest fixture to get the tox binary """ - cmd = (tox_bin, "-l", "--ansible", "--conf", f"{module_fixture_dir}/tox-ansible.ini") + cmd = (str(tox_bin), "-l", "--ansible", "--conf", f"{module_fixture_dir}/tox-ansible.ini") try: - proc = subprocess.run( # noqa: S603 - cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, - check=True, - ) + proc = run(cmd, cwd=module_fixture_dir, check=True, shell=False) except subprocess.CalledProcessError as exc: print(exc.stdout) print(exc.stderr) @@ -59,12 +55,11 @@ def test_gh_matrix( monkeypatch.delenv("GITHUB_OUTPUT", raising=False) cmd = (tox_bin, "--ansible", "--gh-matrix", "--conf", f"{module_fixture_dir}/tox-ansible.ini") - proc = subprocess.run( # noqa: S603 + proc = run( cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=True, + shell=False, ) structured = json.loads(proc.stdout) assert isinstance(structured, list) @@ -110,11 +105,9 @@ def test_no_ansible_flag(module_fixture_dir: Path, tox_bin: Path) -> None: """ cmd = (tox_bin, "--root", str(module_fixture_dir), "--conf", "tox-ansible.ini") - proc = subprocess.run( # noqa: S603 + proc = run( cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=True, ) assert "py: OK" in proc.stdout @@ -129,23 +122,22 @@ def test_no_ansible_flag_gh(module_fixture_dir: Path, tox_bin: Path) -> None: """ cmd = ( - tox_bin, + str(tox_bin), "--gh-matrix", "--root", str(module_fixture_dir), "--conf", - "tox-ansible.ini", + str(module_fixture_dir / "tox-ansible.ini"), ) - with pytest.raises(subprocess.CalledProcessError) as exc: - subprocess.run( # noqa: S603 - cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, - check=True, - ) - assert "The --gh-matrix option requires --ansible" in exc.value.stdout + proc = run( + cmd, + cwd=module_fixture_dir, + check=False, + shell=False, + ) + assert proc.returncode == 1 + assert "The --gh-matrix option requires --ansible" in proc.stdout def test_tox_ini_msg( @@ -161,12 +153,11 @@ def test_tox_ini_msg( """ cmd = (tox_bin, "--ansible", "--root", str(module_fixture_dir), "-e", "non-existent") with pytest.raises(subprocess.CalledProcessError) as exc: - subprocess.run( # noqa: S603 + run( cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=True, + shell=False, ) expected = "Using a default tox.ini file with tox-ansible plugin is not recommended" assert expected in exc.value.stdout @@ -197,12 +188,11 @@ def test_setting_matrix_scope( "--conf", "tox-ansible.ini", ) - proc = subprocess.run( # noqa: S603 + proc = run( cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=False, + shell=False, ) structured = json.loads(proc.stdout) assert isinstance(structured, list) @@ -227,11 +217,10 @@ def test_action_not_output( cmd = (tox_bin, "--ansible", "--gh-matrix", "--conf", "tox-ansible.ini") - proc = subprocess.run( # noqa: S603 + proc = run( cmd, - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=False, + shell=False, ) assert "GITHUB_OUTPUT environment variable not set" in proc.stdout diff --git a/tests/integration/test_user_provided.py b/tests/integration/test_user_provided.py index 09f7334..0ae4888 100644 --- a/tests/integration/test_user_provided.py +++ b/tests/integration/test_user_provided.py @@ -11,6 +11,8 @@ import pytest +from tests.conftest import run + if TYPE_CHECKING: from pathlib import Path @@ -27,14 +29,10 @@ def test_user_provided( tox_bin: pytest fixture for tox binary """ try: - proc = subprocess.run( + proc = run( f"{tox_bin} config --ansible --root {module_fixture_dir} --conf tox-ansible.ini", - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=True, - shell=True, - env=os.environ, ) except subprocess.CalledProcessError as exc: print(exc.stdout) @@ -68,13 +66,10 @@ def test_user_provided_matrix_success( """ monkeypatch.delenv("GITHUB_ACTIONS", raising=False) monkeypatch.delenv("GITHUB_OUTPUT", raising=False) - proc = subprocess.run( + proc = run( f"{tox_bin} --ansible --root {module_fixture_dir} --gh-matrix --conf tox-ansible.ini", - capture_output=True, - cwd=str(module_fixture_dir), - text=True, + cwd=module_fixture_dir, check=True, - shell=True, env=os.environ, ) matrix = json.loads(proc.stdout)