Skip to content

Commit

Permalink
Make testing easier
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jan 28, 2025
1 parent e68f5ef commit 77915c6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,42 @@


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,
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
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=True,
text=True,
env=env,
)


@pytest.fixture(scope="session")
def tox_bin() -> Path:
"""Provide the path to the tox binary.
Expand Down Expand Up @@ -117,14 +148,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)
Expand Down
48 changes: 16 additions & 32 deletions tests/integration/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import pytest

from tests.conftest import run


if TYPE_CHECKING:
from pathlib import Path
Expand All @@ -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)
except subprocess.CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
Expand Down Expand Up @@ -59,11 +55,9 @@ 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,
)
structured = json.loads(proc.stdout)
Expand Down Expand Up @@ -110,11 +104,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
Expand All @@ -138,11 +130,9 @@ def test_no_ansible_flag_gh(module_fixture_dir: Path, tox_bin: Path) -> None:
)

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,
)
assert "The --gh-matrix option requires --ansible" in exc.value.stdout
Expand All @@ -161,11 +151,9 @@ 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,
)
expected = "Using a default tox.ini file with tox-ansible plugin is not recommended"
Expand Down Expand Up @@ -197,11 +185,9 @@ 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,
)
structured = json.loads(proc.stdout)
Expand All @@ -227,11 +213,9 @@ 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,
)
assert "GITHUB_OUTPUT environment variable not set" in proc.stdout
17 changes: 6 additions & 11 deletions tests/integration/test_user_provided.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import pytest

from tests.conftest import run


if TYPE_CHECKING:
from pathlib import Path
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 77915c6

Please sign in to comment.