Compare commits

..

1 Commits

Author SHA1 Message Date
Priya Gupta
b6e0cf734d
Merge 7024c214c6 into 89d709d423 2025-09-23 07:20:42 +00:00
3 changed files with 27 additions and 141 deletions

View File

@ -285,12 +285,13 @@ describe('main tests', () => {
}); });
describe('cache feature tests', () => { describe('cache feature tests', () => {
it('Should enable caching when packageManager is npm and cache input is not provided', async () => { 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['package-manager-cache'] = 'true';
inputs['cache'] = ''; inputs['cache'] = ''; // No cache input is provided
isCacheActionAvailable.mockImplementation(() => true); isCacheActionAvailable.mockImplementation(() => true);
inSpy.mockImplementation(name => inputs[name]); inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync'); const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() => readFileSpy.mockImplementation(() =>
JSON.stringify({ JSON.stringify({
@ -303,106 +304,16 @@ describe('main tests', () => {
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm'); expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm');
}); });
it('Should enable caching when devEngines.packageManager.name is "npm" and cache input is not provided', async () => { 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['package-manager-cache'] = 'true';
inputs['cache'] = ''; inputs['cache'] = ''; // No cache input is provided
isCacheActionAvailable.mockImplementation(() => true);
inSpy.mockImplementation(name => inputs[name]); inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync'); const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() => readFileSpy.mockImplementation(() =>
JSON.stringify({ JSON.stringify({
devEngines: { //packageManager field is not present
packageManager: {name: 'npm'}
}
})
);
await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm');
});
it('Should enable caching when devEngines.packageManager is array and one entry has name "npm"', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = '';
isCacheActionAvailable.mockImplementation(() => true);
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
devEngines: {
packageManager: [{name: 'pnpm'}, {name: 'npm'}]
}
})
);
await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm');
});
it('Should not enable caching if packageManager is "pnpm@8.0.0" and cache input is not provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = '';
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
packageManager: 'pnpm@8.0.0'
})
);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should not enable caching if devEngines.packageManager.name is "pnpm"', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = '';
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
devEngines: {
packageManager: {name: 'pnpm'}
}
})
);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should not enable caching if devEngines.packageManager is array without "npm"', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = '';
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
devEngines: {
packageManager: [{name: 'pnpm'}, {name: 'yarn'}]
}
})
);
await main.run();
expect(saveStateSpy).not.toHaveBeenCalled();
});
it('Should not enable caching if packageManager field is missing in package.json and cache input is not provided', async () => {
inputs['package-manager-cache'] = 'true';
inputs['cache'] = '';
inSpy.mockImplementation(name => inputs[name]);
const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() =>
JSON.stringify({
// packageManager field is not present
}) })
); );
@ -413,18 +324,24 @@ describe('main tests', () => {
it('Should skip caching when package-manager-cache is false', async () => { it('Should skip caching when package-manager-cache is false', async () => {
inputs['package-manager-cache'] = 'false'; inputs['package-manager-cache'] = 'false';
inputs['cache'] = ''; inputs['cache'] = ''; // No cache input is provided
inSpy.mockImplementation(name => inputs[name]); inSpy.mockImplementation(name => inputs[name]);
await main.run(); await main.run();
expect(saveStateSpy).not.toHaveBeenCalled(); expect(saveStateSpy).not.toHaveBeenCalled();
}); });
it('Should enable caching with cache input explicitly provided', async () => { it('Should enable caching with cache input explicitly provided', async () => {
inputs['package-manager-cache'] = 'true'; inputs['package-manager-cache'] = 'true';
inputs['cache'] = 'npm'; inputs['cache'] = 'npm'; // Explicit cache input provided
inSpy.mockImplementation(name => inputs[name]); inSpy.mockImplementation(name => inputs[name]);
isCacheActionAvailable.mockImplementation(() => true); isCacheActionAvailable.mockImplementation(() => true);
await main.run(); await main.run();
expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm'); expect(saveStateSpy).toHaveBeenCalledWith(expect.anything(), 'npm');
}); });
}); });

27
dist/setup/index.js vendored
View File

@ -99838,32 +99838,17 @@ function resolveVersionInput() {
return version; return version;
} }
function getNameFromPackageManagerField() { function getNameFromPackageManagerField() {
var _a; // Enable auto-cache for npm
const npmRegex = /^(\^)?npm(@.*)?$/; // matches "npm", "npm@...", "^npm@..."
try { try {
const packageJson = JSON.parse(fs_1.default.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'package.json'), 'utf-8')); const packageJson = JSON.parse(fs_1.default.readFileSync(path.join(process.env.GITHUB_WORKSPACE, 'package.json'), 'utf-8'));
// Check devEngines.packageManager first (object or array) const pm = packageJson.packageManager;
const devPM = (_a = packageJson === null || packageJson === void 0 ? void 0 : packageJson.devEngines) === null || _a === void 0 ? void 0 : _a.packageManager; if (typeof pm === 'string') {
if (devPM) { const match = pm.match(/^(?:\^)?(npm)@/);
if (Array.isArray(devPM)) { return match ? match[1] : undefined;
for (const obj of devPM) {
if (typeof (obj === null || obj === void 0 ? void 0 : obj.name) === 'string' && npmRegex.test(obj.name)) {
return 'npm';
}
}
}
else if (typeof (devPM === null || devPM === void 0 ? void 0 : devPM.name) === 'string' && npmRegex.test(devPM.name)) {
return 'npm';
}
}
// Check top-level packageManager
const topLevelPM = packageJson === null || packageJson === void 0 ? void 0 : packageJson.packageManager;
if (typeof topLevelPM === 'string' && npmRegex.test(topLevelPM)) {
return 'npm';
} }
return undefined; return undefined;
} }
catch (_b) { catch (err) {
return undefined; return undefined;
} }
} }

View File

@ -138,7 +138,7 @@ function resolveVersionInput(): string {
} }
export function getNameFromPackageManagerField(): string | undefined { export function getNameFromPackageManagerField(): string | undefined {
const npmRegex = /^(\^)?npm(@.*)?$/; // matches "npm", "npm@...", "^npm@..." // Enable auto-cache for npm
try { try {
const packageJson = JSON.parse( const packageJson = JSON.parse(
fs.readFileSync( fs.readFileSync(
@ -146,29 +146,13 @@ export function getNameFromPackageManagerField(): string | undefined {
'utf-8' 'utf-8'
) )
); );
const pm = packageJson.packageManager;
// Check devEngines.packageManager first (object or array) if (typeof pm === 'string') {
const devPM = packageJson?.devEngines?.packageManager; const match = pm.match(/^(?:\^)?(npm)@/);
if (devPM) { return match ? match[1] : undefined;
if (Array.isArray(devPM)) {
for (const obj of devPM) {
if (typeof obj?.name === 'string' && npmRegex.test(obj.name)) {
return 'npm';
}
}
} else if (typeof devPM?.name === 'string' && npmRegex.test(devPM.name)) {
return 'npm';
}
} }
// Check top-level packageManager
const topLevelPM = packageJson?.packageManager;
if (typeof topLevelPM === 'string' && npmRegex.test(topLevelPM)) {
return 'npm';
}
return undefined; return undefined;
} catch { } catch (err) {
return undefined; return undefined;
} }
} }