Files
computer-playbook/tests/unit/cli/create/inventory/test_yaml_io.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

29 lines
941 B
Python

import tempfile
import unittest
from pathlib import Path
import yaml
from cli.create.inventory.yaml_io import load_yaml, dump_yaml
class TestYamlIO(unittest.TestCase):
def test_load_yaml_missing_returns_empty_dict(self):
with tempfile.TemporaryDirectory() as tmpdir:
p = Path(tmpdir) / "missing.yml"
self.assertEqual(load_yaml(p), {})
def test_dump_then_load_roundtrip(self):
with tempfile.TemporaryDirectory() as tmpdir:
p = Path(tmpdir) / "a.yml"
data = {"a": 1, "b": {"c": True}}
dump_yaml(p, data)
self.assertEqual(load_yaml(p), data)
def test_load_yaml_rejects_non_mapping(self):
with tempfile.TemporaryDirectory() as tmpdir:
p = Path(tmpdir) / "bad.yml"
p.write_text(yaml.safe_dump(["not-a-mapping"]), encoding="utf-8")
with self.assertRaises(SystemExit):
load_yaml(p)