forked from mapbox/node-pre-gyp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.test.js
292 lines (249 loc) · 9.75 KB
/
run.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
'use strict';
/**
* test that executing node_pre_gyp.run() works as expected with various
* configurations and command line options.
*/
const fs = require('fs');
const path = require('path');
const { rimraf } = require('rimraf');
const npg = require('../lib/node-pre-gyp.js');
const test = require('tape');
const dir = 'tmp';
const package_json_template = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'binary-module-name',
'module_path': 'binary-module-path',
'host': 'binary-path',
'staging_host': 's3-staging-path',
'production_host': 's3-production-path'
}
};
const all_commands = ['build', 'clean', 'configure', 'info', 'install', 'package', 'publish', 'rebuild',
'reinstall', 'reveal', 'testbinary', 'testpackage', 'unpublish'];
/**
* before testing create a scratch directory to run tests in.
*/
const orig_dir = process.cwd();
const scratch = path.resolve('./scratch');
// execute this as a test so it doesn't change the working directory while the
// previous test (fetch.test.js) is running.
test('setup', (t) => {
try {
fs.mkdirSync(scratch);
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
try {
fs.mkdirSync(path.join(scratch, dir));
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
// cleanup any previous test settings.
delete process.env.node_pre_gyp_s3_host;
process.chdir(scratch);
t.end();
});
test.onFinish(() => {
process.chdir(orig_dir);
rimraf(scratch).then(() => undefined, () => undefined);
});
test('should set staging and production hosts', (t) => {
// make sure it's good when specifying host.
const mock_package_json = makePackageJson();
let { prog } = setupTest(dir, mock_package_json);
t.deepEqual(prog.package_json, mock_package_json);
t.equal(prog.binaryHostSet, false, 'binary host should not be flagged as set');
// test with no s3_host option
all_commands.forEach((cmd) => {
const mpj = clone(mock_package_json);
mpj.binary.host = '';
const opts = { argv: [cmd] };
({ prog } = setupTest(dir, mpj, opts));
mpj.binary.host = (cmd === 'publish' || cmd === 'unpublish') ? mpj.binary.staging_host : mpj.binary.production_host;
t.deepEqual(prog.package_json, mpj, 'host should be correct for command: ' + cmd);
t.equal(prog.binaryHostSet, true, 'binary host should be flagged as set');
});
// test with s3_host set to staging
all_commands.forEach((cmd) => {
const mpj = clone(mock_package_json);
mpj.binary.host = '';
const opts = { argv: [cmd, '--s3_host=staging'] };
({ prog } = setupTest(dir, mpj, opts));
mpj.binary.host = mpj.binary.staging_host;
t.deepEqual(prog.package_json, mpj, 'host should be correct for command: ' + cmd);
t.equal(prog.binaryHostSet, true, 'binary host should be flagged as set');
});
// test with s3_host set to production
all_commands.forEach((cmd) => {
const mpj = clone(mock_package_json);
mpj.binary.host = '';
const opts = { argv: [cmd, '--s3_host=production'] };
({ prog } = setupTest(dir, mpj, opts));
mpj.binary.host = mpj.binary.production_host;
t.deepEqual(prog.package_json, mpj, 'host should be correct for command: ' + cmd);
t.equal(prog.binaryHostSet, true, 'binary host should be flagged as set');
});
t.end();
});
test('should execute setBinaryHostProperty() properly', (t) => {
// it only --s3_host only takes effect if host is falsey.
const mock_package_json = makePackageJson({ binary: { host: '' } });
const opts = { argv: ['publish', '--s3_host=staging'] };
let { prog, binaryHost } = setupTest(dir, mock_package_json, opts);
t.equal(binaryHost, mock_package_json.binary.staging_host);
// set it again to verify that it returns the already set value
binaryHost = prog.setBinaryHostProperty('publish');
t.equal(binaryHost, mock_package_json.binary.staging_host);
// now do this again but expect an empty binary host value because
// staging_host is missing.
const mpj = clone(mock_package_json);
delete mpj.binary.staging_host;
({ prog, binaryHost } = setupTest(dir, mpj, opts));
t.equal(binaryHost, '');
// one more time but with an invalid value for s3_host
opts.argv = ['publish', '--s3_host=bad-news'];
try {
({ prog, binaryHost } = setupTest(dir, mock_package_json, opts));
t.fail('should throw with --s3_host=bad-news');
} catch (e) {
t.equal(e.message, 'invalid s3_host bad-news');
}
t.end();
});
test('verify that the --directory option works', (t) => {
const initial = process.cwd();
// make each unique so it can't match a leftover file (shouldn't happen, but...)
let mock_package_json = makePackageJson({ binary: { host: 'xyzzy' } });
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(mock_package_json));
let argv = ['node', 'program', 'publish', `--directory=${dir}`];
let prog = new npg.Run({ package_json_path: './package.json', argv });
t.deepEqual(prog.package_json, mock_package_json, 'should work with the directory option');
t.equal(process.cwd(), initial, 'the directory should be unchanged after executing');
mock_package_json = makePackageJson({ binary: { host: '42' } });
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(mock_package_json));
argv = ['node', 'program', 'publish'];
prog = new npg.Run({ package_json_path: `${path.join(dir, 'package.json')}`, argv });
t.deepEqual(prog.package_json, mock_package_json, 'should work without changing the directory');
t.equal(process.cwd(), initial, 'the directory should be unchanged after executing');
const badDir = '/xyzzy/fubar';
argv = ['node', 'program', 'publish', `--directory=${badDir}`];
try {
prog = new npg.Run({ package_json_path: 'package.json', argv });
t.fail(`should not find package.json in ${badDir}`);
} catch (e) {
const exist = e.message.indexOf('ENOENT: no such file or directory');
t.equal(exist, 0);
}
t.equal(process.cwd(), initial, 'the directory should be unchanged after failing');
t.end();
});
test('verify that the --directory works with napi_versions', (t) => {
const initial = process.cwd();
// make each unique so it can't match a leftover file (shouldn't happen, but...)
let mock_package_json = makePackageJson({ binary: { host: 'xyzzy', napi_versions: [1, 4] } });
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(mock_package_json));
let argv = ['node', 'program', 'publish', `--directory=${dir}`];
let prog = new npg.Run({ package_json_path: './package.json', argv });
t.deepEqual(prog.package_json, mock_package_json, 'should work with the directory option');
t.equal(process.cwd(), initial, 'the directory should be unchanged after executing');
mock_package_json = makePackageJson({ binary: { host: '42', napi_versions: [1, 4] } });
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(mock_package_json));
argv = ['node', 'program', 'publish'];
prog = new npg.Run({ package_json_path: `${path.join(dir, 'package.json')}`, argv });
t.deepEqual(prog.package_json, mock_package_json, 'should work without changing the directory');
t.equal(process.cwd(), initial, 'the directory should be unchanged after executing');
t.end();
});
test('verify that a non-existent package.json fails', (t) => {
fs.unlink(dir + '/package.json', (e0) => {
if (e0 && e0.code !== 'ENOENT') {
console.error(e0.message);
}
// ignore errors
fs.rmdir(dir, (e1) => {
if (e1 && e1.code !== 'ENOENT') {
console.error(e1.message);
}
try {
new npg.Run({ package_json_path: dir + '/package.json' });
t.fail('new Run() should have thrown');
} catch (e) {
const exist = e.message.indexOf('ENOENT: no such file or directory');
t.equal(exist, 0);
}
t.end();
});
});
});
//
// test helpers.
//
function makePackageJson(options = {}) {
const package_json = clone(package_json_template);
// override binary values if supplied
if (options.binary) {
for (const k in options.binary) {
package_json.binary[k] = options.binary[k];
}
}
return package_json;
}
// helper to write package.json to disk so Run() can be instantiated with it.
function setupTest(directory, package_json, opts) {
opts = opts || {};
let argv = ['node', 'program'];
if (opts.argv) {
argv = argv.concat(opts.argv);
}
const prev_dir = process.cwd();
if (!opts.noChdir) {
try {
fs.mkdirSync(directory);
} catch (e) {
if (e.code !== 'EEXIST') {
throw e;
}
}
process.chdir(directory);
}
try {
fs.writeFileSync('package.json', JSON.stringify(package_json));
const prog = new npg.Run({ package_json_path: './package.json', argv });
const binaryHost = prog.setBinaryHostProperty(prog.todo[0] && prog.todo[0].name);
return { prog, binaryHost };
} finally {
process.chdir(prev_dir);
}
}
// helper to clone mock package.json. it's overkill for existing tests
// but is future-proof.
// https://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript
function clone(obj, hash = new WeakMap()) {
if (Object(obj) !== obj) return obj; // primitives
if (hash.has(obj)) return hash.get(obj); // cyclic reference
let result;
if (obj instanceof Set) {
result = new Set(obj); // treat set as a value
} else if (obj instanceof Map) {
result = new Map(Array.from(obj, ([key, val]) => [key, clone(val, hash)]));
} else if (obj instanceof Date) {
result = new Date(obj);
} else if (obj instanceof RegExp) {
result = new RegExp(obj.source, obj.flags);
} else if (obj.constructor) {
result = new obj.constructor();
} else {
result = Object.create(null);
}
hash.set(obj, result);
return Object.assign(result, ...Object.keys(obj).map((key) => {
return { [key]: clone(obj[key], hash) };
}));
}