1
0
Fork 0
mirror of https://github.com/actions/setup-node synced 2024-11-09 23:22:41 +00:00

refactor: move volta logic

This commit is contained in:
Jef LeCompte 2022-06-29 15:37:53 -07:00
parent 9aa86428fe
commit dbfbe9b6da
No known key found for this signature in database
GPG key ID: 7F5BA322B5DC170C
4 changed files with 40 additions and 24 deletions

View file

@ -560,7 +560,7 @@ describe('setup-node', () => {
expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0); expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0);
}); });
it('reads node-version-file if provided', async () => { it('reads node-version-file if provided (.nvmrc)', async () => {
// Arrange // Arrange
const versionSpec = 'v14'; const versionSpec = 'v14';
const versionFile = '.nvmrc'; const versionFile = '.nvmrc';
@ -572,6 +572,7 @@ describe('setup-node', () => {
existsSpy.mockImplementationOnce( existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile) input => input === path.join(__dirname, 'data', versionFile)
); );
// Act // Act
await main.run(); await main.run();
@ -584,22 +585,37 @@ describe('setup-node', () => {
); );
}); });
it('reads node-version-file if provided with volta', async () => { it('reads node-version-file if provided (package.json, volta)', async () => {
// Arrange // Arrange
const expectedVersionSpec = '16.15.1'; const versionSpec = `{
"name": "test",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "echo test"
},
"volta": {
"node": "16.15.1"
}
}
`;
const versionFile = 'package.json'; const versionFile = 'package.json';
const expectedVersionSpec = '16.15.1';
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data'); process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');
inputs['node-version-file'] = 'volta'; inputs['node-version-file'] = versionFile;
parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
existsSpy.mockImplementationOnce( existsSpy.mockImplementationOnce(
input => input === path.join(__dirname, 'data', versionFile) input => input === path.join(__dirname, 'data', versionFile)
); );
// Act // Act
await main.run(); await main.run();
// Assert // Assert
expect(existsSpy).toHaveBeenCalledTimes(1); expect(existsSpy).toHaveBeenCalledTimes(1);
expect(existsSpy).toHaveReturnedWith(true); expect(existsSpy).toHaveReturnedWith(true);
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
expect(logSpy).toHaveBeenCalledWith( expect(logSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}` `Resolved ${versionFile} as ${expectedVersionSpec}`
); );

18
dist/setup/index.js vendored
View file

@ -71768,7 +71768,13 @@ function translateArchToDistUrl(arch) {
} }
} }
function parseNodeVersionFile(contents) { function parseNodeVersionFile(contents) {
let nodeVersion = contents.trim(); let nodeVersion;
if (contents.includes('volta')) {
nodeVersion = JSON.parse(contents).volta.node;
}
else {
nodeVersion = contents.trim();
}
if (/^v\d/.test(nodeVersion)) { if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1); nodeVersion = nodeVersion.substring(1);
} }
@ -71862,8 +71868,7 @@ function run() {
exports.run = run; exports.run = run;
function resolveVersionInput() { function resolveVersionInput() {
let version = core.getInput('node-version'); let version = core.getInput('node-version');
const nodeVersionFile = core.getInput('node-version-file'); const versionFileInput = core.getInput('node-version-file');
const versionFileInput = nodeVersionFile === 'volta' ? 'package.json' : nodeVersionFile;
if (version && versionFileInput) { if (version && versionFileInput) {
core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used'); core.warning('Both node-version and node-version-file inputs are specified, only node-version will be used');
} }
@ -71875,12 +71880,7 @@ function resolveVersionInput() {
if (!fs_1.default.existsSync(versionFilePath)) { if (!fs_1.default.existsSync(versionFilePath)) {
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`); throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
} }
if (nodeVersionFile === 'volta') { version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
version = JSON.parse(fs_1.default.readFileSync(versionFilePath, 'utf8')).volta.node;
}
else {
version = installer.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
}
core.info(`Resolved ${versionFileInput} as ${version}`); core.info(`Resolved ${versionFileInput} as ${version}`);
} }
return version; return version;

View file

@ -495,7 +495,13 @@ function translateArchToDistUrl(arch: string): string {
} }
export function parseNodeVersionFile(contents: string): string { export function parseNodeVersionFile(contents: string): string {
let nodeVersion = contents.trim(); let nodeVersion;
if (contents.includes('volta')) {
nodeVersion = JSON.parse(contents).volta.node;
} else {
nodeVersion = contents.trim();
}
if (/^v\d/.test(nodeVersion)) { if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1); nodeVersion = nodeVersion.substring(1);

View file

@ -65,9 +65,7 @@ export async function run() {
function resolveVersionInput(): string { function resolveVersionInput(): string {
let version = core.getInput('node-version'); let version = core.getInput('node-version');
const nodeVersionFile = core.getInput('node-version-file'); const versionFileInput = core.getInput('node-version-file');
const versionFileInput =
nodeVersionFile === 'volta' ? 'package.json' : nodeVersionFile;
if (version && versionFileInput) { if (version && versionFileInput) {
core.warning( core.warning(
@ -91,13 +89,9 @@ function resolveVersionInput(): string {
); );
} }
if (nodeVersionFile === 'volta') { version = installer.parseNodeVersionFile(
version = JSON.parse(fs.readFileSync(versionFilePath, 'utf8')).volta.node; fs.readFileSync(versionFilePath, 'utf8')
} else { );
version = installer.parseNodeVersionFile(
fs.readFileSync(versionFilePath, 'utf8')
);
}
core.info(`Resolved ${versionFileInput} as ${version}`); core.info(`Resolved ${versionFileInput} as ${version}`);
} }