Skip to content

Commit

Permalink
Perform a work copy only if Yarn packages are present
Browse files Browse the repository at this point in the history
Cachi2 package managers such as npm and pip will never perform any
modifications on the source code, which makes the copy unnecessary
overhead.

We can decide if the copy is also worth doing for gomod packages
in a follow up PR.

Signed-off-by: Bruno Pimentel <[email protected]>
  • Loading branch information
brunoapimentel committed Nov 28, 2023
1 parent 66a56d6 commit 9b57b51
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
21 changes: 12 additions & 9 deletions cachi2/core/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ def resolve_packages(request: Request) -> RequestOutput:
"""
Resolve all packages specified in a request.
This function performs the operations in a working copy of the source directory to avoid
modifications.
This function performs the operations in a working copy of the source directory in case
a package manager that can make unwanted modifications will be used.
"""
original_source_dir = request.source_dir
if not request.yarn_packages:
return _resolve_packages(request)
else:
original_source_dir = request.source_dir

with TemporaryDirectory(".cachi2-source-copy", dir=".") as temp_dir:
source_backup = copy_directory(original_source_dir.path, Path(temp_dir).resolve())
with TemporaryDirectory(".cachi2-source-copy", dir=".") as temp_dir:
source_backup = copy_directory(original_source_dir.path, Path(temp_dir).resolve())

request.source_dir = RootedPath(source_backup)
output = _resolve_packages(request)
request.source_dir = original_source_dir
request.source_dir = RootedPath(source_backup)
output = _resolve_packages(request)
request.source_dir = original_source_dir

return output
return output


def _resolve_packages(request: Request) -> RequestOutput:
Expand Down
30 changes: 23 additions & 7 deletions tests/unit/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,37 @@ def fetch(req: Request) -> RequestOutput:
assert calls_by_pkgtype == ["gomod", "npm", "pip"]


@pytest.mark.parametrize(
"packages, copy_exists",
[
([{"type": "yarn"}], True),
([{"type": "gomod"}, {"type": "pip"}, {"type": "npm"}], False),
],
)
@mock.patch("cachi2.core.resolver._resolve_packages")
def test_source_dir_copy(mock_resolve_packages: mock.Mock, tmp_path: Path) -> None:
def test_source_dir_copy(
mock_resolve_packages: mock.Mock,
packages: list[dict[str, str]],
copy_exists: bool,
tmp_path: Path,
) -> None:
request = Request(
source_dir=tmp_path,
output_dir=tmp_path,
packages=[{"type": "yarn"}],
packages=packages,
)

def _resolve_packages(request: Request) -> None:
tmp_dir_name = request.source_dir.path.name
if copy_exists:
tmp_dir_name = request.source_dir.path.name

# assert a temporary directory is being used
assert tmp_dir_name != tmp_path.name
assert tmp_dir_name.startswith("tmp")
assert tmp_dir_name.endswith(".cachi2-source-copy")
# assert a temporary directory is being used
assert tmp_dir_name != tmp_path.name
assert tmp_dir_name.startswith("tmp")
assert tmp_dir_name.endswith(".cachi2-source-copy")
else:
# assert the original source_dir is being used
assert request.source_dir == RootedPath(tmp_path)

mock_resolve_packages.side_effect = _resolve_packages

Expand Down

0 comments on commit 9b57b51

Please sign in to comment.