Skip to content

Commit

Permalink
added tests for ar
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Hallman committed Mar 12, 2013
1 parent 1261aed commit cfea35b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/apt-repo.js
Expand Up @@ -10,6 +10,10 @@ var gpg = require('./gpg');

var ARCHITECTURES = exports.ARCHITECTURES = ['i386', 'amd64', 'all'];

//process.on('uncaughtException', function(err) {
// console.log(err);
//});

if (!fs.existsSync('./tmp/')) {
fs.mkdirSync('./tmp/');
}
Expand Down
5 changes: 3 additions & 2 deletions lib/ar.js
Expand Up @@ -37,8 +37,9 @@ exports.extractFile = function(ar_file, packed_file) {
return error;
}
var offset = AR_HEADER_LENGTH;
while (true) {
fs.readSync(fd, buffer_header, 0, AR_FILE_HEADER_LENGTH, offset);
var bytesRead = 1;
while (bytesRead > 0) {
bytesRead = fs.readSync(fd, buffer_header, 0, AR_FILE_HEADER_LENGTH, offset);

var fileheader = buffer_header.toString();
var filename = fileheader.substr(0, 16).trim() // see header for magic numbers
Expand Down
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -9,7 +9,8 @@
"url": "git://github.com/genehallman/node-apt-repo.git"
},
"scripts": {
"start": "node ./lib/apt-repo"
"start": "node ./lib/apt-repo",
"test": "./node_modules/.bin/mocha --ui exports tests/*.test.js --reporter spec"
},
"dependencies": {
"tar": ">=0.1.14",
Expand All @@ -21,5 +22,9 @@
},
"engines": {
"node": ">=0.6.0"
},
"devDependencies": {
"mocha": "*",
"should": "*"
}
}
36 changes: 36 additions & 0 deletions tests/ar.test.js
@@ -0,0 +1,36 @@
var ar = require('../lib/ar');
var should = require('should');

module.exports = {

'test .extractFile() when packed file exists': function(done) {
var fileContents = ar.extractFile("tests/fixtures/test.ar", "test_file.txt");
fileContents.constructor.should.equal(Buffer);
fileContents.toString().should.equal('this is a test\n');
done();
},

'test .extractFile() when packed file doesnt exist': function(done) {
var fileContents = ar.extractFile("tests/fixtures/test.ar", "test_file2.txt");
fileContents.constructor.should.equal(Object);
fileContents.status.should.equal('error');
fileContents.message.should.equal('Packed file not found');
done();
},

'test .extractFile() when archive file doesnt exist': function(done) {
(function(){
ar.extractFile("nonexistant.ar", "test_file2.txt");
}).should.throw()
done();
},

'test .extractFile() when archive file is broken': function(done) {
var fileContents = ar.extractFile("tests/fixtures/broken.ar", "test_file.txt");
fileContents.constructor.should.equal(Object);
fileContents.status.should.equal('error');
fileContents.message.should.equal('Missing file header');
done();
}

};
1 change: 1 addition & 0 deletions tests/fixtures/broken.ar
@@ -0,0 +1 @@
this is not a very good archive
4 changes: 4 additions & 0 deletions tests/fixtures/test.ar
@@ -0,0 +1,4 @@
!<arch>
test_file.txt 1363128337 501 20 100644 15 `
this is a test

0 comments on commit cfea35b

Please sign in to comment.