Developer tools dump stack traces, exit codes, and jargon. Figuring out *what to do next* often means searching the same Stack Overflow answers again.
| 1 | """Extra coverage for CLI edge cases, providers, and formatting.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | from typing import get_type_hints |
| 8 | |
| 9 | import pytest |
| 10 | from typer.testing import CliRunner |
| 11 | |
| 12 | from why_failed.cli import app, main |
| 13 | from why_failed.formatter import ( |
| 14 | format_match_rich, |
| 15 | format_no_match_rich, |
| 16 | format_quiet, |
| 17 | format_rules_list, |
| 18 | ) |
| 19 | from why_failed.matcher import analyze |
| 20 | from why_failed.models import ConfidenceLevel, Pattern |
| 21 | from why_failed.providers import ErrorProvider |
| 22 | from why_failed.providers.base import ErrorProvider as BaseProvider |
| 23 | from why_failed.stdin import InputError, read_file, read_stdin |
| 24 | |
| 25 | runner = CliRunner() |
| 26 | |
| 27 | |
| 28 | def test_provider_protocol_export() -> None: |
| 29 | assert ErrorProvider is BaseProvider |
| 30 | assert get_type_hints(BaseProvider.analyze) |
| 31 | |
| 32 | |
| 33 | def test_confidence_level_enum() -> None: |
| 34 | assert ConfidenceLevel.HIGH.value == "high" |
| 35 | |
| 36 | |
| 37 | def test_pattern_custom_weight() -> None: |
| 38 | p = Pattern("x", kind="exact", weight=9) |
| 39 | assert p.score_value() == 9 |
| 40 | |
| 41 | |
| 42 | def test_cli_force_stdin_empty() -> None: |
| 43 | result = runner.invoke(app, ["--stdin"], input="") |
| 44 | assert result.exit_code == 1 |
| 45 | |
| 46 | |
| 47 | def test_cli_json_input_error() -> None: |
| 48 | result = runner.invoke(app, ["--json", "--file", "missing-xyz.log"]) |
| 49 | assert result.exit_code == 1 |
| 50 | data = json.loads(result.stdout) |
| 51 | assert data["matched"] is False |
| 52 | |
| 53 | |
| 54 | def test_cli_verbose_no_match() -> None: |
| 55 | result = runner.invoke(app, ["--verbose", "zzzz totally unknown error 999"]) |
| 56 | assert result.exit_code == 2 |
| 57 | assert "No matching rule" in result.stdout |
| 58 | |
| 59 | |
| 60 | def test_format_quiet_no_match() -> None: |
| 61 | format_quiet(None) |
| 62 | |
| 63 | |
| 64 | def test_format_rich_single_and_multi_fix() -> None: |
| 65 | result = analyze("ECONNREFUSED 127.0.0.1:6379") |
| 66 | assert result.match is not None |
| 67 | format_match_rich(result.match, result, verbose=True) |
| 68 | format_no_match_rich(analyze("unknown"), verbose=True) |
| 69 | |
| 70 | |
| 71 | def test_format_rules_list_runs() -> None: |
| 72 | format_rules_list() |
| 73 | |
| 74 | |
| 75 | def test_main_entrypoint_callable() -> None: |
| 76 | assert callable(main) |
| 77 | |
| 78 | |
| 79 | def test_read_stdin_not_piped(monkeypatch: pytest.MonkeyPatch) -> None: |
| 80 | monkeypatch.setattr("why_failed.stdin.stdin_is_piped", lambda: False) |
| 81 | with pytest.raises(InputError): |
| 82 | read_stdin(force=False) |
| 83 | |
| 84 | |
| 85 | def test_read_file_oserror(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 86 | path = tmp_path / "x.txt" |
| 87 | path.write_text("hi", encoding="utf-8") |
| 88 | |
| 89 | def boom(*_a: object, **_k: object) -> str: |
| 90 | raise OSError("nope") |
| 91 | |
| 92 | monkeypatch.setattr(Path, "read_text", boom) |
| 93 | with pytest.raises(InputError, match="Failed to read file"): |
| 94 | read_file(path) |
| 95 | |
| 96 | |
| 97 | def test_stdin_isatty_oserror(monkeypatch: pytest.MonkeyPatch) -> None: |
| 98 | from why_failed import stdin as stdin_mod |
| 99 | |
| 100 | class Bad: |
| 101 | def isatty(self) -> bool: |
| 102 | raise OSError("bad") |
| 103 | |
| 104 | monkeypatch.setattr(stdin_mod.sys, "stdin", Bad()) |
| 105 | assert stdin_mod.stdin_is_piped() is False |
| 106 | |
| 107 | |
| 108 | def test_read_stdin_oserror(monkeypatch: pytest.MonkeyPatch) -> None: |
| 109 | from why_failed import stdin as stdin_mod |
| 110 | |
| 111 | class Bad: |
| 112 | def read(self) -> str: |
| 113 | raise OSError("boom") |
| 114 | |
| 115 | monkeypatch.setattr(stdin_mod.sys, "stdin", Bad()) |
| 116 | with pytest.raises(InputError, match="Failed to read stdin"): |
| 117 | stdin_mod.read_stdin(force=True) |
| 118 | |
| 119 | |
| 120 | def test_single_fix_formatting() -> None: |
| 121 | result = analyze("fatal: not a git repository") |
| 122 | assert result.match is not None |
| 123 | assert len(result.match.fixes) == 1 |
| 124 | format_match_rich(result.match, result, verbose=False) |