Skip to content

Commit b560835

Browse files
committed
tests: remove use of the deprecated pkg_resources
1 parent 69455a8 commit b560835

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

tests/util.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1+
import atexit
12
import os
23
import shutil
4+
import sys
5+
from contextlib import ExitStack
36
from pathlib import Path
47

58
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
714

815

916
def get_path(filename: str) -> Path:
17+
"""Get the filepath for a given test file."""
1018
# normalizing path depending on OS or else it will cause problem when joining path
1119
filename = os.path.normpath(filename)
1220
filepath = None
1321
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:
1627
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()
2031

2132

2233
def get_data(filename: str) -> str:

0 commit comments

Comments
 (0)