This commit is contained in:
aparnajyothi-y 2025-08-26 18:54:01 +02:00 committed by GitHub
commit bdbd198b4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 103 additions and 75 deletions

View file

@ -27,51 +27,35 @@ const mockedGetCacheDistributor = getCacheDistributor as jest.Mock;
describe('cacheDependencies', () => {
const mockRestoreCache = jest.fn();
const actionPath = path.resolve('/github/action');
const workspacePath = path.resolve('/github/workspace');
const relPath = path.join('nested', 'deps.lock');
const sourcePath = path.resolve(actionPath, relPath);
const targetPath = path.resolve(workspacePath, relPath);
beforeEach(() => {
jest.clearAllMocks();
process.env.GITHUB_ACTION_PATH = '/github/action';
process.env.GITHUB_WORKSPACE = '/github/workspace';
mockedCore.getInput.mockReturnValue('nested/deps.lock');
process.env.GITHUB_ACTION_PATH = actionPath;
process.env.GITHUB_WORKSPACE = workspacePath;
// 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 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();
});
// Simulate mkdir success
mockedFsPromises.mkdir.mockResolvedValue(undefined);
// Simulate copyFile success
mockedFsPromises.copyFile.mockResolvedValue(undefined);
mockedCore.getInput.mockReturnValue(relPath);
mockedCore.getBooleanInput.mockReturnValue(false);
mockedGetCacheDistributor.mockReturnValue({restoreCache: mockRestoreCache});
mockedFsPromises.mkdir.mockResolvedValue(undefined);
mockedFsPromises.copyFile.mockResolvedValue(undefined);
});
it('copies the dependency file and resolves the path with directory structure', async () => {
it('copies the file if source exists and target does not', async () => {
mockedFsPromises.access.mockImplementation(async p => {
if (p === sourcePath) return Promise.resolve();
throw new Error('target does not exist');
});
await cacheDependencies('pip', '3.12');
const sourcePath = path.resolve('/github/action', 'nested/deps.lock');
const targetPath = path.resolve('/github/workspace', 'nested/deps.lock');
expect(mockedFsPromises.access).toHaveBeenCalledWith(
sourcePath,
fs.constants.F_OK
);
expect(mockedFsPromises.mkdir).toHaveBeenCalledWith(
path.dirname(targetPath),
{
recursive: true
}
);
expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(
sourcePath,
targetPath
@ -79,15 +63,40 @@ describe('cacheDependencies', () => {
expect(mockedCore.info).toHaveBeenCalledWith(
`Copied ${sourcePath} to ${targetPath}`
);
expect(mockedCore.info).toHaveBeenCalledWith(
`Resolved cache-dependency-path: nested/deps.lock`
);
expect(mockRestoreCache).toHaveBeenCalled();
});
it('warns if the dependency file does not exist', async () => {
// Simulate file does not exist by rejecting access
mockedFsPromises.access.mockRejectedValue(new Error('file not found'));
it('overwrites file if target exists and overwrite is true', async () => {
mockedCore.getBooleanInput.mockReturnValue(true);
mockedFsPromises.access.mockResolvedValue(); // both exist
await cacheDependencies('pip', '3.12');
expect(mockedFsPromises.copyFile).toHaveBeenCalledWith(
sourcePath,
targetPath
);
expect(mockedCore.info).toHaveBeenCalledWith(
`Overwrote ${sourcePath} to ${targetPath}`
);
});
it('skips copy if file exists and overwrite is false', async () => {
mockedCore.getBooleanInput.mockReturnValue(false);
mockedFsPromises.access.mockResolvedValue(); // both exist
await cacheDependencies('pip', '3.12');
expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockedCore.info).toHaveBeenCalledWith(
expect.stringContaining('Skipped copying')
);
});
it('logs warning if source file does not exist', async () => {
mockedFsPromises.access.mockImplementation(async p => {
if (p === sourcePath) throw new Error('not found');
return Promise.resolve();
});
await cacheDependencies('pip', '3.12');
@ -95,11 +104,13 @@ describe('cacheDependencies', () => {
expect.stringContaining('does not exist')
);
expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockRestoreCache).toHaveBeenCalled();
});
it('warns if file copy fails', async () => {
// Simulate copyFile failure
it('logs warning if copyFile fails', async () => {
mockedFsPromises.access.mockImplementation(async p => {
if (p === sourcePath) return Promise.resolve();
throw new Error('target missing');
});
mockedFsPromises.copyFile.mockRejectedValue(new Error('copy failed'));
await cacheDependencies('pip', '3.12');
@ -107,43 +118,30 @@ describe('cacheDependencies', () => {
expect(mockedCore.warning).toHaveBeenCalledWith(
expect.stringContaining('Failed to copy file')
);
expect(mockRestoreCache).toHaveBeenCalled();
});
it('skips path logic if no input is provided', async () => {
it('skips everything if cache-dependency-path is not provided', async () => {
mockedCore.getInput.mockReturnValue('');
await cacheDependencies('pip', '3.12');
expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockedCore.warning).not.toHaveBeenCalled();
expect(mockRestoreCache).toHaveBeenCalled();
});
it('does not copy if dependency file is already inside the workspace but still sets resolved path', async () => {
// Simulate cacheDependencyPath inside workspace
it('does not copy if source and target are the same path', async () => {
mockedCore.getInput.mockReturnValue('deps.lock');
const samePath = path.resolve('/github/workspace', 'deps.lock');
process.env.GITHUB_ACTION_PATH = workspacePath;
process.env.GITHUB_WORKSPACE = workspacePath;
// Override sourcePath and targetPath to be equal
const actionPath = '/github/workspace'; // same path for action and workspace
process.env.GITHUB_ACTION_PATH = actionPath;
process.env.GITHUB_WORKSPACE = actionPath;
// access resolves to simulate file exists
mockedFsPromises.access.mockResolvedValue();
await cacheDependencies('pip', '3.12');
const sourcePath = path.resolve(actionPath, 'deps.lock');
const targetPath = sourcePath; // same path
expect(mockedFsPromises.copyFile).not.toHaveBeenCalled();
expect(mockedCore.info).toHaveBeenCalledWith(
`Dependency file is already inside the workspace: ${sourcePath}`
`Dependency file is already inside the workspace: ${samePath}`
);
expect(mockedCore.info).toHaveBeenCalledWith(
`Resolved cache-dependency-path: deps.lock`
);
expect(mockRestoreCache).toHaveBeenCalled();
});
});