Skip to content

Commit

Permalink
Fix error when black raises NoChanges (#241)
Browse files Browse the repository at this point in the history
Fixes #240
  • Loading branch information
amyreese authored Sep 21, 2024
1 parent 8eb3b0a commit 291005c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ufmt/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def ufmt_bytes(
mode=black_config,
)
except black.report.NothingChanged:
content = result.output
pass
elif ufmt_config.formatter == Formatter.ruff_api:
ruff_format_options = ruff_api.FormatOptions(
target_version=str(
Expand Down
30 changes: 30 additions & 0 deletions ufmt/tests/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ def test_ufmt_bytes_alternate_sorter(
)
ruff_mock.assert_not_called()

result = ufmt.ufmt_bytes(
Path("foo.py"),
CORRECTLY_FORMATTED_CODE.encode(),
ufmt_config=UfmtConfig(sorter=Sorter.skip),
black_config=black_config,
usort_config=usort_config,
)
expected = CORRECTLY_FORMATTED_CODE
self.assertEqual(expected.encode(), result)

with self.subTest("ruff-api"):
usort_mock.reset_mock()
result = ufmt.ufmt_bytes(
Expand All @@ -270,6 +280,16 @@ def test_ufmt_bytes_alternate_sorter(
"foo.py", POORLY_FORMATTED_CODE, options=ANY, root=None
)

result = ufmt.ufmt_bytes(
Path("foo.py"),
CORRECTLY_FORMATTED_CODE.encode(),
ufmt_config=UfmtConfig(sorter=Sorter.skip),
black_config=black_config,
usort_config=usort_config,
)
expected = CORRECTLY_FORMATTED_CODE
self.assertEqual(expected.encode(), result)

with self.subTest("skip"):
usort_mock.reset_mock()
result = ufmt.ufmt_bytes(
Expand All @@ -283,6 +303,16 @@ def test_ufmt_bytes_alternate_sorter(
self.assertEqual(expected.encode(), result)
usort_mock.assert_not_called()

result = ufmt.ufmt_bytes(
Path("foo.py"),
CORRECTLY_FORMATTED_CODE.encode(),
ufmt_config=UfmtConfig(sorter=Sorter.skip),
black_config=black_config,
usort_config=usort_config,
)
expected = CORRECTLY_FORMATTED_CODE
self.assertEqual(expected.encode(), result)

with self.subTest("unsupported sorter"):
with self.assertRaisesRegex(
ValueError, "'garbage' is not a supported sorter"
Expand Down

0 comments on commit 291005c

Please sign in to comment.