Unverified Commit cd4295dd authored by Paul "TBBle" Hampson's avatar Paul "TBBle" Hampson Committed by GitHub
Browse files

Indicate that a final newline was added in --diff (#1897) (#1897)

Fixes: #1662

Work-around for https://bugs.python.org/issue2142

The test has to slightly mess with its input data, because the utility
functions default to ensuring the test data has a final newline, which
defeats the point of the test.

Signed-off-by: default avatarPaul "TBBle" Hampson <Paul.Hampson@Pobox.com>
parent 0cbe19c8
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGES.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@

- speed up caching by avoiding pathlib (#1950)

- `--diff` correctly indicates when a file doesn't end in a newline (#1662)

#### _Packaging_

- Self-contained native _Black_ binaries are now provided for releases via GitHub
+1 −0
Changes for docs/authors.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -132,6 +132,7 @@ Multiple contributions by:
- [Pablo Galindo](mailto:Pablogsal@gmail.com)
- [Paul Ganssle](mailto:p.ganssle@gmail.com)
- [Paul Meinhardt](mailto:mnhrdt@gmail.com)
- [Paul "TBBle" Hampson](mailto:Paul.Hampson@Pobox.com)
- [Peter Bengtsson](mailto:mail@peterbe.com)
- [Peter Grayson](mailto:pete@jpgrayson.net)
- [Peter Stensmyr](mailto:peter.stensmyr@gmail.com)
+16 −7
Changes for src/black/__init__.py: 16 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -6425,14 +6425,14 @@ def assert_stable(src: str, dst: str, mode: Mode) -> None:


@mypyc_attr(patchable=True)
def dump_to_file(*output: str) -> str:
def dump_to_file(*output: str, ensure_final_newline: bool = True) -> str:
    """Dump `output` to a temporary file. Return path to the file."""
    with tempfile.NamedTemporaryFile(
        mode="w", prefix="blk_", suffix=".log", delete=False, encoding="utf8"
    ) as f:
        for lines in output:
            f.write(lines)
            if lines and lines[-1] != "\n":
            if ensure_final_newline and lines and lines[-1] != "\n":
                f.write("\n")
    return f.name

@@ -6450,11 +6450,20 @@ 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.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)
    )
    a_lines = [line for line in a.splitlines(keepends=True)]
    b_lines = [line for line in b.splitlines(keepends=True)]
    diff_lines = []
    for line in difflib.unified_diff(
        a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5
    ):
        # Work around https://bugs.python.org/issue2142
        # See https://www.gnu.org/software/diffutils/manual/html_node/Incomplete-Lines.html
        if line[-1] == "\n":
            diff_lines.append(line)
        else:
            diff_lines.append(line + "\n")
            diff_lines.append("\\ No newline at end of file\n")
    return "".join(diff_lines)


def cancel(tasks: Iterable["asyncio.Task[Any]"]) -> None:
+8 −0
Changes for tests/data/missing_final_newline.diff: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
--- [Deterministic header]
+++ [Deterministic header]
@@ -1,3 +1,3 @@
 # A comment-only file, with no final EOL character
 # This triggers https://bugs.python.org/issue2142
-# This is the line without the EOL character
\ No newline at end of file
+# This is the line without the EOL character
+3 −0
Changes for tests/data/missing_final_newline.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
# A comment-only file, with no final EOL character
# This triggers https://bugs.python.org/issue2142
# This is the line without the EOL character
 No newline at end of file
Loading