From: KotlinIsland <65446343+KotlinIsland@users.noreply.github.com> Date: Thu, 7 Sep 2023 07:11:50 +0000 (+1000) Subject: blackd: fix mishandling of single character input (#3558) X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/8daa64a2e10907539094df51f4c51306bb426f07?hp=df50fee7fd85018f8db462774512a83031f00322 blackd: fix mishandling of single character input (#3558) --- diff --git a/CHANGES.md b/CHANGES.md index 2168c1b..af9fc49 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -45,6 +45,8 @@ +- Fix an issue in `blackd` with single character input (#3558) + ### Integrations diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index 4f2d87d..6b0f3d3 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -152,7 +152,8 @@ async def handle(request: web.Request, executor: Executor) -> web.Response: ) # Preserve CRLF line endings - if req_str[req_str.find("\n") - 1] == "\r": + nl = req_str.find("\n") + if nl > 0 and req_str[nl - 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: diff --git a/tests/test_blackd.py b/tests/test_blackd.py index 325bd7d..dd2126e 100644 --- a/tests/test_blackd.py +++ b/tests/test_blackd.py @@ -240,3 +240,9 @@ class BlackDTestCase(AioHTTPTestCase): # type: ignore[misc] response = await self.client.post("/", data=data) self.assertEqual(await response.text(), expected) self.assertEqual(response.status, 200) + + @unittest_run_loop + async def test_single_character(self) -> None: + response = await self.client.post("/", data="1") + self.assertEqual(await response.text(), "1\n") + self.assertEqual(response.status, 200)