Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
support windows env
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Nov 4, 2012
1 parent 9133995 commit 43fa054
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 176 deletions.
190 changes: 95 additions & 95 deletions lib/jscover.js
@@ -1,96 +1,96 @@
/*!
* jscover - lib/jscover.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var exec = require('child_process').exec;
var path = require('path');
var ndir = require('ndir');
var fse = require('fs-extra');
var fs = require('fs');

var root = path.dirname(__dirname);
var JSCoverPath = path.join(root, 'bin', 'JSCover-all.jar');
var JSCoverCommand = 'java -Dfile.encoding=UTF-8 -jar ' + JSCoverPath + ' -fs';

/**
* Pedding codes to support nodejs
* @type {String}
*/
var PEDDING = "/* ****** automatically generated by jscover - do not edit ******/\n\
if (typeof _$jscoverage === 'undefined') { _$jscoverage = {}; }\n\
/* ****** end - do not edit ******/\n";

module.exports = function jscover(source, target, options, callback) {
source = source || '';
target = target || '';
options = options || [];
var tmpTargetDir = path.join(path.dirname(target), '__cov__');
var tmpTarget = path.join(tmpTargetDir, path.basename(target));

var cmd = JSCoverCommand;
if (options && options.length > 0) {
cmd += ' ' + options.join(' ');
}
cmd += ' --exclude=node_modules --exclude=.git/ --exclude=.svn/';
cmd += ' --exclude=' + tmpTarget + ' --exclude=' + target;
cmd += ' ' + source + ' ' + tmpTarget;
var child = exec(cmd, function (err, stdout, stderr) {
var output = '';
if (stdout) {
output += stdout;
}
if (stderr) {
output += stderr;
if (!err) {
err = new Error(stderr.trim());
err.name = 'JSCoverError';
}
}
if (err) {
return callback(err, output);
}

var success = !stdout && !stderr;
if (!success) {
return callback(null, output);
}
ndir.walk(tmpTarget, function onDir(dirpath, items) {
var todir = dirpath.replace(tmpTarget, target);
fse.mkdirpSync(todir);
for (var i = 0; i < items.length; i++) {
var info = items[i];
var from = info[0];
var name = path.basename(from);
if (name === '.git' || name === '.svn' || name.indexOf('jscoverage') === 0) {
continue;
}
var to = path.join(todir, path.basename(from));
if (info[1].isDirectory()) {
fse.mkdirpSync(to);
} else if (info[1].isFile()) {
var content = fs.readFileSync(from);
if (path.extname(to).toLowerCase() === '.js') {
content = PEDDING + content.toString();
}
fs.writeFileSync(to, content);
}
}
}, function end() {
fse.removeSync(tmpTargetDir);
fse.removeSync(path.join(target, '__cov__'));
callback();
}, function error(err, errPath) {
console.error('%s error: %s', errPath, err);
callback(err);
});

});
/*!
* jscover - lib/jscover.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var exec = require('child_process').exec;
var path = require('path');
var ndir = require('ndir');
var fse = require('fs-extra');
var fs = require('fs');

var root = path.dirname(__dirname);
var JSCoverPath = path.join(root, 'bin', 'JSCover-all.jar');
var JSCoverCommand = 'java -Dfile.encoding=UTF-8 -jar "' + JSCoverPath + '" -fs';

/**
* Pedding codes to support nodejs
* @type {String}
*/
var PEDDING = "/* ****** automatically generated by jscover - do not edit ******/\n\
if (typeof _$jscoverage === 'undefined') { _$jscoverage = {}; }\n\
/* ****** end - do not edit ******/\n";

module.exports = function jscover(source, target, options, callback) {
source = source || '';
target = target || '';
options = options || [];
var tmpTargetDir = path.join(path.dirname(target), '__cov__');
var tmpTarget = path.join(tmpTargetDir, path.basename(target));

var cmd = JSCoverCommand;
if (options && options.length > 0) {
cmd += ' ' + options.join(' ');
}
cmd += ' --exclude=node_modules --exclude=.git/ --exclude=.svn/';
cmd += ' --exclude="' + tmpTarget + '" --exclude="' + target + '"';
cmd += ' "' + source + '" "' + tmpTarget + '"';
var child = exec(cmd, function (err, stdout, stderr) {
var output = '';
if (stdout) {
output += stdout;
}
if (stderr) {
output += stderr;
if (!err) {
err = new Error(stderr.trim());
err.name = 'JSCoverError';
}
}
if (err) {
return callback(err, output);
}

var success = !stdout && !stderr;
if (!success) {
return callback(null, output);
}
ndir.walk(tmpTarget, function onDir(dirpath, items) {
var todir = dirpath.replace(tmpTarget, target);
fse.mkdirpSync(todir);
for (var i = 0; i < items.length; i++) {
var info = items[i];
var from = info[0];
var name = path.basename(from);
if (name === '.git' || name === '.svn' || name.indexOf('jscoverage') === 0) {
continue;
}
var to = path.join(todir, path.basename(from));
if (info[1].isDirectory()) {
fse.mkdirpSync(to);
} else if (info[1].isFile()) {
var content = fs.readFileSync(from);
if (path.extname(to).toLowerCase() === '.js') {
content = PEDDING + content.toString();
}
fs.writeFileSync(to, content);
}
}
}, function end() {
fse.removeSync(tmpTargetDir);
fse.removeSync(path.join(target, '__cov__'));
callback();
}, function error(err, errPath) {
console.error('%s error: %s', errPath, err);
callback(err);
});

});
};
5 changes: 5 additions & 0 deletions runtest-cov.bat
@@ -0,0 +1,5 @@
node bin\jscover lib lib-cov

SET JSCOVER_COV=1

node_modules\.bin\mocha --globals _*jscoverage && node_modules\.bin\mocha --globals _*jscoverage --reporter html-cov > coverage.html
1 change: 1 addition & 0 deletions runtest.bat
@@ -0,0 +1 @@
node_modules\.bin\mocha --globals _*jscoverage --reporter spec
162 changes: 81 additions & 81 deletions test/jscover.test.js
@@ -1,82 +1,82 @@
/*!
* jscover - test/jscover.test.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var jscover = require('../');
var should = require('should');
var path = require('path');
var fs = require('fs');
var fse = require('fs-extra');


describe('jscover.test.js', function () {
var source = path.join(__dirname, 'lib');
var target = path.join(__dirname, 'lib-cov');

beforeEach(function () {
fse.removeSync(target);
});

it('should coverage lib to lib-cov', function (done) {
should.ok(!fs.existsSync(target));
jscover(source, target, null, function (err, stdout) {
should.not.exist(err);
should.not.exist(stdout);
should.ok(fs.existsSync(target));
done();
});
});

it('should coverage lib to lib-cov with --exclude=subdir', function (done) {
should.ok(!fs.existsSync(target));
should.ok(!fs.existsSync(path.join(target, 'subdir')));
jscover(source, target, ['--exclude=subdir'], function (err, stdout) {
should.not.exist(err);
should.not.exist(stdout);
should.ok(fs.existsSync(target));
should.ok(!fs.existsSync(path.join(target, 'subdir')));
done();
});
});

it('should return stdout when args missing', function (done) {
jscover('', null, {}, function (err) {
should.exist(err);
err.name.should.equal('JSCoverError');
err.message.should.equal("Source directory '--exclude=' is invalid");
done();
});
});

it('should return error when dir not exists', function (done) {
jscover('a', 'b', {}, function (err) {
should.exist(err);
err.name.should.equal('JSCoverError');
err.message.should.equal("Source directory 'a' is invalid");
done();
});
});

describe('utf8', function () {
it('should coverage no-ascii char success', function (done) {
should.ok(!fs.existsSync(target));
jscover(source, target, null, function (err, output) {
should.not.exist(err);
should.not.exist(output);
var regexp = fs.readFileSync(path.join(source, 'regexp.js'), 'utf8');
fs.readFileSync(path.join(target, 'regexp.js'), 'utf8').should.include(regexp);
var targetFoo = path.join(target, 'subdir', 'foo');
require(path.join(source, 'subdir', 'foo')).hello.should.equal(require(targetFoo).hello);
done();
});
});
});
/*!
* jscover - test/jscover.test.js
* Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var jscover = require('../');
var should = require('should');
var path = require('path');
var fs = require('fs');
var fse = require('fs-extra');


describe('jscover.test.js', function () {
var source = path.join(__dirname, 'lib');
var target = path.join(__dirname, 'lib-cov');

beforeEach(function () {
fse.removeSync(target);
});

it('should coverage lib to lib-cov', function (done) {
should.ok(!fs.existsSync(target));
jscover(source, target, null, function (err, stdout) {
should.not.exist(err);
should.not.exist(stdout);
should.ok(fs.existsSync(target));
done();
});
});

it('should coverage lib to lib-cov with --exclude=subdir', function (done) {
should.ok(!fs.existsSync(target));
should.ok(!fs.existsSync(path.join(target, 'subdir')));
jscover(source, target, ['--exclude=subdir'], function (err, stdout) {
should.not.exist(err);
should.not.exist(stdout);
should.ok(fs.existsSync(target));
should.ok(!fs.existsSync(path.join(target, 'subdir')));
done();
});
});

it('should return stdout when args missing', function (done) {
jscover('', null, {}, function (err) {
should.exist(err);
err.name.should.equal('JSCoverError');
err.message.should.equal("Source directory '' is invalid");
done();
});
});

it('should return error when dir not exists', function (done) {
jscover('a', 'b', {}, function (err) {
should.exist(err);
err.name.should.equal('JSCoverError');
err.message.should.equal("Source directory 'a' is invalid");
done();
});
});

describe('utf8', function () {
it('should coverage no-ascii char success', function (done) {
should.ok(!fs.existsSync(target));
jscover(source, target, null, function (err, output) {
should.not.exist(err);
should.not.exist(output);
var regexp = fs.readFileSync(path.join(source, 'regexp.js'), 'utf8');
fs.readFileSync(path.join(target, 'regexp.js'), 'utf8').should.include(regexp);
var targetFoo = path.join(target, 'subdir', 'foo');
require(path.join(source, 'subdir', 'foo')).hello.should.equal(require(targetFoo).hello);
done();
});
});
});
});

0 comments on commit 43fa054

Please sign in to comment.