logic update

This commit is contained in:
Aparna Jyothi 2025-06-09 13:47:16 +05:30
parent f36c8371b9
commit 0511a23861
3 changed files with 73 additions and 59 deletions

View file

@ -8,11 +8,10 @@ jest.mock('fs', () => {
const actualFs = jest.requireActual('fs'); const actualFs = jest.requireActual('fs');
return { return {
...actualFs, ...actualFs,
copyFileSync: jest.fn(),
existsSync: jest.fn(),
mkdirSync: jest.fn(),
promises: { promises: {
access: jest.fn(), access: jest.fn(),
mkdir: jest.fn(),
copyFile: jest.fn(),
writeFile: jest.fn(), writeFile: jest.fn(),
appendFile: jest.fn() appendFile: jest.fn()
} }
@ -21,7 +20,7 @@ jest.mock('fs', () => {
jest.mock('@actions/core'); jest.mock('@actions/core');
jest.mock('../src/cache-distributions/cache-factory'); jest.mock('../src/cache-distributions/cache-factory');
const mockedFs = fs as jest.Mocked<typeof fs>; const mockedFsPromises = fs.promises as jest.Mocked<typeof fs.promises>;
const mockedCore = core as jest.Mocked<typeof core>; const mockedCore = core as jest.Mocked<typeof core>;
const mockedGetCacheDistributor = getCacheDistributor as jest.Mock; const mockedGetCacheDistributor = getCacheDistributor as jest.Mock;
@ -35,15 +34,25 @@ describe('cacheDependencies', () => {
mockedCore.getInput.mockReturnValue('nested/deps.lock'); 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(); const pathStr = typeof p === 'string' ? p : p.toString();
if (pathStr === '/github/action/nested/deps.lock') return true; if (pathStr === '/github/action/nested/deps.lock') {
if (pathStr === '/github/workspace/nested') return false; // Simulate missing dir return Promise.resolve();
return true; }
// 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); // Simulate mkdir success
mockedFs.mkdirSync.mockImplementation(() => undefined); mockedFsPromises.mkdir.mockResolvedValue(undefined);
// Simulate copyFile success
mockedFsPromises.copyFile.mockResolvedValue(undefined);
mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache}); mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache});
}); });
@ -53,11 +62,11 @@ describe('cacheDependencies', () => {
const sourcePath = path.resolve('/github/action', 'nested/deps.lock'); const sourcePath = path.resolve('/github/action', 'nested/deps.lock');
const targetPath = path.resolve('/github/workspace', 'nested/deps.lock'); const targetPath = path.resolve('/github/workspace', 'nested/deps.lock');
expect(mockedFs.existsSync).toHaveBeenCalledWith(sourcePath); expect(mockedFsPromises.access).toHaveBeenCalledWith(sourcePath, fs.constants.F_OK);
expect(mockedFs.mkdirSync).toHaveBeenCalledWith(path.dirname(targetPath), { expect(mockedFsPromises.mkdir).toHaveBeenCalledWith(path.dirname(targetPath), {
recursive: true recursive: true
}); });
expect(mockedFs.copyFileSync).toHaveBeenCalledWith(sourcePath, targetPath); expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(sourcePath, targetPath);
expect(mockedCore.info).toHaveBeenCalledWith( expect(mockedCore.info).toHaveBeenCalledWith(
`Copied ${sourcePath} to ${targetPath}` `Copied ${sourcePath} to ${targetPath}`
); );
@ -68,21 +77,21 @@ describe('cacheDependencies', () => {
}); });
it('warns if the dependency file does not exist', async () => { 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'); await cacheDependencies('pip', '3.12');
expect(mockedCore.warning).toHaveBeenCalledWith( expect(mockedCore.warning).toHaveBeenCalledWith(
expect.stringContaining('does not exist') expect.stringContaining('does not exist')
); );
expect(mockedFs.copyFileSync).not.toHaveBeenCalled(); expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockRestoreCache).toHaveBeenCalled(); expect(mockRestoreCache).toHaveBeenCalled();
}); });
it('warns if file copy fails', async () => { it('warns if file copy fails', async () => {
mockedFs.copyFileSync.mockImplementation(() => { // Simulate copyFile failure
throw new Error('copy failed'); mockedFsPromises.copyFile.mockRejectedValue(new Error('copy failed'));
});
await cacheDependencies('pip', '3.12'); await cacheDependencies('pip', '3.12');
@ -97,7 +106,7 @@ describe('cacheDependencies', () => {
await cacheDependencies('pip', '3.12'); await cacheDependencies('pip', '3.12');
expect(mockedFs.copyFileSync).not.toHaveBeenCalled(); expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockedCore.warning).not.toHaveBeenCalled(); expect(mockedCore.warning).not.toHaveBeenCalled();
expect(mockRestoreCache).toHaveBeenCalled(); expect(mockRestoreCache).toHaveBeenCalled();
}); });

38
dist/setup/index.js vendored
View file

@ -96931,26 +96931,28 @@ function cacheDependencies(cache, pythonVersion) {
const sourcePath = path.resolve(actionPath, cacheDependencyPath); const sourcePath = path.resolve(actionPath, cacheDependencyPath);
const relativePath = path.relative(actionPath, sourcePath); const relativePath = path.relative(actionPath, sourcePath);
const targetPath = path.resolve(workspace, relativePath); const targetPath = path.resolve(workspace, relativePath);
if (!fs_1.default.existsSync(sourcePath)) { try {
core.warning(`The resolved cache-dependency-path does not exist: ${sourcePath}`); const sourceExists = yield fs_1.default.promises
} .access(sourcePath, fs_1.default.constants.F_OK)
else { .then(() => true)
if (sourcePath !== targetPath) { .catch(() => false);
try { if (!sourceExists) {
const targetDir = path.dirname(targetPath); core.warning(`The resolved cache-dependency-path does not exist: ${sourcePath}`);
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}`);
}
} }
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); const cacheDistributor = (0, cache_factory_1.getCacheDistributor)(cache, pythonVersion, cacheDependencyPath);
yield cacheDistributor.restoreCache(); yield cacheDistributor.restoreCache();

View file

@ -21,7 +21,6 @@ function isPyPyVersion(versionSpec: string) {
function isGraalPyVersion(versionSpec: string) { function isGraalPyVersion(versionSpec: string) {
return versionSpec.startsWith('graalpy'); return versionSpec.startsWith('graalpy');
} }
export async function cacheDependencies(cache: string, pythonVersion: string) { export async function cacheDependencies(cache: string, pythonVersion: string) {
const cacheDependencyPath = const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined; 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 relativePath = path.relative(actionPath, sourcePath);
const targetPath = path.resolve(workspace, relativePath); const targetPath = path.resolve(workspace, relativePath);
if (!fs.existsSync(sourcePath)) { try {
core.warning( const sourceExists = await fs.promises
`The resolved cache-dependency-path does not exist: ${sourcePath}` .access(sourcePath, fs.constants.F_OK)
); .then(() => true)
} else { .catch(() => false);
if (sourcePath !== targetPath) {
try {
const targetDir = path.dirname(targetPath);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, {recursive: true});
}
fs.copyFileSync(sourcePath, targetPath); if (!sourceExists) {
core.info(`Copied ${sourcePath} to ${targetPath}`); core.warning(
} catch (error) { `The resolved cache-dependency-path does not exist: ${sourcePath}`
core.warning( );
`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}` } 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); resolvedDependencyPath = path.relative(workspace, targetPath);
core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`); core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`);
} catch (error) {
core.warning(
`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}`
);
}
} }
const cacheDistributor = getCacheDistributor( const cacheDistributor = getCacheDistributor(