Skip to content

Commit c865fbe

Browse files
committed
add: option to return non-zero if files are changed
1 parent 499b47a commit c865fbe

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ positional arguments:
107107
options:
108108
-h, --help show this help message and exit
109109
--check do not apply changes to files
110+
--exit-non-zero-on-format
111+
return non-zero if files are modified
110112
--no-validate do not validate that the rendered HTML is consistent
111113
--version show program's version number and exit
112114
--number apply consecutive numbering to ordered lists

src/mdformat/_cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def run(cli_args: Sequence[str], cache_toml: bool = True) -> int: # noqa: C901
178178
return 1
179179
if path:
180180
if formatted_str != original_str:
181+
if opts["exit_non_zero_on_format"]:
182+
format_errors_found = True
181183
path.write_bytes(formatted_str.encode())
182184
else:
183185
sys.stdout.buffer.write(formatted_str.encode())
@@ -210,6 +212,11 @@ def make_arg_parser(
210212
parser.add_argument(
211213
"--check", action="store_true", help="do not apply changes to files"
212214
)
215+
parser.add_argument(
216+
"--exit-non-zero-on-format",
217+
action="store_true",
218+
help="return non-zero if files are modified",
219+
)
213220
parser.add_argument(
214221
"--no-validate",
215222
action="store_const",

tests/test_cli.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ def test_format(tmp_path):
2626
assert file_path.read_text() == FORMATTED_MARKDOWN
2727

2828

29+
def test_format__exit_non_zero(tmp_path):
30+
file_path = tmp_path / "test_markdown.md"
31+
file_path.write_text(UNFORMATTED_MARKDOWN)
32+
assert run((str(file_path),), "--exit-non-zero-on-format") == 1
33+
assert file_path.read_text() == FORMATTED_MARKDOWN
34+
35+
2936
def test_format__folder(tmp_path):
3037
file_path_1 = tmp_path / "test_markdown1.md"
3138
file_path_2 = tmp_path / "test_markdown2.md"
@@ -39,6 +46,19 @@ def test_format__folder(tmp_path):
3946
assert file_path_3.read_text() == UNFORMATTED_MARKDOWN
4047

4148

49+
def test_format__folder_exit_non_zero(tmp_path):
50+
file_path_1 = tmp_path / "test_markdown1.md"
51+
file_path_2 = tmp_path / "test_markdown2.md"
52+
file_path_3 = tmp_path / "not_markdown3"
53+
file_path_1.write_text(UNFORMATTED_MARKDOWN)
54+
file_path_2.write_text(UNFORMATTED_MARKDOWN)
55+
file_path_3.write_text(UNFORMATTED_MARKDOWN)
56+
assert run((str(tmp_path),), "--exit-non-zero-on-format") == 1
57+
assert file_path_1.read_text() == FORMATTED_MARKDOWN
58+
assert file_path_2.read_text() == FORMATTED_MARKDOWN
59+
assert file_path_3.read_text() == UNFORMATTED_MARKDOWN
60+
61+
4262
def test_format__folder_leads_to_invalid(tmp_path):
4363
file_path_1 = tmp_path / "test_markdown1.md"
4464
file_path_1.mkdir()

0 commit comments

Comments
 (0)