Skip to content

Commit

Permalink
Make testing easier (#412)
Browse files Browse the repository at this point in the history
Use a run wrapper to avoid passing extra variables
  • Loading branch information
ssbarnea authored Jan 29, 2025
1 parent 40552d2 commit 5e3dd99
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 60 deletions.
4 changes: 0 additions & 4 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 Expand Up @@ -81,8 +79,6 @@ repos:
rev: "0.6.0"
hooks:
- id: pydoclint
# This allows automatic reduction of the baseline file when needed.
entry: sh -ec "pydoclint . && pydoclint --generate-baseline=1 ."
pass_filenames: false

- repo: https://github.com/pycqa/pylint.git
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ strict = true
[tool.pydoclint]
allow-init-docstring = true
arg-type-hints-in-docstring = false
auto-regenerate-baseline = true
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
Expand Down
40 changes: 35 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
67 changes: 28 additions & 39 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, check=True, shell=False)
except subprocess.CalledProcessError as exc:
print(exc.stdout)
print(exc.stderr)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
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 5e3dd99

Please sign in to comment.