forked from mapbox/node-pre-gyp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioning.test.js
315 lines (284 loc) · 10.8 KB
/
versioning.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
'use strict';
const path = require('path');
const versioning = require('../lib/util/versioning.js');
const test = require('tape');
const detect_libc = require('detect-libc');
test('should normalize double slash', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'test',
'module_path': './lib/binding/{configuration}/{toolset}/{name}',
'remote_path': './{name}/v{version}/{configuration}/{version}/{toolset}/',
'package_name': '{module_name}-v{major}.{minor}.{patch}-{prerelease}+{build}-{toolset}-{node_abi}-{platform}-{arch}.tar.gz',
'host': 'https://some-bucket.s3.us-east-1.amazonaws.com'
}
};
const opts = versioning.evaluate(mock_package_json, {});
t.equal(opts.remote_path, './test/v0.1.0/Release/0.1.0/');
// Node v0.11.x on windows lowercases C:// when path.join is called
// https://github.com/joyent/node/issues/7031
t.equal(path.normalize(opts.module_path), path.join(process.cwd(), 'lib/binding/Release/test'));
const opts_toolset = versioning.evaluate(mock_package_json, { toolset: 'custom-toolset' });
t.equal(opts_toolset.remote_path, './test/v0.1.0/Release/0.1.0/custom-toolset/');
t.end();
});
test('should detect abi for node process', (t) => {
const mock_process_versions = {
node: '0.10.33',
v8: '3.14.5.9',
modules: '11'
};
const abi = versioning.get_node_abi('node', mock_process_versions);
t.equal(abi, 'node-v11');
t.equal(versioning.get_runtime_abi('node', undefined), versioning.get_node_abi('node', process.versions));
t.end();
});
test('should detect abi for odd node target', (t) => {
const mock_process_versions = {
node: '0.11.1000000',
modules: 'bogus'
};
const abi = versioning.get_node_abi('node', mock_process_versions);
t.equal(abi, 'node-v0.11.1000000');
t.end();
});
test('should detect abi for custom node target', (t) => {
const mock_process_versions = {
'node': '0.10.0',
'modules': '11'
};
t.equal(versioning.get_runtime_abi('node', '0.10.0'), versioning.get_node_abi('node', mock_process_versions));
const mock_process_versions2 = {
'node': '0.8.0',
'v8': '3.11'
};
t.equal(versioning.get_runtime_abi('node', '0.8.0'), versioning.get_node_abi('node', mock_process_versions2));
t.end();
});
test('should detect runtime for node-webkit and electron', (t) => {
const mock_process_versions = {
'electron': '0.37.3'
};
t.equal(versioning.get_process_runtime(mock_process_versions), 'electron');
const mock_process_versions2 = {
'node': '0.8.0'
};
t.equal(versioning.get_process_runtime(mock_process_versions2), 'node');
const mock_process_versions3 = {
'node-webkit': '0.37.3'
};
t.equal(versioning.get_process_runtime(mock_process_versions3), 'node-webkit');
t.end();
});
test('should detect abi for electron runtime', (t) => {
t.equal(versioning.get_runtime_abi('electron', '0.37.3'), versioning.get_electron_abi('electron', '0.37.3'));
t.end();
});
test('should detect abi for node-webkit runtime', (t) => {
t.equal(versioning.get_runtime_abi('node-webkit', '0.10.5'), versioning.get_node_webkit_abi('node-webkit', '0.10.5'));
t.end();
});
test('should throw when custom node target is not found in abi_crosswalk file', (t) => {
try {
versioning.get_runtime_abi('node', '123456789.0.0');
} catch (e) {
const expectedMessage = 'Unsupported target version: 123456789.0.0';
t.equal(e.message, expectedMessage);
t.end();
}
});
test('should throw when custom node target is not semver', (t) => {
try {
versioning.get_runtime_abi('node', '1.2.3.4');
} catch (e) {
const expectedMessage = 'Unknown target version: 1.2.3.4';
t.equal(e.message, expectedMessage);
t.end();
}
});
test('should detect custom binary host from env', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'test',
'module_path': './lib/binding/{configuration}/{toolset}/{name}',
'remote_path': './{name}/v{version}/{configuration}/{version}/{toolset}/',
'package_name': '{module_name}-v{major}.{minor}.{patch}-{prerelease}+{build}-{toolset}-{node_abi}-{platform}-{arch}.tar.gz',
'host': 'https://some-bucket.s3.us-east-1.amazonaws.com'
}
};
// mock npm_config_test_binary_host_mirror env
process.env.npm_config_test_binary_host_mirror = 'https://npm.taobao.org/mirrors/node-inspector/';
const opts = versioning.evaluate(mock_package_json, {});
t.equal(opts.host, 'https://npm.taobao.org/mirrors/node-inspector/');
delete process.env.npm_config_test_binary_host_mirror;
t.end();
});
test('should detect libc', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'test',
'module_path': './lib/binding/{name}-{libc}',
'remote_path': './{name}/{libc}/',
'package_name': '{module_name}-{libc}.tar.gz',
'host': 'https://some-bucket.s3-us-west-1.amazonaws.com'
}
};
const opts = versioning.evaluate(mock_package_json, { module_root: '/root' });
const expected_libc_token = detect_libc.familySync() || 'unknown';
t.comment('performing test with the following libc token: ' + expected_libc_token);
t.equal(opts.module_path, path.normalize('/root/lib/binding/test-' + expected_libc_token));
t.equal(opts.module, path.normalize('/root/lib/binding/test-' + expected_libc_token + '/test.node'));
t.equal(opts.remote_path, './test/' + expected_libc_token + '/');
t.equal(opts.package_name, 'test-' + expected_libc_token + '.tar.gz');
t.equal(opts.hosted_tarball, 'https://some-bucket.s3-us-west-1.amazonaws.com/test/' + expected_libc_token + '/test-' + expected_libc_token + '.tar.gz');
t.end();
});
//
// validate package.json versioning configurations
//
test('should verify that package.json has required properties', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'binary-module-name',
'module_path': 'binary-module-path',
'host': 'binary-path'
}
};
const requireds = Object.keys(mock_package_json);
for (let i = 0; i < requireds.length; i++) {
const package_json = Object.assign({}, mock_package_json);
delete package_json[requireds[i]];
const missing = [requireds[i]];
try {
// eslint-disable-next-line no-unused-vars
const opts = versioning.evaluate(package_json, { module_root: '/root' });
} catch (e) {
// name won't be there if it's missing but both messages say 'undefined'
const msg = package_json.name + ' package.json is not node-pre-gyp ready:\n';
const expectedMessage = msg + 'package.json must declare these properties: \n' + missing.join('\n');
t.equal(e.message, expectedMessage);
}
}
t.end();
});
test('should verify that the binary property has required properties', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'binary-module-name',
'module_path': 'binary-module-path',
'host': 'binary-path'
}
};
const requireds = Object.keys(mock_package_json.binary);
for (let i = 0; i < requireds.length; i++) {
const package_json = Object.assign({}, mock_package_json);
package_json.binary = Object.assign({}, mock_package_json.binary);
delete package_json.binary[requireds[i]];
const missing = ['binary.' + requireds[i]];
try {
// eslint-disable-next-line no-unused-vars
const opts = versioning.evaluate(package_json, { module_root: '/root' });
} catch (e) {
// name won't be there if it's missing but both messages say 'undefined'
const msg = package_json.name + ' package.json is not node-pre-gyp ready:\n';
const expectedMessage = msg + 'package.json must declare these properties: \n' + missing.join('\n');
t.equal(e.message, expectedMessage);
}
}
t.end();
});
test('should not add bucket name to hosted_path when s3ForcePathStyle is false', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'binary-module-name',
'module_path': 'binary-module-path',
'host': 'binary-path',
'bucket': 'bucket-name',
'region': 'us-west-1',
's3ForcePathStyle': false
}
};
const package_json = Object.assign({}, mock_package_json);
const opts = versioning.evaluate(package_json, { module_root: '/root' });
t.equal(opts.hosted_path, mock_package_json.binary.host + '/');
t.end();
});
test('should add bucket name to hosted_path when s3ForcePathStyle is true', (t) => {
const mock_package_json = {
'name': 'test',
'main': 'test.js',
'version': '0.1.0',
'binary': {
'module_name': 'binary-module-name',
'module_path': 'binary-module-path',
'host': 'binary-path',
'bucket': 'bucket-name',
'region': 'us-west-1',
's3ForcePathStyle': true
}
};
const package_json = Object.assign({}, mock_package_json);
const opts = versioning.evaluate(package_json, { module_root: '/root' });
t.equal(opts.hosted_path, mock_package_json.binary.host + '/' + mock_package_json.binary.bucket + '/');
t.end();
});
test('should verify host overrides staging and production values', (t) => {
const mock_package_json = {
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'
}
};
try {
const opts = versioning.evaluate(mock_package_json, { module_root: '/root' });
t.equal(opts.host, mock_package_json.binary.host + '/');
t.equal(opts.hosted_path, mock_package_json.binary.host + '/');
t.equal(opts.hosted_tarball, mock_package_json.binary.host + '/' + opts.package_name);
} catch (e) {
t.ifError(e, 'staging_host and production_host should be silently ignored');
}
t.end();
});
test('should replace "-" with "_" in custom binary host', (t) => {
const mock_package_json = {
name: 'test',
main: 'test.js',
version: '0.1.0',
binary: {
module_name: 'canvas-prebuilt',
module_path: 'build/Release',
host: 'https://github.com/node-gfx/node-canvas-prebuilt/releases/download/',
remote_path: 'v{version}',
package_name: '{module_name}-v{version}-{node_abi}-{platform}-{libc}-{arch}.tar.gz'
}
};
process.env.npm_config_canvas_prebuilt_binary_host_mirror = 'https://npm.taobao.org/mirrors/node-canvas-prebuilt/';
const opts = versioning.evaluate(mock_package_json, {});
t.equal(opts.host, 'https://npm.taobao.org/mirrors/node-canvas-prebuilt/');
delete process.env.npm_config_canvas_prebuilt_binary_host_mirror;
t.end();
});