Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using .tool-versions for node-version-file #445

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions __tests__/data/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarn 1.22.4
nodejs 17.6.0
26 changes: 26 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,32 @@ describe('setup-node', () => {
);
});

it('reads node-version-file of `asdf` if provided', async () => {
// Arrange
const versionSpec = `yarn 1.22.4
nodejs 17.6.0
`;
const versionFile = '.tool-versions';
const expectedVersionSpec = '17.6.0';
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';
Expand Down
8 changes: 8 additions & 0 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,14 @@ function translateArchToDistUrl(arch: string): string {
export function parseNodeVersionFile(contents: string): string {
let nodeVersion = contents.trim();

if (contents.indexOf('nodejs') !== -1) {
const lineWithNodeJsVersions = contents.match(/^nodejs.*$/gm);
const firstLineWithNodeJsVersion =
lineWithNodeJsVersions && lineWithNodeJsVersions[0];
nodeVersion =
firstLineWithNodeJsVersion?.replace('nodejs', '').trim() || nodeVersion;
}

if (/^v\d/.test(nodeVersion)) {
nodeVersion = nodeVersion.substring(1);
}
Expand Down