|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import difflib |
5 | 6 | import inspect |
6 | 7 | import sys |
7 | 8 | from typing import TYPE_CHECKING, Any, Protocol |
@@ -52,18 +53,116 @@ def _normalize_line_endings(text: str) -> str: |
52 | 53 | return text.replace("\r\n", "\n") |
53 | 54 |
|
54 | 55 |
|
| 56 | +def _get_tox_env() -> str: # pragma: no cover |
| 57 | + """Get the current tox environment name from TOX_ENV_NAME or fallback. |
| 58 | +
|
| 59 | + Strips '-parallel' suffix since inline-snapshot requires -n0 (single process). |
| 60 | + """ |
| 61 | + import os # noqa: PLC0415 |
| 62 | + |
| 63 | + env = os.environ.get("TOX_ENV_NAME", "<version>") |
| 64 | + # Remove -parallel suffix since inline-snapshot needs single process mode |
| 65 | + return env.removesuffix("-parallel") |
| 66 | + |
| 67 | + |
| 68 | +def _format_snapshot_hint(action: str) -> str: # pragma: no cover |
| 69 | + """Format a hint message for inline-snapshot commands with rich formatting.""" |
| 70 | + from io import StringIO # noqa: PLC0415 |
| 71 | + |
| 72 | + from rich.console import Console # noqa: PLC0415 |
| 73 | + from rich.text import Text # noqa: PLC0415 |
| 74 | + |
| 75 | + tox_env = _get_tox_env() |
| 76 | + command = f" tox run -e {tox_env} -- --inline-snapshot={action}" |
| 77 | + |
| 78 | + description = "To update the expected file, run:" if action == "fix" else "To create the expected file, run:" |
| 79 | + |
| 80 | + output = StringIO() |
| 81 | + console = Console(file=output, force_terminal=True, width=200, soft_wrap=False) |
| 82 | + |
| 83 | + console.print(Text(description, style="default")) |
| 84 | + console.print(Text(command, style="bold cyan")) |
| 85 | + |
| 86 | + return output.getvalue() |
| 87 | + |
| 88 | + |
| 89 | +def _format_new_content(content: str) -> str: # pragma: no cover |
| 90 | + """Format new content (for create mode) with green color.""" |
| 91 | + from io import StringIO # noqa: PLC0415 |
| 92 | + |
| 93 | + from rich.console import Console # noqa: PLC0415 |
| 94 | + from rich.text import Text # noqa: PLC0415 |
| 95 | + |
| 96 | + output = StringIO() |
| 97 | + console = Console(file=output, force_terminal=True, width=200, soft_wrap=False) |
| 98 | + |
| 99 | + for line in content.splitlines(): |
| 100 | + console.print(Text(f"+{line}", style="green")) |
| 101 | + |
| 102 | + return output.getvalue() |
| 103 | + |
| 104 | + |
| 105 | +def _format_diff(expected: str, actual: str, expected_path: Path) -> str: # pragma: no cover |
| 106 | + """Format a unified diff between expected and actual content with colors.""" |
| 107 | + from io import StringIO # noqa: PLC0415 |
| 108 | + |
| 109 | + from rich.console import Console # noqa: PLC0415 |
| 110 | + from rich.text import Text # noqa: PLC0415 |
| 111 | + |
| 112 | + expected_lines = expected.splitlines(keepends=True) |
| 113 | + actual_lines = actual.splitlines(keepends=True) |
| 114 | + diff_lines = list( |
| 115 | + difflib.unified_diff( |
| 116 | + expected_lines, |
| 117 | + actual_lines, |
| 118 | + fromfile=str(expected_path), |
| 119 | + tofile="actual", |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + if not diff_lines: |
| 124 | + return "" |
| 125 | + |
| 126 | + output = StringIO() |
| 127 | + console = Console(file=output, force_terminal=True, width=200, soft_wrap=False) |
| 128 | + |
| 129 | + for line in diff_lines: |
| 130 | + line_stripped = line.rstrip("\n") |
| 131 | + # Skip header lines since file path is already in the error message |
| 132 | + if line.startswith(("---", "+++")): |
| 133 | + continue |
| 134 | + if line.startswith("@@"): |
| 135 | + console.print(Text(line_stripped, style="cyan")) |
| 136 | + elif line.startswith("-"): |
| 137 | + console.print(Text(line_stripped, style="red")) |
| 138 | + elif line.startswith("+"): |
| 139 | + console.print(Text(line_stripped, style="green")) |
| 140 | + else: |
| 141 | + # Use default to override pytest's red color for E lines |
| 142 | + console.print(Text(line_stripped, style="default")) |
| 143 | + |
| 144 | + return output.getvalue() |
| 145 | + |
| 146 | + |
55 | 147 | def _assert_with_external_file(content: str, expected_path: Path) -> None: |
56 | 148 | """Assert content matches external file, handling line endings.""" |
57 | 149 | __tracebackhide__ = True |
58 | | - expected = external_file(expected_path) |
| 150 | + try: |
| 151 | + expected = external_file(expected_path) |
| 152 | + except FileNotFoundError: # pragma: no cover |
| 153 | + hint = _format_snapshot_hint("create") |
| 154 | + formatted_content = _format_new_content(content) |
| 155 | + msg = f"Expected file not found: {expected_path}\n{hint}\n{formatted_content}" |
| 156 | + raise AssertionError(msg) from None # pragma: no cover |
59 | 157 | normalized_content = _normalize_line_endings(content) |
60 | 158 | if isinstance(expected, str): # pragma: no branch |
61 | | - if normalized_content != _normalize_line_endings(expected): # pragma: no cover |
62 | | - pytest.fail(f"Content mismatch for {expected_path}\nExpected:\n{expected}\n\nActual:\n{content}") |
| 159 | + normalized_expected = _normalize_line_endings(expected) |
| 160 | + if normalized_content != normalized_expected: # pragma: no cover |
| 161 | + hint = _format_snapshot_hint("fix") |
| 162 | + diff = _format_diff(normalized_expected, normalized_content, expected_path) |
| 163 | + msg = f"Content mismatch for {expected_path}\n{hint}\n{diff}" |
| 164 | + raise AssertionError(msg) from None |
63 | 165 | else: |
64 | | - expected_value = expected._load_value() # pragma: no cover |
65 | | - if _normalize_line_endings(expected_value) == normalized_content: # pragma: no cover |
66 | | - return # pragma: no cover |
67 | 166 | assert expected == normalized_content # pragma: no cover |
68 | 167 |
|
69 | 168 |
|
@@ -171,9 +270,17 @@ def assert_directory_content( |
171 | 270 | assert_directory_content(tmp_path / "model", EXPECTED_PATH / "main_modular") |
172 | 271 | """ |
173 | 272 | __tracebackhide__ = True |
174 | | - for expected_path in expected_dir.rglob(pattern): |
175 | | - relative_path = expected_path.relative_to(expected_dir) |
176 | | - output_path = output_dir / relative_path |
| 273 | + output_files = {p.relative_to(output_dir) for p in output_dir.rglob(pattern)} |
| 274 | + expected_files = {p.relative_to(expected_dir) for p in expected_dir.rglob(pattern)} |
| 275 | + |
| 276 | + # Check for extra expected files (output missing files that are expected) |
| 277 | + extra = expected_files - output_files |
| 278 | + assert not extra, f"Expected files not in output: {extra}" |
| 279 | + |
| 280 | + # Compare all output files (including new ones not yet in expected) |
| 281 | + for output_path in output_dir.rglob(pattern): |
| 282 | + relative_path = output_path.relative_to(output_dir) |
| 283 | + expected_path = expected_dir / relative_path |
177 | 284 | result = output_path.read_text(encoding=encoding) |
178 | 285 | _assert_with_external_file(result, expected_path) |
179 | 286 |
|
|
0 commit comments