]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Preserve crlf line endings in blackd (#3257)
authorKotlinIsland <65446343+KotlinIsland@users.noreply.github.com>
Tue, 4 Oct 2022 20:10:11 +0000 (06:10 +1000)
committerGitHub <noreply@github.com>
Tue, 4 Oct 2022 20:10:11 +0000 (13:10 -0700)
Co-authored-by: KotlinIsland <kotlinisland@users.noreply.github.com>
CHANGES.md
docs/the_black_code_style/current_style.md
src/blackd/__init__.py
tests/test_black.py
tests/test_blackd.py

index d3ba53fd05fe9a1774a9636f706acc0e351fc48b..4ff181674b16dfc072cabaaced06e9ca97273159 100644 (file)
@@ -54,7 +54,7 @@
 
 ### _Blackd_
 
-<!-- Changes to blackd -->
+- Windows style (CRLF) newlines will be preserved (#3257).
 
 ### Integrations
 
index 3db49e2ba0140fd618f257ff775688f342c3e8e2..59d79c4cd0e50038ccaa3cc747d7fa42e482568a 100644 (file)
@@ -406,6 +406,11 @@ file that are not enforced yet but might be in a future version of the formatter
 - use variable annotations instead of type comments, even for stubs that target older
   versions of Python.
 
+### Line endings
+
+_Black_ will normalize line endings (`\n` or `\r\n`) based on the first line ending of
+the file.
+
 ## Pragmatism
 
 Early versions of _Black_ used to be absolutist in some respects. They took after its
index e52a9917cf38a1da2a2cbce68a896f926245f1fe..6bbc7c520866421feefef37befca3e3dd224a11a 100644 (file)
@@ -133,6 +133,13 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
             executor, partial(black.format_file_contents, req_str, fast=fast, mode=mode)
         )
 
+        # Preserve CRLF line endings
+        if req_str[req_str.find("\n") - 1] == "\r":
+            formatted_str = formatted_str.replace("\n", "\r\n")
+            # If, after swapping line endings, nothing changed, then say so
+            if formatted_str == req_str:
+                raise black.NothingChanged
+
         # Only output the diff in the HTTP response
         only_diff = bool(request.headers.get(DIFF_HEADER, False))
         if only_diff:
index abd4d00b8e86ab128dcf16e38a8fcfbd054adfb1..96e6f1e6945d8fa1489b244f03fe3436a1522631 100644 (file)
@@ -1286,6 +1286,17 @@ class BlackTestCase(BlackBaseTestCase):
             if nl == "\n":
                 self.assertNotIn(b"\r\n", output)
 
+    def test_normalize_line_endings(self) -> None:
+        with TemporaryDirectory() as workspace:
+            test_file = Path(workspace) / "test.py"
+            for data, expected in (
+                (b"c\r\nc\n ", b"c\r\nc\r\n"),
+                (b"l\nl\r\n ", b"l\nl\n"),
+            ):
+                test_file.write_bytes(data)
+                ff(test_file, write_back=black.WriteBack.YES)
+                self.assertEqual(test_file.read_bytes(), expected)
+
     def test_assert_equivalent_different_asts(self) -> None:
         with self.assertRaises(AssertionError):
             black.assert_equivalent("{}", "None")
index 511bd86441d81aebc9710f8e489d589df772641c..db9a1652f8cb4a38eacb663fb6d8eb4802a87e3d 100644 (file)
@@ -209,3 +209,20 @@ class BlackDTestCase(AioHTTPTestCase):  # type: ignore[misc]
         response = await self.client.post("/", headers={"Origin": "*"})
         self.assertIsNotNone(response.headers.get("Access-Control-Allow-Origin"))
         self.assertIsNotNone(response.headers.get("Access-Control-Expose-Headers"))
+
+    @unittest_run_loop
+    async def test_preserves_line_endings(self) -> None:
+        for data in (b"c\r\nc\r\n", b"l\nl\n"):
+            # test preserved newlines when reformatted
+            response = await self.client.post("/", data=data + b" ")
+            self.assertEqual(await response.text(), data.decode())
+            # test 204 when no change
+            response = await self.client.post("/", data=data)
+            self.assertEqual(response.status, 204)
+
+    @unittest_run_loop
+    async def test_normalizes_line_endings(self) -> None:
+        for data, expected in ((b"c\r\nc\n", "c\r\nc\r\n"), (b"l\nl\r\n", "l\nl\n")):
+            response = await self.client.post("/", data=data)
+            self.assertEqual(await response.text(), expected)
+            self.assertEqual(response.status, 200)