Several fixes.

- Fix issue with download() emiting two 'resolve' events.
- Do not fallback to package.json and to tag describe if there was an error parsing the json file
- Move out cache dir to the config.js file
- Tweak, fix and add test to the point 2.
This commit is contained in:
André Cruz
2012-10-15 09:51:33 +01:00
parent 24d126e702
commit 71cddb628e
7 changed files with 99 additions and 24 deletions

View File

@@ -1,4 +1,11 @@
var path = require('path');
var temp = process.env.TMPDIR || process.env.TMP || process.env.TEMP || process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp';
var home = (process.platform === 'win32' ? process.env.USERPROFILE : process.env.HOME) || temp;
var cache = process.platform === 'win32' ? path.resolve(process.env.APPDATA || home || temp, 'bower-cache') : path.resolve(home || temp, '.bower');
module.exports = {
directory: 'components',
json: 'component.json'
json: 'component.json',
cache: cache
};

View File

@@ -33,18 +33,7 @@ var template = require('../util/template');
var readJSON = require('../util/read-json');
var UnitWork = require('./unit_work');
var temp = process.env.TMPDIR
|| process.env.TMP
|| process.env.TEMP
|| process.platform === 'win32' ? 'c:\\windows\\temp' : '/tmp';
var home = (process.platform === 'win32'
? process.env.USERPROFILE
: process.env.HOME) || temp;
var cache = process.platform === 'win32'
? path.resolve(process.env.APPDATA || home || temp, 'bower-cache')
: path.resolve(home || temp, '.bower');
var cache = config.cache;
var Package = function (name, endpoint, manager) {
this.dependencies = {};
@@ -228,9 +217,13 @@ Package.prototype.uninstall = function () {
// Private
Package.prototype.loadJSON = function (name) {
var pathname = name || ( this.assetType ? 'index' + this.assetType : config.json );
var jsonFile = path.join(this.path, pathname);
readJSON(path.join(this.path, pathname), function (err, json) {
readJSON(jsonFile, function (err, json) {
if (err) {
// Fallback to the package.json only if the original one does not exists.
// This is because an error other than ENOENT can occur reading the file (e.g.: invalid JSON)
if (!this.assetType && fs.existsSync(jsonFile)) return this.emit('error', err);
if (!name) return this.loadJSON('package.json');
return this.assetUrl ? this.emit('loadJSON') : this.path && this.on('describeTag', function (tag) {
tag = semver.clean(tag);
@@ -270,7 +263,7 @@ Package.prototype.download = function () {
template('action', { name: 'redirect detected', shizzle: this.assetUrl })
.on('data', this.emit.bind(this, 'data'));
this.assetUrl = res.headers.location;
this.download();
return this.download();
}
res.on('data', function (data) {

View File

@@ -26,7 +26,7 @@
"stable" : "latest"
},
"scripts": {
"test": "mocha -R spec -t 10000"
"test": "mocha -R spec -t 30000"
},
"devDependencies": {
"mocha": "latest",

View File

@@ -0,0 +1 @@
{

View File

@@ -0,0 +1 @@
// Here be jQuery

View File

@@ -11,14 +11,21 @@ var path = require('path');
describe('manager', function () {
beforeEach(function (done) {
var del = 0;
if (fs.existsSync(config.directory)) {
rimraf(config.directory, function (err) {
if (err) {
throw new Error('Unable to delete local directory.');
}
done();
// Ignore the error if the local directory was not actually deleted
if (++del >= 2) done();
});
} else done();
} else if (++del >= 2) done();
if (fs.existsSync(config.cache)) {
rimraf(config.cache, function (err) {
// Ignore the error if the cache directory was not actually deleted
if (++del >= 2) done();
});
} else if (++del >= 2) done();
});
it('Should resolve JSON dependencies', function (next) {
@@ -32,6 +39,10 @@ describe('manager', function () {
next();
});
manager.on('error', function (err) {
throw new Error(err);
});
manager.resolve();
});
@@ -46,6 +57,10 @@ describe('manager', function () {
next();
});
manager.on('error', function (err) {
throw new Error(err);
});
manager.resolve();
});
@@ -60,6 +75,10 @@ describe('manager', function () {
next();
});
manager.on('error', function (err) {
throw new Error(err);
});
manager.resolve();
});
@@ -97,6 +116,10 @@ describe('manager', function () {
next();
});
manager.on('error', function (err) {
throw new Error(err);
});
manager.resolve();
});

View File

@@ -9,6 +9,24 @@ var config = require('../lib/core/config');
var Package = require('../lib/core/package');
describe('package', function () {
beforeEach(function (done) {
var del = 0;
if (fs.existsSync(config.directory)) {
rimraf(config.directory, function (err) {
// Ignore the error if the local directory was not actually deleted
if (++del >= 2) done();
});
} else if (++del >= 2) done();
if (fs.existsSync(config.cache)) {
rimraf(config.cache, function (err) {
// Ignore the error if the cache directory was not actually deleted
if (++del >= 2) done();
});
} else if (++del >= 2) done();
});
it('Should resolve git URLs properly', function () {
var pkg = new Package('jquery', 'git://github.com/jquery/jquery.git');
assert.equal(pkg.gitUrl, 'git://github.com/jquery/jquery.git');
@@ -35,7 +53,7 @@ describe('package', function () {
assert.equal(pkg.gitUrl, 'git@github.com:twitter/flight.git');
});
it('Should resolve url when we got redirected', function() {
it('Should resolve url when we got redirected', function (next) {
var redirecting_url = 'http://redirecting-url.com';
var redirecting_to_url = 'http://redirected-to-url.com';
@@ -46,13 +64,19 @@ describe('package', function () {
var redirect_to_scope = nock(redirecting_to_url)
.get('/jquery.zip')
.reply(200, "jquery content");
.reply(200, 'jquery content');
var pkg = new Package('jquery', redirecting_url + '/jquery.zip');
pkg.on('resolve', function () {
assert(pkg.assetUrl);
assert.equal(pkg.assetUrl, redirecting_to_url + '/jquery.zip');
nock.restore();
next();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.download();
@@ -110,6 +134,24 @@ describe('package', function () {
next();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.loadJSON();
});
it('Should not fallback to package.json if there is an error in the components.json', function (next) {
var pkg = new Package('jquery', __dirname + '/assets/package-invalid-json');
pkg.on('error', function (error) {
if (/parse json/i.test(error)) next();
else throw new Error(err);
});
pkg.on('loadJSON', function () {
throw new Error('Should have throw an error parsing the JSON.');
});
pkg.loadJSON();
});
@@ -118,10 +160,14 @@ describe('package', function () {
pkg.on('resolve', function () {
var deps = _.pluck(pkg.getDeepDependencies(), 'name');
assert.deepEqual(_.uniq(deps), ["package-bootstrap", "jquery-ui", "jquery"]);
assert.deepEqual(_.uniq(deps), ['package-bootstrap', 'jquery-ui', 'jquery']);
next();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.resolve();
});
@@ -171,6 +217,10 @@ describe('package', function () {
});
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.clone();
});