|
| 1 | +import atexit |
1 | 2 | import os |
2 | 3 | import shutil |
| 4 | +import sys |
| 5 | +from contextlib import ExitStack |
3 | 6 | from pathlib import Path |
4 | 7 |
|
5 | 8 | import pytest |
6 | | -from pkg_resources import Requirement, ResolutionError, resource_filename |
| 9 | + |
| 10 | +if sys.version_info >= (3, 11): |
| 11 | + from importlib.resources import as_file, files |
| 12 | +else: |
| 13 | + from importlib.resources import as_file, files |
7 | 14 |
|
8 | 15 |
|
9 | 16 | def get_path(filename: str) -> Path: |
| 17 | + """Get the filepath for a given test file.""" |
10 | 18 | # normalizing path depending on OS or else it will cause problem when joining path |
11 | 19 | filename = os.path.normpath(filename) |
12 | 20 | filepath = None |
13 | 21 | try: |
14 | | - filepath = resource_filename(Requirement.parse("cwl-utils"), filename) |
15 | | - except ResolutionError: |
| 22 | + file_manager = ExitStack() |
| 23 | + atexit.register(file_manager.close) |
| 24 | + traversable = files("cwl-utils") / filename |
| 25 | + filepath = file_manager.enter_context(as_file(traversable)) |
| 26 | + except ModuleNotFoundError: |
16 | 27 | pass |
17 | | - if not filepath or not os.path.isfile(filepath): |
18 | | - filepath = os.path.join(os.path.dirname(__file__), os.pardir, filename) |
19 | | - return Path(filepath).resolve() |
| 28 | + if not filepath or not filepath.is_file(): |
| 29 | + filepath = Path(os.path.dirname(__file__), os.pardir, filename) |
| 30 | + return filepath.resolve() |
20 | 31 |
|
21 | 32 |
|
22 | 33 | def get_data(filename: str) -> str: |
|
0 commit comments