Skip to content

Commit 545d2d7

Browse files
committed
Handle non-unicode files being passed as arguments.
1 parent 3420384 commit 545d2d7

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

formate/__main__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ def main(
144144

145145
continue
146146

147-
r = Reformatter(path, config=config)
147+
try:
148+
r = Reformatter(path, config=config)
149+
except UnicodeDecodeError as e:
150+
if verbose >= 2:
151+
click.echo(f"Skipping {path} due to incorrect encoding: {e}")
152+
153+
continue
148154

149155
with handle_tracebacks(show_traceback, cls=SyntaxTracebackHandler):
150156
with syntaxerror_for_file(path):

tests/image.png

2.02 KB
Loading

tests/test_integration.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# stdlib
22
import re
3+
import shutil
34
from typing import List, Mapping, Union, no_type_check
45

56
# 3rd party
@@ -307,6 +308,27 @@ def test_cli_verbose_verbose_no_supported_hooks(
307308
check_out(result, advanced_data_regression)
308309

309310

311+
@pytest.mark.usefixtures("demo_environment")
312+
def test_cli_verbose_verbose_unicode_error(
313+
tmp_pathplus: PathPlus,
314+
advanced_data_regression: AdvancedDataRegressionFixture,
315+
):
316+
317+
result: Result
318+
shutil.copy2(PathPlus(__file__).parent / "image.png", tmp_pathplus / "image.png")
319+
320+
with in_directory(tmp_pathplus):
321+
runner = CliRunner(mix_stderr=False)
322+
result = runner.invoke(
323+
main,
324+
args=["code.py", "image.png", "--no-colour", "--diff", "--verbose", "-v"],
325+
)
326+
327+
assert result.exit_code == 1
328+
329+
check_out(result, advanced_data_regression)
330+
331+
310332
@pytest.mark.usefixtures("demo_environment")
311333
@max_version("3.9.9", reason="Output differs on Python 3.10+")
312334
@not_pypy("Output differs on PyPy")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
err:
2+
- ''
3+
out:
4+
- Reformatting code.py
5+
- "--- code.py\t(original)"
6+
- "+++ code.py\t(reformatted)"
7+
- '@@ -1,12 +1,12 @@'
8+
- ' class F:'
9+
- "-\tfrom collections import ("
10+
- -Iterable,
11+
- "-\tCounter,"
12+
- "-\t\t)"
13+
- "+\t# stdlib"
14+
- "+\tfrom collections import Counter"
15+
- "+\tfrom collections.abc import Iterable"
16+
- ''
17+
- " \tdef foo(self):"
18+
- " \t\tpass"
19+
- ''
20+
- -print('hello world')
21+
- -assert t.uname == '\udce4\udcf6\udcfc'
22+
- ''
23+
- +print("hello world")
24+
- +assert t.uname == "\udce4\udcf6\udcfc"
25+
- +
26+
- ''
27+
- 'Skipping image.png due to incorrect encoding: ''utf-8'' codec can''t decode byte
28+
0x89 in position 0: invalid start byte'
29+
- ''

0 commit comments

Comments
 (0)