From c96bc2e4d6480f21f45567e039b9e43db2e78ecb Mon Sep 17 00:00:00 2001 From: indexzero Date: Fri, 27 Nov 2015 16:43:15 -0500 Subject: [PATCH] [fix refactor test] Fight mocha and "allowUncaught" (see: https://github.com/mochajs/mocha/issues/1985). Move some tests into `test/logger-legacy.test.js` --- test/exception-handler.test.js | 102 +++++++++++++++++++++++----- test/helpers.js | 60 +++++++++++++++++ test/logger-legacy.test.js | 119 +++++++++++++++++++++++++++++++++ test/logger.test.js | 98 ++++++--------------------- 4 files changed, 284 insertions(+), 95 deletions(-) create mode 100644 test/logger-legacy.test.js diff --git a/test/exception-handler.test.js b/test/exception-handler.test.js index 39d259425..833024185 100644 --- a/test/exception-handler.test.js +++ b/test/exception-handler.test.js @@ -6,28 +6,98 @@ * */ -var winston = require('../lib/winston'), +var stream = require('stream'), + assume = require('assume'), + mocha = require('mocha'), + winston = require('../lib/winston'), helpers = require('./helpers'); +// +// This is an awful and fragile hack that +// needs to be changed ASAP. +// https://github.com/mochajs/mocha/issues/1985 +// +var _runTest = mocha.Runner.prototype.runTest; +mocha.Runner.prototype.runTest = function () { + this.allowUncaught = true; + _runTest.apply(this, arguments); +}; + describe('ExceptionHandler', function () { - var logger = new winston.Logger(); - var handler = new winston.ExceptionHandler(logger); + // it('has expected methods', function () { + // var handler = helpers.exceptionHandler(); + // assume(handler.handle).is.a('function'); + // assume(handler.unhandle).is.a('function'); + // assume(handler.getAllInfo).is.a('function'); + // assume(handler.getProcessInfo).is.a('function'); + // assume(handler.getOsInfo).is.a('function'); + // assume(handler.getTrace).is.a('function'); + // }); - it('.getProcessInfo()', function () { - helpers.assertProcessInfo( - handler.getProcessInfo() - ); - }); + // it('new ExceptionHandler()', function () { + // assume(function () { + // new winston.ExceptionHandler(); + // }).throws(/Logger is required/); + // }); + + // it('new ExceptionHandler(logger)', function () { + // var logger = new winston.Logger(); + // var handler = new winston.ExceptionHandler(logger); + // assume(handler.logger).equals(logger); + // }); + + // it('.getProcessInfo()', function () { + // var handler = helpers.exceptionHandler(); + // helpers.assertProcessInfo( + // handler.getProcessInfo() + // ); + // }); + + // it('.getOsInfo()', function () { + // var handler = helpers.exceptionHandler(); + // helpers.assertOsInfo( + // handler.getOsInfo() + // ); + // }); + + // it('.getTrace(new Error)', function () { + // var handler = helpers.exceptionHandler(); + // helpers.assertTrace( + // handler.getTrace(new Error()) + // ); + // }); + + it('.handle()', function (done) { + var existing = helpers.clearExceptions(); + var writeable = new stream.Writable({ + objectMode: true, + write: function (info) { + console.dir(info); + existing.restore(); + done(); + } + }); + + var transport = new winston.transports.Stream({ stream: writeable }); + var handler = helpers.exceptionHandler({ transports: [transport] }); + + assume(handler.catcher).equals(undefined); + + transport.handleExceptions = true; + handler.handle(); + + assume(handler.catcher).is.a('function'); + assume(process.listeners('uncaughtException')) + .deep.equals([handler.catcher]); - it('.getOsInfo()', function () { - helpers.assertOsInfo( - handler.getOsInfo() - ); + helpers.throw('wtf this error'); }); - it('.getTrace()', function () { - helpers.assertTrace( - handler.getTrace(new Error()) - ); + after(function () { + // + // Restore normal `runTest` functionality + // so that we only affect the current suite. + // + mocha.Runner.prototype.runTest = _runTest; }); }); diff --git a/test/helpers.js b/test/helpers.js index b874971fd..9b9230f8c 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -10,11 +10,71 @@ var assume = require('assume'), fs = require('fs'), path = require('path'), spawn = require('child_process').spawn, + stream = require('stream'), util = require('util'), winston = require('../lib/winston'); var helpers = exports; +/** + * Returns a new winston.Logger instance which will invoke + * the `write` method onĀ each call to `.log` + * + * @param {function} write Write function for the specified stream + * @returns {Logger} A winston.Logger instance + */ +helpers.createLogger = function (write) { + var writeable = new stream.Writable({ + objectMode: true, + write: write + }); + + return new winston.Logger({ + transports: [ + new winston.transports.Stream({ stream: writeable }) + ] + }); +}; + +/** + * Creates a new ExceptionHandler instance with a new + * winston.Logger instance with the specified options + * + * @param {Object} opts Options for the logger associated + * with the ExceptionHandler + * @returns {ExceptionHandler} A new ExceptionHandler instance + */ +helpers.exceptionHandler = function (opts) { + var logger = new winston.Logger(opts); + return new winston.ExceptionHandler(logger); +}; + +/** + * Removes all listeners to `process.on('uncaughtException')` + * and returns an object that allows you to restore them later. + * + * @returns {Object} Facade to restore uncaughtException handlers. + */ +helpers.clearExceptions = function () { + var listeners = process.listeners('uncaughtException'); + process.removeAllListeners('uncaughtException'); + + return { + restore: function () { + listeners.forEach(function (fn) { + process.on('uncaughtException', fn); + }); + } + }; +}; + +/** + * Throws an exception with the specified `msg` + */ +helpers.throw = function (msg) { + throw new Error(msg); +}; + helpers.tryUnlink = function (file) { try { fs.unlinkSync(file) } catch (ex) { } diff --git a/test/logger-legacy.test.js b/test/logger-legacy.test.js new file mode 100644 index 000000000..90763d288 --- /dev/null +++ b/test/logger-legacy.test.js @@ -0,0 +1,119 @@ +/* + * logger-legacy.test.js: Tests for Legacy APIs of winston < 3.0.0 + * + * (C) 2010 Charlie Robbins + * MIT LICENSE + * + */ + +'use strict'; + +var assume = require('assume'), + path = require('path'), + stream = require('stream'), + util = require('util'), + isStream = require('isstream'), + stdMocks = require('std-mocks'), + winston = require('../lib/winston'), + LegacyTransport = require('./mocks/legacy-transport'), + TransportStream = require('winston-transport'), + format = require('../lib/winston/formats/format'), + helpers = require('./helpers'); + +describe('Logger (legacy API)', function () { + it('new Logger({ DEPRECATED })', function () { + var deprecated = [ + { colors: true }, + { emitErrs: true }, + { formatters: [] }, + { padLevels: true }, + { rewriters: [] }, + { stripColors: true } + ]; + + deprecated.forEach(function (opts) { + assume(function () { + var logger = new winston.Logger(opts) + }).throws(/Use a custom/); + }); + }); + + it('.add(LegacyTransport)', function () { + stdMocks.use(); + var logger = new winston.Logger(); + var transport = new LegacyTransport(); + logger.add(transport); + stdMocks.restore(); + var output = stdMocks.flush(); + + assume(logger._readableState.pipesCount).equals(1); + assume(logger._readableState.pipes.transport).is.an('object'); + assume(logger._readableState.pipes.transport).equals(transport); + assume(output.stderr).deep.equals(['legacy-test is a Legacy winston transport. Consider upgrading\n']); + }); + + it('.add(LegacyTransport) multiple', function () { + stdMocks.use(); + var logger = new winston.Logger({ + transports: [ + new LegacyTransport(), + new LegacyTransport(), + new LegacyTransport() + ] + }); + + stdMocks.restore(); + var output = stdMocks.flush(); + + assume(logger._readableState.pipesCount).equals(3); + var errorMsg = 'legacy-test is a Legacy winston transport. Consider upgrading\n'; + assume(output.stderr).deep.equals([errorMsg, errorMsg, errorMsg]); + }); + + it('.remove() [LegacyTransportStream]', function () { + var transports = [ + new (winston.transports.Console)(), + new (LegacyTransport)() + ]; + + var logger = new (winston.Logger)({ transports: transports }); + + assume(logger.transports.length).equals(2); + logger.remove(transports[1]); + assume(logger.transports.length).equals(1); + assume(logger.transports[0]).equals(transports[0]); + }); + + it('log(level, msg)', function (done) { + var logger = helpers.createLogger(function (info) { + assume(info).is.an('object'); + assume(info.level).equals('info'); + assume(info.message).equals('Some super awesome log message'); + assume(info.raw).is.a('string'); + done(); + }); + + logger.log('info', 'Some super awesome log message') + }); + + it('log(level, msg, meta)', function (done) { + var meta = { one: 2 }; + var logger = helpers.createLogger(function (info) { + assume(info).is.an('object'); + assume(info).equals(meta); + assume(info.level).equals('info'); + assume(info.message).equals('Some super awesome log message'); + assume(info.one).equals(2); + assume(info.raw).is.a('string'); + done(); + }); + + logger.log('info', 'Some super awesome log message', meta); + }); + + it('.cli() throws', function () { + var logger = new winston.Logger(); + + assume(logger.cli).throws(/Use a custom/); + }); +}); diff --git a/test/logger.test.js b/test/logger.test.js index c10e7cd66..b2a29dd21 100755 --- a/test/logger.test.js +++ b/test/logger.test.js @@ -1,5 +1,5 @@ /* - * log-stream.test.js: Tests for instances of the winston Logger + * logger.test.js: Tests for instances of the winston Logger * * (C) 2010 Charlie Robbins * MIT LICENSE @@ -15,9 +15,9 @@ var assume = require('assume'), isStream = require('isstream'), stdMocks = require('std-mocks'), winston = require('../lib/winston'), - LegacyTransport = require('./mocks/legacy-transport'), TransportStream = require('winston-transport'), - format = require('../lib/winston/formats/format'); + format = require('../lib/winston/formats/format'), + helpers = require('./helpers'); describe('Logger', function () { it('new Logger()', function () { @@ -46,38 +46,6 @@ describe('Logger', function () { assume(logger._readableState.pipesCount).equals(0); }); - it('.add(LegacyTransport)', function () { - stdMocks.use(); - var logger = new winston.Logger(); - var transport = new LegacyTransport(); - logger.add(transport); - stdMocks.restore(); - var output = stdMocks.flush(); - - assume(logger._readableState.pipesCount).equals(1); - assume(logger._readableState.pipes.transport).is.an('object'); - assume(logger._readableState.pipes.transport).equals(transport); - assume(output.stderr).deep.equals(['legacy-test is a Legacy winston transport. Consider upgrading\n']); - }); - - it('.add(LegacyTransport) multiple', function () { - stdMocks.use(); - var logger = new winston.Logger({ - transports: [ - new LegacyTransport(), - new LegacyTransport(), - new LegacyTransport() - ] - }); - - stdMocks.restore(); - var output = stdMocks.flush(); - - assume(logger._readableState.pipesCount).equals(3); - var errorMsg = 'legacy-test is a Legacy winston transport. Consider upgrading\n'; - assume(output.stderr).deep.equals([errorMsg, errorMsg, errorMsg]); - }); - it('.add({ invalid Transport })', function () { var logger = new winston.Logger(); assume(function () { @@ -179,20 +147,6 @@ describe('Logger', function () { assume(logger.transports[0]).equals(transports[1]); }); - it('.remove() [LegacyTransportStream]', function () { - var transports = [ - new (winston.transports.Console)(), - new (LegacyTransport)() - ]; - - var logger = new (winston.Logger)({ transports: transports }); - - assume(logger.transports.length).equals(2); - logger.remove(transports[1]); - assume(logger.transports.length).equals(1); - assume(logger.transports[0]).equals(transports[0]); - }); - it('.clear() [no transports]', function () { var logger = new (winston.Logger)(); assume(logger.transports.length).equals(0); @@ -328,21 +282,14 @@ describe('Logger (levels)', function () { describe('Logger (profile, startTimer)', function (done) { it('profile(id, info)', function (done) { - var writeable = new stream.Writable({ - objectMode: true, - write: function (info) { - assume(info).is.an('object'), - assume(info.something).equals('ok'); - assume(info.level).equals('info'); - assume(info.durationMs).is.a('number'); - assume(info.message).equals('testing1'); - assume(info.raw).is.a('string'); - done(); - } - }); - - var logger = new winston.Logger({ - transports: [new winston.transports.Stream({ stream: writeable })] + var logger = helpers.createLogger(function (info) { + assume(info).is.an('object'), + assume(info.something).equals('ok'); + assume(info.level).equals('info'); + assume(info.durationMs).is.a('number'); + assume(info.message).equals('testing1'); + assume(info.raw).is.a('string'); + done(); }); logger.profile('testing1'); @@ -355,21 +302,14 @@ describe('Logger (profile, startTimer)', function (done) { }); it('startTimer()', function (done) { - var writeable = new stream.Writable({ - objectMode: true, - write: function (info) { - assume(info).is.an('object'), - assume(info.something).equals('ok'); - assume(info.level).equals('info'); - assume(info.durationMs).is.a('number'); - assume(info.message).equals('testing1'); - assume(info.raw).is.a('string'); - done(); - } - }); - - var logger = new winston.Logger({ - transports: [new winston.transports.Stream({ stream: writeable })] + var logger = helpers.createLogger(function (info) { + assume(info).is.an('object'), + assume(info.something).equals('ok'); + assume(info.level).equals('info'); + assume(info.durationMs).is.a('number'); + assume(info.message).equals('testing1'); + assume(info.raw).is.a('string'); + done(); }); var timer = logger.startTimer();