From 6f9d5620e0d3afc666059696657204be9ea115a3 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Fri, 13 Mar 2020 19:15:32 +0000 Subject: [PATCH 1/5] Add --show-symbols --- ward/diff.py | 30 +++++++++++++++++++++++++++--- ward/run.py | 11 ++++++++++- ward/terminal.py | 14 ++++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/ward/diff.py b/ward/diff.py index 47fa6cb1..a9232e12 100644 --- a/ward/diff.py +++ b/ward/diff.py @@ -1,11 +1,12 @@ import difflib +from typing import Generator import pprintpp from colorama import Style, Fore from termcolor import colored -def make_diff(lhs, rhs, width=60) -> str: +def make_diff(lhs, rhs, width=80, show_symbols=False) -> str: """Transform input into best format for diffing, then return output diff.""" if isinstance(lhs, str): lhs_repr = lhs @@ -17,6 +18,8 @@ def make_diff(lhs, rhs, width=60) -> str: else: rhs_repr = pprintpp.pformat(rhs, width=width) + if show_symbols: + return build_symbolic_unified_diff(lhs_repr, rhs_repr) return build_unified_diff(lhs_repr, rhs_repr) @@ -28,12 +31,33 @@ def bright_green(s: str) -> str: return f"{Fore.LIGHTGREEN_EX}{s}{Style.RESET_ALL}" -def build_unified_diff(lhs_repr, rhs_repr) -> str: +def raw_unified_diff(lhs_repr: str, rhs_repr: str) -> Generator[str, None, None]: differ = difflib.Differ() lines_lhs = lhs_repr.splitlines() lines_rhs = rhs_repr.splitlines() - diff = differ.compare(lines_lhs, lines_rhs) + return differ.compare(lines_lhs, lines_rhs) + +def build_symbolic_unified_diff(lhs_repr: str, rhs_repr: str) -> str: + diff = raw_unified_diff(lhs_repr, rhs_repr) + output_lines = [] + last_line_colour = "grey" + for line_idx, line in enumerate(diff): + if line.startswith("- "): + last_line_colour = "green" + output_lines.append(colored(line, color=last_line_colour)) + elif line.startswith("+ "): + last_line_colour = "red" + output_lines.append(colored(line, color=last_line_colour)) + elif line.startswith("? "): + output_lines.append(colored(line[:-1], color=last_line_colour)) + else: + output_lines.append(line) + return "\n".join(output_lines) + + +def build_unified_diff(lhs_repr: str, rhs_repr: str) -> str: + diff = raw_unified_diff(lhs_repr, rhs_repr) output_lines = [] prev_marker = "" for line_idx, line in enumerate(diff): diff --git a/ward/run.py b/ward/run.py index 5c77d18d..0d1bff7b 100644 --- a/ward/run.py +++ b/ward/run.py @@ -59,6 +59,11 @@ multiple=True, help="Paths to ignore while searching for tests. Accepts glob patterns.", ) +@click.option( + "--show-symbols/--hide-symbols", + default=False, + help="If enabled, diffs will use symbols such as '?', '-', '+' and '^' instead of colours to highlight differences.", +) @click.option( "--capture-output/--no-capture-output", default=True, @@ -107,6 +112,7 @@ def run( config: str, config_path: Optional[Path], show_slowest: int, + show_symbols: bool, dry_run: bool, ): start_run = default_timer() @@ -125,7 +131,10 @@ def run( test_results = suite.generate_test_runs(order=order, dry_run=dry_run) writer = SimpleTestResultWrite( - suite=suite, test_output_style=test_output_style, config_path=config_path, + suite=suite, + test_output_style=test_output_style, + config_path=config_path, + show_diff_symbols=show_symbols, ) results = writer.output_all_test_results( test_results, time_to_collect=time_to_collect, fail_limit=fail_limit diff --git a/ward/terminal.py b/ward/terminal.py index cd617d55..39715b28 100644 --- a/ward/terminal.py +++ b/ward/terminal.py @@ -224,11 +224,16 @@ class TestResultWriterBase: } def __init__( - self, suite: Suite, test_output_style: str, config_path: Optional[Path] + self, + suite: Suite, + test_output_style: str, + config_path: Optional[Path], + show_diff_symbols: bool = False, ): self.suite = suite self.test_output_style = test_output_style self.config_path = config_path + self.show_diff_symbols = show_diff_symbols self.terminal_size = get_terminal_size() def output_all_test_results( @@ -385,7 +390,12 @@ def print_failure_equals(self, err: TestFailure): f" vs {colored('RHS', color='red')} shown below\n" ) print(indent(diff_msg, INDENT)) - diff = make_diff(err.lhs, err.rhs, width=self.terminal_size.width - 24) + diff = make_diff( + err.lhs, + err.rhs, + width=self.terminal_size.width - 24, + show_symbols=self.show_diff_symbols, + ) print(indent(diff, DOUBLE_INDENT)) def print_traceback(self, err): From 984f7825fab83e5809f021fe2080d22dd9997ca7 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Fri, 13 Mar 2020 20:11:31 +0000 Subject: [PATCH 2/5] Swap output symbols --- ward/diff.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ward/diff.py b/ward/diff.py index a9232e12..a9ec5d7e 100644 --- a/ward/diff.py +++ b/ward/diff.py @@ -45,10 +45,10 @@ def build_symbolic_unified_diff(lhs_repr: str, rhs_repr: str) -> str: for line_idx, line in enumerate(diff): if line.startswith("- "): last_line_colour = "green" - output_lines.append(colored(line, color=last_line_colour)) + output_lines.append(colored(f"+ {line[2:]}", color=last_line_colour)) elif line.startswith("+ "): last_line_colour = "red" - output_lines.append(colored(line, color=last_line_colour)) + output_lines.append(colored(f"- {line[2:]}", color=last_line_colour)) elif line.startswith("? "): output_lines.append(colored(line[:-1], color=last_line_colour)) else: From fc924eeadf9779d4f94d827b103998fd59b8f12c Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Sat, 2 May 2020 16:04:15 +0100 Subject: [PATCH 3/5] Rename show-symbols to show-diff-symbols --- ward/run.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ward/run.py b/ward/run.py index 7cdcba44..852c6891 100644 --- a/ward/run.py +++ b/ward/run.py @@ -1,13 +1,14 @@ import sys from pathlib import Path from timeit import default_timer -from typing import List, Optional, Tuple +from typing import Optional, Tuple import click from click_default_group import DefaultGroup from colorama import init from cucumber_tag_expressions import parse as parse_tags from cucumber_tag_expressions.model import Expression + from ward._ward_version import __version__ from ward.collect import ( get_info_for_modules, @@ -101,7 +102,7 @@ def run(ctx: click.Context): help="Paths to ignore while searching for tests. Accepts glob patterns.", ) @click.option( - "--show-symbols/--hide-symbols", + "--show-diff-symbols/--hide-diff-symbols", default=False, help="If enabled, diffs will use symbols such as '?', '-', '+' and '^' instead of colours to highlight differences.", ) @@ -136,7 +137,7 @@ def test( order: str, capture_output: bool, show_slowest: int, - show_symbols: bool, + show_diff_symbols: bool, dry_run: bool, ): """Run tests.""" @@ -159,7 +160,7 @@ def test( suite=suite, test_output_style=test_output_style, config_path=config_path, - show_diff_symbols=show_symbols, + show_diff_symbols=show_diff_symbols, ) writer.output_header(time_to_collect=time_to_collect) From b7dadcdffb7ed0a5f8fa676a575e7d17c066edd3 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Sat, 2 May 2020 16:26:11 +0100 Subject: [PATCH 4/5] Ensure colours are correct way around --- ward/diff.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ward/diff.py b/ward/diff.py index fe8547a5..f8eb53f4 100644 --- a/ward/diff.py +++ b/ward/diff.py @@ -45,12 +45,17 @@ def build_symbolic_unified_diff(lhs_repr: str, rhs_repr: str) -> str: for line_idx, line in enumerate(diff): if line.startswith("- "): last_line_colour = "green" - output_lines.append(colored(f"- {line[2:]}", color=last_line_colour)) + output_lines.append(colored(f"+ {line[2:]}", color=last_line_colour)) elif line.startswith("+ "): last_line_colour = "red" - output_lines.append(colored(f"+ {line[2:]}", color=last_line_colour)) + output_lines.append(colored(f"- {line[2:]}", color=last_line_colour)) elif line.startswith("? "): - output_lines.append(colored(line[:-1], color=last_line_colour)) + output_line = line[:-1] + if last_line_colour == "red": + output_line = output_line.replace("+", "-") + elif last_line_colour == "green": + output_line = output_line.replace("-", "+") + output_lines.append(colored(output_line, color=last_line_colour)) else: output_lines.append(line) return "\n".join(output_lines) @@ -102,7 +107,7 @@ def build_unified_diff(lhs_repr: str, rhs_repr: str) -> str: current_span = "" current_span += line_to_rewrite[ index - 2 - ] # Subtract 2 to account for code at start of line + ] # Subtract 2 to account for code at start of line prev_char = char index += 1 From befdea17a3f5ff6243a89274c5ec515c5592e1a4 Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Sat, 2 May 2020 16:26:29 +0100 Subject: [PATCH 5/5] Ensure colours are correct way around --- ward/diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ward/diff.py b/ward/diff.py index f8eb53f4..7ee97664 100644 --- a/ward/diff.py +++ b/ward/diff.py @@ -107,7 +107,7 @@ def build_unified_diff(lhs_repr: str, rhs_repr: str) -> str: current_span = "" current_span += line_to_rewrite[ index - 2 - ] # Subtract 2 to account for code at start of line + ] # Subtract 2 to account for code at start of line prev_char = char index += 1