logic update for robust

This commit is contained in:
Aparna Jyothi 2025-06-09 13:34:23 +05:30
parent 6090563d91
commit 0f4747fd79
3 changed files with 33 additions and 12 deletions

View file

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

7
dist/setup/index.js vendored
View file

@ -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}`);
}

View file

@ -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) {