| 1 |
"""Tests sphinx.util.matching functions.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
from sphinx.util.matching import Matcher, compile_matchers, get_matching_files |
| 8 |
|
| 9 |
if TYPE_CHECKING: |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
|
| 13 |
def test_compile_matchers() -> None: |
| 14 |
# exact matching |
| 15 |
pat = compile_matchers(['hello.py']).pop() |
| 16 |
assert pat('hello.py') |
| 17 |
assert not pat('hello-py') |
| 18 |
assert not pat('subdir/hello.py') |
| 19 |
|
| 20 |
# wild card (*) |
| 21 |
pat = compile_matchers(['hello.*']).pop() |
| 22 |
assert pat('hello.py') |
| 23 |
assert pat('hello.rst') |
| 24 |
|
| 25 |
pat = compile_matchers(['*.py']).pop() |
| 26 |
assert pat('hello.py') |
| 27 |
assert pat('world.py') |
| 28 |
assert not pat('subdir/hello.py') |
| 29 |
|
| 30 |
# wild card (**) |
| 31 |
pat = compile_matchers(['hello.**']).pop() |
| 32 |
assert pat('hello.py') |
| 33 |
assert pat('hello.rst') |
| 34 |
assert pat('hello.py/world.py') |
| 35 |
|
| 36 |
pat = compile_matchers(['**.py']).pop() |
| 37 |
assert pat('hello.py') |
| 38 |
assert pat('world.py') |
| 39 |
assert pat('subdir/hello.py') |
| 40 |
|
| 41 |
pat = compile_matchers(['**/hello.py']).pop() |
| 42 |
assert not pat('hello.py') |
| 43 |
assert pat('subdir/hello.py') |
| 44 |
assert pat('subdir/subdir/hello.py') |
| 45 |
|
| 46 |
# wild card (?) |
| 47 |
pat = compile_matchers(['hello.?']).pop() |
| 48 |
assert pat('hello.c') |
| 49 |
assert not pat('hello.py') |
| 50 |
|
| 51 |
# pattern ([...]) |
| 52 |
pat = compile_matchers(['hello[12\\].py']).pop() |
| 53 |
assert pat('hello1.py') |
| 54 |
assert pat('hello2.py') |
| 55 |
assert pat('hello\\.py') |
| 56 |
assert not pat('hello3.py') |
| 57 |
|
| 58 |
pat = compile_matchers(['hello[^12].py']).pop() # "^" is not negative identifier |
| 59 |
assert pat('hello1.py') |
| 60 |
assert pat('hello2.py') |
| 61 |
assert pat('hello^.py') |
| 62 |
assert not pat('hello3.py') |
| 63 |
|
| 64 |
# negative pattern ([!...]) |
| 65 |
pat = compile_matchers(['hello[!12].py']).pop() |
| 66 |
assert not pat('hello1.py') |
| 67 |
assert not pat('hello2.py') |
| 68 |
assert not pat('hello/.py') # negative pattern does not match to "/" |
| 69 |
assert pat('hello3.py') |
| 70 |
|
| 71 |
# non patterns |
| 72 |
pat = compile_matchers(['hello[.py']).pop() |
| 73 |
assert pat('hello[.py') |
| 74 |
assert not pat('hello.py') |
| 75 |
|
| 76 |
pat = compile_matchers(['hello[].py']).pop() |
| 77 |
assert pat('hello[].py') |
| 78 |
assert not pat('hello.py') |
| 79 |
|
| 80 |
pat = compile_matchers(['hello[!].py']).pop() |
| 81 |
assert pat('hello[!].py') |
| 82 |
assert not pat('hello.py') |
| 83 |
|
| 84 |
|
| 85 |
def test_Matcher() -> None: |
| 86 |
matcher = Matcher(['hello.py', '**/world.py']) |
| 87 |
assert matcher('hello.py') |
| 88 |
assert not matcher('subdir/hello.py') |
| 89 |
assert matcher('world.py') |
| 90 |
assert matcher('subdir/world.py') |
| 91 |
|
| 92 |
|
| 93 |
def test_get_matching_files_all(rootdir: Path) -> None: |
| 94 |
files = get_matching_files(rootdir / 'test-root') |
| 95 |
assert sorted(files) == [ |
| 96 |
'Makefile', |
| 97 |
'_templates/contentssb.html', |
| 98 |
'_templates/customsb.html', |
| 99 |
'_templates/layout.html', |
| 100 |
'autodoc.txt', |
| 101 |
'autodoc_target.py', |
| 102 |
'bom.txt', |
| 103 |
'conf.py', |
| 104 |
'extapi.txt', |
| 105 |
'extensions.txt', |
| 106 |
'file_with_special_#_chars.xyz', |
| 107 |
'footnote.txt', |
| 108 |
'images.txt', |
| 109 |
'img.foo.png', |
| 110 |
'img.gif', |
| 111 |
'img.pdf', |
| 112 |
'img.png', |
| 113 |
'includes.txt', |
| 114 |
'index.txt', |
| 115 |
'lists.txt', |
| 116 |
'literal.inc', |
| 117 |
'literal_orig.inc', |
| 118 |
'markup.txt', |
| 119 |
'math.txt', |
| 120 |
'objects.txt', |
| 121 |
'otherext.foo', |
| 122 |
'parsermod.py', |
| 123 |
'quotes.inc', |
| 124 |
'rimg.png', |
| 125 |
'special/api.h', |
| 126 |
'special/code.py', |
| 127 |
'subdir/excluded.txt', |
| 128 |
'subdir/images.txt', |
| 129 |
'subdir/img.png', |
| 130 |
'subdir/include.inc', |
| 131 |
'subdir/includes.txt', |
| 132 |
'subdir/simg.png', |
| 133 |
'svgimg.pdf', |
| 134 |
'svgimg.svg', |
| 135 |
'tabs.inc', |
| 136 |
'test.inc', |
| 137 |
'wrongenc.inc', |
| 138 |
] |
| 139 |
|
| 140 |
|
| 141 |
def test_get_matching_files_all_exclude_single(rootdir: Path) -> None: |
| 142 |
files = get_matching_files(rootdir / 'test-root', exclude_patterns=['**.html']) |
| 143 |
assert sorted(files) == [ |
| 144 |
'Makefile', |
| 145 |
'autodoc.txt', |
| 146 |
'autodoc_target.py', |
| 147 |
'bom.txt', |
| 148 |
'conf.py', |
| 149 |
'extapi.txt', |
| 150 |
'extensions.txt', |
| 151 |
'file_with_special_#_chars.xyz', |
| 152 |
'footnote.txt', |
| 153 |
'images.txt', |
| 154 |
'img.foo.png', |
| 155 |
'img.gif', |
| 156 |
'img.pdf', |
| 157 |
'img.png', |
| 158 |
'includes.txt', |
| 159 |
'index.txt', |
| 160 |
'lists.txt', |
| 161 |
'literal.inc', |
| 162 |
'literal_orig.inc', |
| 163 |
'markup.txt', |
| 164 |
'math.txt', |
| 165 |
'objects.txt', |
| 166 |
'otherext.foo', |
| 167 |
'parsermod.py', |
| 168 |
'quotes.inc', |
| 169 |
'rimg.png', |
| 170 |
'special/api.h', |
| 171 |
'special/code.py', |
| 172 |
'subdir/excluded.txt', |
| 173 |
'subdir/images.txt', |
| 174 |
'subdir/img.png', |
| 175 |
'subdir/include.inc', |
| 176 |
'subdir/includes.txt', |
| 177 |
'subdir/simg.png', |
| 178 |
'svgimg.pdf', |
| 179 |
'svgimg.svg', |
| 180 |
'tabs.inc', |
| 181 |
'test.inc', |
| 182 |
'wrongenc.inc', |
| 183 |
] |
| 184 |
|
| 185 |
|
| 186 |
def test_get_matching_files_all_exclude_multiple(rootdir: Path) -> None: |
| 187 |
files = get_matching_files( |
| 188 |
rootdir / 'test-root', exclude_patterns=['**.html', '**.inc'] |
| 189 |
) |
| 190 |
assert sorted(files) == [ |
| 191 |
'Makefile', |
| 192 |
'autodoc.txt', |
| 193 |
'autodoc_target.py', |
| 194 |
'bom.txt', |
| 195 |
'conf.py', |
| 196 |
'extapi.txt', |
| 197 |
'extensions.txt', |
| 198 |
'file_with_special_#_chars.xyz', |
| 199 |
'footnote.txt', |
| 200 |
'images.txt', |
| 201 |
'img.foo.png', |
| 202 |
'img.gif', |
| 203 |
'img.pdf', |
| 204 |
'img.png', |
| 205 |
'includes.txt', |
| 206 |
'index.txt', |
| 207 |
'lists.txt', |
| 208 |
'markup.txt', |
| 209 |
'math.txt', |
| 210 |
'objects.txt', |
| 211 |
'otherext.foo', |
| 212 |
'parsermod.py', |
| 213 |
'rimg.png', |
| 214 |
'special/api.h', |
| 215 |
'special/code.py', |
| 216 |
'subdir/excluded.txt', |
| 217 |
'subdir/images.txt', |
| 218 |
'subdir/img.png', |
| 219 |
'subdir/includes.txt', |
| 220 |
'subdir/simg.png', |
| 221 |
'svgimg.pdf', |
| 222 |
'svgimg.svg', |
| 223 |
] |
| 224 |
|
| 225 |
|
| 226 |
def test_get_matching_files_all_exclude_nonexistent(rootdir: Path) -> None: |
| 227 |
files = get_matching_files(rootdir / 'test-root', exclude_patterns=['halibut/**']) |
| 228 |
assert sorted(files) == [ |
| 229 |
'Makefile', |
| 230 |
'_templates/contentssb.html', |
| 231 |
'_templates/customsb.html', |
| 232 |
'_templates/layout.html', |
| 233 |
'autodoc.txt', |
| 234 |
'autodoc_target.py', |
| 235 |
'bom.txt', |
| 236 |
'conf.py', |
| 237 |
'extapi.txt', |
| 238 |
'extensions.txt', |
| 239 |
'file_with_special_#_chars.xyz', |
| 240 |
'footnote.txt', |
| 241 |
'images.txt', |
| 242 |
'img.foo.png', |
| 243 |
'img.gif', |
| 244 |
'img.pdf', |
| 245 |
'img.png', |
| 246 |
'includes.txt', |
| 247 |
'index.txt', |
| 248 |
'lists.txt', |
| 249 |
'literal.inc', |
| 250 |
'literal_orig.inc', |
| 251 |
'markup.txt', |
| 252 |
'math.txt', |
| 253 |
'objects.txt', |
| 254 |
'otherext.foo', |
| 255 |
'parsermod.py', |
| 256 |
'quotes.inc', |
| 257 |
'rimg.png', |
| 258 |
'special/api.h', |
| 259 |
'special/code.py', |
| 260 |
'subdir/excluded.txt', |
| 261 |
'subdir/images.txt', |
| 262 |
'subdir/img.png', |
| 263 |
'subdir/include.inc', |
| 264 |
'subdir/includes.txt', |
| 265 |
'subdir/simg.png', |
| 266 |
'svgimg.pdf', |
| 267 |
'svgimg.svg', |
| 268 |
'tabs.inc', |
| 269 |
'test.inc', |
| 270 |
'wrongenc.inc', |
| 271 |
] |
| 272 |
|
| 273 |
|
| 274 |
def test_get_matching_files_all_include_single(rootdir: Path) -> None: |
| 275 |
files = get_matching_files(rootdir / 'test-root', include_patterns=['subdir/**']) |
| 276 |
assert sorted(files) == [ |
| 277 |
'subdir/excluded.txt', |
| 278 |
'subdir/images.txt', |
| 279 |
'subdir/img.png', |
| 280 |
'subdir/include.inc', |
| 281 |
'subdir/includes.txt', |
| 282 |
'subdir/simg.png', |
| 283 |
] |
| 284 |
|
| 285 |
|
| 286 |
def test_get_matching_files_all_include_multiple(rootdir: Path) -> None: |
| 287 |
files = get_matching_files( |
| 288 |
rootdir / 'test-root', include_patterns=['special/**', 'subdir/**'] |
| 289 |
) |
| 290 |
assert sorted(files) == [ |
| 291 |
'special/api.h', |
| 292 |
'special/code.py', |
| 293 |
'subdir/excluded.txt', |
| 294 |
'subdir/images.txt', |
| 295 |
'subdir/img.png', |
| 296 |
'subdir/include.inc', |
| 297 |
'subdir/includes.txt', |
| 298 |
'subdir/simg.png', |
| 299 |
] |
| 300 |
|
| 301 |
|
| 302 |
def test_get_matching_files_all_include_nonexistent(rootdir: Path) -> None: |
| 303 |
files = get_matching_files(rootdir / 'test-root', include_patterns=['halibut/**']) |
| 304 |
assert sorted(files) == [] |
| 305 |
|
| 306 |
|
| 307 |
def test_get_matching_files_all_include_prefix(rootdir: Path) -> None: |
| 308 |
files = get_matching_files(rootdir / 'test-root', include_patterns=['autodoc*']) |
| 309 |
assert sorted(files) == [ |
| 310 |
'autodoc.txt', |
| 311 |
'autodoc_target.py', |
| 312 |
] |
| 313 |
|
| 314 |
|
| 315 |
def test_get_matching_files_all_include_question_mark(rootdir: Path) -> None: |
| 316 |
files = get_matching_files(rootdir / 'test-root', include_patterns=['img.???']) |
| 317 |
assert sorted(files) == [ |
| 318 |
'img.gif', |
| 319 |
'img.pdf', |
| 320 |
'img.png', |
| 321 |
] |