Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #147 from darrenburns/show-diff-symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns authored May 2, 2020
2 parents 1bda173 + befdea1 commit f5a2ac0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions ward/diff.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)


Expand All @@ -28,12 +31,38 @@ 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(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))
elif line.startswith("? "):
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)


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):
Expand Down
20 changes: 18 additions & 2 deletions ward/run.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -94,6 +95,17 @@ def run(ctx: click.Context):
default="standard",
help="Specify the order in which tests should run.",
)
@click.option(
"--exclude",
type=click.STRING,
multiple=True,
help="Paths to ignore while searching for tests. Accepts glob patterns.",
)
@click.option(
"--show-diff-symbols/--hide-diff-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,
Expand Down Expand Up @@ -125,6 +137,7 @@ def test(
order: str,
capture_output: bool,
show_slowest: int,
show_diff_symbols: bool,
dry_run: bool,
):
"""Run tests."""
Expand All @@ -144,7 +157,10 @@ def test(
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_diff_symbols,
)
writer.output_header(time_to_collect=time_to_collect)

Expand Down
14 changes: 12 additions & 2 deletions ward/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,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_header(self, time_to_collect):
Expand Down Expand Up @@ -394,7 +399,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):
Expand Down

0 comments on commit f5a2ac0

Please sign in to comment.