Skip to content

Commit 18fd17b

Browse files
committed
Test using specific node version.
Fix busted tests on Linux.
1 parent 24f3f20 commit 18fd17b

File tree

5 files changed

+179
-26
lines changed

5 files changed

+179
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Build
22
_build
3+
_download
34
_test
45

56
# Logs

node/buildutils.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
require('shelljs');
3+
var fs = require('fs');
4+
var os = require('os');
5+
var path = require('path');
6+
var process = require('process');
7+
var syncRequest = require('sync-request');
8+
9+
var downloadPath = path.join(__dirname, '_download');
10+
var testPath = path.join(__dirname, '_test');
11+
12+
exports.run = function(cl) {
13+
console.log('> ' + cl);
14+
var rc = exec(cl).code;
15+
if (rc !== 0) {
16+
echo('Exec failed with rc ' + rc);
17+
exit(rc);
18+
}
19+
}
20+
var run = exports.run;
21+
22+
exports.getExternals = function () {
23+
// determine the platform
24+
var platform = os.platform();
25+
if (platform != 'darwin' && platform != 'linux' && platform != 'win32') {
26+
throw new Error('Unexpected platform: ' + platform);
27+
}
28+
29+
// download the same version of node used by the agent
30+
// and add node to the PATH
31+
var nodeUrl = 'https://nodejs.org/dist';
32+
var nodeVersion = 'v5.10.1';
33+
switch (platform) {
34+
case 'darwin':
35+
var nodeArchivePath = downloadArchive(nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-darwin-x64.tar.gz');
36+
addPath(path.join(nodeArchivePath, 'node-' + nodeVersion + '-darwin-x64', 'bin'));
37+
break;
38+
case 'linux':
39+
var nodeArchivePath = downloadArchive(nodeUrl + '/' + nodeVersion + '/node-' + nodeVersion + '-linux-x64.tar.gz');
40+
addPath(path.join(nodeArchivePath, 'node-' + nodeVersion + '-linux-x64', 'bin'));
41+
break;
42+
case 'win32':
43+
var nodeExePath = downloadFile(nodeUrl + '/' + nodeVersion + '/win-x64/node.exe');
44+
var nodeLibPath = downloadFile(nodeUrl + '/' + nodeVersion + '/win-x64/node.lib');
45+
var nodeDirectory = path.join(testPath, 'node');
46+
mkdir('-p', nodeDirectory);
47+
cp(nodeExePath, path.join(nodeDirectory, 'node.exe'));
48+
cp(nodeLibPath, path.join(nodeDirectory, 'node.lib'));
49+
addPath(nodeDirectory);
50+
break;
51+
}
52+
}
53+
54+
var downloadFile = function (url) {
55+
// validate parameters
56+
if (!url) {
57+
throw new Error('Parameter "url" must be set.');
58+
}
59+
60+
// short-circuit if already downloaded
61+
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
62+
var targetPath = path.join(downloadPath, 'file', scrubbedUrl);
63+
var marker = targetPath + '.completed';
64+
if (!test('-f', marker)) {
65+
console.log('Downloading file: ' + url);
66+
67+
// delete any previous partial attempt
68+
if (test('-f', targetPath)) {
69+
rm('-f', targetPath);
70+
}
71+
72+
// download the file
73+
mkdir('-p', path.join(downloadPath, 'file'));
74+
var result = syncRequest('GET', url);
75+
fs.writeFileSync(targetPath, result.getBody());
76+
77+
// write the completed marker
78+
fs.writeFileSync(marker, '');
79+
}
80+
81+
return targetPath;
82+
}
83+
84+
var downloadArchive = function (url) {
85+
// validate platform
86+
var platform = os.platform();
87+
if (platform != 'darwin' && platform != 'linux') {
88+
throw new Error('Unexpected platform: ' + platform);
89+
}
90+
91+
// validate parameters
92+
if (!url) {
93+
throw new Error('Parameter "url" must be set.');
94+
}
95+
96+
if (!url.match(/\.tar\.gz$/)) {
97+
throw new Error('Expected .tar.gz');
98+
}
99+
100+
// short-circuit if already downloaded and extracted
101+
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
102+
var targetPath = path.join(downloadPath, 'archive', scrubbedUrl);
103+
var marker = targetPath + '.completed';
104+
if (!test('-f', marker)) {
105+
// download the archive
106+
var archivePath = downloadFile(url);
107+
console.log('Extracting archive: ' + url);
108+
109+
// delete any previously attempted extraction directory
110+
if (test('-d', targetPath)) {
111+
rm('-rf', targetPath);
112+
}
113+
114+
// extract
115+
mkdir('-p', targetPath);
116+
var cwd = process.cwd();
117+
process.chdir(targetPath);
118+
try {
119+
run('tar -xzf "' + archivePath + '"');
120+
}
121+
finally {
122+
process.chdir(cwd);
123+
}
124+
125+
// write the completed marker
126+
fs.writeFileSync(marker, '');
127+
}
128+
129+
return targetPath;
130+
}
131+
132+
var addPath = function (directory) {
133+
var separator;
134+
if (os.platform() == 'win32') {
135+
separator = ';';
136+
}
137+
else {
138+
separator = ':';
139+
}
140+
141+
var existing = process.env['PATH'];
142+
if (existing) {
143+
process.env['PATH'] = directory + separator + existing;
144+
}
145+
else {
146+
process.env['PATH'] = directory;
147+
}
148+
}

node/make.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
require('shelljs/make');
3-
var path = require('path');
43
var fs = require('fs');
4+
var path = require('path');
5+
var buildutils = require('./buildutils');
6+
var run = buildutils.run;
57

68
var rp = function (relPath) {
79
return path.join(__dirname, relPath);
@@ -10,15 +12,6 @@ var rp = function (relPath) {
1012
var buildPath = path.join(__dirname, '_build');
1113
var testPath = path.join(__dirname, '_test');
1214

13-
var run = function(cl) {
14-
console.log('> ' + cl);
15-
var rc = exec(cl).code;
16-
if (rc !== 0) {
17-
echo('Exec failed with rc ' + rc);
18-
exit(rc);
19-
}
20-
}
21-
2215
target.clean = function () {
2316
rm('-Rf', buildPath);
2417
rm('-Rf', testPath);
@@ -28,7 +21,7 @@ target.build = function() {
2821
target.clean();
2922

3023
run('tsc --outDir ' + buildPath);
31-
cp(rp('dependencies/typings.json'), buildPath);
24+
cp(rp('dependencies/typings.json'), buildPath);
3225
cp(rp('package.json'), buildPath);
3326
cp(rp('README.md'), buildPath);
3427
cp(rp('../LICENSE'), buildPath);
@@ -43,6 +36,7 @@ target.build = function() {
4336
target.test = function() {
4437
target.build();
4538

39+
buildutils.getExternals();
4640
run('tsc --outDir ' + testPath + ' test/tasklib.ts');
4741
cp('-Rf', rp('test/scripts'), testPath);
4842
run('mocha ' + path.join(testPath, 'tasklib.js'));
@@ -59,7 +53,7 @@ target.loc = function() {
5953
}
6054
}
6155

62-
// Create the en-US resjson file.
56+
// create the en-US resjson file.
6357
var enContents = JSON.stringify(strings, null, 2);
6458
fs.writeFileSync(path.join(strPath, 'resources.resjson'), enContents)
65-
}
59+
}

node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"merge2": "^0.3.6",
4040
"mocha": "^2.2.5",
4141
"semver": "^5.1.0",
42+
"sync-request": "3.0.1",
4243
"typescript": "1.8.7"
4344
}
4445
}

node/test/tasklib.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ describe('Test vsts-task-lib', function () {
7575

7676
});
7777

78+
describe('node', () => {
79+
it('is expected version', (done: MochaDone) => {
80+
this.timeout(1000);
81+
82+
assert.equal(process.version, 'v5.10.1');
83+
84+
done();
85+
});
86+
});
87+
7888
describe('Dir Operations', function () {
7989
it('creates folder with mkdirP', function (done) {
8090
this.timeout(1000);
@@ -1268,22 +1278,21 @@ describe('Test vsts-task-lib', function () {
12681278
errStream: _nullTestStream
12691279
}
12701280

1281+
var tool;
12711282
if (plat === 'win32') {
1272-
var cmd = tl.tool(tl.which('cmd', true));
1273-
cmd.arg('/c notExist');
1274-
1275-
var ret = cmd.execSync(_testExecOptions);
1276-
assert.equal(ret.code, 1, 'return code of cmd should be 1 on failure');
1283+
tool = tl.tool(tl.which('cmd', true));
1284+
tool.arg('/c');
1285+
tool.arg('echo hello from stderr 1>&2 && exit 123');
12771286
}
12781287
else {
1279-
var ls = tl.tool(tl.which('ls', true));
1280-
ls.arg('-j');
1281-
1282-
var ret = ls.execSync(_testExecOptions);
1283-
assert.equal(ret.code, 1, 'return code of ls should be 1 on failure');
1288+
tool = tl.tool(tl.which('bash', true));
1289+
tool.arg('-c');
1290+
tool.arg('echo hello from stderr 1>&2 ; exit 123');
12841291
}
12851292

1286-
assert(ret.stderr && ret.stderr.length > 0, 'should have emitted stderr');
1293+
var ret = tool.execSync(_testExecOptions);
1294+
assert.equal(ret.code, 123, 'return code of tool should be 1');
1295+
assert.equal(ret.stderr.toString().trim(), 'hello from stderr');
12871296
tl.popd();
12881297
done();
12891298
})
@@ -1835,7 +1844,7 @@ describe('Test vsts-task-lib', function () {
18351844
var jsonPath = path.join(tempFolder, 'task.json');
18361845
fs.writeFileSync(jsonPath, jsonStr);
18371846

1838-
var tempLocFolder = path.join(tempFolder, 'strings', 'resources.resjson', 'zh-CN');
1847+
var tempLocFolder = path.join(tempFolder, 'Strings', 'resources.resjson', 'zh-CN');
18391848
shell.mkdir('-p', tempLocFolder);
18401849
var locJsonStr = "{\"loc.messages.key1\" : \"loc cn-string for key 1.\", \"loc.messages.key3\" : \"loc cn-string for key %%.\"}";
18411850
var locJsonPath = path.join(tempLocFolder, 'resources.resjson');
@@ -1857,7 +1866,7 @@ describe('Test vsts-task-lib', function () {
18571866
var jsonPath = path.join(tempFolder, 'task.json');
18581867
fs.writeFileSync(jsonPath, jsonStr);
18591868

1860-
var tempLocFolder = path.join(tempFolder, 'strings', 'resources.resjson', 'en-US');
1869+
var tempLocFolder = path.join(tempFolder, 'Strings', 'resources.resjson', 'en-US');
18611870
shell.mkdir('-p', tempLocFolder);
18621871
var locJsonStr = "{\"loc.messages.key1\" : \"loc en-string for key 1.\", \"loc.messages.key2\" : \"loc en-string for key %d.\", \"loc.messages.key3\" : \"loc en-string for key %%.\"}";
18631872
var locJsonPath = path.join(tempLocFolder, 'resources.resjson');

0 commit comments

Comments
 (0)