Enhance caching in setup-node with automatic package manager detection (#1348)

* setup node in local

* Enhance caching in setup-node with package manager filed detection

* updated with array

* update the field
This commit is contained in:
priya-kinthali
2025-08-26 08:10:12 +05:30
committed by GitHub
parent 5e2628c959
commit d7a11313b5
7 changed files with 167 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ describe('main tests', () => {
let infoSpy: jest.SpyInstance;
let warningSpy: jest.SpyInstance;
let saveStateSpy: jest.SpyInstance;
let inSpy: jest.SpyInstance;
let setOutputSpy: jest.SpyInstance;
let startGroupSpy: jest.SpyInstance;
@@ -53,6 +54,8 @@ describe('main tests', () => {
setOutputSpy.mockImplementation(() => {});
warningSpy = jest.spyOn(core, 'warning');
warningSpy.mockImplementation(() => {});
saveStateSpy = jest.spyOn(core, 'saveState');
saveStateSpy.mockImplementation(() => {});
startGroupSpy = jest.spyOn(core, 'startGroup');
startGroupSpy.mockImplementation(() => {});
endGroupSpy = jest.spyOn(core, 'endGroup');
@@ -280,4 +283,65 @@ describe('main tests', () => {
);
});
});
describe('cache feature tests', () => {
it('Should enable caching with the resolved package manager from packageManager field in package.json when the cache input is not provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
packageManager: 'yarn@3.2.0'
})
);
await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'yarn');
});
it('Should not enable caching if the packageManager field is missing in package.json and the cache input is not provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
//packageManager field is not present
})
);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should skip caching when package-manager-cache is false', async () => {
inputs['package-manager-cache'] = 'false';
inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should enable caching with cache input explicitly provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = 'npm'; // Explicit cache input provided
inSpy.mockImplementation(name => inputs[name]);
isCacheActionAvailable.mockReturnValue(true);
await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm');
});
});
});