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');
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<typeof fs>;
const mockedFsPromises = fs.promises as jest.Mocked<typeof fs.promises>;
const mockedCore = core as jest.Mocked<typeof core>;
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();
});

38
dist/setup/index.js vendored
View file

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

View file

@ -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(