Skip to content

Commit

Permalink
Reporting Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Boyd committed May 14, 2015
1 parent 78ba0d9 commit 2382891
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 71 deletions.
23 changes: 0 additions & 23 deletions .gruntfile

This file was deleted.

4 changes: 2 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ var gruntConfig = {
test: {
options: {
reporter: 'spec',
quiet: false,
clearRequireCache: true
//quiet: false,
//clearRequireCache: true
},
src: ['test/*.js']
},
Expand Down
68 changes: 51 additions & 17 deletions lib/report-generator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

var html = require("html");
var fs = require('fs');
var async = require('async');

(function () {

function ReportGenerator(reportFileName, columnNames, next) {

var self = this;

next = (typeof next === 'function') ? next : function () {
};

Expand All @@ -20,11 +23,11 @@ var fs = require('fs');
return next(new Error('columnNames parameter is not an array'));
}

var _reportFileName = reportFileName;
var _columnNames = columnNames;
self._reportFileName = reportFileName;
self._columnNames = columnNames;

var headers = '';
_columnNames.forEach(function (columnName) {
self._columnNames.forEach(function (columnName) {
headers = headers.concat('<TD><pre>' + columnName + '</pre></TD>');
});

Expand All @@ -35,18 +38,44 @@ var fs = require('fs');
headers +
'</TR>';

try {
fs.writeFile(_reportFileName, block, function () {
next(null);
self._writeQueue = async.queue(function (block, next) {
try {
fs.writeFile(self._reportFileName, block, function (err) {
return next(err);
});
}
catch (ex) {
return next(new Error(ex));
}
}, 2);

self._appendQueue = async.queue(function (block, next) {
try {
fs.appendFile(self._reportFileName, block, function (err) {
return next(err);
});
}
catch (ex) {
return next(new Error(ex));
}
}, 2);

self._appendQueue.drain = function () {
fs.readFile(self._reportFileName, 'utf8', function (err, data) {

/* jshint camelcase:false */
var prettyHtml = html.prettyPrint(data, {indent_size: 2});
fs.writeFile(self._reportFileName, prettyHtml, next);
});
}
catch (ex) {
next(ex);
}
};

self._writeQueue.push(block, next);
}

ReportGenerator.prototype.writeRow = function (row, next) {

var self = this;

next = (typeof next === 'function') ? next : function () {
};

Expand All @@ -58,18 +87,23 @@ var fs = require('fs');
return next(new Error('row parameter is not an array'));
}

var cells = '';
row.forEach(function (columnName) {
cells = cells.concat('<TD><pre>' + columnName + '</pre></TD>');
var cells = '<TR>';
row.forEach(function (row) {
cells = cells.concat('<TD><pre>' + row + '</pre></TD>');
});
cells = cells.concat('</TR>');

next(null);
self._appendQueue.push(cells, next);
};

ReportGenerator.prototype.closeReport = function (next) {
next(null);

var block = '</Table>' +
'</BODY>' +
'</HTML>';

this._appendQueue.push(block, next);
};

module.exports = ReportGenerator;

}());
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@
"mocha-istanbul": "^0.2.0",
"mocha-lcov-reporter": "0.0.2",
"wd": "^0.3.11"
},
"dependencies": {
"async": "^0.9.0",
"html": "0.0.10"
}
}
119 changes: 90 additions & 29 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/*global describe, it*/
/*global describe, it, before*/

var assert = require('assert');
var chai = require('chai');
Expand All @@ -11,114 +11,175 @@ var reportGenerator = require('../lib/report-generator');

describe('report generator', function () {

var testReportName = 'results/testReport.html';
this.timeout(5000);

var generator = new reportGenerator(testReportName, ['foo'], function (err) {
assert(err === null);
});
var testReportName = 'testReport.html';

describe('public functions', function () {

describe('ReportGenerator', function () {

it('calls back with no error when properly invoked', function () {
reportGenerator(testReportName, ['foo'], function (err) {
it('calls back with no error when properly invoked', function (done) {
new reportGenerator(testReportName, ['foo'], function (err) {
assert(err === null);
done();
});
});

describe('validates reportFileName parameter', function () {

it('calls back with error if reportFileName is null', function () {
reportGenerator(null, ['foo'], function (err) {
it('calls back with error if reportFileName is null', function (done) {
new reportGenerator(null, ['foo'], function (err) {
assert(err !== null);
done();
});
});

it('calls back with error if reportFileName is invalid', function () {
reportGenerator({}, ['foo'], function (err) {
it('calls back with error if reportFileName is invalid', function (done) {
new reportGenerator('', ['foo'], function (err) {
assert(err !== null);
done();
});
});
});

describe('validates columnNames parameter', function () {

it('calls back with error if columnNames is null', function () {
it('calls back with error if columnNames is null', function (done) {
reportGenerator(testReportName, null, function (err) {
assert(err !== null);
done();
});
});

it('calls back with error if columnNames is not an array', function () {
it('calls back with error if columnNames is not an array', function (done) {
reportGenerator(testReportName, 'foo', function (err) {
assert(err !== null);
done();
});
});
});

describe('has an optional callback', function () {

it('should not throw when next is invalid', function () {
it('should not throw when next is invalid', function (done) {
(function () {
reportGenerator(testReportName, ['foo'], '');
new reportGenerator(testReportName, ['foo'], '');
done();
}).should.not.throw(Error);
});

it('should treat next as optional', function () {
it('should treat next as optional', function (done) {
(function () {
reportGenerator(testReportName, ['foo']);
new reportGenerator(testReportName, ['foo']);
done();
}).should.not.throw(Error);
});
});

describe('when writing to the report file fails', function () {

it('calls back with error ', function (done) {
var generator = new reportGenerator({}, ['foo']);
generator.closeReport(function (err) {
assert(err !== null);
done();
});
});

});
});

describe('writeRow', function () {

var generator;

before(function () {
generator = new reportGenerator(testReportName, ['foo'], function (err) {
assert(err === null);
});
});

describe('validates row parameter', function () {

it('calls back with error if row is null', function () {
it('calls back with error if row is null', function (done) {
generator.writeRow(null, function (err) {
assert(err !== null);
done();
});
});

it('calls back with error if row is not an array', function () {
it('calls back with error if row is not an array', function (done) {
generator.writeRow('foo', function (err) {
assert(err !== null);
done();
});
});
});

describe('writes multiple rows', function(){

it('writes multiple rows', function(done){

generator.writeRow(['1'], function (err) {
assert(err === null);
});

generator.writeRow(['2'], function (err) {
assert(err === null);
});

generator.closeReport(done);

});

});

describe('has an optional callback', function () {

it('should not throw when next is invalid', function () {
it('should not throw when next is invalid', function (done) {
(function () {
generator.writeRow(['foo'], '');
generator.writeRow(['1'], {});
done();
}).should.not.throw(Error);
});

it('should treat next as optional', function () {
it('should treat next as optional', function (done) {
(function () {
generator.writeRow(['foo']);
generator.writeRow(['1']);
done();
}).should.not.throw(Error);
});

});
});

describe('closeReport', function () {

describe('valid params', function () {
var generator;

it('has an optional callback', function () {
before(function () {
generator = new reportGenerator(testReportName, ['foo'], function (err) {
assert(err === null);
});
});

describe('validates parameters', function () {

it('should treat next as optional', function (done) {
(function () {
generator.closeReport();
done();
}).should.not.throw(Error);
});

it('should not throw when next is invalid', function (done) {
(function () {
generator.closeReport(function (err) {
assert(err === null);
});
generator.closeReport('');
done();
}).should.not.throw(Error);
});
});

});
});
});

0 comments on commit 2382891

Please sign in to comment.