Some cleanup, add tests for tar and zip files.

Also improve tests.
This commit is contained in:
André Cruz
2012-11-11 16:19:17 +00:00
parent d59e1e45a8
commit b3d04e9083
2 changed files with 129 additions and 40 deletions

View File

@@ -62,7 +62,6 @@ var Package = function (name, endpoint, manager) {
this.tag = endpoint;
} else if (/^[\.\/~]\.?[^.]*\.(js|css)/.test(endpoint) && fs.statSync(endpoint).isFile()) {
this.path = path.resolve(endpoint);
this.assetType = path.extname(endpoint);
this.name = this.name.replace(this.assetType, '');
@@ -77,6 +76,7 @@ var Package = function (name, endpoint, manager) {
} else if (fileExists.sync(endpoint)) {
this.path = path.resolve(endpoint);
} else {
this.tag = endpoint.split('#', 2)[1];
}
@@ -312,11 +312,7 @@ Package.prototype.extract = function () {
var file = path.join(this.path, 'index' + this.assetType);
template('action', { name: 'unziping', shizzle: file }).on('data', this.emit.bind(this, 'data'));
var stream = fs.createReadStream(file);
stream.pause();
stream
.pipe(this.assetType === '.zip' ? unzip.Extract({ path: this.path }) : tar.Extract({ path: this.path }))
fs.createReadStream(file).pipe(this.assetType === '.zip' ? unzip.Extract({ path: this.path }) : tar.Extract({ path: this.path }))
.on('error', this.emit.bind(this, 'error'))
.on('end', function () {
@@ -353,8 +349,6 @@ Package.prototype.extract = function () {
}.bind(this));
}.bind(this));
}.bind(this));
stream.resume();
};
Package.prototype.copy = function () {

View File

@@ -53,25 +53,12 @@ describe('package', function () {
assert.equal(pkg.gitUrl, 'git@github.com:twitter/flight.git');
});
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';
var redirect_scope = nock(redirecting_url)
.defaultReplyHeaders({'location': redirecting_to_url + '/jquery.zip'})
.get('/jquery.zip')
.reply(302);
var redirect_to_scope = nock(redirecting_to_url)
.get('/jquery.zip')
.reply(200, 'jquery content');
var pkg = new Package('jquery', redirecting_url + '/jquery.zip');
it('Should resolve normal HTTP URLs', function (next) {
var pkg = new Package('bootstrap', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
pkg.on('resolve', function () {
assert(pkg.assetUrl);
assert.equal(pkg.assetUrl, redirecting_to_url + '/jquery.zip');
nock.restore();
assert.equal(pkg.assetUrl, 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
next();
});
@@ -79,7 +66,39 @@ describe('package', function () {
throw new Error(err);
});
pkg.download();
pkg.resolve();
});
it('Should resolve url when we got redirected', function (next) {
after(function () {
nock.cleanAll();
});
var redirecting_url = 'http://redirecting-url.com';
var redirecting_to_url = 'http://redirected-to-url.com';
var redirect_scope = nock(redirecting_url)
.defaultReplyHeaders({'location': redirecting_to_url + '/jquery.js'})
.get('/jquery.js')
.reply(302);
var redirect_to_scope = nock(redirecting_to_url)
.get('/jquery.js')
.reply(200, 'jquery content');
var pkg = new Package('jquery', redirecting_url + '/jquery.js');
pkg.on('resolve', function () {
assert(pkg.assetUrl);
assert.equal(pkg.assetUrl, redirecting_to_url + '/jquery.js');
next();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.resolve();
});
it('Should clone git packages', function (next) {
@@ -95,7 +114,19 @@ describe('package', function () {
throw new Error(err);
});
pkg.clone();
pkg.resolve();
});
it('Should error on clone fail', function (next) {
var pkg = new Package('random', 'git://example.com');
pkg.on('error', function (err) {
assert(err);
next();
});
pkg.resolve();
});
it('Should copy path packages', function (next) {
@@ -111,18 +142,7 @@ describe('package', function () {
throw new Error(err);
});
pkg.copy();
});
it('Should error on clone fail', function (next) {
var pkg = new Package('random', 'git://example.com');
pkg.on('error', function (err) {
assert(err);
next();
});
pkg.clone();
pkg.resolve();
});
it('Should load correct json', function (next) {
@@ -200,7 +220,7 @@ describe('package', function () {
});
});
pkg.clone();
pkg.resolve();
});
it('Should have accessible file permissions on temp folder', function (next) {
@@ -231,7 +251,82 @@ describe('package', function () {
throw new Error(err);
});
pkg.clone();
pkg.resolve();
});
it('Should download normal URL packages', function (next) {
var pkg = new Package('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
pkg.on('resolve', function () {
pkg.install();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.on('install',function () {
fs.readdir(pkg.localPath, function (err, files) {
if (err) throw new Error(err);
assert(files.indexOf('index.js') !== -1);
next();
});
});
pkg.resolve();
});
it('Should extract tar and zip files from normal URL packages', function (next) {
var pkg = new Package('jquery', 'http://github.com/satazor/SparkMD5/archive/master.zip');
pkg.on('resolve', function () {
pkg.install();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.on('install',function () {
fs.readdir(pkg.localPath, function (err, files) {
if (err) throw new Error(err);
assert(files.indexOf('index.js') === -1);
assert(files.indexOf('master.zip') === -1);
assert(files.indexOf('spark-md5.js') !== -1);
assert(files.indexOf('spark-md5.min.js') !== -1);
next();
});
});
pkg.resolve();
});
it('Should extract tar and zip files from normal URL packages and move them if the archive only contains a folder', function (next) {
var pkg = new Package('jquery', 'http://twitter.github.com/bootstrap/assets/bootstrap.zip');
pkg.on('resolve', function () {
pkg.install();
});
pkg.on('error', function (err) {
throw new Error(err);
});
pkg.on('install',function () {
fs.readdir(pkg.localPath, function (err, files) {
if (err) throw new Error(err);
assert(files.indexOf('index.js') === -1);
assert(files.indexOf('bootstrap.zip') === -1);
assert(files.indexOf('js') !== -1);
assert(files.indexOf('css') !== -1);
assert(files.indexOf('img') !== -1);
next();
});
});
pkg.resolve();
});
});