Skip to content

Commit 6c7450c

Browse files
authored
Use exit code 2 to indicate errors (#272)
1 parent 99dfc8d commit 6c7450c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Changelog
1212

1313
Thanks to Julianus Pfeuffer for the report in `Issue #217 <https://github.com/adamchainz/blacken-docs/issues/217>`__.
1414

15+
* Use exit code 2 to indicate errors from Black, whilst exit code 1 remains for “files have been formatted”.
16+
17+
Thanks to Julianus Pfeuffer for the report in `Issue #218 <https://github.com/adamchainz/blacken-docs/issues/218>`__.
18+
1519
* Remove ``language_version`` from ``.pre-commit-hooks.yaml``.
1620
This change allows ``default_language_version`` in ``.pre-commit-config.yaml` to take precedence.
1721

src/blacken_docs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def format_file(
243243
lineno = contents[: error.offset].count("\n") + 1
244244
print(f"{filename}:{lineno}: code block parse error {error.exc}")
245245
if errors and not skip_errors:
246-
return 1
246+
return 2
247247
if contents != new_contents:
248248
print(f"{filename}: Rewriting...")
249249
with open(filename, "w", encoding="UTF-8") as f:

tests/test_blacken_docs.py

+44-12
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ def test_integration_ok(tmp_path, capsys):
439439
f.write_text(
440440
"```python\n" "f(1, 2, 3)\n" "```\n",
441441
)
442-
assert not blacken_docs.main((str(f),))
442+
443+
result = blacken_docs.main((str(f),))
444+
445+
assert result == 0
443446
assert not capsys.readouterr()[1]
444447
assert f.read_text() == ("```python\n" "f(1, 2, 3)\n" "```\n")
445448

@@ -449,7 +452,10 @@ def test_integration_modifies(tmp_path, capsys):
449452
f.write_text(
450453
"```python\n" "f(1,2,3)\n" "```\n",
451454
)
452-
assert blacken_docs.main((str(f),))
455+
456+
result = blacken_docs.main((str(f),))
457+
458+
assert result == 1
453459
out, _ = capsys.readouterr()
454460
assert out == f"{f}: Rewriting...\n"
455461
assert f.read_text() == ("```python\n" "f(1, 2, 3)\n" "```\n")
@@ -462,7 +468,10 @@ def test_integration_line_length(tmp_path):
462468
"foo(very_very_very_very_very_very_very, long_long_long_long_long)\n"
463469
"```\n",
464470
)
465-
assert not blacken_docs.main((str(f), "--line-length=80"))
471+
472+
result = blacken_docs.main((str(f), "--line-length=80"))
473+
474+
assert result == 0
466475
assert blacken_docs.main((str(f), "--line-length=50"))
467476
assert f.read_text() == (
468477
"```python\n"
@@ -486,8 +495,13 @@ def test_integration_py36(tmp_path):
486495
" pass\n"
487496
"```\n",
488497
)
489-
assert not blacken_docs.main((str(f),))
490-
assert blacken_docs.main((str(f), "--target-version=py36"))
498+
499+
result = blacken_docs.main((str(f),))
500+
assert result == 0
501+
502+
result2 = blacken_docs.main((str(f), "--target-version=py36"))
503+
504+
assert result2 == 1
491505
assert f.read_text() == (
492506
"```python\n"
493507
"def very_very_long_function_name(\n"
@@ -512,8 +526,13 @@ def test_integration_filename_last(tmp_path):
512526
" pass\n"
513527
"```\n",
514528
)
515-
assert not blacken_docs.main((str(f),))
516-
assert blacken_docs.main(("--target-version", "py36", str(f)))
529+
530+
result = blacken_docs.main((str(f),))
531+
assert result == 0
532+
533+
result2 = blacken_docs.main(("--target-version", "py36", str(f)))
534+
535+
assert result2 == 1
517536
assert f.read_text() == (
518537
"```python\n"
519538
"def very_very_long_function_name(\n"
@@ -538,18 +557,25 @@ def test_integration_multiple_target_version(tmp_path):
538557
" pass\n"
539558
"```\n",
540559
)
541-
assert not blacken_docs.main((str(f),))
542-
assert not blacken_docs.main(
560+
561+
result = blacken_docs.main((str(f),))
562+
assert result == 0
563+
564+
result2 = blacken_docs.main(
543565
("--target-version", "py35", "--target-version", "py36", str(f)),
544566
)
567+
assert result2 == 0
545568

546569

547570
def test_integration_skip_string_normalization(tmp_path):
548571
f = tmp_path / "f.md"
549572
f.write_text(
550573
"```python\n" "f('hi')\n" "```\n",
551574
)
552-
assert not blacken_docs.main((str(f), "--skip-string-normalization"))
575+
576+
result = blacken_docs.main((str(f), "--skip-string-normalization"))
577+
578+
assert result == 0
553579
assert f.read_text() == ("```python\n" "f('hi')\n" "```\n")
554580

555581

@@ -558,7 +584,10 @@ def test_integration_syntax_error(tmp_path, capsys):
558584
f.write_text(
559585
"```python\n" "f(\n" "```\n",
560586
)
561-
assert blacken_docs.main((str(f),))
587+
588+
result = blacken_docs.main((str(f),))
589+
590+
assert result == 2
562591
out, _ = capsys.readouterr()
563592
assert out.startswith(f"{f}:1: code block parse error")
564593
assert f.read_text() == ("```python\n" "f(\n" "```\n")
@@ -569,7 +598,10 @@ def test_integration_ignored_syntax_error(tmp_path, capsys):
569598
f.write_text(
570599
"```python\n" "f( )\n" "```\n" "\n" "```python\n" "f(\n" "```\n",
571600
)
572-
assert blacken_docs.main((str(f), "--skip-errors"))
601+
602+
result = blacken_docs.main((str(f), "--skip-errors"))
603+
604+
assert result == 1
573605
out, _ = capsys.readouterr()
574606
assert f.read_text() == (
575607
"```python\n" "f()\n" "```\n" "\n" "```python\n" "f(\n" "```\n"

0 commit comments

Comments
 (0)