From: RĂ©mi Verschelde Date: Sun, 5 Apr 2020 05:02:57 +0000 (+0200) Subject: Fix --diff output when encountering EOF (#1328) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/959848c17639bfc646128f6b582c5858164a5001?ds=sidebyside Fix --diff output when encountering EOF (#1328) `split("\n")` includes a final empty element `""` if the final line ends with `\n` (as it should for POSIX-compliant text files), which then became an extra `"\n"`. `splitlines()` solves that, but there's a caveat, as it will split on other types of line breaks too (like `\r`), which may not be desired. Fixes #526. --- diff --git a/black.py b/black.py index 31859d1..c9c20df 100644 --- a/black.py +++ b/black.py @@ -3876,8 +3876,8 @@ def diff(a: str, b: str, a_name: str, b_name: str) -> str: """Return a unified diff string between strings `a` and `b`.""" import difflib - a_lines = [line + "\n" for line in a.split("\n")] - b_lines = [line + "\n" for line in b.split("\n")] + a_lines = [line + "\n" for line in a.splitlines()] + b_lines = [line + "\n" for line in b.splitlines()] return "".join( difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5) ) diff --git a/tests/data/blackd_diff.diff b/tests/data/blackd_diff.diff index c1aa52e..6226858 100644 --- a/tests/data/blackd_diff.diff +++ b/tests/data/blackd_diff.diff @@ -1,6 +1,6 @@ --- [Deterministic header] +++ [Deterministic header] -@@ -1,7 +1,6 @@ +@@ -1,6 +1,5 @@ -def abc (): - return ["hello", "world", - "!"] @@ -9,6 +9,5 @@ -print( "Incorrect formatting" -) - ++ +print("Incorrect formatting") -+ \ No newline at end of file diff --git a/tests/data/expression.diff b/tests/data/expression.diff index 629e101..8c9e8a7 100644 --- a/tests/data/expression.diff +++ b/tests/data/expression.diff @@ -160,7 +160,7 @@ slice[0:1:2] slice[:] slice[:-1] -@@ -134,113 +169,171 @@ +@@ -134,112 +169,170 @@ numpy[-(c + 1) :, d] numpy[:, l[-2]] numpy[:, ::-1] @@ -404,4 +404,3 @@ return True last_call() # standalone comment at ENDMARKER -