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();
}); });

26
dist/setup/index.js vendored
View file

@ -96931,27 +96931,29 @@ 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 {
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}`); core.warning(`The resolved cache-dependency-path does not exist: ${sourcePath}`);
} }
else { else if (sourcePath !== targetPath) {
if (sourcePath !== targetPath) {
try {
const targetDir = path.dirname(targetPath); const targetDir = path.dirname(targetPath);
if (!fs_1.default.existsSync(targetDir)) { // Create target directory if it doesn't exist
fs_1.default.mkdirSync(targetDir, { recursive: true }); yield fs_1.default.promises.mkdir(targetDir, { recursive: true });
} // Copy file asynchronously
fs_1.default.copyFileSync(sourcePath, targetPath); yield fs_1.default.promises.copyFile(sourcePath, targetPath);
core.info(`Copied ${sourcePath} to ${targetPath}`); core.info(`Copied ${sourcePath} to ${targetPath}`);
} }
resolvedDependencyPath = path.relative(workspace, targetPath);
core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`);
}
catch (error) { catch (error) {
core.warning(`Failed to copy file from ${sourcePath} to ${targetPath}: ${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,31 +34,35 @@ 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 {
const sourceExists = await fs.promises
.access(sourcePath, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
if (!sourceExists) {
core.warning( core.warning(
`The resolved cache-dependency-path does not exist: ${sourcePath}` `The resolved cache-dependency-path does not exist: ${sourcePath}`
); );
} else { } else if (sourcePath !== targetPath) {
if (sourcePath !== targetPath) {
try {
const targetDir = path.dirname(targetPath); const targetDir = path.dirname(targetPath);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, {recursive: true}); // 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}`);
} }
fs.copyFileSync(sourcePath, targetPath); resolvedDependencyPath = path.relative(workspace, targetPath);
core.info(`Copied ${sourcePath} to ${targetPath}`); core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`);
} catch (error) { } catch (error) {
core.warning( core.warning(
`Failed to copy file from ${sourcePath} to ${targetPath}: ${error}` `Failed to copy file from ${sourcePath} to ${targetPath}: ${error}`
); );
} }
} }
}
resolvedDependencyPath = path.relative(workspace, targetPath);
core.info(`Resolved cache-dependency-path: ${resolvedDependencyPath}`);
}
const cacheDistributor = getCacheDistributor( const cacheDistributor = getCacheDistributor(
cache, cache,