Simplify version guard

- Updated index.js test to run in Node.js and browsers
- Simplified message and clarified case where two of the same versions would conflict
This commit is contained in:
Braydon Fuller 2015-05-18 14:49:29 -04:00
parent 7e3993578c
commit 1c8ebc0eb5
2 changed files with 18 additions and 43 deletions

View File

@ -1,23 +1,21 @@
'use strict';
var bitcore = module.exports; var bitcore = module.exports;
// module information // module information
bitcore.version = 'v' + require('./package.json').version; bitcore.version = 'v' + require('./package.json').version;
bitcore.versionGuard = function(version) {
if (global._bitcore !== undefined) { if (version !== undefined) {
var versions = bitcore.version + ' and ' + global._bitcore; var message = 'More than one instance of bitcore found with versions: ' + bitcore.version +
var message = 'More than one instance of bitcore found with different versions: ' + versions; ' and ' + version + '. Please make sure to require bitcore and check that submodules do' +
if (typeof window === 'undefined') { ' not also include their own bitcore dependency.';
message += '. Make sure there are no version conflicts between package.json files of your ' + throw new Error(message);
'dependencies. This could also happen when a package depends on a git repository.';
} else {
message += '. Make sure any scripts included don\'t contain their own bitcore bundle.';
} }
};
throw new Error(message); bitcore.versionGuard(global._bitcore);
}
global._bitcore = bitcore.version; global._bitcore = bitcore.version;
// crypto // crypto
bitcore.crypto = {}; bitcore.crypto = {};
bitcore.crypto.BN = require('./lib/crypto/bn'); bitcore.crypto.BN = require('./lib/crypto/bn');
bitcore.crypto.ECDSA = require('./lib/crypto/ecdsa'); bitcore.crypto.ECDSA = require('./lib/crypto/ecdsa');

View File

@ -1,39 +1,16 @@
'use strict'; 'use strict';
var expect = require('chai').expect; var should = require('chai').should();
var bitcore = require('../'); var bitcore = require('../');
// current tests works only in node.js describe('#versionGuard', function() {
var bdescribe = typeof window === 'undefined' ? describe : xdescribe
bdescribe('index.js', function() {
var bitcoreModulePath;
var bitcoreModule;
before(function() {
bitcoreModulePath = require.resolve('../');
bitcoreModule = require.cache[bitcoreModulePath];
delete require.cache[bitcoreModulePath];
});
after(function() {
require.cache[bitcoreModulePath] = bitcoreModule;
});
function importBitcore() {
require('../');
}
it('global._bitcore should be defined', function() { it('global._bitcore should be defined', function() {
expect(global._bitcore).to.equal(bitcore.version); should.equal(global._bitcore, bitcore.version);
}); });
it('throw error on importing other bitcore module', function() { it('throw an error if version is already defined', function() {
expect(importBitcore).to.throw(Error); (function() {
}); bitcore.versionGuard('version');
}).should.throw('More than one instance of bitcore');
it('throw error on importing with defined window', function () {
global.window = 'window hack';
expect(importBitcore).to.throw(Error);
delete global.window;
}); });
}); });