Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exit code 2 to indicate errors #272

Merged
merged 1 commit into from Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -12,6 +12,10 @@ Changelog

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

* Use exit code 2 to indicate errors from Black, whilst exit code 1 remains for “files have been formatted”.

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

* Remove ``language_version`` from ``.pre-commit-hooks.yaml``.
This change allows ``default_language_version`` in ``.pre-commit-config.yaml` to take precedence.

Expand Down
2 changes: 1 addition & 1 deletion src/blacken_docs/__init__.py
Expand Up @@ -243,7 +243,7 @@ def format_file(
lineno = contents[: error.offset].count("\n") + 1
print(f"{filename}:{lineno}: code block parse error {error.exc}")
if errors and not skip_errors:
return 1
return 2
if contents != new_contents:
print(f"{filename}: Rewriting...")
with open(filename, "w", encoding="UTF-8") as f:
Expand Down
56 changes: 44 additions & 12 deletions tests/test_blacken_docs.py
Expand Up @@ -439,7 +439,10 @@ def test_integration_ok(tmp_path, capsys):
f.write_text(
"```python\n" "f(1, 2, 3)\n" "```\n",
)
assert not blacken_docs.main((str(f),))

result = blacken_docs.main((str(f),))

assert result == 0
assert not capsys.readouterr()[1]
assert f.read_text() == ("```python\n" "f(1, 2, 3)\n" "```\n")

Expand All @@ -449,7 +452,10 @@ def test_integration_modifies(tmp_path, capsys):
f.write_text(
"```python\n" "f(1,2,3)\n" "```\n",
)
assert blacken_docs.main((str(f),))

result = blacken_docs.main((str(f),))

assert result == 1
out, _ = capsys.readouterr()
assert out == f"{f}: Rewriting...\n"
assert f.read_text() == ("```python\n" "f(1, 2, 3)\n" "```\n")
Expand All @@ -462,7 +468,10 @@ def test_integration_line_length(tmp_path):
"foo(very_very_very_very_very_very_very, long_long_long_long_long)\n"
"```\n",
)
assert not blacken_docs.main((str(f), "--line-length=80"))

result = blacken_docs.main((str(f), "--line-length=80"))

assert result == 0
assert blacken_docs.main((str(f), "--line-length=50"))
assert f.read_text() == (
"```python\n"
Expand All @@ -486,8 +495,13 @@ def test_integration_py36(tmp_path):
" pass\n"
"```\n",
)
assert not blacken_docs.main((str(f),))
assert blacken_docs.main((str(f), "--target-version=py36"))

result = blacken_docs.main((str(f),))
assert result == 0

result2 = blacken_docs.main((str(f), "--target-version=py36"))

assert result2 == 1
assert f.read_text() == (
"```python\n"
"def very_very_long_function_name(\n"
Expand All @@ -512,8 +526,13 @@ def test_integration_filename_last(tmp_path):
" pass\n"
"```\n",
)
assert not blacken_docs.main((str(f),))
assert blacken_docs.main(("--target-version", "py36", str(f)))

result = blacken_docs.main((str(f),))
assert result == 0

result2 = blacken_docs.main(("--target-version", "py36", str(f)))

assert result2 == 1
assert f.read_text() == (
"```python\n"
"def very_very_long_function_name(\n"
Expand All @@ -538,18 +557,25 @@ def test_integration_multiple_target_version(tmp_path):
" pass\n"
"```\n",
)
assert not blacken_docs.main((str(f),))
assert not blacken_docs.main(

result = blacken_docs.main((str(f),))
assert result == 0

result2 = blacken_docs.main(
("--target-version", "py35", "--target-version", "py36", str(f)),
)
assert result2 == 0


def test_integration_skip_string_normalization(tmp_path):
f = tmp_path / "f.md"
f.write_text(
"```python\n" "f('hi')\n" "```\n",
)
assert not blacken_docs.main((str(f), "--skip-string-normalization"))

result = blacken_docs.main((str(f), "--skip-string-normalization"))

assert result == 0
assert f.read_text() == ("```python\n" "f('hi')\n" "```\n")


Expand All @@ -558,7 +584,10 @@ def test_integration_syntax_error(tmp_path, capsys):
f.write_text(
"```python\n" "f(\n" "```\n",
)
assert blacken_docs.main((str(f),))

result = blacken_docs.main((str(f),))

assert result == 2
out, _ = capsys.readouterr()
assert out.startswith(f"{f}:1: code block parse error")
assert f.read_text() == ("```python\n" "f(\n" "```\n")
Expand All @@ -569,7 +598,10 @@ def test_integration_ignored_syntax_error(tmp_path, capsys):
f.write_text(
"```python\n" "f( )\n" "```\n" "\n" "```python\n" "f(\n" "```\n",
)
assert blacken_docs.main((str(f), "--skip-errors"))

result = blacken_docs.main((str(f), "--skip-errors"))

assert result == 1
out, _ = capsys.readouterr()
assert f.read_text() == (
"```python\n" "f()\n" "```\n" "\n" "```python\n" "f(\n" "```\n"
Expand Down