Skip to content

Commit

Permalink
blackd: fix mishandling of single character input (#3558)
Browse files Browse the repository at this point in the history
  • Loading branch information
KotlinIsland committed Sep 7, 2023
1 parent df50fee commit 8daa64a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -45,6 +45,8 @@

<!-- Changes to blackd -->

- Fix an issue in `blackd` with single character input (#3558)

### Integrations

<!-- For example, Docker, GitHub Actions, pre-commit, editors -->
Expand Down
3 changes: 2 additions & 1 deletion src/blackd/__init__.py
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_blackd.py
Expand Up @@ -240,3 +240,9 @@ async def test_normalizes_line_endings(self) -> None:
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)

0 comments on commit 8daa64a

Please sign in to comment.