1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
from __future__ import annotations
import pytest
from watchdog.utils.patterns import _match_path, filter_paths, match_any_paths
@pytest.mark.parametrize(
("raw_path", "included_patterns", "excluded_patterns", "case_sensitive", "expected"),
[
("/users/gorakhargosh/foobar.py", {"*.py"}, {"*.PY"}, True, True),
("/users/gorakhargosh/", {"*.py"}, {"*.txt"}, False, False),
("/users/gorakhargosh/foobar.py", {"*.py"}, {"*.PY"}, False, ValueError),
],
)
def test_match_path(raw_path, included_patterns, excluded_patterns, case_sensitive, expected):
if expected is ValueError:
with pytest.raises(expected):
_match_path(raw_path, included_patterns, excluded_patterns, case_sensitive=case_sensitive)
else:
assert _match_path(raw_path, included_patterns, excluded_patterns, case_sensitive=case_sensitive) is expected
@pytest.mark.parametrize(
("included_patterns", "excluded_patterns", "case_sensitive", "expected"),
[
(None, None, True, None),
(None, None, False, None),
(
["*.py", "*.conf"],
["*.status"],
True,
{"/users/gorakhargosh/foobar.py", "/etc/pdnsd.conf"},
),
],
)
def test_filter_paths(included_patterns, excluded_patterns, case_sensitive, expected):
pathnames = {
"/users/gorakhargosh/foobar.py",
"/var/cache/pdnsd.status",
"/etc/pdnsd.conf",
"/usr/local/bin/python",
}
actual = set(
filter_paths(
pathnames,
included_patterns=included_patterns,
excluded_patterns=excluded_patterns,
case_sensitive=case_sensitive,
)
)
assert actual == expected if expected else pathnames
@pytest.mark.parametrize(
("included_patterns", "excluded_patterns", "case_sensitive", "expected"),
[
(None, None, True, True),
(None, None, False, True),
(["*py", "*.conf"], ["*.status"], True, True),
(["*.txt"], None, False, False),
(["*.txt"], None, True, False),
],
)
def test_match_any_paths(included_patterns, excluded_patterns, case_sensitive, expected):
pathnames = {
"/users/gorakhargosh/foobar.py",
"/var/cache/pdnsd.status",
"/etc/pdnsd.conf",
"/usr/local/bin/python",
}
assert (
match_any_paths(
pathnames,
included_patterns=included_patterns,
excluded_patterns=excluded_patterns,
case_sensitive=case_sensitive,
)
== expected
)
|