Skip to content

Commit 7262132

Browse files
committed
change: show diff when output differs
1 parent 55c355a commit 7262132

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies = [
2323
"pexpect==4.*",
2424
"termcolor==2.*",
2525
"PyYAML>=6",
26+
"rich>=14",
2627
]
2728
dynamic = ["version"]
2829

src/shellinspector/reporter.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
import os
33
import sys
44
from pathlib import Path
5+
import rich
56

67
from termcolor import colored
78

89
from shellinspector.logging import get_logger
910
from shellinspector.runner import RunnerEvent
1011

12+
import difflib
13+
14+
from rich.console import Console
15+
from rich.syntax import Syntax
16+
from rich.padding import Padding
17+
1118
LOGGER = get_logger(Path(__file__).name)
1219

1320

@@ -16,6 +23,7 @@ def __init__(self, only_show_failed_runs=False):
1623
self.has_unfinished_line = False
1724
self.only_show_failed_runs = only_show_failed_runs
1825
self.buffer = []
26+
self.rich_console = Console()
1927

2028
def print_indented(self, prefix, text, color):
2129
if not text:
@@ -81,21 +89,41 @@ def __call__(self, event, cmd, **kwargs):
8189
elif event == RunnerEvent.COMMAND_FAILED:
8290
self.print(colored(f"{prefix} FAIL {line}", "red"))
8391
if "message" in kwargs:
84-
self.print(colored(f' {kwargs["message"]}', "red"))
92+
self.print(colored(f" {kwargs['message']}", "red"))
8593
if "returncode" in kwargs["reasons"]:
8694
rc = kwargs["returncode"]
8795
self.print(colored(" command failed", "red"))
8896
self.print(colored(" expected: 0", "light_grey"))
8997
self.print(colored(f" actual: {rc}", "light_grey"))
9098
self.print_indented(" output:", kwargs["actual"], "white")
9199
if "output" in kwargs["reasons"]:
100+
expected = cmd.get_expected_with_vars(kwargs.get("env", {}))
101+
actual = kwargs["actual"]
102+
92103
self.print(colored(" output did not match", "red"))
93104
self.print_indented(
94-
" expected:",
95-
cmd.get_expected_with_vars(kwargs.get("env", {})),
105+
" expected:",
106+
expected,
96107
"light_grey",
97108
)
98-
self.print_indented(" actual:", kwargs["actual"], "white")
109+
self.print_indented(" actual:", actual, "white")
110+
111+
self.print(colored(" diff:", "white"))
112+
diff = difflib.unified_diff(
113+
expected.splitlines(),
114+
actual.splitlines(),
115+
fromfile="expected",
116+
tofile="actual",
117+
lineterm="",
118+
n=0,
119+
)
120+
diff_syntax = Syntax(
121+
"\n".join(diff),
122+
"diff",
123+
theme="monokai",
124+
background_color="default",
125+
)
126+
self.rich_console.print(Padding(diff_syntax, (0, 0, 0, 4)))
99127
elif event == RunnerEvent.RUN_FAILED:
100128
self._print_buffer()
101129
elif event == RunnerEvent.RUN_SUCCEEDED:

0 commit comments

Comments
 (0)