Skip to content
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
28 changes: 26 additions & 2 deletions dev_tools/check_useless_exclude_paths_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import itertools
import re
import sys
from collections import Counter
from pathlib import Path
Expand All @@ -12,6 +13,7 @@
from ruamel.yaml import YAML

CONFIG_FILE = ".pre-commit-config.yaml"
REGEX_ESCAPE_SEQUENCE = re.compile(r"\\([.^$*+?{}\[\]()|/\\])")


class Hook:
Expand Down Expand Up @@ -62,7 +64,29 @@ def count_excluded_files(self) -> int:


def is_regex_pattern(exclude: str) -> bool:
return any(regex_key in exclude for regex_key in ["*", "$", "^"])
return any(_contains_unescaped_char(exclude, regex_key) for regex_key in ["*", "$", "^"])


def _contains_unescaped_char(text: str, char: str) -> bool:
escaped = False

for current_char in text:
if escaped:
escaped = False
continue

if current_char == "\\":
escaped = True
continue

if current_char == char:
return True

return False


def _unescape_literal_regex_elements(text: str) -> str:
return REGEX_ESCAPE_SEQUENCE.sub(r"\1", text)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This is basically the solution from https://mentaljetsam.wordpress.com/2007/04/13/unescape-a-python-escaped-string/ for unescaping



def extract_literal_exclude_paths(exclude_regex: str) -> list[str]:
Expand All @@ -76,7 +100,7 @@ def extract_literal_exclude_paths(exclude_regex: str) -> list[str]:
.split("|")
)

return [exclude for exclude in exclude_list if not is_regex_pattern(exclude)]
return [_unescape_literal_regex_elements(exclude) for exclude in exclude_list if not is_regex_pattern(exclude)]


def _remove_verbose_regex_comments(exclude: str) -> str:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_check_useless_exclude_paths_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ def test_is_regex_pattern_for_regex_should_be_true(pattern: str) -> None:
assert is_regex_pattern(pattern)


@pytest.mark.parametrize("pattern", [r"foo\$bar", r"foo\^bar", r"foo\*bar"])
def test_is_regex_pattern_for_escaped_regex_characters_should_be_false(pattern: str) -> None:
assert is_regex_pattern(pattern) is False


def test_is_regex_pattern_for_no_regex_should_be_false() -> None:
assert is_regex_pattern("packages/thirdparty/") is False

Expand Down Expand Up @@ -58,6 +63,14 @@ def test_extract_literal_exclude_paths_for_multiple_literals_should_return_paths
]


def test_extract_literal_exclude_paths_should_unescape_literal_regex_elements() -> None:
assert extract_literal_exclude_paths(r"(?x)^(foo\.txt|bar\/baz|foo\$bar)") == [
"foo.txt",
"bar/baz",
"foo$bar",
]


def test_from_hook_config_for_single_path(fs: FakeFilesystem) -> None:
root_directory = Path("Test_directory/")
fs.create_dir(root_directory)
Expand Down