-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathbuild.test.js
369 lines (334 loc) · 12.2 KB
/
build.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
'use strict';
const test = require('tape');
const run = require('./run.util.js');
const existsSync = require('fs').existsSync || require('path').existsSync;
const fs = require('fs');
const os = require('os');
const rm = require('rimraf');
const path = require('path');
const napi = require('../lib/util/napi.js');
const versioning = require('../lib/util/versioning.js');
const tar = require('tar');
const localVer = [versioning.get_runtime_abi('node'), process.platform, process.arch].join('-');
const SOEXT = { 'darwin': 'dylib', 'linux': 'so', 'win32': 'dll' }[process.platform];
if (process.env.node_pre_gyp_mock_s3) {
process.env.node_pre_gyp_mock_s3 = `${os.tmpdir()}/mock`;
}
// The list of different sample apps that we use to test
const apps = [
{
'name': 'app1',
'args': '',
'files': {
'base': ['binding/app1.node']
}
},
{
'name': 'app2',
'args': '--custom_include_path=../include --debug',
'files': {
'base': ['node-pre-gyp-test-app2/app2.node']
}
},
{
'name': 'app2',
'args': '--custom_include_path=../include --toolset=cpp11',
'files': {
'base': ['node-pre-gyp-test-app2/app2.node']
}
},
{
'name': 'app3',
'args': '',
'files': {
'base': [[localVer, 'app3.node'].join('/')]
}
},
{
'name': 'app4',
'args': '',
'files': {
'base': [[localVer, 'app4.node'].join('/'), [localVer, 'mylib.' + SOEXT].join('/')]
}
},
{
'name': 'app7',
'args': ''
}
];
// https://stackoverflow.com/questions/38599457/how-to-write-a-custom-assertion-for-testing-node-or-javascript-with-tape-or-che
test.Test.prototype.stringContains = function(actual, contents, message) {
this._assert(actual.indexOf(contents) > -1, {
message: message || 'should contain ' + contents,
operator: 'stringContains',
actual: actual,
expected: contents
});
};
// Because the below tests only ensure that flags can be correctly passed to node-gyp is it not
// likely they will behave differently for different apps. So we save time by avoiding running these for each app.
const appOne = apps[0];
// make sure node-gyp options are passed by passing invalid values
// and ensuring the expected errors are returned from node-gyp
test(appOne.name + ' passes --nodedir down to node-gyp via node-pre-gyp ' + appOne.args, (t) => {
run('node-pre-gyp', 'configure', '--nodedir=invalid-value', appOne, {}, (err, stdout, stderr) => {
t.ok(err, 'Expected command to fail');
t.stringContains(stderr, 'common.gypi not found');
t.end();
});
});
// NOTE: currently fails with npm v3.x on windows (hence downgrade in appveyor.yml)
test(appOne.name + ' passes --nodedir down to node-gyp via npm' + appOne.args, (t) => {
run('npm', 'install', '--build-from-source --nodedir=invalid-value', appOne, {}, (err, stdout, stderr) => {
t.ok(err, 'Expected command to fail');
t.stringContains(stderr, 'common.gypi not found');
t.end();
});
});
// note: --ensure=false tells node-gyp to attempt to re-download the node headers
// even if they already exist on disk at ~/.node-gyp/{version}
test(appOne.name + ' passes --dist-url down to node-gyp via node-pre-gyp ' + appOne.args, (t) => {
run('node-pre-gyp', 'configure', '--ensure=false --dist-url=invalid-value', appOne, {}, (err) => {
t.ok(err, 'Expected command to fail');
t.end();
});
});
test(appOne.name + ' passes --dist-url down to node-gyp via npm ' + appOne.args, (t) => {
run('npm', 'install', '--build-from-source --ensure=false --dist-url=invalid-value', appOne, {}, (err) => {
t.ok(err, 'Expected command to fail');
t.end();
});
});
// Tests run for all apps
apps.forEach((app) => {
if (app.name === 'app7' && !napi.get_napi_version()) return;
// clear out entire binding directory
// to ensure no stale builds. This is needed
// because "node-pre-gyp clean" only removes
// the current target and not alternative builds
test('cleanup of app', (t) => {
const binding_directory = path.join(__dirname, app.name, 'lib/binding');
if (fs.existsSync(binding_directory)) {
rm.sync(binding_directory);
}
t.end();
});
test(app.name + ' configures ' + app.args, (t) => {
run('node-pre-gyp', 'configure', '--loglevel=error', app, {}, (err) => {
t.ifError(err);
t.end();
});
});
test(app.name + ' configures with unparsed options ' + app.args, (t) => {
run('node-pre-gyp', 'configure', '--loglevel=info -- -Dfoo=bar', app, {}, (err, stdout, stderr) => {
t.ifError(err);
t.equal(stdout, '');
t.ok(stderr.search(/(gyp info spawn args).*(-Dfoo=bar)/) > -1);
t.end();
});
});
if (process.platform !== 'win32') {
test(app.name + ' builds with unparsed options ' + app.args, (t) => {
// clean and build as separate steps here because configure only works with -Dfoo=bar
// and build only works with FOO=bar
run('node-pre-gyp', 'clean', '', app, {}, (err) => {
t.ifError(err);
const propertyPrefix = (process.platform === 'win32') ? '/p:' : '';
run('node-pre-gyp', 'build', '--loglevel=info -- ' + propertyPrefix + 'FOO=bar', app, {}, (err2, stdout, stderr) => {
t.ifError(err2);
t.ok(stderr.search(/(gyp info spawn args).*(FOO=bar)/) > -1);
if (process.platform !== 'win32') {
if (app.args.indexOf('--debug') > -1) {
t.stringContains(stdout, 'Debug/' + app.name + '.node');
} else {
t.stringContains(stdout, 'Release/' + app.name + '.node');
}
}
t.end();
});
});
});
} else {
// Skipping since this support broke upstream in node-gyp: https://github.com/nodejs/node-gyp/pull/1616
test.skip(app.name + ' builds with unparsed options ' + app.args, () => {});
}
test(app.name + ' builds ' + app.args, (t) => {
run('node-pre-gyp', 'rebuild', '--fallback-to-build', app, {}, (err, stdout, stderr) => {
t.ifError(err);
if (err) {
console.log(stdout);
console.log(stderr);
}
if (process.platform !== 'win32') {
if (app.args.indexOf('--debug') > -1) {
t.stringContains(stdout, 'Debug/' + app.name + '.node');
} else {
t.stringContains(stdout, 'Release/' + app.name + '.node');
}
}
t.end();
});
});
test(app.name + ' is found ' + app.args, (t) => {
run('node-pre-gyp', 'reveal', 'module_path --silent', app, {}, (err, stdout) => {
t.ifError(err);
let module_path = stdout.trim();
if (module_path.indexOf('\n') !== -1) { // take just the first line
module_path = module_path.substr(0, module_path.indexOf('\n'));
}
t.stringContains(module_path, app.name);
t.ok(existsSync(module_path), 'is valid path to existing binary: ' + module_path);
const module_binary = path.join(module_path, app.name + '.node');
t.ok(existsSync(module_binary));
t.end();
});
});
test(app.name + ' passes tests ' + app.args, (t) => {
run('npm', 'test', '', app, { cwd: path.join(__dirname, app.name) }, (err, stdout) => {
t.ifError(err);
// we expect app2 to console.log on success
if (app.name === 'app2') {
if (app.args.indexOf('--debug') > -1) {
t.stringContains(stdout, 'Loaded Debug build');
} else {
t.stringContains(stdout, 'Loaded Release build');
}
} else {
// we expect some npm output
t.notEqual(stdout, '');
}
t.end();
});
});
test(app.name + ' packages ' + app.args, (t) => {
run('node-pre-gyp', 'package', '', app, {}, (err) => {
t.ifError(err);
// Make sure a tarball was created
run('node-pre-gyp', 'reveal', 'staged_tarball --silent', app, {}, (err2, stdout) => {
t.ifError(err2);
let staged_tarball = stdout.trim();
if (staged_tarball.indexOf('\n') !== -1) { // take just the first line
staged_tarball = staged_tarball.substr(0, staged_tarball.indexOf('\n'));
}
const tarball_path = path.join(__dirname, app.name, staged_tarball);
t.ok(existsSync(tarball_path), 'staged tarball is a valid file');
if (!app.files) {
return t.end();
}
// Make sure the package contains what we expect
const entries = [];
tar.t({
file: tarball_path,
sync: true,
onentry: function(entry) {
entries.push(entry.path);
}
});
let files = app.files.base;
const nodever = versioning.get_runtime_abi('node');
// Look for a more specific choice
if (Object.hasOwnProperty.call(app.files, process.platform)) {
const appPlatList = app.files[process.platform];
if (Object.hasOwnProperty.call(appPlatList, nodever)) {
files = appPlatList[nodever];
} else if (Object.hasOwnProperty.call(appPlatList, 'base')) {
files = appPlatList.base;
} else {
files = appPlatList;
}
}
// windows is too variable to keep this test up to date across node versions
if (process.platform !== 'win32') {
t.same(entries.sort(), files.sort(), 'staged tarball contains the right files');
}
t.end();
});
});
});
test(app.name + ' package is valid ' + app.args, (t) => {
run('node-pre-gyp', 'testpackage', '', app, {}, (err) => {
t.ifError(err);
t.end();
});
});
const env = process.env;
if (env.AWS_ACCESS_KEY_ID || env.node_pre_gyp_accessKeyId || env.node_pre_gyp_mock_s3) {
test(app.name + ' publishes ' + app.args, (t) => {
run('node-pre-gyp', 'unpublish publish', '', app, {}, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
test(app.name + ' info shows it ' + app.args, (t) => {
run('node-pre-gyp', 'reveal', 'package_name', app, {}, (err, stdout) => {
t.ifError(err);
let package_name = stdout.trim();
if (package_name.indexOf('\n') !== -1) { // take just the first line
package_name = package_name.substr(0, package_name.indexOf('\n'));
}
run('node-pre-gyp', 'info', '', app, {}, (err2, stdout2) => {
t.ifError(err2);
t.stringContains(stdout2, package_name);
t.end();
});
});
});
test(app.name + ' can be uninstalled ' + app.args, (t) => {
run('node-pre-gyp', 'clean', '', app, {}, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
test(app.name + ' can be installed via remote ' + app.args, (t) => {
const opts = {
cwd: path.join(__dirname, app.name),
npg_debug: false
};
run('npm', 'install', '--fallback-to-build=false', app, opts, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
test(app.name + ' can be reinstalled via remote ' + app.args, (t) => {
const opts = {
cwd: path.join(__dirname, app.name),
npg_debug: false
};
run('npm', 'install', '--update-binary --fallback-to-build=false', app, opts, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
test(app.name + ' via remote passes tests ' + app.args, (t) => {
const opts = {
cwd: path.join(__dirname, app.name),
npg_debug: false
};
run('npm', 'install', '', app, opts, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
test(app.name + ' unpublishes ' + app.args, (t) => {
run('node-pre-gyp', 'unpublish', '', app, {}, (err, stdout) => {
t.ifError(err);
t.notEqual(stdout, '');
t.end();
});
});
} else {
test.skip(app.name + ' publishes ' + app.args, () => {});
}
// note: the above test will result in a non-runnable binary, so the below test must succeed otherwise all following tests will fail
test(app.name + ' builds with custom --target ' + app.args, (t) => {
run('node-pre-gyp', 'rebuild', '--loglevel=error --fallback-to-build --target=' + process.versions.node, app, {}, (err) => {
t.ifError(err);
t.end();
});
});
});