Skip to content

Commit

Permalink
feat: Generate unified diff using difflib in code_diff function
Browse files Browse the repository at this point in the history
  • Loading branch information
bscriver123 committed Dec 11, 2024
1 parent 3d78ba9 commit 7598bc0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pytest_examples/lint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations as _annotations

import re
import difflib
from subprocess import PIPE, Popen
from textwrap import indent
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -68,7 +69,9 @@ def replace_offset(m: re.Match):


def code_diff(example: CodeExample, after: str, config: ExamplesConfig) -> str:
def replace_at_line(match: re.Match) -> str:
before_lines = sub_space(example.source, config).splitlines(keepends=True)
after_lines = sub_space(after, config).splitlines(keepends=True)
diff = ''.join(difflib.unified_diff(before_lines, after_lines, 'before', 'after'))
offset = re.sub(r'\d+', lambda m: str(int(m.group(0)) + example.start_line), match.group(2))
return f'{match.group(1)}{offset}{match.group(3)}'

Expand Down

0 comments on commit 7598bc0

Please sign in to comment.