Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
add logger tests, fix logger bug with maxLength, add coverage reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon Hall committed Dec 7, 2015
1 parent 10ec821 commit eac4cde
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 5 deletions.
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: 48dxbsCqHkimJwy14eDE51M4GcRXK62km
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Thumbs.db
/releases/
/tmp/
typings/
coverage
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
language: node_js
node_js:
- "4.2.2"
after_script:
- npm run coverage-unit
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls
8 changes: 4 additions & 4 deletions app/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

'use strict';

var ipc = require('electron-safe-ipc/guest');
var events = require('events');
var util = require('util');

Expand All @@ -30,10 +29,11 @@ util.inherits(Logger, events.EventEmitter);
* #_realize
*/
Logger.prototype._realize = function() {
var index = this._output.indexOf('\n') + 1;
var entries = this._output.split('\n');

if (this._output.length > this._maxLength) {
this._output = this._output.substr(index, this._output.length);
while (this._output.length > this._maxLength) {
entries.shift();
this._output = entries.join('\n');
}

this.emit('log');
Expand Down
95 changes: 95 additions & 0 deletions app/test/unit/logger.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

var expect = require('chai').expect;
var Logger = require('../../lib/logger');

describe('Logger', function() {

describe('@constructor', function() {

it('should create an instance without the `new` keyword', function() {
expect(Logger()).to.be.instanceOf(Logger);
});

it('should create an instance with the `new` keyword', function() {
expect(new Logger()).to.be.instanceOf(Logger);
});

it('should inherit from EventEmitter', function() {
var logger = new Logger();
expect(typeof logger.emit).to.equal('function');
expect(typeof logger.removeListener).to.equal('function');
expect(typeof logger.on).to.equal('function');
});

it('should start with empty output', function() {
var logger = new Logger();
expect(logger._output).to.equal('');
});

it('should set maximum length correctly', function() {
var logger = new Logger();
expect(logger._maxLength).to.equal(65536);
});

});

describe('#_realize', function() {

it('should emit a `log` event', function(done) {
var logger = new Logger();
logger.once('log', done);
logger._realize();
});

it('should trim the output to the max length', function() {
var logger = new Logger();
logger._output = (new Array(600)).join(new Array(200).join('0') + '\n');
logger._realize();
expect(logger._output.length).to.be.lessThan(logger._maxLength);
});

});

describe('#append', function() {

it('should add the log record to the end of the output', function() {
var logger = new Logger();
logger.append('one');
logger.append('two');
logger.append('three');
var entries = logger._output.split('\n');
expect(entries[0]).to.equal('one');
expect(entries[1]).to.equal('two');
expect(entries[2]).to.equal('three');
});

});

describe('#prepend', function() {

it('should add the log record to the beginning of the output', function() {
var logger = new Logger();
logger.prepend('one');
logger.prepend('two');
logger.prepend('three');
var entries = logger._output.split('\n');
expect(entries[2]).to.equal('one');
expect(entries[1]).to.equal('two');
expect(entries[0]).to.equal('three');
});

});

describe('#clear', function() {

it('should empty the logger output', function() {
var logger = new Logger();
logger._output = 'g0rd0n w45 h3r3';
logger.clear();
expect(logger._output).to.equal('');
});

});

});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"devDependencies": {
"asar": "^0.7.2",
"chai": "^3.4.1",
"coveralls": "^2.11.4",
"electron-mocha": "^0.6.3",
"electron-prebuilt": "^0.32.2",
"fs-jetpack": "^0.7.0",
Expand All @@ -10,6 +11,7 @@
"gulp-less": "^3.0.3",
"gulp-sourcemaps": "^1.5.2",
"gulp-util": "^3.0.6",
"istanbul": "^0.4.1",
"q": "^1.4.1",
"sinon": "^1.17.2",
"tree-kill": "^0.1.1",
Expand All @@ -27,7 +29,10 @@
"start": "node ./tasks/start",
"test": "npm run test-unit && npm run test-integration",
"test-unit": "./node_modules/.bin/electron-mocha app/test/unit/**",
"test-integration": "./node_modules/.bin/electron-mocha --renderer app/test/integration/**"
"test-integration": "./node_modules/.bin/electron-mocha --renderer app/test/integration/**",
"coverage-unit": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha app/test/unit/** -- --recursive",
"coverage-integration": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --renderer app/test/integration/** -- --recursive",
"coverage": "npm run coverage-unit && npm run coverage-integration"
},
"dependencies": {
"async": "^1.5.0",
Expand Down

0 comments on commit eac4cde

Please sign in to comment.