summaryrefslogtreecommitdiffstats
path: root/tests/test_util/test_util_matching.py
blob: 7d865ba3b7d115eb56efb36b54f6e07da889d63c (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""Tests sphinx.util.matching functions."""
from sphinx.util.matching import Matcher, compile_matchers, get_matching_files


def test_compile_matchers():
    # exact matching
    pat = compile_matchers(['hello.py']).pop()
    assert pat('hello.py')
    assert not pat('hello-py')
    assert not pat('subdir/hello.py')

    # wild card (*)
    pat = compile_matchers(['hello.*']).pop()
    assert pat('hello.py')
    assert pat('hello.rst')

    pat = compile_matchers(['*.py']).pop()
    assert pat('hello.py')
    assert pat('world.py')
    assert not pat('subdir/hello.py')

    # wild card (**)
    pat = compile_matchers(['hello.**']).pop()
    assert pat('hello.py')
    assert pat('hello.rst')
    assert pat('hello.py/world.py')

    pat = compile_matchers(['**.py']).pop()
    assert pat('hello.py')
    assert pat('world.py')
    assert pat('subdir/hello.py')

    pat = compile_matchers(['**/hello.py']).pop()
    assert not pat('hello.py')
    assert pat('subdir/hello.py')
    assert pat('subdir/subdir/hello.py')

    # wild card (?)
    pat = compile_matchers(['hello.?']).pop()
    assert pat('hello.c')
    assert not pat('hello.py')

    # pattern ([...])
    pat = compile_matchers(['hello[12\\].py']).pop()
    assert pat('hello1.py')
    assert pat('hello2.py')
    assert pat('hello\\.py')
    assert not pat('hello3.py')

    pat = compile_matchers(['hello[^12].py']).pop()  # "^" is not negative identifier
    assert pat('hello1.py')
    assert pat('hello2.py')
    assert pat('hello^.py')
    assert not pat('hello3.py')

    # negative pattern ([!...])
    pat = compile_matchers(['hello[!12].py']).pop()
    assert not pat('hello1.py')
    assert not pat('hello2.py')
    assert not pat('hello/.py')  # negative pattern does not match to "/"
    assert pat('hello3.py')

    # non patterns
    pat = compile_matchers(['hello[.py']).pop()
    assert pat('hello[.py')
    assert not pat('hello.py')

    pat = compile_matchers(['hello[].py']).pop()
    assert pat('hello[].py')
    assert not pat('hello.py')

    pat = compile_matchers(['hello[!].py']).pop()
    assert pat('hello[!].py')
    assert not pat('hello.py')


def test_Matcher():
    matcher = Matcher(['hello.py', '**/world.py'])
    assert matcher('hello.py')
    assert not matcher('subdir/hello.py')
    assert matcher('world.py')
    assert matcher('subdir/world.py')


def test_get_matching_files_all(rootdir):
    files = get_matching_files(rootdir / "test-root")
    assert sorted(files) == [
        'Makefile', '_templates/contentssb.html', '_templates/customsb.html',
        '_templates/layout.html', 'autodoc.txt', 'autodoc_target.py', 'bom.txt', 'conf.py',
        'extapi.txt', 'extensions.txt', 'file_with_special_#_chars.xyz', 'footnote.txt',
        'images.txt', 'img.foo.png', 'img.gif', 'img.pdf', 'img.png', 'includes.txt',
        'index.txt', 'lists.txt', 'literal.inc', 'literal_orig.inc', 'markup.txt', 'math.txt',
        'objects.txt', 'otherext.foo', 'parsermod.py', 'quotes.inc', 'rimg.png',
        'special/api.h', 'special/code.py', 'subdir/excluded.txt', 'subdir/images.txt',
        'subdir/img.png', 'subdir/include.inc', 'subdir/includes.txt', 'subdir/simg.png',
        'svgimg.pdf', 'svgimg.svg', 'tabs.inc', 'test.inc', 'wrongenc.inc',
    ]


def test_get_matching_files_all_exclude_single(rootdir):
    files = get_matching_files(rootdir / "test-root", exclude_patterns=["**.html"])
    assert sorted(files) == [
        'Makefile', 'autodoc.txt', 'autodoc_target.py', 'bom.txt', 'conf.py',
        'extapi.txt', 'extensions.txt', 'file_with_special_#_chars.xyz', 'footnote.txt',
        'images.txt', 'img.foo.png', 'img.gif', 'img.pdf', 'img.png', 'includes.txt',
        'index.txt', 'lists.txt', 'literal.inc', 'literal_orig.inc', 'markup.txt', 'math.txt',
        'objects.txt', 'otherext.foo', 'parsermod.py', 'quotes.inc', 'rimg.png',
        'special/api.h', 'special/code.py', 'subdir/excluded.txt', 'subdir/images.txt',
        'subdir/img.png', 'subdir/include.inc', 'subdir/includes.txt', 'subdir/simg.png',
        'svgimg.pdf', 'svgimg.svg', 'tabs.inc', 'test.inc', 'wrongenc.inc',
    ]


def test_get_matching_files_all_exclude_multiple(rootdir):
    files = get_matching_files(rootdir / "test-root", exclude_patterns=["**.html", "**.inc"])
    assert sorted(files) == [
        'Makefile', 'autodoc.txt', 'autodoc_target.py', 'bom.txt', 'conf.py',
        'extapi.txt', 'extensions.txt', 'file_with_special_#_chars.xyz', 'footnote.txt',
        'images.txt', 'img.foo.png', 'img.gif', 'img.pdf', 'img.png', 'includes.txt',
        'index.txt', 'lists.txt', 'markup.txt', 'math.txt', 'objects.txt', 'otherext.foo',
        'parsermod.py', 'rimg.png', 'special/api.h', 'special/code.py', 'subdir/excluded.txt',
        'subdir/images.txt', 'subdir/img.png', 'subdir/includes.txt', 'subdir/simg.png',
        'svgimg.pdf', 'svgimg.svg',
    ]


def test_get_matching_files_all_exclude_nonexistent(rootdir):
    files = get_matching_files(rootdir / "test-root", exclude_patterns=["halibut/**"])
    assert sorted(files) == [
        'Makefile', '_templates/contentssb.html', '_templates/customsb.html',
        '_templates/layout.html', 'autodoc.txt', 'autodoc_target.py', 'bom.txt', 'conf.py',
        'extapi.txt', 'extensions.txt', 'file_with_special_#_chars.xyz', 'footnote.txt',
        'images.txt', 'img.foo.png', 'img.gif', 'img.pdf', 'img.png', 'includes.txt',
        'index.txt', 'lists.txt', 'literal.inc', 'literal_orig.inc', 'markup.txt', 'math.txt',
        'objects.txt', 'otherext.foo', 'parsermod.py', 'quotes.inc', 'rimg.png',
        'special/api.h', 'special/code.py', 'subdir/excluded.txt', 'subdir/images.txt',
        'subdir/img.png', 'subdir/include.inc', 'subdir/includes.txt', 'subdir/simg.png',
        'svgimg.pdf', 'svgimg.svg', 'tabs.inc', 'test.inc', 'wrongenc.inc',
    ]


def test_get_matching_files_all_include_single(rootdir):
    files = get_matching_files(rootdir / "test-root", include_patterns=["subdir/**"])
    assert sorted(files) == [
        'subdir/excluded.txt', 'subdir/images.txt', 'subdir/img.png', 'subdir/include.inc',
        'subdir/includes.txt', 'subdir/simg.png',
    ]


def test_get_matching_files_all_include_multiple(rootdir):
    files = get_matching_files(rootdir / "test-root", include_patterns=["special/**", "subdir/**"])
    assert sorted(files) == [
        'special/api.h', 'special/code.py', 'subdir/excluded.txt', 'subdir/images.txt',
        'subdir/img.png', 'subdir/include.inc', 'subdir/includes.txt', 'subdir/simg.png',
    ]


def test_get_matching_files_all_include_nonexistent(rootdir):
    files = get_matching_files(rootdir / "test-root", include_patterns=["halibut/**"])
    assert sorted(files) == []


def test_get_matching_files_all_include_prefix(rootdir):
    files = get_matching_files(rootdir / "test-root", include_patterns=["autodoc*"])
    assert sorted(files) == [
        'autodoc.txt', 'autodoc_target.py',
    ]


def test_get_matching_files_all_include_question_mark(rootdir):
    files = get_matching_files(rootdir / "test-root", include_patterns=["img.???"])
    assert sorted(files) == [
        'img.gif', 'img.pdf', 'img.png',
    ]