From f7e98b6118987842e42c3a9df3915d1ed1c0a076 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 21 Jan 2025 09:21:31 +0000 Subject: [PATCH] Refactor tests (#4364) --- .config/pydoclint-baseline.txt | 6 --- tests/conftest.py | 16 +++++++- tests/integration/test_command.py | 60 ++++++++++++++++++------------ tests/unit/model/v2/test_schema.py | 13 ++++--- 4 files changed, 59 insertions(+), 36 deletions(-) diff --git a/.config/pydoclint-baseline.txt b/.config/pydoclint-baseline.txt index 399cb2b1a..c400cd5a3 100644 --- a/.config/pydoclint-baseline.txt +++ b/.config/pydoclint-baseline.txt @@ -62,18 +62,12 @@ tests/integration/test_command.py DOC103: Function `test_command`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. DOC101: Function `test_command_idempotence`: Docstring contains fewer arguments than in function signature. DOC103: Function `test_command_idempotence`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. - DOC101: Function `test_command_dependency`: Docstring contains fewer arguments than in function signature. - DOC103: Function `test_command_dependency`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. - DOC101: Function `test_command_init_scenario`: Docstring contains fewer arguments than in function signature. - DOC103: Function `test_command_init_scenario`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. DOC101: Function `test_command_list_with_format_plain`: Docstring contains fewer arguments than in function signature. DOC103: Function `test_command_list_with_format_plain`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. DOC101: Function `test_with_missing_platform_name`: Docstring contains fewer arguments than in function signature. DOC103: Function `test_with_missing_platform_name`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. DOC101: Function `test_role_name_check_one`: Docstring contains fewer arguments than in function signature. DOC103: Function `test_role_name_check_one`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. - DOC101: Function `test_sample_collection`: Docstring contains fewer arguments than in function signature. - DOC103: Function `test_sample_collection`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. DOC101: Function `test_podman`: Docstring contains fewer arguments than in function signature. DOC103: Function `test_podman`: Docstring arguments are different from function arguments. (Or could be other formatting issues: https://jsh9.github.io/pydoclint/violation_codes.html#notes-on-doc103 ). Arguments in the function signature but not in the docstring: [app: App]. DOC101: Function `test_docker`: Docstring contains fewer arguments than in function signature. diff --git a/tests/conftest.py b/tests/conftest.py index b3abfc880..1edd24cab 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -22,9 +22,10 @@ import os import platform import shutil +import subprocess from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any import pytest @@ -274,3 +275,16 @@ def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None: test_cache_dir = Path(git_root() / ".cache" / ".molecule" / "tests") if test_cache_dir.exists(): shutil.rmtree(test_cache_dir) + + +def run(cmd: list[str], env: dict[str, str] | None = None) -> subprocess.CompletedProcess[Any]: + """Run a command. + + Args: + cmd: The command to run. + env: The environment. + + Returns: + The result. + """ + return subprocess.run(cmd, capture_output=True, text=True, check=False, env=env) diff --git a/tests/integration/test_command.py b/tests/integration/test_command.py index a62d3f392..5d199b477 100644 --- a/tests/integration/test_command.py +++ b/tests/integration/test_command.py @@ -19,10 +19,11 @@ # DEALINGS IN THE SOFTWARE. from __future__ import annotations +import subprocess import sys from pathlib import Path -from typing import TYPE_CHECKING, TypedDict +from typing import TYPE_CHECKING, Any, TypedDict import pytest @@ -36,13 +37,26 @@ from molecule.app import App +def run(cmd: list[str], env: dict[str, str] | None = None) -> subprocess.CompletedProcess[Any]: + """Run a command. + + Args: + cmd: The command to run. + env: The environment. + + Returns: + The result. + """ + return subprocess.run(cmd, capture_output=True, text=True, check=False, env=env) + + @pytest.fixture(name="scenario_to_test") -def fixture_scenario_to_test(request): # type: ignore[no-untyped-def] # noqa: ANN201, D103 +def fixture_scenario_to_test(request: FixtureRequest): # type: ignore[no-untyped-def] # noqa: ANN201, D103 return request.param @pytest.fixture(name="scenario_name") -def fixture_scenario_name(request): # type: ignore[no-untyped-def] # noqa: ANN201, D103 +def fixture_scenario_name(request: FixtureRequest): # type: ignore[no-untyped-def] # noqa: ANN201, D103 try: return request.param except AttributeError: @@ -107,8 +121,8 @@ def test_command( command: The command to run. """ cmd = ["molecule", command, "--scenario-name", scenario_name] - result = app.run_command(cmd=cmd, env=test_ephemeral_dir_env) - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + result = run(cmd=cmd, env=test_ephemeral_dir_env) + assert result.returncode == 0 if command == "syntax": assert "converge.yml" in result.stdout else: @@ -130,13 +144,13 @@ def test_command_idempotence( scenario_name: The scenario name. """ cmd = ["molecule", "create", "--scenario-name", scenario_name] - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 cmd = ["molecule", "converge", "--scenario-name", scenario_name] - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 cmd = ["molecule", "idempotence", "--scenario-name", scenario_name] - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 @pytest.mark.parametrize( @@ -147,7 +161,6 @@ def test_command_idempotence( def test_command_dependency( test_ephemeral_dir_env: dict[str, str], scenario_name: str, - app: App, ) -> None: """Test scenario dependency. @@ -156,18 +169,17 @@ def test_command_dependency( scenario_name: The scenario name. """ cmd = ["molecule", "dependency", "--scenario-name", scenario_name] - assert app.run_command(cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd, env=test_ephemeral_dir_env).returncode == 0 # Validate that dependency worked by running converge, which make use it cmd = ["molecule", "converge", "--scenario-name", scenario_name] - assert app.run_command(cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd, env=test_ephemeral_dir_env).returncode == 0 def test_command_init_scenario( monkeypatch: pytest.MonkeyPatch, tmp_path: Path, test_ephemeral_dir_env: dict[str, str], - app: App, ) -> None: """Test scenario initialization. @@ -183,14 +195,14 @@ def test_command_init_scenario( scenario_name = "test-scenario" cmd = ["molecule", "init", "scenario", scenario_name] - assert app.run_command(cmd).returncode == 0 + assert run(cmd).returncode == 0 assert (tmp_path / "molecule" / scenario_name).is_dir() cmd = ["molecule", "test", "--scenario-name", "test-scenario", "--all"] - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 cmd = ["molecule", "destroy", "--scenario-name", "test-scenario"] - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 def test_command_list_with_format_plain( @@ -208,7 +220,7 @@ def test_command_list_with_format_plain( """ monkeypatch.chdir(test_fixture_dir / "scenarios" / "driver" / "delegated") cmd = ["molecule", "list", "--format", "plain"] - result = app.run_command(cmd=cmd, env=test_ephemeral_dir_env) + result = run(cmd=cmd, env=test_ephemeral_dir_env) assert result.returncode == 0 assert result.stdout == "instance default ansible default false false\n" @@ -233,7 +245,7 @@ def test_with_missing_platform_name( missing: If platform name is missing. """ cmd = ["molecule", "test", "-s", scenario_name, "-p", platform] - result = app.run_command(cmd=cmd, env=test_ephemeral_dir_env) + result = run(cmd=cmd, env=test_ephemeral_dir_env) assert bool(result.returncode) == missing if missing: assert "Instances missing" in result.stderr @@ -257,12 +269,12 @@ def test_role_name_check_one( scenario_dir = "delegated_invalid_role_name_with_role_name_check_equals_to_1" monkeypatch.chdir(test_fixture_dir / "scenarios" / "driver" / scenario_dir) cmd = ["molecule", "dependency"] - result = app.run_command(cmd=cmd, env=test_ephemeral_dir_env) + result = run(cmd=cmd, env=test_ephemeral_dir_env) assert result.returncode == 0 string = "molecule.delegated-test does not follow current galaxy requirements" assert string in result.stderr cmd = ["molecule", "destroy"] - result = app.run_command(cmd=cmd, env=test_ephemeral_dir_env) + result = run(cmd=cmd, env=test_ephemeral_dir_env) assert result.returncode == 0 @@ -270,7 +282,6 @@ def test_sample_collection( monkeypatch: pytest.MonkeyPatch, resources_folder_path: Path, test_ephemeral_dir_env: dict[str, str], - app: App, ) -> None: """Test the sample collection. @@ -281,7 +292,7 @@ def test_sample_collection( """ monkeypatch.chdir(resources_folder_path / "sample-collection") cmd = ["molecule", "test"] - assert app.run_command(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 + assert run(cmd=cmd, env=test_ephemeral_dir_env).returncode == 0 @pytest.mark.usefixtures("test_cache_path") @@ -335,7 +346,7 @@ def test_podman(monkeypatch: pytest.MonkeyPatch, test_fixture_dir: Path, app: Ap """ monkeypatch.chdir(test_fixture_dir) command = ["molecule", "test", "--scenario-name", "podman"] - assert app.run_command(command).returncode == 0 + assert run(command).returncode == 0 @mac_on_gh @@ -359,7 +370,8 @@ def test_docker(monkeypatch: pytest.MonkeyPatch, test_fixture_dir: Path, app: Ap monkeypatch.setenv("DOCKER_SOCKET", docker_socket.as_posix()) command = ["molecule", "test", "--scenario-name", "docker"] - assert app.run_command(command).returncode == 0 + proc = subprocess.run(command, capture_output=True, text=True, check=False) + assert proc.returncode == 0 def test_smoke(monkeypatch: pytest.MonkeyPatch, test_fixture_dir: Path, app: App) -> None: @@ -371,5 +383,5 @@ def test_smoke(monkeypatch: pytest.MonkeyPatch, test_fixture_dir: Path, app: App """ monkeypatch.chdir(test_fixture_dir) command = ["molecule", "test", "--scenario-name", "smoke"] - result = app.run_command(command) + result = run(command) assert result.returncode == 0, result diff --git a/tests/unit/model/v2/test_schema.py b/tests/unit/model/v2/test_schema.py index 9e3079270..a6c5ce882 100644 --- a/tests/unit/model/v2/test_schema.py +++ b/tests/unit/model/v2/test_schema.py @@ -19,10 +19,14 @@ # DEALINGS IN THE SOFTWARE. from __future__ import annotations -from pathlib import Path +import typing -from molecule.app import get_app from molecule.model import schema_v3 +from tests.conftest import run + + +if typing.TYPE_CHECKING: + from pathlib import Path def test_base_config(config): # type: ignore[no-untyped-def] # noqa: ANN201, D103 @@ -35,7 +39,6 @@ def test_molecule_schema(resources_folder_path: Path) -> None: Args: resources_folder_path: Path to the resources folder. """ - app = get_app(Path()) cmd = [ "uv", "tool", @@ -46,7 +49,7 @@ def test_molecule_schema(resources_folder_path: Path) -> None: "src/molecule/data/molecule.json", f"{resources_folder_path}/schema_instance_files/valid/molecule.yml", ] - assert app.run_command(cmd).returncode == 0 + assert run(cmd).returncode == 0 cmd = [ "uv", @@ -58,4 +61,4 @@ def test_molecule_schema(resources_folder_path: Path) -> None: "src/molecule/data/driver.json", f"{resources_folder_path}/schema_instance_files/invalid/molecule_delegated.yml", ] - assert app.run_command(cmd).returncode != 0 + assert run(cmd).returncode != 0