Skip to content

Commit cc63b29

Browse files
committed
Simplify and fix test_importlib_resources
Signed-off-by: Cristian Le <[email protected]>
1 parent e39cf5a commit cc63b29

File tree

1 file changed

+48
-95
lines changed

1 file changed

+48
-95
lines changed

tests/test_editable.py

+48-95
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import platform
12
import sys
23
import textwrap
34
from pathlib import Path
@@ -206,6 +207,10 @@ def _setup_package_for_editable_layout_tests(
206207
("editable", "editable_mode"), [(False, ""), (True, "redirect"), (True, "inplace")]
207208
)
208209
def test_direct_import(monkeypatch, tmp_path, editable, editable_mode, isolated):
210+
# TODO: Investigate these failures
211+
if platform.system() == "Windows" and editable_mode == "inplace":
212+
pytest.xfail("Windows fails to import the top-level extension module")
213+
209214
_setup_package_for_editable_layout_tests( # type: ignore[no-untyped-call]
210215
monkeypatch, tmp_path, editable, editable_mode, isolated
211216
)
@@ -218,114 +223,62 @@ def test_direct_import(monkeypatch, tmp_path, editable, editable_mode, isolated)
218223
@pytest.mark.configure
219224
@pytest.mark.integration
220225
@pytest.mark.parametrize(
221-
("editable", "editable_mode", "check"),
226+
("editable", "editable_mode"),
222227
[
223-
# Without editable
224-
(False, "", "isinstance(files(pkg), pathlib.Path)"),
225-
(False, "", "any(str(x).endswith('.so') for x in files(pkg).iterdir())"),
226-
(False, "", "isinstance(files(pkg.sub_a), pathlib.Path)"),
227-
(
228-
False,
229-
"",
230-
"any(str(x).endswith('.so') for x in files(pkg.sub_a).iterdir())",
231-
),
232-
(False, "", "isinstance(files(pkg.sub_b), pathlib.Path)"),
233-
(
234-
False,
235-
"",
236-
"any(str(x).endswith('.so') for x in files(pkg.sub_b).iterdir())",
237-
),
238-
(False, "", "isinstance(files(pkg.sub_b.sub_c), pathlib.Path)"),
239-
(
240-
False,
241-
"",
242-
"any(str(x).endswith('.so') for x in files(pkg.sub_b.sub_c).iterdir())",
243-
),
244-
(False, "", "isinstance(files(pkg.sub_b.sub_d), pathlib.Path)"),
245-
(
246-
False,
247-
"",
248-
"any(str(x).endswith('.so') for x in files(pkg.sub_b.sub_d).iterdir())",
249-
),
250-
# Editable redirect
251-
(True, "redirect", "isinstance(files(pkg), pathlib.Path)"),
252-
pytest.param(
253-
True,
254-
"redirect",
255-
"any(str(x).endswith('.so') for x in files(pkg).iterdir())",
256-
marks=pytest.mark.xfail,
257-
),
258-
(True, "redirect", "isinstance(files(pkg.sub_a), pathlib.Path)"),
259-
pytest.param(
260-
True,
261-
"redirect",
262-
"any(str(x).endswith('.so') for x in files(pkg.sub_a).iterdir())",
263-
marks=pytest.mark.xfail,
264-
),
265-
(True, "redirect", "isinstance(files(pkg.sub_b), pathlib.Path)"),
266-
pytest.param(
267-
True,
268-
"redirect",
269-
"any(str(x).endswith('.so') for x in files(pkg.sub_b).iterdir())",
270-
marks=pytest.mark.xfail,
271-
),
272-
(True, "redirect", "isinstance(files(pkg.sub_b.sub_c), pathlib.Path)"),
228+
(False, ""),
273229
pytest.param(
274230
True,
275231
"redirect",
276-
"any(str(x).endswith('.so') for x in files(pkg.sub_b.sub_c).iterdir())",
277232
marks=pytest.mark.xfail,
278233
),
279-
(True, "redirect", "isinstance(files(pkg.sub_b.sub_d), pathlib.Path)"),
280-
pytest.param(
281-
True,
282-
"redirect",
283-
"any(str(x).endswith('.so') for x in files(pkg.sub_b.sub_d).iterdir())",
284-
marks=pytest.mark.xfail,
285-
),
286-
# Editable inplace
287-
(True, "inplace", "isinstance(files(pkg), pathlib.Path)"),
288-
(True, "inplace", "any(str(x).endswith('.so') for x in files(pkg).iterdir())"),
289-
(True, "inplace", "isinstance(files(pkg.sub_a), pathlib.Path)"),
290-
(
291-
True,
292-
"inplace",
293-
"any(str(x).endswith('.so') for x in files(pkg.sub_a).iterdir())",
294-
),
295-
(True, "inplace", "isinstance(files(pkg.sub_b), pathlib.Path)"),
296-
(
297-
True,
298-
"inplace",
299-
"any(str(x).endswith('.so') for x in files(pkg.sub_b).iterdir())",
300-
),
301-
(True, "inplace", "isinstance(files(pkg.sub_b.sub_c), pathlib.Path)"),
302-
(
303-
True,
304-
"inplace",
305-
"any(str(x).endswith('.so') for x in files(pkg.sub_b.sub_c).iterdir())",
306-
),
307-
(True, "inplace", "isinstance(files(pkg.sub_b.sub_d), pathlib.Path)"),
308-
(
309-
True,
310-
"inplace",
311-
"any(str(x).endswith('.so') for x in files(pkg.sub_b.sub_d).iterdir())",
312-
),
234+
(True, "inplace"),
313235
],
314236
)
315-
def test_importlib_resources(
316-
monkeypatch, tmp_path, editable, editable_mode, isolated, check
317-
):
318-
_setup_package_for_editable_layout_tests( # type: ignore[no-untyped-call]
237+
def test_importlib_resources(monkeypatch, tmp_path, editable, editable_mode, isolated):
238+
if sys.version_info < (3, 9):
239+
pytest.skip("importlib.resources.files is introduced in Python 3.9")
240+
241+
# TODO: Investigate these failures
242+
if editable_mode == "redirect":
243+
pytest.xfail("Redirect mode is at navigating importlib.resources.files")
244+
if platform.system() == "Windows" and editable_mode == "inplace":
245+
pytest.xfail("Windows fails to import the top-level extension module")
246+
247+
_setup_package_for_editable_layout_tests(
319248
monkeypatch, tmp_path, editable, editable_mode, isolated
320249
)
250+
321251
isolated.execute(
322252
textwrap.dedent(
323-
f"""
253+
"""
254+
from importlib import import_module
324255
from importlib.resources import files
325-
from importlib.readers import MultiplexedPath
326-
import pkg
327-
import pathlib
328-
assert {check}
256+
from pathlib import Path
257+
258+
def is_extension(path):
259+
for ext in (".so", ".pyd"):
260+
if ext in path.suffixes:
261+
return True
262+
return False
263+
264+
def check_pkg(pkg_name):
265+
try:
266+
pkg = import_module(pkg_name)
267+
pkg_root = files(pkg)
268+
print(f"pkg_root: [{type(pkg_root)}] {pkg_root}")
269+
pkg_files = list(pkg_root.iterdir())
270+
for path in pkg_files:
271+
print(f"path: [{type(path)}] {path}")
272+
assert any(is_extension(path) for path in pkg_files if isinstance(path, Path))
273+
except Exception as err:
274+
msg = f"Failed in {str(pkg)}"
275+
raise RuntimeError(msg) from err
276+
277+
check_pkg("pkg")
278+
check_pkg("pkg.sub_a")
279+
check_pkg("pkg.sub_b")
280+
check_pkg("pkg.sub_b.sub_c")
281+
check_pkg("pkg.sub_b.sub_d")
329282
"""
330283
)
331284
)

0 commit comments

Comments
 (0)