Skip to content

Commit

Permalink
Add additional tests (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
cidrblock authored Jul 29, 2024
1 parent 4a1828f commit 94d5dae
Show file tree
Hide file tree
Showing 4 changed files with 409 additions and 90 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cpython
delenv
devel
endgroup
envdir
envlist
envtmpdir
fileh
Expand Down
Empty file.
139 changes: 50 additions & 89 deletions tests/integration/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from __future__ import annotations

import json
import os
import runpy
import subprocess

from typing import TYPE_CHECKING
Expand All @@ -25,15 +23,14 @@ 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")
try:
proc = subprocess.run(
f"{tox_bin} -l --ansible --conf {module_fixture_dir}/tox-ansible.ini",
proc = subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=True,
shell=True,
env=os.environ,
)
except subprocess.CalledProcessError as exc:
print(exc.stdout)
Expand Down Expand Up @@ -61,14 +58,13 @@ def test_gh_matrix(
monkeypatch.delenv("GITHUB_ACTIONS", raising=False)
monkeypatch.delenv("GITHUB_OUTPUT", raising=False)

proc = subprocess.run(
f"{tox_bin} --ansible --gh-matrix --root {module_fixture_dir} --conf tox-ansible.ini",
cmd = (tox_bin, "--ansible", "--gh-matrix", "--conf", f"{module_fixture_dir}/tox-ansible.ini")
proc = subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=True,
shell=True,
env=os.environ,
)
structured = json.loads(proc.stdout)
assert isinstance(structured, list)
Expand Down Expand Up @@ -113,14 +109,13 @@ def test_no_ansible_flag(module_fixture_dir: Path, tox_bin: Path) -> None:
tox_bin: pytest fixture to get the tox binary
"""
proc = subprocess.run(
f"{tox_bin} --root {module_fixture_dir} --conf tox-ansible.ini",
cmd = (tox_bin, "--root", str(module_fixture_dir), "--conf", "tox-ansible.ini")
proc = subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=True,
shell=True,
env=os.environ,
)
assert "py: OK" in proc.stdout

Expand All @@ -133,15 +128,22 @@ def test_no_ansible_flag_gh(module_fixture_dir: Path, tox_bin: Path) -> None:
tox_bin: pytest fixture to get the tox binary
"""
cmd = (
tox_bin,
"--gh-matrix",
"--root",
str(module_fixture_dir),
"--conf",
"tox-ansible.ini",
)

with pytest.raises(subprocess.CalledProcessError) as exc:
subprocess.run(
f"{tox_bin} --gh-matrix --root {module_fixture_dir} --conf tox-ansible.ini",
subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=True,
shell=True,
env=os.environ,
)
assert "The --gh-matrix option requires --ansible" in exc.value.stdout

Expand All @@ -157,87 +159,52 @@ def test_tox_ini_msg(
tox_bin: pytest fixture to get the tox binary
"""
cmd = (tox_bin, "--ansible", "--root", str(module_fixture_dir), "-e", "non-existent")
with pytest.raises(subprocess.CalledProcessError) as exc:
subprocess.run(
f"{tox_bin} --ansible --root {module_fixture_dir} -e non-existent",
subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=True,
shell=True,
env=os.environ,
)
expected = "Using a default tox.ini file with tox-ansible plugin is not recommended"
assert expected in exc.value.stdout


def test_tox_invalid(
module_fixture_dir: Path,
tox_bin: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test an invalid environment is ignored. The plugin should not raise an error.
Args:
module_fixture_dir: pytest fixture to get the fixtures directory
tox_bin: pytest fixture to get the tox binary
monkeypatch: pytest fixture to patch modules
capsys: pytest fixture to capture stdout/stderr
"""
monkeypatch.setattr("tox_ansible.plugin.ENV_LIST", "insanity-py3.13-devel")

monkeypatch.setattr(
"sys.argv",
[
str(tox_bin),
"config",
"--ansible",
"--root",
str(module_fixture_dir),
"--conf",
"tox-ansible.ini",
],
)
with pytest.raises(SystemExit, match="0"):
runpy.run_module("tox", run_name="__main__")
captured = capsys.readouterr()
assert "Unable to find galaxy.yml file" not in captured.out


def test_setting_matrix_scope(
module_fixture_dir: Path,
tox_bin: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test setting the matrix scope to a specific section.
Args:
module_fixture_dir: pytest fixture to get the fixtures directory
tox_bin: pytest fixture to get the tox binary
monkeypatch: pytest fixture to patch modules
capsys: pytest fixture to capture stdout/stderr
"""
monkeypatch.delenv("GITHUB_ACTIONS", raising=False)
monkeypatch.delenv("GITHUB_OUTPUT", raising=False)
monkeypatch.chdir(module_fixture_dir)
monkeypatch.setattr(
"sys.argv",
[
str(tox_bin),
"--ansible",
"--gh-matrix",
"--matrix-scope",
"integration",
"--conf",
"tox-ansible.ini",
],

cmd = (
tox_bin,
"--ansible",
"--gh-matrix",
"--matrix-scope",
"integration",
"--conf",
"tox-ansible.ini",
)
with pytest.raises(SystemExit, match="0"):
runpy.run_module("tox", run_name="__main__")
captured = capsys.readouterr()
structured = json.loads(captured.out)
proc = subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=False,
)
structured = json.loads(proc.stdout)
assert isinstance(structured, list)
assert all(entry["name"].startswith("integration") for entry in structured)

Expand All @@ -246,31 +213,25 @@ def test_action_not_output(
module_fixture_dir: Path,
tox_bin: Path,
monkeypatch: pytest.MonkeyPatch,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test for exit when action is set but not output.
Args:
module_fixture_dir: pytest fixture to get the fixtures directory
tox_bin: pytest fixture to get the tox binary
monkeypatch: pytest fixture to patch modules
capsys: pytest fixture to capture
"""
monkeypatch.setenv("GITHUB_ACTIONS", "true")
monkeypatch.delenv("GITHUB_OUTPUT", raising=False)
monkeypatch.chdir(module_fixture_dir)
monkeypatch.setattr(
"sys.argv",
[
str(tox_bin),
"--ansible",
"--gh-matrix",
"--conf",
"tox-ansible.ini",
],
)

with pytest.raises(SystemExit, match="1"):
runpy.run_module("tox", run_name="__main__")
captured = capsys.readouterr()
assert "GITHUB_OUTPUT environment variable not set" in captured.out
cmd = (tox_bin, "--ansible", "--gh-matrix", "--conf", "tox-ansible.ini")

proc = subprocess.run( # noqa: S603
cmd,
capture_output=True,
cwd=str(module_fixture_dir),
text=True,
check=False,
)
assert "GITHUB_OUTPUT environment variable not set" in proc.stdout
Loading

0 comments on commit 94d5dae

Please sign in to comment.