fix(cfg): show only the duplicate-key warning, once

This commit is contained in:
imSp4rky
2026-10-22 20:43:40 -06:00
parent 3e9c26bd46
commit 71f293161e
4 changed files with 34 additions and 25 deletions
+19 -10
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import logging
from pathlib import Path
import pytest
@@ -15,19 +16,25 @@ def config_file(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
return path
def test_a_duplicate_key_names_its_lines_but_no_key_or_value(config_file: Path) -> None:
def test_a_duplicate_key_logs_its_lines_once_and_names_no_key_or_value(
config_file: Path, caplog: pytest.LogCaptureFixture
) -> None:
config_file.write_text(
"theme: rainbow\ncredentials:\n EXAMPLE: first-secret\n OTHER: other\n EXAMPLE: second-secret\n"
"serve:\n users:\n key-secret: {tier: a}\n key-secret: {tier: b}\n",
encoding="utf-8",
)
result = CliRunner().invoke(cfg_module.cfg, ["theme"])
assert isinstance(result.exception, SystemExit), result.exception # a ClickException, not a crash
with caplog.at_level(logging.WARNING, logger="config"):
result = CliRunner().invoke(cfg_module.cfg, ["theme"])
CliRunner().invoke(cfg_module.cfg, ["theme"])
assert isinstance(result.exception, SystemExit), result.exception
assert result.exit_code == 1
text = " ".join(result.output.replace("│", " ").split()) # the error panel wraps its lines
assert "unshackle.yaml: duplicate key on lines 3 and 5, lines 8 and 9. Unshackle uses the later one." in text
assert "secret" not in result.output
assert "EXAMPLE" not in result.output
assert caplog.messages == [
f"{config_file}: duplicate key on lines 3 and 5, lines 8 and 9. Unshackle uses the later one."
]
assert "Error" not in result.output and "lines" not in result.output
for output in (result.output, caplog.text):
assert "secret" not in output and "EXAMPLE" not in output
def test_a_duplicate_key_blocks_a_write(config_file: Path) -> None:
@@ -45,7 +52,9 @@ def test_a_valid_config_still_reads_a_key(config_file: Path) -> None:
assert "rainbow" in result.output
def test_a_duplicate_only_ruamel_sees_still_gives_its_line(config_file: Path) -> None:
def test_a_duplicate_only_ruamel_sees_still_gives_its_line(config_file: Path, caplog: pytest.LogCaptureFixture) -> None:
config_file.write_text("a: 1\n1: x\n0x1: y\n", encoding="utf-8")
result = CliRunner().invoke(cfg_module.cfg, ["a"])
assert "unshackle.yaml: duplicate key on line 3." in " ".join(result.output.replace("│", " ").split())
with caplog.at_level(logging.WARNING):
result = CliRunner().invoke(cfg_module.cfg, ["a"])
assert result.exit_code == 1
assert caplog.messages == [f"{config_file}: duplicate key on line 3. Unshackle uses the later one."]