Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support windows env

  • Loading branch information...
commit 43fa054450c78082a52892401e5b5712db8f1e2f 1 parent 9133995
@fengmk2 fengmk2 authored
Showing with 182 additions and 176 deletions.
  1. +95 −95 lib/jscover.js
  2. +5 −0 runtest-cov.bat
  3. +1 −0  runtest.bat
  4. +81 −81 test/jscover.test.js
View
190 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);
+ });
+
+ });
};
View
5 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
View
1  runtest.bat
@@ -0,0 +1 @@
+node_modules\.bin\mocha --globals _*jscoverage --reporter spec
View
162 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();
+ });
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.