From 9b57b515334994fd49a42a4bee34e733ddc2e9ad Mon Sep 17 00:00:00 2001 From: Bruno Pimentel Date: Wed, 15 Nov 2023 20:46:50 -0300 Subject: [PATCH] Perform a work copy only if Yarn packages are present 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 --- cachi2/core/resolver.py | 21 ++++++++++++--------- tests/unit/test_resolver.py | 30 +++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/cachi2/core/resolver.py b/cachi2/core/resolver.py index 331e93065..9b0df3e0e 100644 --- a/cachi2/core/resolver.py +++ b/cachi2/core/resolver.py @@ -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: diff --git a/tests/unit/test_resolver.py b/tests/unit/test_resolver.py index c90e6b12c..c27afbdf3 100644 --- a/tests/unit/test_resolver.py +++ b/tests/unit/test_resolver.py @@ -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