Skip to content

Commit

Permalink
Some cleanup, add tests for tar and zip files.
Browse files Browse the repository at this point in the history
Also improve tests.
  • Loading branch information
satazor committed Nov 11, 2012
1 parent d59e1e4 commit b3d04e9
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 30 deletions.
10 changes: 2 additions & 8 deletions lib/core/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
Expand All @@ -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];
}
Expand Down Expand Up @@ -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 () {

Expand Down Expand Up @@ -353,8 +349,6 @@ Package.prototype.extract = function () {
}.bind(this));
}.bind(this));
}.bind(this));

stream.resume();
};

Package.prototype.copy = function () {
Expand Down
139 changes: 117 additions & 22 deletions test/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,52 @@ describe('package', function () {
assert.equal(pkg.gitUrl, 'git@github.com:twitter/flight.git');
});

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, 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
next();
});

pkg.on('error', function (err) {
throw new Error(err);
});

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.zip'})
.get('/jquery.zip')
.defaultReplyHeaders({'location': redirecting_to_url + '/jquery.js'})
.get('/jquery.js')
.reply(302);

var redirect_to_scope = nock(redirecting_to_url)
.get('/jquery.zip')
.get('/jquery.js')
.reply(200, 'jquery content');

var pkg = new Package('jquery', redirecting_url + '/jquery.zip');
var pkg = new Package('jquery', redirecting_url + '/jquery.js');

pkg.on('resolve', function () {
assert(pkg.assetUrl);
assert.equal(pkg.assetUrl, redirecting_to_url + '/jquery.zip');
nock.restore();
assert.equal(pkg.assetUrl, redirecting_to_url + '/jquery.js');
next();
});

pkg.on('error', function (err) {
throw new Error(err);
});

pkg.download();
pkg.resolve();
});

it('Should clone git packages', function (next) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -200,7 +220,7 @@ describe('package', function () {
});
});

pkg.clone();
pkg.resolve();
});

it('Should have accessible file permissions on temp folder', function (next) {
Expand Down Expand Up @@ -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();
});
});

0 comments on commit b3d04e9

Please sign in to comment.