Skip to content

Commit 0ddb6e7

Browse files
committed
test: Add data.find() and extend pip_install_local() helpers
Makes it easier to write tests that exclusively use local test data (and don't use the Internet).
1 parent 18694f6 commit 0ddb6e7

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

tests/lib/__init__.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ def __init__(
151151
self.source = source or DATA_DIR
152152
self.root = root.resolve()
153153

154+
def find(self, identifier: str) -> pathlib.Path:
155+
"""Convenience method for finding a data file."""
156+
path = pathlib.Path(identifier)
157+
if path.parent == pathlib.Path("."):
158+
# No group provided but it's a distribution file, assume it's from packages/
159+
if path.name.endswith((".whl", ".tar.gz")):
160+
group = "packages"
161+
else:
162+
raise ValueError("must provide a data group prefix")
163+
else:
164+
group = path.parent.name
165+
166+
resource = self.root / group / path.name
167+
if not resource.exists():
168+
raise ValueError(f"data '{identifier}' does not exist!")
169+
return resource
170+
154171
@classmethod
155172
def copy(cls, root: pathlib.Path) -> TestData:
156173
obj = cls(root)
@@ -725,13 +742,24 @@ def pip(
725742
def pip_install_local(
726743
self,
727744
*args: StrPath,
745+
find_links: StrPath | list[StrPath] = "packages",
728746
**kwargs: Any,
729747
) -> TestPipResult:
748+
if not isinstance(find_links, list):
749+
find_links = [find_links]
750+
find_links_args: list[StrPath] = []
751+
for folder in find_links:
752+
path = pathlib.Path(folder)
753+
find_links_args.append("--find-links")
754+
if path.parent == pathlib.Path("."):
755+
find_links_args.append(pathlib.Path(DATA_DIR, folder).as_uri())
756+
else:
757+
find_links_args.append(path)
758+
730759
return self.pip(
731760
"install",
732761
"--no-index",
733-
"--find-links",
734-
pathlib.Path(DATA_DIR, "packages").as_uri(),
762+
*find_links_args,
735763
*args,
736764
**kwargs,
737765
)

0 commit comments

Comments
 (0)