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

NodeToolV0: allow reading versionSpec from file #15615

Closed
wants to merge 9 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"loc.description": "Finds or downloads and caches the specified version spec of Node.js and adds it to the PATH",
"loc.instanceNameFormat": "Use Node $(versionSpec)",
"loc.input.label.versionSpec": "Version Spec",
"loc.input.help.versionSpec": "Version Spec of version to get. Examples: 6.x, 4.x, 6.10.0, >=6.10.0",
"loc.input.help.versionSpec": "Version Spec of version to get. Examples: 6.x, 4.x, 6.10.0, >=6.10.0. Also supports a path to a file. Examples: src/.nvmrc, .node-version",
"loc.input.label.checkLatest": "Check for Latest Version",
"loc.input.help.checkLatest": "Always checks online for the latest available version that satisfies the version spec. This is typically false unless you have a specific scenario to always get latest. This will cause it to incur download costs when potentially not necessary, especially with the hosted build pool.",
"loc.input.label.force32bit": "Use 32 bit version on x64 agents",
Expand Down
14 changes: 14 additions & 0 deletions Tasks/NodeToolV0/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ describe('NodeTool Suite', function () {
}, tr, done);
});

it('Reads node version from nvmrc', (done: Mocha.Done) => {
this.timeout(5000);

let tp: string = path.join(__dirname, 'L0ReadsNvmrc.js');
let tr: ttm.MockTestRunner = new ttm.MockTestRunner(tp);

tr.run();

runValidations(() => {
assert(tr.succeeded, 'NodeTool should have succeeded.');
assert(tr.stderr.length === 0, 'NodeTool should not have written to stderr');
}, tr, done);
});

});
100 changes: 100 additions & 0 deletions Tasks/NodeToolV0/Tests/L0ReadsNvmrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import ma = require('azure-pipelines-task-lib/mock-answer');
import tmrm = require('azure-pipelines-task-lib/mock-run');
import os = require('os');
import path = require('path');
import semver = require('semver');

let taskPath = path.join(__dirname, '..', 'nodetool.js');
let tmr: tmrm.TaskMockRunner = new tmrm.TaskMockRunner(taskPath);

tmr.setInput('versionSpec', 'src/.nvmrc');

let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
"assertAgent": {
"2.115.1": true
},
"checkPath": {
".nvmrc": true,
},
};
tmr.setAnswers(a);


//Create assertAgent and getVariable mocks
const tl = require('azure-pipelines-task-lib/mock-task');
const tlClone = Object.assign({}, tl);
tlClone.getVariable = function (variable: string) {
if (variable.toLowerCase() == 'agent.tempdirectory') {
return 'temp';
}
return null;
};
tlClone.assertAgent = function (variable: string) {
return;
};
tmr.registerMock('azure-pipelines-task-lib/mock-task', tlClone);

tmr.registerMock('fs', {
readFileSync: function (path, options) {
if (path != 'src/.nvmrc') {
throw new Error(`reading wrong .nvmrc: '${[path]}'`);
}

return '0.6.21';
}
});

//Create tool-lib mock
tmr.registerMock('azure-pipelines-tool-lib/tool', {
isExplicitVersion: semver.valid,
findLocalTool: function (toolName, versionSpec) {
if (toolName != 'node') {
throw new Error('Searching for wrong tool');
}
return false;
},
evaluateVersions: function (versions, versionSpec) {
if (versionSpec !== 'v0.6.21') {
throw new Error(`Incorrect versionSpec: ${versionSpec}`);
}
return versionSpec;
},
cleanVersion: semver.clean,
downloadTool(url) {
let err = new Error();
err['httpStatusCode'] = '404';
if (url === `https://nodejs.org/dist/v0.6.21/node-v0.6.21-win-${os.arch()}.7z` ||
url === `https://nodejs.org/dist/v0.6.21/node-v0.6.21-${os.platform()}-${os.arch()}.tar.gz`) {
throw err;
}
else if (url === `https://nodejs.org/dist/v0.6.21/win-${os.arch()}/node.exe`) {
throw err;
}
else if (url === `https://nodejs.org/dist/v0.6.21/win-${os.arch()}/node.lib`) {
throw err;
}
else if (url === `https://nodejs.org/dist/v0.6.21/node.exe`) {
return 'exe_loc';
}
else if (url === `https://nodejs.org/dist/v0.6.21/node.lib`) {
return 'exe_lib';
}
else {
throw new Error(`Incorrect URL: ${url}`);
}
},
extract7z(downloadPath, extPath, _7zPath) {
return 'extPath';
},
extractTar(downloadPath, extPath, _7zPath) {
return 'extPath';
},
cacheDir(dir, tool, version) {
return 'path to tool';
},
prependPath(toolPath) {
return;
}
});

tmr.run();
17 changes: 16 additions & 1 deletion Tasks/NodeToolV0/nodetool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import * as restm from 'typed-rest-client/RestClient';
import * as telemetry from 'azure-pipelines-tasks-utility-common/telemetry';
import * as os from 'os';
import * as path from 'path';
import * as fs from 'fs';

const force32bit: boolean = taskLib.getBoolInput('force32bit', false);
let osPlat: string = os.platform();
let osArch: string = getArch();

async function run() {
try {
let versionSpec = taskLib.getInput('versionSpec', true);
let versionSpec = getVersionSpec();
let checkLatest: boolean = taskLib.getBoolInput('checkLatest', false);
await getNode(versionSpec, checkLatest);
telemetry.emitTelemetry('TaskHub', 'NodeToolV0', { versionSpec, checkLatest, force32bit });
Expand All @@ -21,6 +22,20 @@ async function run() {
}
}

function getVersionSpec() {
const versionSpecInput = taskLib.getInput('versionSpec', true);

const cleanVersion = toolLib.cleanVersion(versionSpecInput);
if (cleanVersion) {
taskLib.debug(`version spec '${cleanVersion}' from '${versionSpecInput}'`);
return versionSpecInput;
}

const versionSpec = fs.readFileSync(versionSpecInput, { 'encoding': 'utf' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it make sense to add separate task input for version spec specified as file path - to avoid any confusion around it?

taskLib.debug(`Got node version spec '${versionSpec}' from file '${versionSpecInput}'`);
return versionSpec;
}

//
// Node versions interface
// see https://nodejs.org/dist/index.json
Expand Down
2 changes: 1 addition & 1 deletion Tasks/NodeToolV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"label": "Version Spec",
"defaultValue": "6.x",
"required": true,
"helpMarkDown": "Version Spec of version to get. Examples: 6.x, 4.x, 6.10.0, >=6.10.0"
"helpMarkDown": "Version Spec of version to get. Examples: 6.x, 4.x, 6.10.0, >=6.10.0. Also supports a path to a file. Examples: src/.nvmrc, .node-version"
},
{
"name": "checkLatest",
Expand Down