|
| 1 | +"""Targeted coverage tests for uncovered lines in loader.py and cli.py.""" |
| 2 | +import pytest |
| 3 | +import tempfile |
| 4 | +import yaml |
| 5 | +from configdrift.cli import app |
| 6 | +from configdrift.loader import _strip_inline_comment, load_file |
| 7 | +from pathlib import Path |
| 8 | +from typer.testing import CliRunner |
| 9 | + |
| 10 | +runner = CliRunner() |
| 11 | + |
| 12 | + |
| 13 | +class TestStripInlineCommentToggles: |
| 14 | + """Cover loader.py:72, 74 — quote toggling in _strip_inline_comment.""" |
| 15 | + |
| 16 | + def test_double_quote_toggle_with_hash(self): |
| 17 | + """Line 72: in_double should toggle when encountering " outside single quotes.""" |
| 18 | + # A value with a " inside it, followed by # outside quotes |
| 19 | + # The " should be detected as the start/end of double-quoting |
| 20 | + result = _strip_inline_comment('prefix "hello" # comment') |
| 21 | + assert result == 'prefix "hello"', f"Expected comment stripped after quoted section, got: {result!r}" |
| 22 | + |
| 23 | + def test_single_quote_toggle_with_hash(self): |
| 24 | + """Line 74: in_single should toggle when encountering ' outside double quotes.""" |
| 25 | + result = _strip_inline_comment("prefix 'hello' # comment") |
| 26 | + assert result == "prefix 'hello'", f"Expected comment stripped after quoted section, got: {result!r}" |
| 27 | + |
| 28 | + |
| 29 | +class TestLoadDotenvQuoteStrip: |
| 30 | + """Cover loader.py:99 — quote stripping in _load_dotenv.""" |
| 31 | + |
| 32 | + def test_quote_stripping_double(self): |
| 33 | + """Line 99: surrounding double quotes via _load_dotenv direct.""" |
| 34 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 35 | + p = Path(tmpdir) / "test.env" |
| 36 | + p.write_text('DB_HOST="localhost"\n') |
| 37 | + result = load_file(str(p)) |
| 38 | + assert result == {"DB_HOST": "localhost"} |
| 39 | + |
| 40 | + def test_quote_stripping_single(self): |
| 41 | + """Line 99: surrounding single quotes via _load_dotenv direct.""" |
| 42 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 43 | + p = Path(tmpdir) / "test.env" |
| 44 | + p.write_text("DB_HOST='localhost'\n") |
| 45 | + result = load_file(str(p)) |
| 46 | + assert result == {"DB_HOST": "localhost"} |
| 47 | + |
| 48 | + |
| 49 | +class TestLoaderLine3132: |
| 50 | + """Cover loader.py:31-32 — ValueError fallback when _load_dotenv fails.""" |
| 51 | + |
| 52 | + def test_unknown_ext_invalid_utf8_raises_valueerror(self): |
| 53 | + """Line 31-32: unknown extension with non-UTF-8 content triggers ValueError.""" |
| 54 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 55 | + p = Path(tmpdir) / "config.bin" |
| 56 | + # Binary bytes that can't be decoded as UTF-8 |
| 57 | + p.write_bytes(b'\xff\xfe\x00\x00hello') |
| 58 | + with pytest.raises(ValueError, match="Unsupported file format: .bin"): |
| 59 | + load_file(str(p)) |
| 60 | + |
| 61 | + |
| 62 | +class TestCliLine206: |
| 63 | + """Cover cli.py:206 — has_drift check after JSON output with breaking drift.""" |
| 64 | + |
| 65 | + def test_scan_silent_strict_any_drift(self): |
| 66 | + """Line 206: scan --strict --output silent with non-breaking drift exits 1.""" |
| 67 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 68 | + dev_dir = Path(tmpdir) / "dev" |
| 69 | + prod_dir = Path(tmpdir) / "prod" |
| 70 | + dev_dir.mkdir() |
| 71 | + prod_dir.mkdir() |
| 72 | + # Non-breaking drift (info-level): host change |
| 73 | + (dev_dir / "c.yaml").write_text(yaml.dump({"host": "localhost"})) |
| 74 | + (prod_dir / "c.yaml").write_text(yaml.dump({"host": "prod.example.com"})) |
| 75 | + |
| 76 | + result = runner.invoke(app, ["scan", str(dev_dir), str(prod_dir), "--output", "silent", "--strict"]) |
| 77 | + assert result.exit_code == 1 |
0 commit comments