Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make testing easier #412

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Make testing easier
Use a run wrapper to avoid passing extra variables
ssbarnea committed Jan 29, 2025
commit ffa2a44f660b5bf58bd26291d487fead57f306af
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
@@ -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
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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
40 changes: 35 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
67 changes: 28 additions & 39 deletions tests/integration/test_basic.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 6 additions & 11 deletions tests/integration/test_user_provided.py
Original file line number Diff line number Diff line change
@@ -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)