From dc446cc560df4224fe60bbefdb905d9a4eaab385 Mon Sep 17 00:00:00 2001 From: DevForge Engineer Date: Mon, 18 May 2026 15:03:07 -0400 Subject: [PATCH] test: add coverage for check with 3+ files and Change.__str__ - Add test_check_three_files covering multi-file auto-labeling path - Add TestChangeStr covering Change.__str__ for added/removed/changed - Remove dead code (env_labels = []) in check() function --- src/configdrift/cli.py | 2 -- tests/test_cli.py | 15 +++++++++++++++ tests/test_diff.py | 27 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/configdrift/cli.py b/src/configdrift/cli.py index 5b52c34..d2c7c5b 100644 --- a/src/configdrift/cli.py +++ b/src/configdrift/cli.py @@ -70,8 +70,6 @@ def check( raise typer.Exit(code=1) env_configs: dict[str, dict[str, Any]] = {} - env_labels = [] - env_labels = [baseline, target] if len(files) == 2 else [f"file_{i + 1}" for i in range(len(files))] for label, filepath in zip(env_labels, files, strict=False): diff --git a/tests/test_cli.py b/tests/test_cli.py index 71fd749..7828bb5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -66,6 +66,21 @@ def test_check_error_file_not_found(self): assert result.exit_code == 1 assert "Error loading" in result.stdout + def test_check_three_files(self): + """Compare three config files with auto-labeled environments.""" + with tempfile.TemporaryDirectory() as tmpdir: + a = Path(tmpdir) / "a.yaml" + b = Path(tmpdir) / "b.yaml" + c = Path(tmpdir) / "c.yaml" + a.write_text(yaml.dump({"host": "localhost", "port": 8080})) + b.write_text(yaml.dump({"host": "staging.example.com", "port": 8080})) + c.write_text(yaml.dump({"host": "prod.example.com", "port": 443})) + + result = runner.invoke(app, ["check", str(a), str(b), str(c)]) + assert result.exit_code == 0 + assert "file_1" in result.stdout or "Config Drift" in result.stdout + assert "prod.example.com" in result.stdout + def test_check_less_than_two_files(self): """Only one file should error.""" with tempfile.TemporaryDirectory() as tmpdir: diff --git a/tests/test_diff.py b/tests/test_diff.py index a5a096c..40689d4 100644 --- a/tests/test_diff.py +++ b/tests/test_diff.py @@ -10,6 +10,33 @@ ) +class TestChangeStr: + def test_str_added(self): + """Change.__str__ for added key.""" + c = Change(key="port", change_type=ChangeType.ADDED, new_value=8080, env="prod") + s = str(c) + assert "[+]" in s + assert "port" in s + assert "8080" in s + assert "prod" in s + + def test_str_removed(self): + """Change.__str__ for removed key.""" + c = Change(key="debug", change_type=ChangeType.REMOVED, old_value=True, env="dev") + s = str(c) + assert "[-]" in s + assert "debug" in s + assert "True" in s or "true" in s + + def test_str_changed(self): + """Change.__str__ for changed key.""" + c = Change(key="host", change_type=ChangeType.CHANGED, old_value="localhost", new_value="prod.example.com", env="prod") + s = str(c) + assert "[~]" in s + assert "localhost" in s + assert "prod.example.com" in s + + class TestDiffConfigs: def test_no_changes(self): base = {"host": "localhost", "port": 8080}