Skip to content

Commit 32c4464

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 b00124d commit 32c4464

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
@@ -759,6 +759,66 @@ test('should use host specified by the --s3_host option', (t) => {
759759
};
760760
};
761761

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

0 commit comments

Comments
 (0)