Skip to content

Commit

Permalink
feat: isolate changes context in packages cache (#2518)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Feb 18, 2025
1 parent 0d61d25 commit d504d18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ape/managers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,25 @@ def remove(self, package_id: str, version: str):
manifest_file = self.get_manifest_path(package_id, version)
manifest_file.unlink(missing_ok=True)

@contextmanager
def isolate_changes(self):
"""
Allows you to make changes affecting the Ape packages cache in a context.
For example, temporarily install local "dev" packages for testing purposes.
"""
with create_tempdir() as tmpdir:
packages_cache = tmpdir / "packages"
packages_cache.parent.mkdir(parents=True, exist_ok=True)
if self.root.is_dir():
shutil.copytree(self.root, packages_cache)
try:
yield
finally:
shutil.rmtree(self.root)
if packages_cache.is_dir():
# Restore.
shutil.copytree(packages_cache, self.root)


def _version_to_options(version: str) -> tuple[str, ...]:
if version.startswith("v"):
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ def test_get_project_path_unneeded_v_prefix(self, cache, data_folder):
actual = cache.get_project_path("project-test-2", "v1.0.0")
assert actual == path

def test_isolate_cache_changes(self, cache):
dep = LocalDependency(name="isotestdep", local=Path("isotestdep"), version="v1.0.0")
with cache.isolate_changes():
path = cache.cache_api(dep)
assert path.is_file()

assert not path.is_file()


class TestLocalDependency:
NAME = "testlocaldep"
Expand Down

0 comments on commit d504d18

Please sign in to comment.