Skip to content

Commit

Permalink
diff.py(diff): make number of context lines configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
glacambre committed Dec 18, 2023
1 parent abf72fb commit 33c6897
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/e3/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def diff(
item1name: str = "expected",
item2name: str = "output",
ignore_white_chars: bool = True,
context: int = 1,
) -> str:
"""Compute diff between two files or list of strings.
Expand All @@ -48,10 +49,12 @@ def diff(
:param item2name: name to display for b in the diff
:param ignore_white_chars: if True (default) then empty lines,
trailing and leading white chars on each line are ignored
:param context: positive number of context lines to add to the diff
:return: A diff string. If the string is equal to '' it means that there
is no difference
"""
assert context >= 0
contents: list[list[str]] = [[], []]

# Read first item
Expand Down Expand Up @@ -92,7 +95,15 @@ def diff(
line for line in contents[k] if re.search(ignore, line) is None
]

return "".join(unified_diff(contents[0], contents[1], item1name, item2name, n=1))
return "".join(
unified_diff(
contents[0],
contents[1],
fromfile=item1name,
tofile=item2name,
n=context,
)
)


def patch(
Expand Down

0 comments on commit 33c6897

Please sign in to comment.