Files
computer-playbook/tests/unit/cli/create/inventory/test_role_resolver.py
T
kevinveenbirkenbach a354992f27 refactor(create-inventory): modularize CLI, stabilize path handling, add unit tests
- Split create-inventory logic into focused modules (cli, project, filters, host_vars, credentials, etc.)
- Replace fragile path construction with python -m module execution
- Add robust project root detection
- Centralize password generation
- Improve test coverage with per-module unit tests
- Preserve existing inventory and host_vars behavior

https://chatgpt.com/share/69471157-79c0-800f-8dec-25c0f8e38f03
2025-12-20 22:12:44 +01:00

72 lines
2.3 KiB
Python

import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from cli.create.inventory.role_resolver import resolve_role_path
class _Result:
def __init__(self, stdout: str):
self.stdout = stdout
class TestRoleResolver(unittest.TestCase):
def test_resolve_role_path_prefers_roles_dir(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
roles_dir = tmp / "roles"
roles_dir.mkdir()
(roles_dir / "web-app-nextcloud").mkdir()
with patch(
"cli.create.inventory.role_resolver.run_subprocess",
return_value=_Result("web-app-nextcloud\n"),
) as rs:
p = resolve_role_path(
application_id="nextcloud",
roles_dir=roles_dir,
project_root=tmp,
env={"PYTHONPATH": "x"},
)
self.assertEqual(p, roles_dir / "web-app-nextcloud")
cmd = rs.call_args[0][0]
self.assertIn("-m", cmd)
self.assertIn("cli.meta.applications.role_name", cmd)
def test_resolve_role_path_falls_back_to_project_root(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
roles_dir = tmp / "roles"
roles_dir.mkdir()
(tmp / "roles" / "web-app-matomo").mkdir(parents=True)
with patch(
"cli.create.inventory.role_resolver.run_subprocess",
return_value=_Result("roles/web-app-matomo\n"),
):
p = resolve_role_path(
application_id="matomo",
roles_dir=roles_dir,
project_root=tmp,
env=None,
)
self.assertEqual(p, tmp / "roles" / "web-app-matomo")
def test_resolve_role_path_returns_none_on_empty_output(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmp = Path(tmpdir)
roles_dir = tmp / "roles"
roles_dir.mkdir()
with patch(
"cli.create.inventory.role_resolver.run_subprocess",
return_value=_Result("\n"),
):
p = resolve_role_path("x", roles_dir, tmp, env=None)
self.assertIsNone(p)