Respect package.json's engines.node field when used as a node-version-file (#485)

* Allow reading 'package.json' as node-version-file

* Run 'npm run build'

* Read package.json contents directly during tests

- this eliminates OS-specific line-ending issues

* Run project Prettier 💅
This commit is contained in:
mannie.exe
2022-07-21 10:17:21 -07:00
committed by GitHub
parent 2fddd8803e
commit 2a814b57e1
4 changed files with 66 additions and 14 deletions

View File

@ -0,0 +1,5 @@
{
"engines": {
"node": ">=14.0.0"
}
}

View File

@ -591,6 +591,33 @@ describe('setup-node', () => {
);
});
it('reads package.json as node-version-file if provided', async () => {
// Arrange
const versionSpec = fs.readFileSync(
path.join(__dirname, 'data/package.json'),
'utf-8'
);
const versionFile = 'package.json';
const expectedVersionSpec = '14';
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
inputs['node-version-file'] = versionFile;
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile)
);
// Act
await main.run();
// Assert
expect(existsSpy).toHaveBeenCalledTimes(1);
expect(existsSpy).toHaveReturnedWith(true);
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
expect(logSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}`
);
});
it('both node-version-file and node-version are provided', async () => {
inputs['node-version'] = '12';
const versionSpec = 'v14';