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 | """CLI integration tests.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | |
| 8 | from typer.testing import CliRunner |
| 9 | |
| 10 | from why_failed import __version__ |
| 11 | from why_failed.cli import app |
| 12 | |
| 13 | runner = CliRunner() |
| 14 | |
| 15 | |
| 16 | def test_cli_git_error() -> None: |
| 17 | result = runner.invoke(app, ["fatal: not a git repository"]) |
| 18 | assert result.exit_code == 0 |
| 19 | assert "git" in result.stdout.lower() or "Not inside" in result.stdout |
| 20 | assert "WHY" in result.stdout |
| 21 | |
| 22 | |
| 23 | def test_cli_stdin() -> None: |
| 24 | result = runner.invoke(app, [], input="ECONNREFUSED 127.0.0.1:6379\n") |
| 25 | assert result.exit_code == 0 |
| 26 | assert "Redis" in result.stdout or "redis" in result.stdout.lower() |
| 27 | |
| 28 | |
| 29 | def test_cli_file(tmp_path: Path) -> None: |
| 30 | path = tmp_path / "error.log" |
| 31 | path.write_text("fatal: not a git repository\n", encoding="utf-8") |
| 32 | result = runner.invoke(app, ["--file", str(path)]) |
| 33 | assert result.exit_code == 0 |
| 34 | assert "WHY" in result.stdout |
| 35 | |
| 36 | |
| 37 | def test_cli_json() -> None: |
| 38 | result = runner.invoke(app, ["--json", "ECONNREFUSED 127.0.0.1:6379"]) |
| 39 | assert result.exit_code == 0 |
| 40 | data = json.loads(result.stdout) |
| 41 | assert data["matched"] is True |
| 42 | assert data["rule_id"] == "redis.connection_refused" |
| 43 | |
| 44 | |
| 45 | def test_cli_quiet() -> None: |
| 46 | result = runner.invoke(app, ["--quiet", "ECONNREFUSED 127.0.0.1:6379"]) |
| 47 | assert result.exit_code == 0 |
| 48 | assert "docker compose up -d redis" in result.stdout |
| 49 | assert "WHY" not in result.stdout |
| 50 | |
| 51 | |
| 52 | def test_cli_unknown() -> None: |
| 53 | result = runner.invoke(app, ["completely unknown random error xyzzy"]) |
| 54 | assert result.exit_code == 2 |
| 55 | assert "No matching rule" in result.stdout |
| 56 | |
| 57 | |
| 58 | def test_cli_unknown_json() -> None: |
| 59 | result = runner.invoke(app, ["--json", "completely unknown random error"]) |
| 60 | assert result.exit_code == 2 |
| 61 | data = json.loads(result.stdout) |
| 62 | assert data["matched"] is False |
| 63 | assert data["rule_id"] is None |
| 64 | |
| 65 | |
| 66 | def test_cli_no_input() -> None: |
| 67 | result = runner.invoke(app, []) |
| 68 | assert result.exit_code == 1 |
| 69 | |
| 70 | |
| 71 | def test_cli_version() -> None: |
| 72 | result = runner.invoke(app, ["--version"]) |
| 73 | assert result.exit_code == 0 |
| 74 | assert __version__ in result.stdout |
| 75 | |
| 76 | |
| 77 | def test_cli_help() -> None: |
| 78 | result = runner.invoke(app, ["--help"]) |
| 79 | assert result.exit_code == 0 |
| 80 | assert "why-failed" in result.stdout.lower() or "Analyze" in result.stdout |
| 81 | |
| 82 | |
| 83 | def test_cli_list_rules() -> None: |
| 84 | result = runner.invoke(app, ["--list-rules"]) |
| 85 | assert result.exit_code == 0 |
| 86 | assert "Available rules:" in result.stdout |
| 87 | assert "git.not_repository" in result.stdout |
| 88 | |
| 89 | |
| 90 | def test_cli_verbose() -> None: |
| 91 | result = runner.invoke(app, ["--verbose", "ECONNREFUSED 127.0.0.1:6379"]) |
| 92 | assert result.exit_code == 0 |
| 93 | assert "Score:" in result.stdout |
| 94 | assert "redis.connection_refused" in result.stdout |
| 95 | |
| 96 | |
| 97 | def test_cli_multiple_args() -> None: |
| 98 | result = runner.invoke( |
| 99 | app, |
| 100 | ["npm", "ERR!", "ERESOLVE", "unable", "to", "resolve", "dependency", "tree"], |
| 101 | ) |
| 102 | assert result.exit_code == 0 |
| 103 | assert "ERESOLVE" in result.stdout or "dependency" in result.stdout.lower() |
| 104 | |
| 105 | |
| 106 | def test_cli_missing_file() -> None: |
| 107 | result = runner.invoke(app, ["--file", "does-not-exist-xyz.log"]) |
| 108 | assert result.exit_code == 1 |