Skip to content

Commit

Permalink
fix(#50): add --no-validate
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Dec 17, 2024
1 parent 61cc2a5 commit 207ad63
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ If a file is not properly formatted, the exit code will be non-zero.

```console
foo@bar:~$ mdformat --help
usage: mdformat [-h] [--check] [--version] [--number] [--wrap {keep,no,INTEGER}]
[--end-of-line {lf,crlf,keep}] [--exclude PATTERN]
[--extensions EXTENSION] [--codeformatters LANGUAGE]
[paths ...]
usage: mdformat [-h] [--check] [--no-validate] [--version] [--number]
[--wrap {keep,no,INTEGER}] [--end-of-line {lf,crlf,keep}]
[--extensions EXTENSION] [--codeformatters LANGUAGE] [paths ...]

CommonMark compliant Markdown formatter

Expand All @@ -106,6 +105,7 @@ positional arguments:
options:
-h, --help show this help message and exit
--check do not apply changes to files
--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
--wrap {keep,no,INTEGER}
Expand Down
21 changes: 15 additions & 6 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@ def run(cli_args: Sequence[str]) -> int: # noqa: C901
getattr(plugin, "CHANGES_AST", False)
for plugin in enabled_parserplugins.values()
)
if not changes_ast and not is_md_equal(
original_str,
formatted_str,
options=opts,
extensions=enabled_parserplugins,
codeformatters=enabled_codeformatters,
if (
not opts["no_validate"]
and not changes_ast
and not is_md_equal(
original_str,
formatted_str,
options=opts,
extensions=enabled_parserplugins,
codeformatters=enabled_codeformatters,
)
):
print_error(
f'Could not format "{path_str}".',
Expand Down Expand Up @@ -198,6 +202,11 @@ def make_arg_parser(
parser.add_argument(
"--check", action="store_true", help="do not apply changes to files"
)
parser.add_argument(
"--no-validate",
action="store_true",
help="do not validate that the rendered HTML is consistent",
)
version_str = f"mdformat {mdformat.__version__}"
plugin_version_str = get_plugin_version_str(
{**parser_extension_dists, **codeformatter_dists}
Expand Down
4 changes: 4 additions & 0 deletions src/mdformat/_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

DEFAULT_OPTS = {
"wrap": "keep",
"no_validate": False,
"number": False,
"end_of_line": "lf",
"exclude": [],
Expand Down Expand Up @@ -59,6 +60,9 @@ def _validate_values(opts: Mapping, conf_path: Path) -> None: # noqa: C901
if "end_of_line" in opts:
if opts["end_of_line"] not in {"crlf", "lf", "keep"}:
raise InvalidConfError(f"Invalid 'end_of_line' value in {conf_path}")
if "no_validate" in opts:
if not isinstance(opts["no_validate"], bool):
raise InvalidConfError(f"Invalid 'no_validate' value in {conf_path}")
if "number" in opts:
if not isinstance(opts["number"], bool):
raise InvalidConfError(f"Invalid 'number' value in {conf_path}")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ def test_eol__check_keep_crlf(tmp_path):
assert run((str(file_path), "--check", "--end-of-line=keep")) == 1


@pytest.mark.skip
def test_no_validate(tmp_path):
# FIXME: Fake an mdformat bug that renders to a different HTML

file_path = tmp_path / "test.md"
content = "2. ordered"
file_path.write_text(content)

assert run((str(file_path),)) == 1
assert file_path.read_text() == content

assert run((str(file_path), "--no-validate")) == 0
assert file_path.read_text() != content


def test_get_plugin_info_str():
info = get_plugin_info_str(
{"mdformat-tables": ("0.1.0", ["tables"])},
Expand Down
1 change: 1 addition & 0 deletions tests/test_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_invalid_toml(tmp_path, capsys):
[
("wrap", "wrap = -3"),
("end_of_line", "end_of_line = 'lol'"),
("no_validate", "no_validate = 1"),
("number", "number = 0"),
("exclude", "exclude = '**'"),
("exclude", "exclude = ['1',3]"),
Expand Down

0 comments on commit 207ad63

Please sign in to comment.