diff --git a/pyproject.toml b/pyproject.toml index bd3db91..977325b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,13 +1,13 @@ [tool.poetry] name = "jinjaturtle" -version = "0.1.2" +version = "0.1.3" description = "Convert config files into Ansible defaults and Jinja2 templates." authors = ["Miguel Jacq "] license = "GPL-3.0-or-later" readme = "README.md" packages = [{ include = "jinjaturtle", from = "src" }] -keywords = ["ansible", "jinja2", "config", "toml", "ini", "devops"] +keywords = ["ansible", "jinja2", "config", "toml", "ini", "yaml", "json", "devops"] homepage = "https://git.mig5.net/mig5/jinjaturtle" repository = "https://git.mig5.net/mig5/jinjaturtle" diff --git a/src/jinjaturtle/cli.py b/src/jinjaturtle/cli.py index 582a920..9b13502 100644 --- a/src/jinjaturtle/cli.py +++ b/src/jinjaturtle/cli.py @@ -30,7 +30,7 @@ def _build_arg_parser() -> argparse.ArgumentParser: ap.add_argument( "-f", "--format", - choices=["ini", "toml"], + choices=["ini", "json", "toml", "yaml"], help="Force config format instead of auto-detecting from filename.", ) ap.add_argument( diff --git a/src/jinjaturtle/core.py b/src/jinjaturtle/core.py index b2ea3d2..bc5f822 100644 --- a/src/jinjaturtle/core.py +++ b/src/jinjaturtle/core.py @@ -6,11 +6,6 @@ from pathlib import Path from typing import Any, Iterable import yaml -try: - from ruamel.yaml import YAML as RuamelYAML # for comment-preserving YAML -except ImportError: # pragma: no cover - RuamelYAML = None - try: import tomllib # Python 3.11+ except ModuleNotFoundError: # pragma: no cover @@ -26,6 +21,14 @@ class QuotedString(str): pass +def _fallback_str_representer(dumper: yaml.SafeDumper, data: Any): + """ + Fallback for objects the dumper doesn't know about. Represent them as + plain strings. + """ + return dumper.represent_scalar("tag:yaml.org,2002:str", str(data)) + + class _TurtleDumper(yaml.SafeDumper): """Custom YAML dumper that always double-quotes QuotedString values.""" @@ -37,6 +40,8 @@ def _quoted_str_representer(dumper: yaml.SafeDumper, data: QuotedString): _TurtleDumper.add_representer(QuotedString, _quoted_str_representer) +# Use our fallback for any unknown object types +_TurtleDumper.add_representer(None, _fallback_str_representer) def detect_format(path: Path, explicit: str | None = None) -> str: @@ -76,15 +81,7 @@ def parse_config(path: Path, fmt: str | None = None) -> tuple[str, Any]: if fmt == "yaml": text = path.read_text(encoding="utf-8") - if RuamelYAML is not None: - # ruamel.yaml preserves comments; we'll reuse them in template gen - y = RuamelYAML() - y.preserve_quotes = True - data = y.load(text) or {} - else: - # Fallback: PyYAML (drops comments in parsed structure, but we still - # have the original text for comment-preserving template generation). - data = yaml.safe_load(text) or {} + data = yaml.safe_load(text) or {} return fmt, data if fmt == "json": diff --git a/tests/samples/bar.yaml b/tests/samples/bar.yaml new file mode 100644 index 0000000..1b63dbf --- /dev/null +++ b/tests/samples/bar.yaml @@ -0,0 +1,7 @@ +--- +# Top comment +foo: "bar" + +blah: + - something + - else diff --git a/tests/samples/foo.json b/tests/samples/foo.json new file mode 100644 index 0000000..11093b5 --- /dev/null +++ b/tests/samples/foo.json @@ -0,0 +1,11 @@ +{ + "foo": "bar", + "nested": { + "a": 1, + "b": true + }, + "list": [ + 10, + 20 + ] +} diff --git a/tests/test_core.py b/tests/test_core.py index 7056518..7cfee90 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -200,6 +200,10 @@ def test_generate_template_type_and_format_errors(): with pytest.raises(TypeError): generate_template("yaml", parsed=None, role_prefix="role") + # wrong type for JSON + with pytest.raises(TypeError): + generate_template("json", parsed=None, role_prefix="role") + # unsupported format, no original_text with pytest.raises(ValueError): generate_template("bogusfmt", parsed=None, role_prefix="role") @@ -295,18 +299,11 @@ def test_generate_toml_template_from_text_edge_cases(): def test_yaml_roundtrip_with_list_and_comment(tmp_path: Path): - yaml_text = """ - # Top comment - foo: "bar" + yaml_path = SAMPLES_DIR / "bar.yaml" + assert yaml_path.is_file(), f"Missing sample YAML file: {yaml_path}" - blah: - - something - - else - """ - cfg_path = tmp_path / "config.yaml" - cfg_path.write_text(textwrap.dedent(yaml_text), encoding="utf-8") + fmt, parsed = parse_config(yaml_path) - fmt, parsed = parse_config(cfg_path) assert fmt == "yaml" flat_items = flatten_config(fmt, parsed) @@ -319,7 +316,7 @@ def test_yaml_roundtrip_with_list_and_comment(tmp_path: Path): assert defaults["foobar_blah_1"] == "else" # Template generation (preserving comments) - original_text = cfg_path.read_text(encoding="utf-8") + original_text = yaml_path.read_text(encoding="utf-8") template = generate_template(fmt, parsed, "foobar", original_text=original_text) # Comment preserved @@ -337,20 +334,10 @@ def test_yaml_roundtrip_with_list_and_comment(tmp_path: Path): def test_json_roundtrip(tmp_path: Path): - json_text = """ - { - "foo": "bar", - "nested": { - "a": 1, - "b": true - }, - "list": [10, 20] - } - """ - cfg_path = tmp_path / "config.json" - cfg_path.write_text(textwrap.dedent(json_text), encoding="utf-8") + json_path = SAMPLES_DIR / "foo.json" + assert json_path.is_file(), f"Missing sample JSON file: {json_path}" - fmt, parsed = parse_config(cfg_path) + fmt, parsed = parse_config(json_path) assert fmt == "json" flat_items = flatten_config(fmt, parsed) @@ -373,3 +360,85 @@ def test_json_roundtrip(tmp_path: Path): assert "foobar_nested_b" in template assert "foobar_list_0" in template assert "foobar_list_1" in template + + +def test_generate_yaml_template_from_text_edge_cases(): + """ + Exercise YAML text edge cases: + - indentation dedent (stack pop) + - empty key before ':' + - quoted and unquoted list items + """ + text = textwrap.dedent( + """ + root: + child: 1 + other: 2 + : 3 + list: + - "quoted" + - unquoted + """ + ) + + tmpl = core._generate_yaml_template_from_text("role", text) + + # Dedent from "root -> child" back to "other" exercises the stack-pop path. + # Just check the expected variable names appear. + assert "role_root_child" in tmpl + assert "role_other" in tmpl + + # The weird " : 3" line has no key and should be left untouched. + assert " : 3" in tmpl + + # The list should generate indexed variables for each item. + # First item is quoted (use_quotes=True), second is unquoted. + assert "role_list_0" in tmpl + assert "role_list_1" in tmpl + + +def test_generate_template_yaml_structural_fallback(): + """ + When original_text is not provided for YAML, generate_template should use + the structural fallback path (yaml.safe_dump + _generate_yaml_template_from_text). + """ + parsed = {"outer": {"inner": "val"}} + + tmpl = generate_template("yaml", parsed=parsed, role_prefix="role") + + # We don't care about exact formatting, just that the expected variable + # name shows up, proving we went through the structural path. + assert "role_outer_inner" in tmpl + + +def test_generate_template_json_type_error(): + """ + Wrong type for JSON in generate_template should raise TypeError. + """ + with pytest.raises(TypeError): + generate_template("json", parsed="not a dict", role_prefix="role") + + +def test_fallback_str_representer_for_unknown_type(): + """ + Ensure that the _fallback_str_representer is used for objects that + PyYAML doesn't know how to represent. + """ + + class Weird: + def __str__(self) -> str: + return "weird-value" + + data = {"foo": Weird()} + + # This will exercise _fallback_str_representer, because Weird has no + # dedicated representer and _TurtleDumper registers our fallback for None. + dumped = yaml.dump( + data, + Dumper=core._TurtleDumper, + sort_keys=False, + default_flow_style=False, + ) + + # It should serialize without error, and the string form should appear. + assert "weird-value" in dumped