-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
159 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,10 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from mkdocs_deploy import abstract | ||
from mkdocs_deploy.plugins import local_filesystem | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def _clean_plugins(monkeypatch: pytest.MonkeyPatch): | ||
"""Ensure that all tests run with uninitialized plugins""" | ||
monkeypatch.setattr(abstract, "_SOURCES", {}) | ||
monkeypatch.setattr(abstract, "_TARGETS", {}) | ||
|
||
@pytest.fixture() | ||
def mock_source_path(tmp_path: Path) -> Path: | ||
base_path = tmp_path / "mock_source" | ||
base_path.mkdir(exist_ok=False, parents=True) | ||
return base_path | ||
|
||
|
||
@pytest.fixture() | ||
def mock_source(mock_source_path: Path) -> abstract.Source: | ||
return local_filesystem.LocalFileTreeSource(mock_source_path) | ||
|
||
|
||
@pytest.fixture() | ||
def mock_target_path(tmp_path: Path) -> Path: | ||
base_path = tmp_path / "mock_target" | ||
base_path.mkdir(exist_ok=False, parents=True) | ||
return base_path | ||
|
||
|
||
@pytest.fixture() | ||
def mock_target(mock_target_path: Path) -> abstract.Target: | ||
return local_filesystem.LocalFileTreeTarget(str(mock_target_path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import io | ||
from copy import deepcopy | ||
from io import BytesIO | ||
from typing import IO, Iterable | ||
|
||
from mkdocs_deploy import abstract, versions | ||
from mkdocs_deploy.abstract import TargetSession, Version | ||
from mkdocs_deploy.versions import DeploymentAlias, DeploymentSpec | ||
|
||
|
||
class BaseMockPlugin: | ||
|
||
files: dict[str, bytes] | ||
|
||
def __init__(self): | ||
self.files = {} | ||
|
||
|
||
class MockSource(abstract.Source): | ||
|
||
def __init__(self, files: dict[str, bytes] | None = None): | ||
self.files = files.copy() if files is not None else {} | ||
|
||
def iter_files(self) -> Iterable[str]: | ||
yield from self.files.keys() | ||
|
||
def open_file_for_read(self, filename: str) -> IO[bytes]: | ||
return io.BytesIO(initial_bytes=self.files[filename]) | ||
|
||
|
||
class MockRedirectMechanism(abstract.RedirectMechanism): | ||
|
||
def create_redirect(self, session: TargetSession, alias: Version, version_id: str) -> None: | ||
pass | ||
|
||
def delete_redirect(self, session: TargetSession, alias: Version) -> None: | ||
pass | ||
|
||
|
||
class MockTargetSession(abstract.TargetSession): | ||
files: dict[tuple[Version, str], bytes] | ||
internal_deployment_spec: abstract.DeploymentSpec | ||
closed: bool = False | ||
close_success: bool = False | ||
aliases: dict[Version, versions.DeploymentAlias] | ||
|
||
def __init__(self): | ||
self.files = {} | ||
self.deleted_files = set() | ||
self.internal_deployment_spec = abstract.DeploymentSpec() | ||
self.aliases = {} | ||
|
||
def start_version(self, version_id: str, title: str) -> None: | ||
self.internal_deployment_spec.versions[version_id] = versions.DeploymentVersion(title=title) | ||
|
||
def delete_version(self, version_id: str) -> None: | ||
existing_files = [f for v, f in self.files.keys() if v == version_id] | ||
del self.internal_deployment_spec.versions[version_id] | ||
for file in existing_files: | ||
del self.files[(version_id, file)] | ||
|
||
def upload_file(self, version_id: Version, filename: str, file_obj: IO[bytes]) -> None: | ||
if version_id not in self.internal_deployment_spec.versions: | ||
raise abstract.VersionNotFound(version_id) | ||
self.files[(version_id, filename)] = file_obj.read() | ||
|
||
def download_file(self, version_id: Version, filename: str) -> IO[bytes]: | ||
if version_id not in self.internal_deployment_spec.versions: | ||
raise abstract.VersionNotFound(version_id) | ||
return BytesIO(self.files[(version_id, filename)]) | ||
|
||
def delete_file(self, version_id: Version, filename: str) -> None: | ||
if version_id not in self.internal_deployment_spec.versions: | ||
raise abstract.VersionNotFound(version_id) | ||
del self.files[(version_id, filename)] | ||
|
||
def iter_files(self, version_id: str) -> Iterable[str]: | ||
for version, file in self.files: | ||
if version_id == version: | ||
yield file | ||
|
||
def close(self, success: bool = False) -> None: | ||
self.closed = True | ||
self.close_success = success | ||
|
||
def set_alias(self, alias_id: Version, alias: DeploymentAlias | None) -> None: | ||
if alias is None: | ||
del self.aliases[alias_id] | ||
return | ||
self.aliases[alias_id] = deepcopy(alias) | ||
|
||
@property | ||
def available_redirect_mechanisms(self) -> dict[str, abstract.RedirectMechanism]: | ||
return {"mock": MockRedirectMechanism()} | ||
|
||
@property | ||
def deployment_spec(self) -> DeploymentSpec: | ||
return deepcopy(self.internal_deployment_spec) | ||
|
||
|
||
class MockTarget(abstract.Target): | ||
|
||
files: dict[tuple[str, str], bytes] | ||
internal_deployment_spec: versions.DeploymentSpec | ||
|
||
def __init__(self): | ||
self.files = {} | ||
self.internal_deployment_spec = versions.DeploymentSpec() | ||
|
||
def start_session(self) -> MockTargetSession: | ||
return MockTargetSession() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from typing import Any, NamedTuple, TypeVar | ||
from copy import deepcopy | ||
|
||
_T = TypeVar("_T") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters