Skip to content

Commit

Permalink
tests: improve fixture handling for repos
Browse files Browse the repository at this point in the history
  • Loading branch information
abn committed Mar 21, 2024
1 parent 7ab37fd commit 4e41660
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
39 changes: 30 additions & 9 deletions tests/repositories/fixtures/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,31 @@
def package_distribution_locations() -> list[Path]:
return [
FIXTURE_PATH_REPOSITORIES_PYPI / "dists",
FIXTURE_PATH_REPOSITORIES_PYPI / "dists" / "mocked",
FIXTURE_PATH_REPOSITORIES_PYPI / "stubbed",
FIXTURE_PATH_DISTRIBUTIONS,
]


@pytest.fixture
def package_metadata_path() -> Path | None:
return FIXTURE_PATH_REPOSITORIES_PYPI / "metadata"
def package_json_locations() -> list[Path]:
return [
FIXTURE_PATH_REPOSITORIES_PYPI / "json",
FIXTURE_PATH_REPOSITORIES_PYPI / "json" / "mocked",
]


@pytest.fixture
def package_metadata_locations() -> list[Path]:
return [
FIXTURE_PATH_REPOSITORIES_PYPI / "metadata",
FIXTURE_PATH_REPOSITORIES_PYPI / "metadata" / "mocked",
]


@pytest.fixture
def package_distribution_lookup(
package_distribution_locations: list[Path], package_metadata_path: Path | None
package_distribution_locations: list[Path],
) -> PackageDistributionLookup:
def lookup(name: str) -> Path | None:
for location in package_distribution_locations:
Expand All @@ -63,6 +75,7 @@ def lookup(name: str) -> Path | None:
def pypi_repository(
http: type[httpretty],
legacy_repository_html_callback: HTTPrettyRequestCallback,
package_json_locations: list[Path],
mock_files_python_hosted: None,
) -> PyPiRepository:
def default_callback(
Expand All @@ -83,20 +96,28 @@ def simple_callback(
return json_callback(request, uri, headers)
return legacy_repository_html_callback(request, uri, headers)

def _get_json_filepath(name: str, version: str | None = None) -> Path | None:
for base in package_json_locations:
if not version:
fixture = base / f"{name}.json"
else:
fixture = base / name / f"{version}.json"

if fixture.exists():
return fixture

return None

def json_callback(
request: HTTPrettyRequest, uri: str, headers: dict[str, Any]
) -> HTTPrettyResponse:
path = urlparse(uri).path
parts = path.rstrip("/").split("/")[2:]
name = parts[0]
version = parts[1] if len(parts) == 3 else None
json_fixtures = FIXTURE_PATH_REPOSITORIES_PYPI / "json"
if not version:
fixture = json_fixtures / f"{name}.json"
else:
fixture = json_fixtures / name / (version + ".json")
fixture = _get_json_filepath(name, version)

if not fixture.exists():
if fixture is None or not fixture.exists():
return default_callback(request, uri, headers)

return 200, headers, fixture.read_bytes()
Expand Down
16 changes: 8 additions & 8 deletions tests/repositories/fixtures/python_hosted.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
@pytest.fixture
def mock_files_python_hosted_factory(http: type[httpretty]) -> PythonHostedFileMocker:
def factory(
distribution_locations: list[Path], metadata: Path | None = None
distribution_locations: list[Path], metadata_locations: list[Path] | None = None
) -> None:
def file_callback(
request: HTTPrettyRequest, uri: str, headers: dict[str, Any]
) -> list[int | dict[str, Any] | bytes | str]:
name = Path(urlparse(uri).path).name

if metadata and name.endswith(".metadata"):
fixture = metadata / name

if fixture.exists():
return [200, headers, fixture.read_text()]
if metadata_locations and name.endswith(".metadata"):
for location in metadata_locations:
fixture = location / name
if fixture.exists():
return [200, headers, fixture.read_text()]
else:
for location in distribution_locations:
fixture = location / name
Expand All @@ -57,10 +57,10 @@ def file_callback(
def mock_files_python_hosted(
mock_files_python_hosted_factory: PythonHostedFileMocker,
package_distribution_locations: list[Path],
package_metadata_path: Path | None,
package_metadata_locations: list[Path] | None,
) -> Iterator[None]:
mock_files_python_hosted_factory(
distribution_locations=package_distribution_locations,
metadata=package_metadata_path,
metadata_locations=package_metadata_locations,
)
yield None
4 changes: 3 additions & 1 deletion tests/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ def __call__(

class PythonHostedFileMocker(Protocol):
def __call__(
self, distribution_locations: list[Path], metadata: Path | None = None
self,
distribution_locations: list[Path],
metadata_locations: list[Path] | None = None,
) -> None: ...


Expand Down

0 comments on commit 4e41660

Please sign in to comment.