From 566d40e37b39633480c386ee4e6def2ed883f677 Mon Sep 17 00:00:00 2001 From: Aparna Jyothi Date: Mon, 9 Jun 2025 14:01:20 +0530 Subject: [PATCH] update cacheDependencies tests to cover resolved paths and copy edge cases --- __tests__/setup-python.test.ts | 27 +++++++++++++++++++++++++++ dist/setup/index.js | 27 +++++++++++++++++---------- src/setup-python.ts | 32 +++++++++++++++++++------------- 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/__tests__/setup-python.test.ts b/__tests__/setup-python.test.ts index 805e56ac..bb27289d 100644 --- a/__tests__/setup-python.test.ts +++ b/__tests__/setup-python.test.ts @@ -119,4 +119,31 @@ describe('cacheDependencies', () => { expect(mockedCore.warning).not.toHaveBeenCalled(); expect(mockRestoreCache).toHaveBeenCalled(); }); + + it('does not copy if dependency file is already inside the workspace but still sets resolved path', async () => { + // Simulate cacheDependencyPath inside workspace + mockedCore.getInput.mockReturnValue('deps.lock'); + + // Override sourcePath and targetPath to be equal + const actionPath = '/github/workspace'; // same path for action and workspace + process.env.GITHUB_ACTION_PATH = actionPath; + process.env.GITHUB_WORKSPACE = actionPath; + + // access resolves to simulate file exists + mockedFsPromises.access.mockResolvedValue(); + + await cacheDependencies('pip', '3.12'); + + const sourcePath = path.resolve(actionPath, 'deps.lock'); + const targetPath = sourcePath; // same path + + expect(mockedFsPromises.copyFile).not.toHaveBeenCalled(); + expect(mockedCore.info).toHaveBeenCalledWith( + `Dependency file is already inside the workspace: ${sourcePath}` + ); + expect(mockedCore.info).toHaveBeenCalledWith( + `Resolved cache-dependency-path: deps.lock` + ); + expect(mockRestoreCache).toHaveBeenCalled(); + }); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 76ddee08..70d05b69 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -96939,22 +96939,29 @@ function cacheDependencies(cache, pythonVersion) { if (!sourceExists) { core.warning(`The resolved cache-dependency-path does not exist: ${sourcePath}`); } - else if (sourcePath !== targetPath) { - const targetDir = path.dirname(targetPath); - // Create target directory if it doesn't exist - yield fs_1.default.promises.mkdir(targetDir, { recursive: true }); - // Copy file asynchronously - yield fs_1.default.promises.copyFile(sourcePath, targetPath); - core.info(`Copied ${sourcePath} to ${targetPath}`); + else { + if (sourcePath !== targetPath) { + const targetDir = path.dirname(targetPath); + // Create target directory if it doesn't exist + yield fs_1.default.promises.mkdir(targetDir, { recursive: true }); + // Copy file asynchronously + yield fs_1.default.promises.copyFile(sourcePath, targetPath); + core.info(`Copied ${sourcePath} to ${targetPath}`); + } + else { + core.info(`Dependency file is already inside the workspace: ${sourcePath}`); + } + resolvedDependencyPath = path.relative(workspace, targetPath); + core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`); } - resolvedDependencyPath = path.relative(workspace, targetPath); - core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`); } catch (error) { core.warning(`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}`); } } - const cacheDistributor = (0, cache_factory_1.getCacheDistributor)(cache, pythonVersion, cacheDependencyPath); + // Pass resolvedDependencyPath if available, else fallback to original input + const dependencyPathForCache = resolvedDependencyPath !== null && resolvedDependencyPath !== void 0 ? resolvedDependencyPath : cacheDependencyPath; + const cacheDistributor = (0, cache_factory_1.getCacheDistributor)(cache, pythonVersion, dependencyPathForCache); yield cacheDistributor.restoreCache(); }); } diff --git a/src/setup-python.ts b/src/setup-python.ts index 2895d885..a1758eaf 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -44,19 +44,22 @@ export async function cacheDependencies(cache: string, pythonVersion: string) { core.warning( `The resolved cache-dependency-path does not exist: ${sourcePath}` ); - } else if (sourcePath !== targetPath) { - const targetDir = path.dirname(targetPath); - - // Create target directory if it doesn't exist - await fs.promises.mkdir(targetDir, {recursive: true}); - - // Copy file asynchronously - await fs.promises.copyFile(sourcePath, targetPath); - core.info(`Copied ${sourcePath} to ${targetPath}`); + } else { + if (sourcePath !== targetPath) { + const targetDir = path.dirname(targetPath); + // Create target directory if it doesn't exist + await fs.promises.mkdir(targetDir, {recursive: true}); + // Copy file asynchronously + await fs.promises.copyFile(sourcePath, targetPath); + core.info(`Copied ${sourcePath} to ${targetPath}`); + } else { + core.info( + `Dependency file is already inside the workspace: ${sourcePath}` + ); + } + resolvedDependencyPath = path.relative(workspace, targetPath); + core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`); } - - resolvedDependencyPath = path.relative(workspace, targetPath); - core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`); } catch (error) { core.warning( `Failed to copy file from ${sourcePath} to ${targetPath}: ${error}` @@ -64,10 +67,13 @@ export async function cacheDependencies(cache: string, pythonVersion: string) { } } + // Pass resolvedDependencyPath if available, else fallback to original input + const dependencyPathForCache = resolvedDependencyPath ?? cacheDependencyPath; + const cacheDistributor = getCacheDistributor( cache, pythonVersion, - cacheDependencyPath + dependencyPathForCache ); await cacheDistributor.restoreCache(); }