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 | """Golden fixture tests and robustness checks.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from why_failed.matcher import analyze |
| 10 | from why_failed.normalize import normalize |
| 11 | |
| 12 | FIXTURES = Path(__file__).parent / "fixtures" |
| 13 | |
| 14 | GOLDEN = { |
| 15 | "git_not_repository.txt": "git.not_repository", |
| 16 | "npm_eresolve.txt": "npm.eresolve", |
| 17 | "docker_port.txt": "docker.port_allocated", |
| 18 | "redis_refused.txt": "redis.connection_refused", |
| 19 | "mongo_auth.txt": "mongodb.auth_failed", |
| 20 | "python_module.txt": "python.module_not_found", |
| 21 | "typescript_module.txt": "typescript.ts2307", |
| 22 | } |
| 23 | |
| 24 | |
| 25 | @pytest.mark.parametrize("filename,rule_id", list(GOLDEN.items())) |
| 26 | def test_golden_fixture(filename: str, rule_id: str) -> None: |
| 27 | text = (FIXTURES / filename).read_text(encoding="utf-8") |
| 28 | result = analyze(text) |
| 29 | assert result.matched, f"Expected match for {filename}" |
| 30 | assert result.match is not None |
| 31 | assert result.match.rule_id == rule_id |
| 32 | |
| 33 | |
| 34 | ROBUST_CASES = [ |
| 35 | ("huge", "x" * 50_000), |
| 36 | ("binary", "\x00\x01\x02binary\xff"), |
| 37 | ("unicode", "错误🔥" * 100), |
| 38 | ("punct", "!!!!????@@@@####$$$$"), |
| 39 | ("ansi", "\x1b[31m" + "red" * 1000 + "\x1b[0m"), |
| 40 | ("long_lines", "a" * 10_000 + "\n" + "b" * 10_000), |
| 41 | ("empty", ""), |
| 42 | ("newlines", "\n\n\n"), |
| 43 | ("nulls", "null\x00bytes\x00everywhere"), |
| 44 | ] |
| 45 | |
| 46 | |
| 47 | @pytest.mark.parametrize("label,payload", ROBUST_CASES, ids=[c[0] for c in ROBUST_CASES]) |
| 48 | def test_robustness_no_crash(label: str, payload: str) -> None: |
| 49 | del label |
| 50 | normalized = normalize(payload) |
| 51 | assert isinstance(normalized.text, str) |
| 52 | result = analyze(payload) |
| 53 | assert isinstance(result.matched, bool) |