Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial Commit of YUITest Coverage wrapper for nodejs

  • Loading branch information...
commit 8b8b001bf3abfdc0cabb79ede92fc1ed489c6a33 1 parent 9dcd553
@davglass davglass authored
View
43 nodejs-coverage-tests/tests.js
@@ -0,0 +1,43 @@
+var YUITest = require('yuitest'),
+ fs = require('fs'),
+ path = require('path'),
+ Assert = YUITest.Assert,
+ coverage = require('../nodejs-coverage/lib/index'),
+ suite = new YUITest.TestSuite('Node.js Coverage Wrapper');
+
+
+suite.add(new YUITest.TestCase({
+ name: 'coverage testing',
+ 'test: string passing': function() {
+ var test = this;
+ coverage.cover('var foo = function() { var bar; };', function(err, data) {
+ test.resume(function() {
+ Assert.isNull(err, 'err object should be null');
+ Assert.isTrue(data.indexOf('_yuitest_coverage') > -1, 'Failed to cover file');
+ });
+ });
+ test.wait();
+ },
+ 'test: file passing': function() {
+ var test = this;
+ coverage.cover(path.join(__dirname, '../nodejs-coverage/lib/index.js'), function(err, data) {
+ test.resume(function() {
+ Assert.isNull(err, 'err object should be null');
+ Assert.isTrue(data.indexOf('_yuitest_coverage') > -1, 'Failed to cover file');
+ });
+ });
+ test.wait();
+ },
+ 'test: bad file': function() {
+ var test = this;
+ coverage.cover('/foo/bar/bax', function(err, data) {
+ test.resume(function() {
+ Assert.isNotNull(err, 'err object should not be null');
+ });
+ });
+ test.wait();
+ }
+}));
+
+
+YUITest.TestRunner.add(suite);
View
3  nodejs-coverage/Makefile
@@ -0,0 +1,3 @@
+all:
+ @echo "Copying Jar file"
+ cp ../java/build/yuitest-coverage.jar ./build
View
BIN  nodejs-coverage/build/yuitest-coverage.jar
Binary file not shown
View
78 nodejs-coverage/lib/index.js
@@ -0,0 +1,78 @@
+
+var spawn = require('child_process').spawn,
+ fs = require('fs'),
+ path = require('path'),
+ jar, idx = 0,
+ exists = fs.exists || path.exists,
+ lists = fs.readdirSync(path.join(__dirname, '../build'));
+
+lists.some(function(item) {
+ if (path.extname(item) === '.jar') {
+ jar = path.join(__dirname, '../build/', item);
+ return true;
+ }
+});
+
+exports.jar = jar;
+
+
+var cover = function(item, charset, callback) {
+ if (typeof charset === 'function') {
+ callback = charset;
+ charset = 'utf8';
+ }
+
+ getFile(item, function(err, file, rm) {
+ coverFile(file, charset, callback, rm);
+ });
+};
+
+var getFile = function(str, callback) {
+ exists(str, function(y) {
+ if (y) {
+ callback(null, str);
+ } else {
+ //Write Temp File..
+ var tmpFile = path.join(__dirname, 'tmp-' + (new Date()).getTime() + '.' + (++idx));
+ fs.writeFile(tmpFile, str, 'utf8', function(err) {
+ callback(null, tmpFile, true);
+ });
+ }
+ });
+};
+
+
+var coverFile = function(file, charset, callback, rm) {
+
+ var args = [
+ '-jar',
+ jar,
+ '--charset',
+ charset,
+ file
+ ], buffer = '', errBuffer = '', child;
+
+ child = spawn('java', args, {
+ stdio: ['pipe', 'pipe', 'pipe']
+ });
+
+ child.stdout.on('data', function(chunk) {
+ buffer += chunk;
+ });
+ child.stderr.on('data', function(chunk) {
+ errBuffer += chunk;
+ });
+
+ child.on('exit', function() {
+ var err = null;
+ if (errBuffer) {
+ err = errBuffer;
+ }
+ if (rm) {
+ fs.unlink(file);
+ }
+ callback(err, buffer, errBuffer);
+ });
+};
+
+exports.cover = cover;
View
6 nodejs-coverage/package.json
@@ -0,0 +1,6 @@
+{
+ "name": "yuitest-coverage",
+ "description": "Node.js wrapper for yuitest-coverage java package",
+ "version": "0.0.1",
+ "main": "./lib/index.js"
+}
Please sign in to comment.
Something went wrong with that request. Please try again.