Return a unified diff string between strings `a` and `b`.
(a: str, b: str, a_name: str, b_name: str)
| 73 | |
| 74 | |
| 75 | def diff(a: str, b: str, a_name: str, b_name: str) -> str: |
| 76 | """Return a unified diff string between strings `a` and `b`.""" |
| 77 | import difflib |
| 78 | |
| 79 | a_lines = _splitlines_no_ff(a) |
| 80 | b_lines = _splitlines_no_ff(b) |
| 81 | diff_lines = [] |
| 82 | for line in difflib.unified_diff( |
| 83 | a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5 |
| 84 | ): |
| 85 | # Work around https://bugs.python.org/issue2142 |
| 86 | # See: |
| 87 | # https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html |
| 88 | if line[-1] == "\n": |
| 89 | diff_lines.append(line) |
| 90 | else: |
| 91 | diff_lines.append(line + "\n") |
| 92 | diff_lines.append("\\ No newline at end of file\n") |
| 93 | return "".join(diff_lines) |
| 94 | |
| 95 | |
| 96 | def color_diff(contents: str) -> str: |