Skip to content

Commit

Permalink
Merge pull request #63 from oncletom/feature-coverage
Browse files Browse the repository at this point in the history
Add code coverage and add additional tests
  • Loading branch information
Thomas Parisot authored Sep 22, 2016
2 parents be724ca + aa612cb commit cf16c5c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ test/*.crx
test/*.zip
test/*.xml
node_modules
coverage
.nyc_output
tmp
3 changes: 2 additions & 1 deletion bin/crx.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,6 @@ function pack (dir, program) {
console.log('%s has been generated in %s', output, cwd);
});
});

}

module.exports = program;
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
"iojs": ">=1.0.0 <2.0.0"
},
"scripts": {
"test": "node ./test/index.js",
"test": "nyc tape ./test/*.js",
"posttest": "nyc report --reporter=html",
"version": "npm run changelog && git add CHANGELOG.md",
"changelog": "github-changes -o oncletom -r crx -n ${npm_package_version} --only-pulls --use-commit-body"
},
"nyc": {
"functions": 100,
"statements": 95,
"branches": 88,
"check-coverage": true,
"reporter": [
"text"
]
},
"dependencies": {
"archiver": "^1.1.0",
"commander": "^2.5.0",
Expand All @@ -32,6 +42,7 @@
},
"devDependencies": {
"github-changes": "^1.0.0",
"nyc": "^8.3.0",
"sinon": "^1.12.1",
"tape": "^3.0.3"
}
Expand Down
2 changes: 0 additions & 2 deletions src/crx.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ ChromeExtension.prototype = {
var signature = selfie.generateSignature(contents);

return selfie.generatePackage(signature, publicKey, contents);
}, function (err) {
throw new Error(err.toString());
});
},

Expand Down
6 changes: 6 additions & 0 deletions test/expectations/update.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='eoilidhiokfphdhpmhoaengdkehanjif'>
<updatecheck codebase='http://localhost:8000/myFirstExtension.crx' version='1.0' />
</app>
</gupdate>
91 changes: 55 additions & 36 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var test = require("tape");
var ChromeExtension = require("../");
var join = require("path").join;
var privateKey = fs.readFileSync(join(__dirname, "key.pem"));
var updateXml = fs.readFileSync(join(__dirname, "expectations", "update.xml"));
var sinon = require('sinon');
var sandbox = sinon.sandbox.create();

Expand All @@ -17,59 +18,77 @@ function newCrx(){
});
}

test('it should pack the test extension', function(t){
t.plan(3);
test('#ChromeExtension', function(t){
t.plan(2);

var crx = newCrx();
t.ok(ChromeExtension({}) instanceof ChromeExtension);
t.ok(newCrx());
});

crx.pack().then(function(packageData){
t.ok(packageData instanceof Buffer);

var updateXML = crx.generateUpdateXML();
test('#load', function(t){
t.plan(1);

t.ok(updateXML instanceof Buffer);
newCrx().loadContents().catch(function(err){
t.ok(err instanceof Error);
});
});

fs.writeFile(join(__dirname, "update.xml"), updateXML);
fs.writeFile(join(__dirname, "myFirstExtension.crx"), packageData);
test('#pack', function(t){
t.plan(1);

var crx = newCrx();

crx.pack().then(function(packageData){
t.ok(packageData instanceof Buffer);
})
.then(t.pass.bind(t))
.catch(t.error.bind(t));
});

test('it should pack from preloaded contents', function(t){
t.plan(3);
test('#loadContents', function(t){
t.plan(2);

var crx = newCrx();
var loadContentsSpy = sandbox.spy(crx, 'loadContents');

crx.load().then(function(){
return crx.loadContents();
})
.then(function(contentsBuffer){
t.ok(contentsBuffer instanceof Buffer);

return crx.pack(contentsBuffer);
})
.then(function(packageData){
t.ok(loadContentsSpy.callCount === 1);
t.ok(packageData instanceof Buffer);
})
.then(function() {
sandbox.restore();
})
.catch(t.error.bind(t));
return crx.loadContents();
})
.then(function(contentsBuffer){
t.ok(contentsBuffer instanceof Buffer);
t.ok(loadContentsSpy.callCount === 1);
})
.then(function() {
sandbox.restore();
})
.catch(t.error.bind(t));
});

test('it should fail if the extension content is loaded without having preliminary called the `load` method', function(t){
t.plan(1);

newCrx().loadContents().catch(function(err){
t.ok(err instanceof Error);
});
});
test('#generateUpdateXML', function(t){
t.plan(2);

t.throws(function(){ ChromeExtension({}).generateUpdateXML() }, 'No URL provided for update.xml');

test('it should fail if the public key was not set prior to trying to generate the app ID', function(t) {
t.plan(1);
var crx = newCrx();
t.throws(function() { crx.generateAppId(); }, /Public key is neither set, nor given/);

crx.pack().then(function(){
var xmlBuffer = crx.generateUpdateXML();

t.equals(xmlBuffer.toString(), updateXml.toString());
})
.catch(t.error.bind(t));
});

test('#generateAppId', function(t) {
t.plan(2);

t.throws(function() { newCrx().generateAppId(); }, /Public key is neither set, nor given/);

var crx = newCrx()

crx.generatePublicKey().then(function(publicKey){
t.equals(crx.generateAppId(publicKey), 'eoilidhiokfphdhpmhoaengdkehanjif');
})
.catch(t.error.bind(t));
});

0 comments on commit cf16c5c

Please sign in to comment.