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

Support .node-version and .nvmrc by default #684

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions __tests__/data/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16
55 changes: 47 additions & 8 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,6 @@ describe('main tests', () => {
expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0);
}, 10000);

it('not used if node-version-file not provided', async () => {
// Act
await main.run();

// Assert
expect(parseNodeVersionSpy).toHaveBeenCalledTimes(0);
});

it('reads node-version-file if provided', async () => {
// Arrange
const versionSpec = 'v14';
Expand Down Expand Up @@ -215,6 +207,53 @@ describe('main tests', () => {
);
}, 10000);

it('reads .node-version if node-version and node-version-file were not provided', async () => {
// Arrange
const versionSpec = 'v16';
const versionFile = '.node-version';
const expectedVersionSpec = '16';
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');

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(infoSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}`
);
}, 10000);

it('reads .nvmrc if node-version and node-version-file were not provided', async () => {
// Arrange
const versionSpec = 'v14';
const versionFile = '.nvmrc';
const expectedVersionSpec = '14';
process.env['GITHUB_WORKSPACE'] = path.join(__dirname, 'data');

parseNodeVersionSpy.mockImplementation(() => expectedVersionSpec);
existsSpy.mockImplementation(
input => input === path.join(__dirname, 'data', versionFile)
);

// Act
await main.run();

// Assert
expect(existsSpy).toHaveBeenCalledTimes(2);
expect(parseNodeVersionSpy).toHaveBeenCalledWith(versionSpec);
expect(infoSpy).toHaveBeenCalledWith(
`Resolved ${versionFile} as ${expectedVersionSpec}`
);
}, 10000);

it('both node-version-file and node-version are provided', async () => {
inputs['node-version'] = '12';
const versionSpec = 'v14';
Expand Down
17 changes: 14 additions & 3 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74003,11 +74003,22 @@ function resolveVersionInput() {
}
if (versionFileInput) {
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFileInput);
if (!fs_1.default.existsSync(versionFilePath)) {
if (fs_1.default.existsSync(versionFilePath)) {
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFileInput} as ${version}`);
return version;
}
else {
throw new Error(`The specified node version file at: ${versionFilePath} does not exist`);
}
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFileInput} as ${version}`);
}
for (const versionFile of ['.node-version', '.nvmrc']) {
const versionFilePath = path.join(process.env.GITHUB_WORKSPACE, versionFile);
if (fs_1.default.existsSync(versionFilePath)) {
version = util_1.parseNodeVersionFile(fs_1.default.readFileSync(versionFilePath, 'utf8'));
core.info(`Resolved ${versionFile} as ${version}`);
return version;
}
}
return version;
}
Expand Down
23 changes: 20 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,32 @@ function resolveVersionInput(): string {
versionFileInput
);

if (!fs.existsSync(versionFilePath)) {
if (fs.existsSync(versionFilePath)) {
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));

core.info(`Resolved ${versionFileInput} as ${version}`);

return version;
} else {
throw new Error(
`The specified node version file at: ${versionFilePath} does not exist`
);
}
}

version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
for (const versionFile of ['.node-version', '.nvmrc']) {
const versionFilePath = path.join(
process.env.GITHUB_WORKSPACE!,
versionFile
);

if (fs.existsSync(versionFilePath)) {
version = parseNodeVersionFile(fs.readFileSync(versionFilePath, 'utf8'));

core.info(`Resolved ${versionFileInput} as ${version}`);
core.info(`Resolved ${versionFile} as ${version}`);

return version;
}
}

return version;
Expand Down