Skip to content

Commit 62c656b

Browse files
committed
wip: polish the globs_to_regex parametrization
1 parent 87d2009 commit 62c656b

File tree

1 file changed

+96
-81
lines changed

1 file changed

+96
-81
lines changed

tests/test_files.py

Lines changed: 96 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,18 @@ def test_flat_rootname(original, flat):
106106
assert flat_rootname(original) == flat
107107

108108

109-
def gen_params(patterns, case_insensitive=False, partial=False, matches=(), nomatches=()):
109+
def globs_to_regex_params(
110+
patterns, case_insensitive=False, partial=False, matches=(), nomatches=(),
111+
):
112+
"""Generate parameters for `test_globs_to_regex`.
113+
114+
`patterns`, `case_insensitive`, and `partial` are arguments for
115+
`globs_to_regex`. `matches` is a list of strings that should match, and
116+
`nomatches` is a list of strings that should not match.
117+
118+
Everything is yielded so that `test_globs_to_regex` can call
119+
`globs_to_regex` once and check one result.
120+
"""
110121
pat_id = "|".join(patterns)
111122
for text in matches:
112123
yield pytest.param(
@@ -121,86 +132,90 @@ def gen_params(patterns, case_insensitive=False, partial=False, matches=(), noma
121132

122133
@pytest.mark.parametrize(
123134
"patterns, case_insensitive, partial, text, result",
124-
list(itertools.chain.from_iterable([
125-
gen_params(
126-
["abc", "xyz"],
127-
matches=["abc", "xyz", "sub/mod/abc"],
128-
nomatches=["ABC", "xYz", "abcx", "xabc", "axyz", "xyza", "sub/mod/abcd", "sub/abc/more"],
129-
),
130-
gen_params(
131-
["abc", "xyz"], case_insensitive=True,
132-
matches=["abc", "xyz", "Abc", "XYZ", "AbC"],
133-
nomatches=["abcx", "xabc", "axyz", "xyza"],
134-
),
135-
gen_params(
136-
["a*c", "x*z"],
137-
matches=["abc", "xyz", "xYz", "azc", "xaz", "axyzc"],
138-
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
139-
),
140-
gen_params(
141-
["a?c", "x?z"],
142-
matches=["abc", "xyz", "xYz", "azc", "xaz"],
143-
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
144-
),
145-
gen_params(
146-
["a??d"],
147-
matches=["abcd", "azcd", "a12d"],
148-
nomatches=["ABCD", "abcx", "axyz", "abcde"],
149-
),
150-
gen_params(
151-
["abc/hi.py"], case_insensitive=True,
152-
matches=["abc/hi.py", "ABC/hi.py", r"ABC\hi.py"],
153-
nomatches=["abc_hi.py", "abc/hi.pyc"],
154-
),
155-
gen_params(
156-
[r"abc\hi.py"], case_insensitive=True,
157-
matches=[r"abc\hi.py", r"ABC\hi.py", "abc/hi.py", "ABC/hi.py"],
158-
nomatches=["abc_hi.py", "abc/hi.pyc"],
159-
),
160-
gen_params(
161-
["abc/*/hi.py"], case_insensitive=True,
162-
matches=["abc/foo/hi.py", r"ABC\foo/hi.py"],
163-
nomatches=["abc/hi.py", "abc/hi.pyc", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
164-
),
165-
gen_params(
166-
["abc/**/hi.py"], case_insensitive=True,
167-
matches=[
168-
"abc/foo/hi.py", r"ABC\foo/hi.py", "abc/hi.py", "ABC/foo/bar/hi.py",
169-
r"ABC\foo/bar/hi.py",
170-
],
171-
nomatches=["abc/hi.pyc"],
172-
),
173-
gen_params(
174-
["abc/[a-f]*/hi.py"], case_insensitive=True,
175-
matches=["abc/foo/hi.py", r"ABC\boo/hi.py"],
176-
nomatches=[
177-
"abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py", r"abc\foo/bar/hi.py",
178-
],
179-
),
180-
gen_params(
181-
["abc/[a-f]/hi.py"], case_insensitive=True,
182-
matches=["abc/f/hi.py", r"ABC\b/hi.py"],
183-
nomatches=[
184-
"abc/foo/hi.py", "abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py",
185-
r"abc\foo/bar/hi.py",
186-
],
187-
),
188-
gen_params(
189-
["abc/"], case_insensitive=True, partial=True,
190-
matches=["abc/foo/hi.py", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
191-
nomatches=["abcd/foo.py", "xabc/hi.py"],
192-
),
193-
gen_params(
194-
["*/foo"], case_insensitive=False, partial=True,
195-
matches=["abc/foo/hi.py", "foo/hi.py"],
196-
nomatches=["abc/xfoo/hi.py"],
197-
),
198-
gen_params(
199-
["**/foo"],
200-
matches=["foo", "hello/foo", "hi/there/foo"],
201-
nomatches=["foob", "hello/foob", "hello/Foo"],
202-
),
203-
])))
135+
list(itertools.chain.from_iterable([
136+
globs_to_regex_params(
137+
["abc", "xyz"],
138+
matches=["abc", "xyz", "sub/mod/abc"],
139+
nomatches=[
140+
"ABC", "xYz", "abcx", "xabc", "axyz", "xyza", "sub/mod/abcd", "sub/abc/more",
141+
],
142+
),
143+
globs_to_regex_params(
144+
["abc", "xyz"], case_insensitive=True,
145+
matches=["abc", "xyz", "Abc", "XYZ", "AbC"],
146+
nomatches=["abcx", "xabc", "axyz", "xyza"],
147+
),
148+
globs_to_regex_params(
149+
["a*c", "x*z"],
150+
matches=["abc", "xyz", "xYz", "azc", "xaz", "axyzc"],
151+
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
152+
),
153+
globs_to_regex_params(
154+
["a?c", "x?z"],
155+
matches=["abc", "xyz", "xYz", "azc", "xaz"],
156+
nomatches=["ABC", "abcx", "xabc", "axyz", "xyza", "a/c"],
157+
),
158+
globs_to_regex_params(
159+
["a??d"],
160+
matches=["abcd", "azcd", "a12d"],
161+
nomatches=["ABCD", "abcx", "axyz", "abcde"],
162+
),
163+
globs_to_regex_params(
164+
["abc/hi.py"], case_insensitive=True,
165+
matches=["abc/hi.py", "ABC/hi.py", r"ABC\hi.py"],
166+
nomatches=["abc_hi.py", "abc/hi.pyc"],
167+
),
168+
globs_to_regex_params(
169+
[r"abc\hi.py"], case_insensitive=True,
170+
matches=[r"abc\hi.py", r"ABC\hi.py", "abc/hi.py", "ABC/hi.py"],
171+
nomatches=["abc_hi.py", "abc/hi.pyc"],
172+
),
173+
globs_to_regex_params(
174+
["abc/*/hi.py"], case_insensitive=True,
175+
matches=["abc/foo/hi.py", r"ABC\foo/hi.py"],
176+
nomatches=["abc/hi.py", "abc/hi.pyc", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
177+
),
178+
globs_to_regex_params(
179+
["abc/**/hi.py"], case_insensitive=True,
180+
matches=[
181+
"abc/foo/hi.py", r"ABC\foo/hi.py", "abc/hi.py", "ABC/foo/bar/hi.py",
182+
r"ABC\foo/bar/hi.py",
183+
],
184+
nomatches=["abc/hi.pyc"],
185+
),
186+
globs_to_regex_params(
187+
["abc/[a-f]*/hi.py"], case_insensitive=True,
188+
matches=["abc/foo/hi.py", r"ABC\boo/hi.py"],
189+
nomatches=[
190+
"abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py",
191+
r"abc\foo/bar/hi.py",
192+
],
193+
),
194+
globs_to_regex_params(
195+
["abc/[a-f]/hi.py"], case_insensitive=True,
196+
matches=["abc/f/hi.py", r"ABC\b/hi.py"],
197+
nomatches=[
198+
"abc/foo/hi.py", "abc/zoo/hi.py", "abc/hi.py", "abc/hi.pyc", "abc/foo/bar/hi.py",
199+
r"abc\foo/bar/hi.py",
200+
],
201+
),
202+
globs_to_regex_params(
203+
["abc/"], case_insensitive=True, partial=True,
204+
matches=["abc/foo/hi.py", "ABC/foo/bar/hi.py", r"ABC\foo/bar/hi.py"],
205+
nomatches=["abcd/foo.py", "xabc/hi.py"],
206+
),
207+
globs_to_regex_params(
208+
["*/foo"], case_insensitive=False, partial=True,
209+
matches=["abc/foo/hi.py", "foo/hi.py"],
210+
nomatches=["abc/xfoo/hi.py"],
211+
),
212+
globs_to_regex_params(
213+
["**/foo"],
214+
matches=["foo", "hello/foo", "hi/there/foo"],
215+
nomatches=["foob", "hello/foob", "hello/Foo"],
216+
),
217+
]))
218+
)
204219
def test_globs_to_regex(patterns, case_insensitive, partial, text, result):
205220
regex = globs_to_regex(patterns, case_insensitive=case_insensitive, partial=partial)
206221
assert bool(regex.match(text)) == result

0 commit comments

Comments
 (0)