From 0f4747fd7991ed268255447a505fb7186ad9ea28 Mon Sep 17 00:00:00 2001 From: Aparna Jyothi Date: Mon, 9 Jun 2025 13:34:23 +0530 Subject: [PATCH] logic update for robust --- __tests__/setup-python.test.ts | 27 ++++++++++++++++++++------- dist/setup/index.js | 7 ++++++- src/setup-python.ts | 11 +++++++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/__tests__/setup-python.test.ts b/__tests__/setup-python.test.ts index b454eeb2..e4d59ac6 100644 --- a/__tests__/setup-python.test.ts +++ b/__tests__/setup-python.test.ts @@ -10,6 +10,7 @@ jest.mock('fs', () => { ...actualFs, copyFileSync: jest.fn(), existsSync: jest.fn(), + mkdirSync: jest.fn(), promises: { access: jest.fn(), writeFile: jest.fn(), @@ -32,25 +33,37 @@ describe('cacheDependencies', () => { process.env.GITHUB_ACTION_PATH = '/github/action'; process.env.GITHUB_WORKSPACE = '/github/workspace'; - mockedCore.getInput.mockReturnValue('deps.lock'); - mockedFs.existsSync.mockReturnValue(true); - mockedFs.copyFileSync.mockImplementation(() => {}); + mockedCore.getInput.mockReturnValue('nested/deps.lock'); + + mockedFs.existsSync.mockImplementation((p: any) => { + const pathStr = typeof p === 'string' ? p : p.toString(); + if (pathStr === '/github/action/nested/deps.lock') return true; + if (pathStr === '/github/workspace/nested') return false; // Simulate missing dir + return true; + }); + + mockedFs.copyFileSync.mockImplementation(() => undefined); + mockedFs.mkdirSync.mockImplementation(() => undefined); mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache}); }); - it('copies the dependency file and resolves the path', async () => { + it('copies the dependency file and resolves the path with directory structure', async () => { await cacheDependencies('pip', '3.12'); - const sourcePath = path.resolve('/github/action', 'deps.lock'); - const targetPath = path.resolve('/github/workspace', 'deps.lock'); + const sourcePath = path.resolve('/github/action', 'nested/deps.lock'); + const targetPath = path.resolve('/github/workspace', 'nested/deps.lock'); expect(mockedFs.existsSync).toHaveBeenCalledWith(sourcePath); + expect(mockedFs.mkdirSync).toHaveBeenCalledWith( + path.dirname(targetPath), + {recursive: true} + ); expect(mockedFs.copyFileSync).toHaveBeenCalledWith(sourcePath, targetPath); expect(mockedCore.info).toHaveBeenCalledWith( `Copied ${sourcePath} to ${targetPath}` ); expect(mockedCore.info).toHaveBeenCalledWith( - `Resolved cache-dependency-path: deps.lock` + `Resolved cache-dependency-path: nested/deps.lock` ); expect(mockRestoreCache).toHaveBeenCalled(); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 9c4cb00a..854f9490 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -96929,13 +96929,18 @@ function cacheDependencies(cache, pythonVersion) { const actionPath = process.env.GITHUB_ACTION_PATH || ''; const workspace = process.env.GITHUB_WORKSPACE || process.cwd(); const sourcePath = path.resolve(actionPath, cacheDependencyPath); - const targetPath = path.resolve(workspace, path.basename(cacheDependencyPath)); + const relativePath = path.relative(actionPath, sourcePath); + const targetPath = path.resolve(workspace, relativePath); if (!fs_1.default.existsSync(sourcePath)) { core.warning(`The resolved cache-dependency-path does not exist: ${sourcePath}`); } else { if (sourcePath !== targetPath) { try { + const targetDir = path.dirname(targetPath); + if (!fs_1.default.existsSync(targetDir)) { + fs_1.default.mkdirSync(targetDir, { recursive: true }); + } fs_1.default.copyFileSync(sourcePath, targetPath); core.info(`Copied ${sourcePath} to ${targetPath}`); } diff --git a/src/setup-python.ts b/src/setup-python.ts index 06682b79..ed76469c 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -32,10 +32,8 @@ export async function cacheDependencies(cache: string, pythonVersion: string) { const workspace = process.env.GITHUB_WORKSPACE || process.cwd(); const sourcePath = path.resolve(actionPath, cacheDependencyPath); - const targetPath = path.resolve( - workspace, - path.basename(cacheDependencyPath) - ); + const relativePath = path.relative(actionPath, sourcePath); + const targetPath = path.resolve(workspace, relativePath); if (!fs.existsSync(sourcePath)) { core.warning( @@ -44,6 +42,11 @@ export async function cacheDependencies(cache: string, pythonVersion: string) { } else { if (sourcePath !== targetPath) { try { + const targetDir = path.dirname(targetPath); + if (!fs.existsSync(targetDir)) { + fs.mkdirSync(targetDir, {recursive: true}); + } + fs.copyFileSync(sourcePath, targetPath); core.info(`Copied ${sourcePath} to ${targetPath}`); } catch (error) {