-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from blackflux/dev
[Gally]: master <- dev
- Loading branch information
Showing
7 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
module.exports.desc = require('./util/desc'); | ||
module.exports.EnvManager = require('./util/env-manager'); | ||
module.exports.timeKeeper = require('./util/time-keeper'); | ||
module.exports.ConsoleRecorder = require('./util/console-recorder'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-disable no-console */ | ||
const assert = require('assert'); | ||
|
||
module.exports = (verbose) => { | ||
let consoleOriginal = null; | ||
const logs = []; | ||
const defaultLogs = []; | ||
const errorLogs = []; | ||
return { | ||
inject: () => { | ||
assert(consoleOriginal === null); | ||
consoleOriginal = ['log', 'info', 'error', 'warn'] | ||
.reduce((p, c) => Object.assign(p, { [c]: console[c] }), {}); | ||
logs.length = 0; | ||
defaultLogs.length = 0; | ||
errorLogs.length = 0; | ||
Object.keys(consoleOriginal).forEach((logLevel) => { | ||
console[logLevel] = (...args) => { | ||
if (verbose === true) { | ||
consoleOriginal[logLevel](...args); | ||
} | ||
logs.push(...args); | ||
(['log', 'info'].includes(logLevel) ? defaultLogs : errorLogs).push(...args); | ||
}; | ||
}); | ||
}, | ||
release: () => { | ||
assert(consoleOriginal !== null); | ||
Object.assign(console, consoleOriginal); | ||
consoleOriginal = null; | ||
}, | ||
get: () => ({ | ||
logs: logs.slice(), | ||
defaultLogs: defaultLogs.slice(), | ||
errorLogs: errorLogs.slice() | ||
}) | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable no-console */ | ||
const expect = require('chai').expect; | ||
const ConsoleRecorder = require('../../src/util/console-recorder'); | ||
|
||
const testConsole = (verbose) => { | ||
const logs = []; | ||
const consoleLogOriginal = console.log; | ||
const consoleErrorOriginal = console.error; | ||
console.log = (...args) => { | ||
logs.push(...args); | ||
}; | ||
console.error = (...args) => { | ||
logs.push(...args); | ||
}; | ||
const consoleRecorder = ConsoleRecorder(verbose); | ||
consoleRecorder.inject(); | ||
console.log('test-log1'); | ||
console.log('test-log2'); | ||
console.error('test-log3'); | ||
consoleRecorder.release(); | ||
const result = consoleRecorder.get(); | ||
expect(result.logs).to.deep.equal(['test-log1', 'test-log2', 'test-log3']); | ||
expect(result.defaultLogs).to.deep.equal(['test-log1', 'test-log2']); | ||
expect(result.errorLogs).to.deep.equal(['test-log3']); | ||
if (verbose === true) { | ||
expect(logs).to.deep.equal(['test-log1', 'test-log2', 'test-log3']); | ||
} else { | ||
expect(logs).to.deep.equal([]); | ||
} | ||
console.log = consoleLogOriginal; | ||
console.error = consoleErrorOriginal; | ||
}; | ||
|
||
describe('Testing ConsoleRecorder', () => { | ||
it('Testing Logging Silent', () => { | ||
testConsole(false); | ||
}); | ||
|
||
it('Testing Logging Verbose', () => { | ||
testConsole(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters