-
Notifications
You must be signed in to change notification settings - Fork 262
/
build.test.js
281 lines (249 loc) · 11.3 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
"use strict";
var test = require('tape');
var run = require('./run.util.js');
var existsSync = require('fs').existsSync || require('path').existsSync;
var fs = require('fs');
var rm = require('rimraf');
var path = require('path');
var getPrevious = require('./target_version.util.js');
// The list of different sample apps that we use to test
var apps = [
{
'name': 'app1',
'args': ''
},
{
'name': 'app2',
'args': '--custom_include_path=../include --debug'
},
{
'name': 'app2',
'args': '--custom_include_path=../include --toolset=cpp11'
},
{
'name': 'app3',
'args': ''
},
{
'name': 'app4',
'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.
var app = apps[0];
// make sure node-gyp options are passed by passing invalid values
// and ensuring the expected errors are returned from node-gyp
test(app.name + ' passes --nodedir down to node-gyp via node-pre-gyp ' + app.args, function(t) {
run('node-pre-gyp', 'configure', '--nodedir=invalid-value', app, {}, function(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 (hench downgrade in appveyor.yml)
test(app.name + ' passes --nodedir down to node-gyp via npm' + app.args, function(t) {
run('npm', 'install', '--build-from-source --nodedir=invalid-value', app, {}, function(err,stdout,stderr) {
t.ok(err, 'Expected command to fail');
t.stringContains(stderr,"common.gypi not found");
t.end();
});
});
// these will not fail on windows because node-gyp falls back to the python launcher instead of erroring out:
// https://github.com/nodejs/node-gyp/blob/c84a54194781410743efe353d18ca7d20fc9d3a3/lib/configure.js#L396-L397
if (process.platform !== 'win32') {
test(app.name + ' passes --python down to node-gyp via node-pre-gyp ' + app.args, function(t) {
run('node-pre-gyp', 'configure', '--python=invalid-value', app, {}, function(err,stdout,stderr) {
t.ok(err, 'Expected command to fail');
t.stringContains(stderr,"Can't find Python executable");
t.end();
});
});
test(app.name + ' passes --python down to node-gyp via npm ' + app.args, function(t) {
run('node-pre-gyp', 'configure', '--build-from-source --python=invalid-value', app, {}, function(err,stdout,stderr) {
t.ok(err, 'Expected command to fail');
t.stringContains(stderr,"Can't find Python executable");
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(app.name + ' passes --dist-url down to node-gyp via node-pre-gyp ' + app.args, function(t) {
run('node-pre-gyp', 'configure', '--ensure=false --dist-url=invalid-value', app, {}, function(err,stdout,stderr) {
t.ok(err, 'Expected command to fail');
t.end();
});
});
test(app.name + ' passes --dist-url down to node-gyp via npm ' + app.args, function(t) {
run('npm', 'install', '--build-from-source --ensure=false --dist-url=invalid-value', app, {}, function(err,stdout,stderr) {
t.ok(err, 'Expected command to fail');
t.end();
});
});
// Tests run for all apps
apps.forEach(function(app) {
// 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', function(t) {
var 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, function(t) {
run('node-pre-gyp', 'configure', '--loglevel=error', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.end();
});
});
test(app.name + ' configures with unparsed options ' + app.args, function(t) {
run('node-pre-gyp', 'configure', '--loglevel=info -- -Dfoo=bar', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.ok(stderr.search(/(gyp info spawn args).*(-Dfoo=bar)/) > -1);
t.end();
});
});
test(app.name + ' builds with unparsed options ' + app.args, function(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, {}, function(err) {
t.ifError(err);
var propertyPrefix = (process.platform === 'win32') ? '/p:' : '';
run('node-pre-gyp', 'build', '--loglevel=info -- ' + propertyPrefix + 'FOO=bar', app, {}, function(err,stdout,stderr) {
t.ifError(err);
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();
});
});
});
test(app.name + ' builds ' + app.args, function(t) {
run('node-pre-gyp', 'rebuild', '--fallback-to-build --loglevel=error', app, {}, function(err,stdout,stderr) {
t.ifError(err);
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, function(t) {
run('node-pre-gyp', 'reveal', 'module_path --silent', app, {}, function(err,stdout,stderr) {
t.ifError(err);
var module_path = stdout.trim();
t.stringContains(module_path,app.name);
t.ok(existsSync(module_path),'is valid path to existing binary: '+ module_path);
var module_binary = path.join(module_path,app.name+'.node');
t.ok(existsSync(module_binary));
t.end();
});
});
test(app.name + ' passes tests ' + app.args, function(t) {
run('npm','test','', app, {cwd: path.join(__dirname,app.name)}, function(err,stdout,stderr) {
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, function(t) {
run('node-pre-gyp', 'package', '', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.end();
});
});
test(app.name + ' package is valid ' + app.args, function(t) {
run('node-pre-gyp', 'testpackage', '', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.end();
});
});
if (process.env.AWS_ACCESS_KEY_ID || process.env.node_pre_gyp_accessKeyId) {
test(app.name + ' publishes ' + app.args, function(t) {
run('node-pre-gyp', 'unpublish publish', '', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.notEqual(stdout,'');
t.end();
});
});
test(app.name + ' info shows it ' + app.args, function(t) {
run('node-pre-gyp', 'reveal', 'package_name', app, {}, function(err,stdout,stderr) {
t.ifError(err);
var package_name = stdout.trim();
run('node-pre-gyp', 'info', '', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.stringContains(stdout,package_name);
t.end();
});
});
});
test(app.name + ' can be uninstalled ' + app.args, function(t) {
run('node-pre-gyp', 'clean', '', app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.notEqual(stdout,'');
t.end();
});
});
test(app.name + ' can be installed via remote ' + app.args, function(t) {
run('npm', 'install', '--fallback-to-build=false', app, {cwd: path.join(__dirname,app.name)}, function(err,stdout,stderr) {
t.ifError(err);
t.notEqual(stdout,'');
t.end();
});
});
test(app.name + ' can be reinstalled via remote ' + app.args, function(t) {
run('npm', 'install', '--update-binary --fallback-to-build=false', app, {cwd: path.join(__dirname,app.name)}, function(err,stdout,stderr) {
t.ifError(err);
t.notEqual(stdout,'');
t.end();
});
});
test(app.name + ' via remote passes tests ' + app.args, function(t) {
run('npm', 'install', '', app, {cwd: path.join(__dirname,app.name)}, function(err,stdout,stderr) {
t.ifError(err);
t.notEqual(stdout,'');
t.end();
});
});
} else {
test.skip(app.name + ' publishes ' + app.args, function() {});
}
// 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, function(t) {
run('node-pre-gyp', 'rebuild', '--loglevel=error --fallback-to-build --target='+process.versions.node, app, {}, function(err,stdout,stderr) {
t.ifError(err);
t.end();
});
});
});