Skip to content
Open
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
29 changes: 29 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,35 @@ describe('main tests', () => {

expect(util.getNodeVersionFromFile('file')).toBe(expected);
});

each`
name | contents | expected
${'ignores blank lines and comments'} | ${'# use the version below\n\n 20.16.0 # maintenance release\n'} | ${'20.16.0'}
${'strips an inline comment'} | ${'v20.16.0 # maintenance release'} | ${'20.16.0'}
${'preserves an alias before a comment'} | ${'lts/* # use the latest LTS'} | ${'lts/*'}
${'rejects a comment-only file'} | ${'# comment only'} | ${null}
${'rejects comments and whitespace only'} | ${'#\r\n # another comment\r\n'} | ${null}
`.it('$name', ({contents, expected}: any) => {
const existsSpy = jest.spyOn(fs, 'existsSync');
existsSpy.mockImplementation(() => true);

const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(() => contents);

expect(util.getNodeVersionFromFile('.nvmrc')).toBe(expected);
});

it('preserves .tool-versions parsing', () => {
const existsSpy = jest.spyOn(fs, 'existsSync');
existsSpy.mockImplementation(() => true);

const readFileSpy = jest.spyOn(fs, 'readFileSync');
readFileSpy.mockImplementation(
() => 'ruby 3.3.0\nnodejs 20.16.0\npython 3.12.0'
);

expect(util.getNodeVersionFromFile('.tool-versions')).toBe('20.16.0');
});
});

describe('printEnvDetailsAndSetOutput', () => {
Expand Down
15 changes: 13 additions & 2 deletions dist/cache-save/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92709,8 +92709,19 @@ function getNodeVersionFromFile(versionFilePath) {
catch {
core.info('Node version file is not JSON file');
}
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
return found?.groups?.version ?? contents.trim();
let versionFileContents = contents;
if (path.basename(versionFilePath) === '.nvmrc') {
versionFileContents = contents
.split(/\r?\n/)
.map(line => line.replace(/#.*/, '').trim())
.filter(Boolean)
.join('\n');
if (!versionFileContents) {
return null;
}
}
const found = versionFileContents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
return found?.groups?.version ?? versionFileContents.trim();
}
async function printEnvDetailsAndSetOutput() {
core.startGroup('Environment details');
Expand Down
15 changes: 13 additions & 2 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98103,8 +98103,19 @@ function getNodeVersionFromFile(versionFilePath) {
catch {
core_info('Node version file is not JSON file');
}
const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
return found?.groups?.version ?? contents.trim();
let versionFileContents = contents;
if (external_path_default().basename(versionFilePath) === '.nvmrc') {
versionFileContents = contents
.split(/\r?\n/)
.map(line => line.replace(/#.*/, '').trim())
.filter(Boolean)
.join('\n');
if (!versionFileContents) {
return null;
}
}
const found = versionFileContents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
return found?.groups?.version ?? versionFileContents.trim();
}
async function printEnvDetailsAndSetOutput() {
startGroup('Environment details');
Expand Down
20 changes: 18 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,24 @@ export function getNodeVersionFromFile(versionFilePath: string): string | null {
core.info('Node version file is not JSON file');
}

const found = contents.match(/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m);
return found?.groups?.version ?? contents.trim();
let versionFileContents = contents;

if (path.basename(versionFilePath) === '.nvmrc') {
versionFileContents = contents
.split(/\r?\n/)
.map(line => line.replace(/#.*/, '').trim())
.filter(Boolean)
.join('\n');

if (!versionFileContents) {
return null;
}
}

const found = versionFileContents.match(
/^(?:node(js)?\s+)?v?(?<version>[^\s]+)$/m
);
return found?.groups?.version ?? versionFileContents.trim();
}

export async function printEnvDetailsAndSetOutput() {
Expand Down