Skip to content

Commit bbe0e41

Browse files
committed
Fixed bug and added test coverage for case. Now obeys production s3_host option when package.json has only host key specified.
1 parent 2a52ea0 commit bbe0e41

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

lib/util/versioning.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,8 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
367367
// the environment variable has priority over the the command line.
368368
let targetHost = process.env.node_pre_gyp_s3_host || options.s3_host;
369369

370-
// if value is not one of the allowed or the matching key is not found in package.json
371-
// silently ignore the option
372-
if (['production', 'staging', 'development'].indexOf(targetHost) === -1 || !package_json.binary[`${targetHost}_host`]) {
370+
// if value is not one of the allowed silently ignore the option
371+
if (['production', 'staging', 'development'].indexOf(targetHost) === -1) {
373372
targetHost = '';
374373
}
375374

@@ -379,17 +378,20 @@ module.exports.evaluate = function(package_json, options, napi_build_version) {
379378
let hostData = package_json.binary.host;
380379

381380
// when a valid target is specified by user, the host is from that target (or 'host')
382-
if (targetHost === 'staging') {
381+
if (targetHost === 'production') {
382+
// all set. catch case so as to not change host based on commands.
383+
}
384+
else if (targetHost === 'staging' && package_json.binary.staging_host) {
383385
hostData = package_json.binary.staging_host;
384-
} else if (targetHost === 'development') {
386+
} else if (targetHost === 'development' && package_json.binary.development_host) {
385387
hostData = package_json.binary.development_host;
386-
} else if (!targetHost && (package_json.binary.development_host || package_json.binary.staging_host)) {
388+
} else if ((package_json.binary.development_host || package_json.binary.staging_host)) {
387389
// when host not specifically set via command line or environment variable
388390
// but staging and/or development host are present in package.json
389391
// for any command (or command chain) that includes publish or unpublish
390392
// default to lower host (development, and if not preset, staging).
391393
if (options.argv && options.argv.remain.some((item) => (item === 'publish' || item === 'unpublish'))) {
392-
if (package_json.binary.development_host) {
394+
if (!targetHost && package_json.binary.development_host) {
393395
hostData = package_json.binary.development_host;
394396
} else if (package_json.binary.staging_host) {
395397
hostData = package_json.binary.staging_host;

test/versioning.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,66 @@ test('should use host specified by the --s3_host option', (t) => {
758758
};
759759
};
760760

761+
const parsed_package_json = {
762+
name: 'test',
763+
main: 'test.js',
764+
version: '0.1.0',
765+
binary: {
766+
module_name: 'binary-module-name',
767+
module_path: 'binary-module-path',
768+
host: 's3-production-path',
769+
development_host: 's3-development-path',
770+
staging_host: 's3-staging-path'
771+
}
772+
};
773+
774+
const hosts = ['production', 'staging', 'development'];
775+
const cmds = ['install', 'info', 'publish', 'unpublish'];
776+
cmds.forEach((cmd) => {
777+
hosts.forEach((host) => {
778+
const checkAgainst = host !== 'production' ? `${host}_host` : 'host';
779+
const cloned = JSON.parse(JSON.stringify(parsed_package_json));
780+
const opts = versioning.evaluate(cloned, makeOoptions(cmd, host));
781+
782+
t.equal(opts.host, parsed_package_json.binary[checkAgainst] + '/');
783+
t.equal(opts.hosted_path, parsed_package_json.binary[checkAgainst] + '/');
784+
t.equal(opts.hosted_tarball, parsed_package_json.binary[checkAgainst] + '/' + opts.package_name);
785+
});
786+
});
787+
cmds.forEach((cmd) => {
788+
hosts.forEach((host) => {
789+
parsed_package_json.binary = {
790+
module_name: 'binary-module-name',
791+
module_path: 'binary-module-path',
792+
host: { endpoint: 's3-production-path' },
793+
development_host: { endpoint: 's3-development-path' },
794+
staging_host: { endpoint: 's3-staging-path' }
795+
};
796+
797+
const checkAgainst = host !== 'production' ? `${host}_host` : 'host';
798+
const cloned = JSON.parse(JSON.stringify(parsed_package_json));
799+
const opts = versioning.evaluate(cloned, makeOoptions(cmd, host));
800+
801+
t.equal(opts.host, parsed_package_json.binary[checkAgainst].endpoint + '/');
802+
t.equal(opts.hosted_path, parsed_package_json.binary[checkAgainst].endpoint + '/');
803+
t.equal(opts.hosted_tarball, parsed_package_json.binary[checkAgainst].endpoint + '/' + opts.package_name);
804+
});
805+
});
806+
t.end();
807+
});
808+
809+
test('should use host specified by the --s3_host option (production_host used)', (t) => {
810+
const makeOoptions = (cmd, host) => {
811+
return {
812+
s3_host: host,
813+
argv: {
814+
remain: [cmd],
815+
cooked: [cmd, '--s3_host', host],
816+
original: [cmd, `--s3_host=${host}`]
817+
}
818+
};
819+
};
820+
761821
const parsed_package_json = {
762822
name: 'test',
763823
main: 'test.js',

0 commit comments

Comments
 (0)