Skip to content

Commit

Permalink
Implemented plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Nov 17, 2013
1 parent aff8a46 commit 5a07343
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 21 deletions.
17 changes: 10 additions & 7 deletions Makefile
Expand Up @@ -4,15 +4,18 @@ test:
@NODE_ENV=test ./node_modules/.bin/mocha --reporter $(REPORTER) ./test/*.js

test-cov: lib-cov
@COMPONENT_JSCOVERAGE_COV=1 $(MAKE) test REPORTER=html-cov > coverage.html
@rm -rf lib-cov

lib-cov:
@./node_modules/.bin/jscoverage ./index.js ./lib-cov/index.js
@COMPONENT_JSCOVERAGE_COV=1 $(MAKE) test REPORTER=html-cov > ./coverage.html
@rm -rf ./lib-cov

test-coveralls: lib-cov
echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@COMPONENT_JSCOVERAGE_COV=1 $(MAKE) test REPORTER=mocha-lcov-reporter | ./node_modules/.bin/coveralls
@rm -rf lib-cov
@rm -rf ./lib-cov

lib-cov:
@./node_modules/.bin/jscoverage ./index.js ./lib-cov/index.js

clean:
@rm ./coverage.html

.PHONY: test
.PHONY: test test-cov test-coveralls lib-cov clean
37 changes: 34 additions & 3 deletions index.js
Expand Up @@ -2,8 +2,12 @@
* Import(s)
*/

var processFile = require('jscoverage');
var debug = require('debug')('component-jscoverage');
var path = require('path');
var fs = require('fs');
var read = fs.readFileSync;
var write = fs.writeFileSync;
var debug = require('debug')('component:jscoverage');
var generateJsCoverage = require('jscoverage').process;

/**
* Export(s)
Expand All @@ -13,5 +17,32 @@ module.exports = plugin;


function plugin (builder, options) {
return 1 + 1;
builder.hook('before scripts', function (pkg, next) {
var scripts = pkg.config.scripts;
debug('before scripts: %j', scripts);
if (!scripts) return next();

var ignore_scripts = [];
scripts.forEach(function (script) {
var ext = path.extname(script);
if (ext !== '.js') return;

var script_path = pkg.path(script);
var str = read(script_path, 'utf8').toString();
var content = generateJsCoverage(script_path, str);
var coverage_script_path = path.normalize(path.dirname(script_path)
+ '/' + path.basename(script_path, ext) + '-cov' + ext);
write(coverage_script_path, content, 'utf8');
pkg.addFile('scripts', path.basename(script, ext) + '-cov' + ext);
ignore_scripts.push(script);
});
debug('after scripts: %j', scripts);

ignore_scripts.forEach(function (script) {
pkg.removeFile('scripts', script);
});
debug('build scripts: %j', scripts);

next();
});
}
6 changes: 0 additions & 6 deletions test/fixtures/component.json

This file was deleted.

8 changes: 8 additions & 0 deletions test/fixtures/hello/component.json
@@ -0,0 +1,8 @@
{
"name": "hello",
"scripts": [
"hello.js",
"world.js",
"hello.coffee"
]
}
3 changes: 3 additions & 0 deletions test/fixtures/hello/hello.coffee
@@ -0,0 +1,3 @@
a = 1
b = 1
c = a + b
4 changes: 4 additions & 0 deletions test/fixtures/hello/hello.js
@@ -0,0 +1,4 @@
var a = 1;
var b = 2;
var c = a + b;
console.log("ret:", c);
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/null/component.json
@@ -0,0 +1,3 @@
{
"name": "null"
}
4 changes: 4 additions & 0 deletions test/fixtures/null/hello.js
@@ -0,0 +1,4 @@
var a = 1;
var b = 2;
var c = a + b;
console.log("ret:", c);
93 changes: 88 additions & 5 deletions test/index.js
Expand Up @@ -4,17 +4,100 @@
* import(s)
*/

var fs = require('fs');
var exists = fs.openSync;
var open = fs.openSync;
var read = fs.readFileSync;
var unlink = fs.unlinkSync;
var generateJsCoverage = require('jscoverage').process;
var expect = require('expect.js');
var Builder = require('component-builder');
var jscover = require(process.env.COMPONENT_JSCOVERAGE_COV ? '../lib-cov/' : '../');
var plugin = require(process.env.COMPONENT_JSCOVERAGE_COV ? '../lib-cov/' : '../');


/**
* test(s)
*/
describe('component-jsoverage', function () {
it('should be 2', function (done) {
expect(jscover()).to.eql(2);
done();

describe('component-jscoverage', function () {
var hello_path = __dirname + '/fixtures/hello/hello.js';
var world_path = __dirname + '/fixtures/hello/world.js';
var hello_coverage_path = __dirname + '/fixtures/hello/hello-cov.js';
var world_coverage_path = __dirname + '/fixtures/hello/world-cov.js';

describe('exist scripts', function () {
before(function (done) {
this.hello_coverage_str = generateJsCoverage(hello_path, read(hello_path, 'utf8').toString());
this.world_coverage_str = generateJsCoverage(world_path, read(world_path, 'utf8').toString());

var builder = new Builder(__dirname + '/fixtures/hello');
builder.use(plugin);
builder.build(function (err, res) {
if (err) return done(err);
this.resource = res;
done();
}.bind(this));
});

after(function (done) {
unlink(hello_coverage_path);
unlink(world_coverage_path);
done();
});

describe('generated coverage file', function () {
describe('javascript', function () {
it('expect to exists', function (done) {
expect(open(hello_coverage_path, 'r')).to.be.ok();
expect(read(hello_coverage_path, 'utf8')).to.eql(this.hello_coverage_str)
expect(open(world_coverage_path, 'r')).to.be.ok();
expect(read(world_coverage_path, 'utf8')).to.eql(this.world_coverage_str)
done();
});
});
describe('other lang', function () {
it('expect to not exists', function (done) {
expect(function () { open(__dirname + '/fixtures/hello/coffee-cov.js'); }).to.throwException(function (e) {
expect(e).to.be.an(Error);
done();
});
});
});
});

describe('build javascript', function () {
it('expect to contain coverage script string', function (done) {
expect(this.resource.js).to.contain(this.hello_coverage_str);
expect(this.resource.js).to.contain(this.world_coverage_str);
done();
});
});
});

describe('not exist scripts', function () {
before(function (done) {
var builder = new Builder(__dirname + '/fixtures/null');
builder.use(plugin);
builder.build(function (err, res) {
if (err) return done(err);
this.resource = res;
done();
}.bind(this));
});

describe('generated coverage file', function () {
it('expect to not exists', function (done) {
expect(function () { open(__dirname + '/fixtures/null/hello-cov.js'); }).to.throwException(function (e) {
expect(e).to.be.an(Error);
done();
});
});
});
describe('build javascript', function () {
it('exptect to be empty', function (done) {
expect(this.resource.js).to.be.empty();
done();
});
});
});
});

0 comments on commit 5a07343

Please sign in to comment.