initial commit

This commit is contained in:
priya-kinthali 2025-08-08 18:32:34 +05:30
parent 4dd8002ba1
commit fdbef2dddd
13 changed files with 11282 additions and 24803 deletions

View file

@ -1,4 +1,9 @@
{
"devEngines": {
"packageManager": {
"name": "npm"
}
},
"engines": {
"node": "^20.0.0"
}

View file

@ -240,6 +240,15 @@ describe('main tests', () => {
});
describe('cache on GHES', () => {
beforeEach(() => {
// Reset mocks and environment variables before each test
jest.resetModules();
jest.clearAllMocks();
jest.spyOn(core, 'saveState').mockImplementation(() => {});
jest.spyOn(core, 'warning').mockImplementation(() => {});
delete process.env['GITHUB_SERVER_URL'];
});
it('Should throw an error, because cache is not supported', async () => {
inputs['node-version'] = '12';
inputs['cache'] = 'npm';
@ -253,6 +262,9 @@ describe('main tests', () => {
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => false);
// Mock package.json with no packageManager field
jest.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify({}));
await main.run();
expect(warningSpy).toHaveBeenCalledWith(
@ -273,11 +285,107 @@ describe('main tests', () => {
process.env['GITHUB_SERVER_URL'] = '';
isCacheActionAvailable.mockImplementation(() => false);
// Mock package.json with no packageManager field
jest.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify({}));
await main.run();
expect(warningSpy).toHaveBeenCalledWith(
'The runner was not able to contact the cache service. Caching will be skipped'
);
});
it('Should enable caching by default when packageManager field is defined', async () => {
inputs['node-version'] = '12';
inputs['cache'] = undefined; // User did not specify cache
inSpy.mockImplementation(name => inputs[name]);
const toolPath = path.normalize('/cache/node/18.0.0/x64');
findSpy.mockImplementation(() => toolPath);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => true);
// packageManager in package.json
(fs.readFileSync as jest.Mock).mockReturnValue(
JSON.stringify({
packageManager: 'yarn@3.2.0'
})
);
await main.run();
// Should enable caching (saveState called with 'yarn')
expect(core.saveState).toHaveBeenCalledWith(expect.anything(), 'yarn');
});
it('Should enable cache if devEngines.packageManager field is present', async () => {
inputs['node-version'] = '12';
inputs['cache'] = undefined; // Explicitly opt-in via package.json
inSpy.mockImplementation(name => inputs[name]);
findSpy.mockImplementation(() =>
path.normalize('/cache/node/12.16.1/x64')
);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => true);
// devEngines.packageManager in package.json
(fs.readFileSync as jest.Mock).mockReturnValue(
JSON.stringify({
devEngines: {
packageManager: {name: 'pnpm'}
}
})
);
await main.run();
// Should enable caching (saveState called with 'pnpm')
expect(core.saveState).toHaveBeenCalledWith(expect.anything(), 'pnpm');
});
it('Should NOT enable caching when neither cache nor packageManager is set', async () => {
inputs['node-version'] = '12';
inputs['cache'] = undefined;
inSpy.mockImplementation(name => inputs[name]);
findSpy.mockImplementation(() =>
path.normalize('/cache/node/12.16.1/x64')
);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => true);
// package.json with no cache info
(fs.readFileSync as jest.Mock).mockReturnValue(JSON.stringify({}));
await main.run();
expect(core.saveState).not.toHaveBeenCalled();
});
it('Should NOT enable caching when cache is explicitly disabled', async () => {
inputs['node-version'] = '12';
inputs['cache'] = ''; // Explicitly opt-out
inSpy.mockImplementation(name => inputs[name]);
findSpy.mockImplementation(() =>
path.normalize('/cache/node/12.16.1/x64')
);
process.env['GITHUB_SERVER_URL'] = 'https://www.test.com';
isCacheActionAvailable.mockImplementation(() => true);
// packageManager in package.json
(fs.readFileSync as jest.Mock).mockReturnValue(
JSON.stringify({
packageManager: 'yarn@3.2.0'
})
);
await main.run();
// Should NOT enable caching (saveState not called)
expect(core.saveState).not.toHaveBeenCalled();
});
});
});