Skip to content

Commit

Permalink
avoid hardcoded path in test conf (#258)
Browse files Browse the repository at this point in the history
* avoid hardcoded path in test conf

This change uses pathlib in the test conf instead of hardcoding it to
the home directory for the ansible user.

* chore: auto fixes from pre-commit.com hooks

* combine linux and mac os paths

* fix type and hint

* chore: auto fixes from pre-commit.com hooks

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
oraNod and pre-commit-ci[bot] authored Jul 24, 2024
1 parent 00874c5 commit 6e4f0a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
14 changes: 12 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import subprocess

from pathlib import Path
from subprocess import CalledProcessError, CompletedProcess
from typing import TYPE_CHECKING, Any

Expand All @@ -16,10 +17,9 @@

if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path


os.environ["HOME"] = "/home/ansible"
os.environ["HOME"] = str(Path.home())
os.environ["DEV_WORKSPACE"] = "collections/ansible_collections"


Expand Down Expand Up @@ -53,6 +53,16 @@ def output(tmp_path: Path) -> Output:
)


@pytest.fixture()
def home_path() -> Path:
"""Create the home directory as a fixture.
Returns:
Path: Home directory.
"""
return Path.home()


def cli_run(args: list[str]) -> CompletedProcess[str] | CalledProcessError:
"""Execute a command using subprocess.
Expand Down
28 changes: 14 additions & 14 deletions tests/units/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import runpy
import sys

from pathlib import Path
from pathlib import Path, PosixPath

import pytest

Expand All @@ -33,9 +33,8 @@ def test_configuration_class(output: Output) -> None:
)
assert app_config.namespace == "testorg"
assert app_config.collection_name == "testcol"
linux_path = Path("/home/ansible")
mac_os_path = Path("/System/Volumes/Data/home/ansible")
assert app_config.init_path in [linux_path, mac_os_path]
home_path = Path.home()
assert app_config.init_path == home_path


@pytest.mark.parametrize(
Expand All @@ -62,7 +61,7 @@ def test_configuration_class(output: Output) -> None:
"ansible-creator",
"init",
"--project=ansible-project",
"--init-path=/home/ansible/my-ansible-project",
f"--init-path={Path.home()}/my-ansible-project",
"--scm-org=weather",
"--scm-project=demo",
],
Expand All @@ -75,7 +74,7 @@ def test_configuration_class(output: Output) -> None:
"json": False,
"verbose": 0,
"collection": None,
"init_path": "/home/ansible/my-ansible-project",
"init_path": f"{Path.home()}/my-ansible-project",
"force": False,
"project": "ansible-project",
"scm_org": "weather",
Expand All @@ -87,7 +86,7 @@ def test_configuration_class(output: Output) -> None:
"ansible-creator",
"init",
"testorg.testcol",
"--init-path=/home/ansible",
f"--init-path={Path.home()}",
"-vvv",
"--json",
"--no-ansi",
Expand All @@ -105,7 +104,7 @@ def test_configuration_class(output: Output) -> None:
"json": True,
"verbose": 3,
"collection": "testorg.testcol",
"init_path": "/home/ansible",
"init_path": f"{Path.home()}",
"force": True,
"project": "collection", # default value
},
Expand All @@ -117,7 +116,7 @@ def test_configuration_class(output: Output) -> None:
"--project=ansible-project",
"--scm-org=weather",
"--scm-project=demo",
"--init-path=/home/ansible/my-ansible-project",
f"--init-path={Path.home()}/my-ansible-project",
"-vvv",
"--json",
"--no-ansi",
Expand All @@ -135,7 +134,7 @@ def test_configuration_class(output: Output) -> None:
"json": True,
"verbose": 3,
"collection": None,
"init_path": "/home/ansible/my-ansible-project",
"init_path": f"{Path.home()}/my-ansible-project",
"force": True,
"project": "ansible-project",
"scm_org": "weather",
Expand Down Expand Up @@ -231,17 +230,18 @@ def test_missing_j2(monkeypatch: pytest.MonkeyPatch) -> None:
ansible_creator.templar.Templar()


def test_cli_init_output(monkeypatch: pytest.MonkeyPatch) -> None:
def test_cli_init_output(monkeypatch: pytest.MonkeyPatch, home_path: PosixPath) -> None:
"""Test CLI init_output method.
Args:
monkeypatch: Pytest monkeypatch fixture.
home_path: Home directory.
"""
sysargs = [
"ansible-creator",
"init",
"testorg.testcol",
"--init-path=/home/ansible",
f"--init-path={home_path}",
"-vvv",
"--json",
"--no-ansi",
Expand Down Expand Up @@ -360,7 +360,7 @@ def test_is_a_tty(monkeypatch: pytest.MonkeyPatch) -> None:
"ansible-creator",
"init",
"testorg.testcol",
"/home/ansible",
f"{Path.home()}",
]

monkeypatch.setattr("sys.argv", sysargs)
Expand All @@ -383,7 +383,7 @@ def test_not_a_tty(monkeypatch: pytest.MonkeyPatch) -> None:
"ansible-creator",
"init",
"testorg.testcol",
"/home/ansible",
f"{Path.home()}",
]

monkeypatch.setattr("sys.argv", sysargs)
Expand Down

0 comments on commit 6e4f0a6

Please sign in to comment.