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
8 changes: 8 additions & 0 deletions src/watchdog/utils/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def _get_sep(path: PurePath) -> str:


def _full_match(path: PurePath, pattern: str) -> bool:
# Escape square brackets in the pattern so they match file/directory names
# containing literal brackets rather than being interpreted as character
# classes. For example, the pattern "[test].txt" should match a file
# literally named "[test].txt", not a file like "t.txt" (where [test] is a
# character class matching t, e, s). Replacing "[" with "[[[]" makes the
# opening bracket literal in fnmatch/glob syntax.
# See: https://github.com/gorakhargosh/watchdog/issues/991
pattern = pattern.replace("[", "[[[]")
try:
return path.full_match(pattern)
except AttributeError:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,19 @@ def test_match_any_paths(included_patterns, excluded_patterns, case_sensitive, e
)
== expected
)


@pytest.mark.parametrize(
("raw_path", "included_patterns", "expected"),
[
# Regression tests for https://github.com/gorakhargosh/watchdog/issues/991:
# square brackets in patterns must match literal bracket characters
# instead of being interpreted as fnmatch character classes.
("/users/gorakhargosh/[test].txt", {"**/[test].txt"}, True),
("/users/gorakhargosh/t.txt", {"**/[test].txt"}, False),
("/users/gorakhargosh/file[1].py", {"**/file[1].py"}, True),
("/users/gorakhargosh/file1.py", {"**/file[1].py"}, False),
],
)
def test_match_path_literal_brackets(raw_path, included_patterns, expected):
assert _match_path(raw_path, included_patterns, set(), case_sensitive=True) is expected