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

Add exclude option to cli and configuration toml #391

Closed
wants to merge 2 commits into from
Closed
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: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
hooks:
- id: absolufy-imports
- repo: https://github.com/PyCQA/isort
rev: c5e8fa75dda5f764d20f66a215d71c21cfa198e1 # frozen: 5.10.1
rev: e44834b7b294701f596c9118d6c370f86671a50d # frozen: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
Expand Down
8 changes: 8 additions & 0 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def run(cli_args: Sequence[str]) -> int: # noqa: C901
return 1
opts: Mapping = {**DEFAULT_OPTS, **toml_opts, **cli_opts}

if _is_excluded(path, opts["exclude"]):
continue

if path:
path_str = str(path)
# Unlike `path.read_text(encoding="utf-8")`, this preserves
Expand Down Expand Up @@ -143,6 +146,7 @@ def make_arg_parser(
else None,
)
parser.add_argument("paths", nargs="*", help="files to format")
parser.add_argument("--exclude", nargs="*", help="files to ignore")
parser.add_argument(
"--check", action="store_true", help="do not apply changes to files"
)
Expand Down Expand Up @@ -219,6 +223,10 @@ def _resolve_path(path: Path) -> Path:
return path


def _is_excluded(file_path: Path, exclude_strings: Iterable[str]) -> bool:
return any(file_path.match(exclude_string) for exclude_string in exclude_strings)


def print_paragraphs(paragraphs: Iterable[str]) -> None:
assert not isinstance(paragraphs, str)
sys.stderr.write(wrap_paragraphs(paragraphs))
Expand Down
4 changes: 4 additions & 0 deletions src/mdformat/_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"wrap": "keep",
"number": False,
"end_of_line": "lf",
"exclude": [],
}


Expand Down Expand Up @@ -58,6 +59,9 @@ def _validate_values(opts: Mapping, conf_path: Path) -> None:
if "number" in opts:
if not isinstance(opts["number"], bool):
raise InvalidConfError(f"Invalid 'number' value in {conf_path}")
if "exclude" in opts:
if not isinstance(opts["exclude"], list):
raise InvalidConfError(f"Invalid 'exclude' value in {conf_path}")


def _validate_keys(opts: Mapping, conf_path: Path) -> None:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ def test_invalid_file(capsys):
assert "does not exist" in captured.err


def test_exclude(tmp_path):
file_path_1 = tmp_path / "test_markdown1.md"
file_path_2 = tmp_path / "test_markdown2.md"
file_path_3 = tmp_path / "test_markdown3.md"
subdir_path = tmp_path / "subdir"
subdir_path.mkdir()
file_path_4 = subdir_path / "test_markdown4.md"
file_path_1.write_text(UNFORMATTED_MARKDOWN)
file_path_2.write_text(UNFORMATTED_MARKDOWN)
file_path_3.write_text(UNFORMATTED_MARKDOWN)
file_path_4.write_text(UNFORMATTED_MARKDOWN)
assert (
run(
[
str(tmp_path),
"--exclude",
"test_markdown3.md",
"*2.md",
"subdir/*",
"abc.md",
]
)
== 0
)
assert file_path_1.read_text() == FORMATTED_MARKDOWN
assert file_path_2.read_text() == UNFORMATTED_MARKDOWN
assert file_path_3.read_text() == UNFORMATTED_MARKDOWN
assert file_path_4.read_text() == UNFORMATTED_MARKDOWN


def test_check(tmp_path):
file_path = tmp_path / "test_markdown.md"
file_path.write_bytes(FORMATTED_MARKDOWN.encode())
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 @@ -65,6 +65,7 @@ def test_invalid_toml(tmp_path, capsys):
("wrap", "wrap = -3"),
("end_of_line", "end_of_line = 'lol'"),
("number", "number = 0"),
("exclude", "exclude = 'lol'"),
],
)
def test_invalid_conf_value(bad_conf, conf_key, tmp_path, capsys):
Expand Down