Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ positional arguments:
options:
-h, --help show this help message and exit
--check do not apply changes to files
--exit-non-zero-on-format
return non-zero if files are modified
--no-validate do not validate that the rendered HTML is consistent
--version show program's version number and exit
--number apply consecutive numbering to ordered lists
Expand Down
7 changes: 7 additions & 0 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def run(cli_args: Sequence[str], cache_toml: bool = True) -> int: # noqa: C901
return 1
if path:
if formatted_str != original_str:
if opts["exit_non_zero_on_format"]:
format_errors_found = True
path.write_bytes(formatted_str.encode())
else:
sys.stdout.buffer.write(formatted_str.encode())
Expand Down Expand Up @@ -210,6 +212,11 @@ def make_arg_parser(
parser.add_argument(
"--check", action="store_true", help="do not apply changes to files"
)
parser.add_argument(
"--exit-non-zero-on-format",
action="store_true",
help="return non-zero if files are modified",
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new option should be mutually exclusive with --check (and maybe better document that both are CLI only because it doesn't make sense to configure these permanently in toml)

parser.add_argument(
"--no-validate",
action="store_const",
Expand Down
20 changes: 20 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def test_format(tmp_path):
assert file_path.read_text() == FORMATTED_MARKDOWN


def test_format__exit_non_zero(tmp_path):
file_path = tmp_path / "test_markdown.md"
file_path.write_text(UNFORMATTED_MARKDOWN)
assert run((str(file_path),), "--exit-non-zero-on-format") == 1
assert file_path.read_text() == FORMATTED_MARKDOWN


def test_format__folder(tmp_path):
file_path_1 = tmp_path / "test_markdown1.md"
file_path_2 = tmp_path / "test_markdown2.md"
Expand All @@ -39,6 +46,19 @@ def test_format__folder(tmp_path):
assert file_path_3.read_text() == UNFORMATTED_MARKDOWN


def test_format__folder_exit_non_zero(tmp_path):
file_path_1 = tmp_path / "test_markdown1.md"
file_path_2 = tmp_path / "test_markdown2.md"
file_path_3 = tmp_path / "not_markdown3"
file_path_1.write_text(UNFORMATTED_MARKDOWN)
file_path_2.write_text(UNFORMATTED_MARKDOWN)
file_path_3.write_text(UNFORMATTED_MARKDOWN)
assert run((str(tmp_path),), "--exit-non-zero-on-format") == 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there might be an error in this test if I understand it correctly:

Suggested change
assert run((str(tmp_path),), "--exit-non-zero-on-format") == 1
assert run((str(tmp_path), "--exit-non-zero-on-format")) == 0

assert file_path_1.read_text() == FORMATTED_MARKDOWN
assert file_path_2.read_text() == FORMATTED_MARKDOWN
assert file_path_3.read_text() == UNFORMATTED_MARKDOWN


def test_format__folder_leads_to_invalid(tmp_path):
file_path_1 = tmp_path / "test_markdown1.md"
file_path_1.mkdir()
Expand Down