diff --git a/__tests__/setup-python.test.ts b/__tests__/setup-python.test.ts index 5d68482b..940cb402 100644 --- a/__tests__/setup-python.test.ts +++ b/__tests__/setup-python.test.ts @@ -8,11 +8,10 @@ jest.mock('fs', () => { const actualFs = jest.requireActual('fs'); return { ...actualFs, - copyFileSync: jest.fn(), - existsSync: jest.fn(), - mkdirSync: jest.fn(), promises: { access: jest.fn(), + mkdir: jest.fn(), + copyFile: jest.fn(), writeFile: jest.fn(), appendFile: jest.fn() } @@ -21,7 +20,7 @@ jest.mock('fs', () => { jest.mock('@actions/core'); jest.mock('../src/cache-distributions/cache-factory'); -const mockedFs = fs as jest.Mocked; +const mockedFsPromises = fs.promises as jest.Mocked; const mockedCore = core as jest.Mocked; const mockedGetCacheDistributor = getCacheDistributor as jest.Mock; @@ -35,15 +34,25 @@ describe('cacheDependencies', () => { mockedCore.getInput.mockReturnValue('nested/deps.lock'); - mockedFs.existsSync.mockImplementation((p: any) => { + // Simulate file exists by resolving access without error + mockedFsPromises.access.mockImplementation(async (p) => { 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; + if (pathStr === '/github/action/nested/deps.lock') { + return Promise.resolve(); + } + // Simulate directory doesn't exist to test mkdir + if (pathStr === path.dirname('/github/workspace/nested/deps.lock')) { + return Promise.reject(new Error('no dir')); + } + return Promise.resolve(); }); - mockedFs.copyFileSync.mockImplementation(() => undefined); - mockedFs.mkdirSync.mockImplementation(() => undefined); + // Simulate mkdir success + mockedFsPromises.mkdir.mockResolvedValue(undefined); + + // Simulate copyFile success + mockedFsPromises.copyFile.mockResolvedValue(undefined); + mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache}); }); @@ -53,11 +62,11 @@ describe('cacheDependencies', () => { 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), { + expect(mockedFsPromises.access).toHaveBeenCalledWith(sourcePath, fs.constants.F_OK); + expect(mockedFsPromises.mkdir).toHaveBeenCalledWith(path.dirname(targetPath), { recursive: true }); - expect(mockedFs.copyFileSync).toHaveBeenCalledWith(sourcePath, targetPath); + expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(sourcePath, targetPath); expect(mockedCore.info).toHaveBeenCalledWith( `Copied ${sourcePath} to ${targetPath}` ); @@ -68,21 +77,21 @@ describe('cacheDependencies', () => { }); it('warns if the dependency file does not exist', async () => { - mockedFs.existsSync.mockReturnValue(false); + // Simulate file does not exist by rejecting access + mockedFsPromises.access.mockRejectedValue(new Error('file not found')); await cacheDependencies('pip', '3.12'); expect(mockedCore.warning).toHaveBeenCalledWith( expect.stringContaining('does not exist') ); - expect(mockedFs.copyFileSync).not.toHaveBeenCalled(); + expect(mockedFsPromises.copyFile).not.toHaveBeenCalled(); expect(mockRestoreCache).toHaveBeenCalled(); }); it('warns if file copy fails', async () => { - mockedFs.copyFileSync.mockImplementation(() => { - throw new Error('copy failed'); - }); + // Simulate copyFile failure + mockedFsPromises.copyFile.mockRejectedValue(new Error('copy failed')); await cacheDependencies('pip', '3.12'); @@ -97,7 +106,7 @@ describe('cacheDependencies', () => { await cacheDependencies('pip', '3.12'); - expect(mockedFs.copyFileSync).not.toHaveBeenCalled(); + expect(mockedFsPromises.copyFile).not.toHaveBeenCalled(); expect(mockedCore.warning).not.toHaveBeenCalled(); expect(mockRestoreCache).toHaveBeenCalled(); }); diff --git a/dist/setup/index.js b/dist/setup/index.js index 854f9490..76ddee08 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -96931,26 +96931,28 @@ function cacheDependencies(cache, pythonVersion) { const sourcePath = path.resolve(actionPath, 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}`); - } - catch (error) { - core.warning(`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}`); - } + try { + const sourceExists = yield fs_1.default.promises + .access(sourcePath, fs_1.default.constants.F_OK) + .then(() => true) + .catch(() => false); + 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}`); + } + 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}`); } - resolvedDependencyPath = path.relative(workspace, targetPath); - core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`); } const cacheDistributor = (0, cache_factory_1.getCacheDistributor)(cache, pythonVersion, cacheDependencyPath); yield cacheDistributor.restoreCache(); diff --git a/src/setup-python.ts b/src/setup-python.ts index ed76469c..2895d885 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -21,7 +21,6 @@ function isPyPyVersion(versionSpec: string) { function isGraalPyVersion(versionSpec: string) { return versionSpec.startsWith('graalpy'); } - export async function cacheDependencies(cache: string, pythonVersion: string) { const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined; @@ -35,30 +34,34 @@ export async function cacheDependencies(cache: string, pythonVersion: string) { const relativePath = path.relative(actionPath, sourcePath); const targetPath = path.resolve(workspace, relativePath); - if (!fs.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.existsSync(targetDir)) { - fs.mkdirSync(targetDir, {recursive: true}); - } + try { + const sourceExists = await fs.promises + .access(sourcePath, fs.constants.F_OK) + .then(() => true) + .catch(() => false); - fs.copyFileSync(sourcePath, targetPath); - core.info(`Copied ${sourcePath} to ${targetPath}`); - } catch (error) { - core.warning( - `Failed to copy file from ${sourcePath} to ${targetPath}: ${error}` - ); - } + 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 + await fs.promises.mkdir(targetDir, {recursive: true}); + + // Copy file asynchronously + await fs.promises.copyFile(sourcePath, targetPath); + core.info(`Copied ${sourcePath} to ${targetPath}`); } - } - 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 = getCacheDistributor(