Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding junit report, making it possible to specify unit test files/dirs

from cmd line param and also possible to specify report from cmd line
param
  • Loading branch information...
commit baf1b95187738f4cf1df676ee16a28cc58b1a884 1 parent cd5b97e
@gitaaron authored
Showing with 292 additions and 270 deletions.
  1. +114 −112 deps/ejs.js
  2. +3 −0  lib/nodeunit.js
  3. +154 −156 lib/reporters/junit.js
  4. +21 −2 test_main.js
View
226 deps/ejs.js
@@ -8,118 +8,120 @@
/**
* Module dependencies.
*/
+define(['exports'], function(exports) {
+ var sys = require('sys');
+
+ /**
+ * Library version.
+ */
+
+ exports.version = '0.0.3';
+
+ /**
+ * Intermediate js cache.
+ *
+ * @type Object
+ */
+
+ var cache = {};
+
+ /**
+ * Clear intermediate js cache.
+ *
+ * @api public
+ */
+
+ exports.clearCache = function(){
+ cache = {};
+ };
+
+ /**
+ * Escape the given string of `html`.
+ *
+ * @param {String} html
+ * @return {String}
+ * @api private
+ */
+
+ function escape(html){
+ return String(html)
+ .replace(/&(?!\w+;)/g, '&')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/"/g, '&quot;');
+ }
-var sys = require('sys');
-
-/**
- * Library version.
- */
-
-exports.version = '0.0.3';
-
-/**
- * Intermediate js cache.
- *
- * @type Object
- */
-
-var cache = {};
-
-/**
- * Clear intermediate js cache.
- *
- * @api public
- */
-
-exports.clearCache = function(){
- cache = {};
-};
-
-/**
- * Escape the given string of `html`.
- *
- * @param {String} html
- * @return {String}
- * @api private
- */
-
-function escape(html){
- return String(html)
- .replace(/&(?!\w+;)/g, '&amp;')
- .replace(/</g, '&lt;')
- .replace(/>/g, '&gt;')
- .replace(/"/g, '&quot;');
-}
-
-/**
- * Parse the given `str` of ejs, returning the function body.
- *
- * @param {String} str
- * @return {String}
- * @api public
- */
-
-var parse = exports.parse = function(str){
- return 'var buf = [];\n'
- + "with (locals) {\nbuf.push('"
- + String(str)
- .replace(/[\r\t]/g, " ")
- .replace(/\n/g, "\\n")
- .split("<%").join("\t")
- .replace(/((^|%>)[^\t]*)'/g, "$1\r")
- .replace(/\t=(.*?)%>/g, "', escape($1) ,'")
- .replace(/\t-(.*?)%>/g, "', $1 ,'")
- .split("\t").join("');")
- .split("%>").join("buf.push('")
- .split("\r").join("\\'")
- + "');\n}\nreturn buf.join('');";
-};
-
-/**
- * Compile the given `str` of ejs into a `Function`.
- *
- * @param {String} str
- * @param {Object} options
- * @return {Function}
- * @api public
- */
-
-var compile = exports.compile = function(str, options){
- if (options.debug) sys.puts(parse(str));
- return new Function('locals, escape', parse(str));
-};
-
-/**
- * Render the given `str` of ejs.
- *
- * Options:
- *
- * - `locals` Local variables object
- * - `cache` Compiled functions are cached, requires `filename`
- * - `filename` Used by `cache` to key caches
- * - `context|scope` Function execution context
- * - `debug` Output generated function body
- *
- * @param {String} str
- * @param {Object} options
- * @return {String}
- * @api public
- */
-
-exports.render = function(str, options){
- var fn,
- options = options || {};
- if (options.cache) {
- if (options.filename) {
- fn = cache[options.filename] = compile(str, options);
+ /**
+ * Parse the given `str` of ejs, returning the function body.
+ *
+ * @param {String} str
+ * @return {String}
+ * @api public
+ */
+
+ var parse = exports.parse = function(str){
+ return 'var buf = [];\n'
+ + "with (locals) {\nbuf.push('"
+ + String(str)
+ .replace(/[\r\t]/g, " ")
+ .replace(/\n/g, "\\n")
+ .split("<%").join("\t")
+ .replace(/((^|%>)[^\t]*)'/g, "$1\r")
+ .replace(/\t=(.*?)%>/g, "', escape($1) ,'")
+ .replace(/\t-(.*?)%>/g, "', $1 ,'")
+ .split("\t").join("');")
+ .split("%>").join("buf.push('")
+ .split("\r").join("\\'")
+ + "');\n}\nreturn buf.join('');";
+ };
+
+ /**
+ * Compile the given `str` of ejs into a `Function`.
+ *
+ * @param {String} str
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+ var compile = exports.compile = function(str, options){
+ if (options.debug) sys.puts(parse(str));
+ return new Function('locals, escape', parse(str));
+ };
+
+ /**
+ * Render the given `str` of ejs.
+ *
+ * Options:
+ *
+ * - `locals` Local variables object
+ * - `cache` Compiled functions are cached, requires `filename`
+ * - `filename` Used by `cache` to key caches
+ * - `context|scope` Function execution context
+ * - `debug` Output generated function body
+ *
+ * @param {String} str
+ * @param {Object} options
+ * @return {String}
+ * @api public
+ */
+
+ exports.render = function(str, options){
+ var fn,
+ options = options || {};
+ if (options.cache) {
+ if (options.filename) {
+ fn = cache[options.filename] = compile(str, options);
+ } else {
+ throw new Error('"cache" option requires "filename".');
+ }
} else {
- throw new Error('"cache" option requires "filename".');
+ fn = compile(str, options);
}
- } else {
- fn = compile(str, options);
- }
- return fn.call(
- options.context || options.scope,
- options.locals || {},
- escape);
-};
+ return fn.call(
+ options.context || options.scope,
+ options.locals || {},
+ escape);
+ };
+ return this;
+});
View
3  lib/nodeunit.js
@@ -64,6 +64,9 @@ define(['exports', '../deps/async', './types', './utils', './core', './reporters
if (err) throw err;
async.concatSeries(files, function (file, cb) {
var name = path.basename(file);
+
+ if(file.slice(-3)!='.js') file = file+'.js'; // if file is missing extension then add it
+
exports.runModule(name, require(file), options, cb);
},
function (err, all_assertions) {
View
310 lib/reporters/junit.js
@@ -7,177 +7,175 @@
/**
* Module dependencies
*/
+define(['exports', 'lib/nodeunit', 'lib/utils', 'fs', 'path', 'deps/async', 'assert', 'child_process', 'deps/ejs'], function(exports, nodeunit, utils, fs, path, async, assert, child_process, ejs) {
-var nodeunit = require('../nodeunit'),
- utils = require('../utils'),
- fs = require('fs'),
- path = require('path'),
- async = require('../../deps/async'),
- AssertionError = require('assert').AssertionError,
- child_process = require('child_process'),
- ejs = require('../../deps/ejs');
+ var AssertionError = assert.AssertionError;
-/**
- * Reporter info string
- */
-
-exports.info = "jUnit XML test reports";
+ /**
+ * Reporter info string
+ */
+ exports.info = "jUnit XML test reports";
-/**
- * Ensures a directory exists using mkdir -p.
- *
- * @param {String} path
- * @param {Function} callback
- * @api private
- */
-var ensureDir = function (path, callback) {
- var mkdir = child_process.spawn('mkdir', ['-p', path]);
- mkdir.on('error', function (err) {
- callback(err);
- callback = function(){};
- });
- mkdir.on('exit', function (code) {
- if (code === 0) callback();
- else callback(new Error('mkdir exited with code: ' + code));
- });
-};
-
-
-/**
- * Returns absolute version of a path. Relative paths are interpreted
- * relative to process.cwd() or the cwd parameter. Paths that are already
- * absolute are returned unaltered.
- *
- * @param {String} p
- * @param {String} cwd
- * @return {String}
- * @api public
- */
-
-var abspath = function (p, /*optional*/cwd) {
- if (p[0] === '/') return p;
- cwd = cwd || process.cwd();
- return path.normalize(path.join(cwd, p));
-};
+ /**
+ * Ensures a directory exists using mkdir -p.
+ *
+ * @param {String} path
+ * @param {Function} callback
+ * @api private
+ */
+ var ensureDir = function (path, callback) {
+ var mkdir = child_process.spawn('mkdir', ['-p', path]);
+ mkdir.on('error', function (err) {
+ callback(err);
+ callback = function(){};
+ });
+ mkdir.on('exit', function (code) {
+ if (code === 0) callback();
+ else callback(new Error('mkdir exited with code: ' + code));
+ });
+ };
-/**
- * Run all tests within each module, reporting the results to the command-line,
- * then writes out junit-compatible xml documents.
- *
- * @param {Array} files
- * @api public
- */
-exports.run = function (files, opts, callback) {
- if (!opts.output) {
- console.error(
- 'Error: No output directory defined.\n' +
- '\tEither add an "output" property to your nodeunit.json config ' +
- 'file, or\n\tuse the --output command line option.'
- );
- return;
- }
- opts.output = abspath(opts.output);
- var error = function (str) {
- return opts.error_prefix + str + opts.error_suffix;
- };
- var ok = function (str) {
- return opts.ok_prefix + str + opts.ok_suffix;
- };
- var bold = function (str) {
- return opts.bold_prefix + str + opts.bold_suffix;
+ /**
+ * Returns absolute version of a path. Relative paths are interpreted
+ * relative to process.cwd() or the cwd parameter. Paths that are already
+ * absolute are returned unaltered.
+ *
+ * @param {String} p
+ * @param {String} cwd
+ * @return {String}
+ * @api public
+ */
+
+ var abspath = function (p, /*optional*/cwd) {
+ if (p[0] === '/') return p;
+ cwd = cwd || process.cwd();
+ console.log('cwd : ' + cwd);
+ return path.normalize(path.join(cwd, p));
};
- var start = new Date().getTime();
- var paths = files.map(function (p) {
- return path.join(process.cwd(), p);
- });
-
- var modules = {}
- var curModule;
-
- nodeunit.runFiles(paths, {
- testspec: opts.testspec,
- moduleStart: function (name) {
- curModule = {
- errorCount: 0,
- failureCount: 0,
- tests: 0,
- testcases: [],
- name: name
- };
- modules[name] = curModule;
- },
- testDone: function (name, assertions) {
- var testcase = {name: name};
- for (var i=0; i<assertions.length; i++) {
- var a = assertions[i];
- if (a.failed()) {
- a = utils.betterErrors(a);
- testcase.failure = {
- message: a.message,
- backtrace: a.error.stack
- };
-
- if (a.error instanceof AssertionError) {
- curModule.failureCount++;
- }
- else {
- curModule.errorCount++;
+
+ /**
+ * Run all tests within each module, reporting the results to the command-line,
+ * then writes out junit-compatible xml documents.
+ *
+ * @param {Array} files
+ * @api public
+ */
+
+ exports.run = function (files, opts, callback) {
+ if (!opts.output) {
+ console.error(
+ 'Error: No output directory defined.\n' +
+ '\tEither add an "output" property to your nodeunit.json config ' +
+ 'file, or\n\tuse the --output command line option.'
+ );
+ return;
+ }
+ opts.output = abspath(opts.output);
+ var error = function (str) {
+ return opts.error_prefix + str + opts.error_suffix;
+ };
+ var ok = function (str) {
+ return opts.ok_prefix + str + opts.ok_suffix;
+ };
+ var bold = function (str) {
+ return opts.bold_prefix + str + opts.bold_suffix;
+ };
+
+ var start = new Date().getTime();
+ var paths = files.map(function (p) {
+ return path.join(process.cwd(), p);
+ });
+
+ var modules = {}
+ var curModule;
+
+ nodeunit.runFiles(paths, {
+ testspec: opts.testspec,
+ moduleStart: function (name) {
+ curModule = {
+ errorCount: 0,
+ failureCount: 0,
+ tests: 0,
+ testcases: [],
+ name: name
+ };
+ modules[name] = curModule;
+ },
+ testDone: function (name, assertions) {
+ var testcase = {name: name};
+ for (var i=0; i<assertions.length; i++) {
+ var a = assertions[i];
+ if (a.failed()) {
+ a = utils.betterErrors(a);
+ testcase.failure = {
+ message: a.message,
+ backtrace: a.error.stack
+ };
+
+ if (a.error instanceof AssertionError) {
+ curModule.failureCount++;
+ }
+ else {
+ curModule.errorCount++;
+ }
+ break;
}
- break;
}
- }
- curModule.tests++;
- curModule.testcases.push(testcase);
- },
- done: function (assertions) {
- var end = new Date().getTime();
- var duration = end - start;
-
- ensureDir(opts.output, function (err) {
- var tmpl = __dirname + "/../../share/junit.xml.ejs";
- fs.readFile(tmpl, function (err, data) {
- if (err) throw err;
- var tmpl = data.toString();
-
- async.forEach(Object.keys(modules), function (k, cb) {
- var module = modules[k];
- var rendered = ejs.render(tmpl, {
- locals: {suites: [module]}
- });
- var filename = path.join(
- opts.output,
- module.name + '.xml'
- );
- console.log('Writing ' + filename);
- fs.writeFile(filename, rendered, cb);
- },
- function (err) {
+ curModule.tests++;
+ curModule.testcases.push(testcase);
+ },
+ done: function (assertions) {
+ var end = new Date().getTime();
+ var duration = end - start;
+
+ ensureDir(opts.output, function (err) {
+ console.log('resolve : ' + path.resolve());
+ var tmpl = path.resolve() + "/share/junit.xml.ejs";
+ fs.readFile(tmpl, function (err, data) {
if (err) throw err;
- else if (assertions.failures()) {
- console.log(
- '\n' + bold(error('FAILURES: ')) +
- assertions.failures() + '/' +
- assertions.length + ' assertions failed (' +
- assertions.duration + 'ms)'
+ var tmpl = data.toString();
+
+ async.forEach(Object.keys(modules), function (k, cb) {
+ var module = modules[k];
+ var rendered = ejs.render(tmpl, {
+ locals: {suites: [module]}
+ });
+ var filename = path.join(
+ opts.output,
+ module.name + '.xml'
);
- }
- else {
- console.log(
- '\n' + bold(ok('OK: ')) + assertions.length +
- ' assertions (' + assertions.duration + 'ms)'
- );
- }
- });
+ console.log('Writing ' + filename);
+ fs.writeFile(filename, rendered, cb);
+ },
+ function (err) {
+ if (err) throw err;
+ else if (assertions.failures()) {
+ console.log(
+ '\n' + bold(error('FAILURES: ')) +
+ assertions.failures() + '/' +
+ assertions.length + ' assertions failed (' +
+ assertions.duration + 'ms)'
+ );
+ }
+ else {
+ console.log(
+ '\n' + bold(ok('OK: ')) + assertions.length +
+ ' assertions (' + assertions.duration + 'ms)'
+ );
+ }
+ });
+ });
});
- });
- }
- });
-}
+ }
+ });
+ }
+ return this;
+});
View
23 test_main.js
@@ -1,4 +1,23 @@
require([], function() {
- var testrunner = require('lib/reporters/default');
- testrunner.run(['test/example.js']);
+
+ var files = [];
+ var reporter_path = 'lib/reporters/';
+ var reporter_module = reporter_path + 'default';
+ var args = process.ARGV.slice(2);
+ var options = {};
+
+ args.forEach(function(arg) {
+ if(arg.slice(0,11)=='--reporter=') {
+ reporter_module = reporter_path + arg.slice(11);
+ } else if(arg.slice(0,9)=='--output=') {
+ options.output = arg.slice(9);
+ } else if(arg!='test_main.js') {
+ files.push(arg);
+ }
+
+ });
+
+ var testrunner = require(reporter_module);
+ testrunner.run(files, options);
+
});

0 comments on commit baf1b95

Please sign in to comment.
Something went wrong with that request. Please try again.