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, ...actualFs,
copyFileSync: jest.fn(), copyFileSync: jest.fn(),
existsSync: jest.fn(), existsSync: jest.fn(),
mkdirSync: jest.fn(),
promises: { promises: {
access: jest.fn(), access: jest.fn(),
writeFile: jest.fn(), writeFile: jest.fn(),
@ -32,25 +33,37 @@ describe('cacheDependencies', () => {
process.env.GITHUB_ACTION_PATH = '/github/action'; process.env.GITHUB_ACTION_PATH = '/github/action';
process.env.GITHUB_WORKSPACE = '/github/workspace'; process.env.GITHUB_WORKSPACE = '/github/workspace';
mockedCore.getInput.mockReturnValue('deps.lock'); mockedCore.getInput.mockReturnValue('nested/deps.lock');
mockedFs.existsSync.mockReturnValue(true);
mockedFs.copyFileSync.mockImplementation(() => {}); 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}); 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'); await cacheDependencies('pip', '3.12');
const sourcePath = path.resolve('/github/action', 'deps.lock'); const sourcePath = path.resolve('/github/action', 'nested/deps.lock');
const targetPath = path.resolve('/github/workspace', 'deps.lock'); const targetPath = path.resolve('/github/workspace', 'nested/deps.lock');
expect(mockedFs.existsSync).toHaveBeenCalledWith(sourcePath); expect(mockedFs.existsSync).toHaveBeenCalledWith(sourcePath);
expect(mockedFs.mkdirSync).toHaveBeenCalledWith(
path.dirname(targetPath),
{recursive: true}
);
expect(mockedFs.copyFileSync).toHaveBeenCalledWith(sourcePath, targetPath); expect(mockedFs.copyFileSync).toHaveBeenCalledWith(sourcePath, targetPath);
expect(mockedCore.info).toHaveBeenCalledWith( expect(mockedCore.info).toHaveBeenCalledWith(
`Copied ${sourcePath} to ${targetPath}` `Copied ${sourcePath} to ${targetPath}`
); );
expect(mockedCore.info).toHaveBeenCalledWith( expect(mockedCore.info).toHaveBeenCalledWith(
`Resolved cache-dependency-path: deps.lock` `Resolved cache-dependency-path: nested/deps.lock`
); );
expect(mockRestoreCache).toHaveBeenCalled(); 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 actionPath = process.env.GITHUB_ACTION_PATH || '';
const workspace = process.env.GITHUB_WORKSPACE || process.cwd(); const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
const sourcePath = path.resolve(actionPath, cacheDependencyPath); 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)) { if (!fs_1.default.existsSync(sourcePath)) {
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 { 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); fs_1.default.copyFileSync(sourcePath, targetPath);
core.info(`Copied ${sourcePath} to ${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 workspace = process.env.GITHUB_WORKSPACE || process.cwd();
const sourcePath = path.resolve(actionPath, cacheDependencyPath); const sourcePath = path.resolve(actionPath, cacheDependencyPath);
const targetPath = path.resolve( const relativePath = path.relative(actionPath, sourcePath);
workspace, const targetPath = path.resolve(workspace, relativePath);
path.basename(cacheDependencyPath)
);
if (!fs.existsSync(sourcePath)) { if (!fs.existsSync(sourcePath)) {
core.warning( core.warning(
@ -44,6 +42,11 @@ export async function cacheDependencies(cache: string, pythonVersion: string) {
} else { } else {
if (sourcePath !== targetPath) { if (sourcePath !== targetPath) {
try { try {
const targetDir = path.dirname(targetPath);
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, {recursive: true});
}
fs.copyFileSync(sourcePath, targetPath); fs.copyFileSync(sourcePath, targetPath);
core.info(`Copied ${sourcePath} to ${targetPath}`); core.info(`Copied ${sourcePath} to ${targetPath}`);
} catch (error) { } catch (error) {