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