From 6f2191aaefd375dd2d6efaad2e89ddd8e9893057 Mon Sep 17 00:00:00 2001 From: Tom Spencer Date: Wed, 18 Nov 2015 21:38:56 +0000 Subject: [PATCH] Fix colorize not working in non-TTY environments --- lib/winston/config.js | 3 +++ package.json | 1 + test/colorize-test.js | 43 +++++++++++++++++++++++++++++++ test/fixtures/scripts/colorize.js | 16 ++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 test/colorize-test.js create mode 100644 test/fixtures/scripts/colorize.js diff --git a/lib/winston/config.js b/lib/winston/config.js index bc55c1b87..1bb52fdae 100644 --- a/lib/winston/config.js +++ b/lib/winston/config.js @@ -8,6 +8,9 @@ var colors = require('colors/safe'); +// Fix colors not appearing in non-tty environments +colors.enabled = true; + var config = exports, allColors = exports.allColors = {}; diff --git a/package.json b/package.json index fae725171..910d2e7a4 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "stack-trace": "0.0.x" }, "devDependencies": { + "cross-spawn-async": "^2.0.0", "hock": "1.x.x", "std-mocks": "~1.0.0", "vows": "0.7.x" diff --git a/test/colorize-test.js b/test/colorize-test.js new file mode 100644 index 000000000..4fb137ed2 --- /dev/null +++ b/test/colorize-test.js @@ -0,0 +1,43 @@ +/* + * colorize-test.js: Tests for colorizing in non-TTY environments. + * + * (C) 2015 Tom Spencer + * MIT LICENSE + * + */ + +var path = require('path'), + vows = require('vows'), + assert = require('assert'), + winston = require('../lib/winston'), + spawn = require('cross-spawn-async'); + +var spawnTest = function(colorize, cb) { + var data = ''; + var ps = spawn(process.execPath, [path.join(__dirname, 'fixtures', 'scripts', 'colorize.js'), colorize], { stdio: 'pipe' }); + + ps.stdout.on('data', function(buf) { + data += buf.toString(); + }); + + ps.on('close', function() { + cb(null, data); + }); +}; + +vows.describe('winston/colorize').addBatch({ + "When using winston in a non-TTY environment": { + "the logger when setup with colorize: true": { + topic: function() { spawnTest(true, this.callback) }, + "should colorize": function (log) { + assert.strictEqual(log, '\u001b[32minfo\u001b[39m: Simply a test\n'); + } + }, + "the logger when setup with colorize: false": { + topic: function() { spawnTest(false, this.callback) }, + "should not colorize": function (log) { + assert.strictEqual(log, 'info: Simply a test\n'); + } + } + } +}).export(module); diff --git a/test/fixtures/scripts/colorize.js b/test/fixtures/scripts/colorize.js new file mode 100644 index 000000000..3d6df7418 --- /dev/null +++ b/test/fixtures/scripts/colorize.js @@ -0,0 +1,16 @@ +/* + * colorize.js: A test fixture for logging colorized messages + * + * (C) 2015 Tom Spencer + * MIT LICENCE + * + */ + +var winston = require('../../../lib/winston'); + +var logger = new (winston.Logger)({ + transports: [ + new winston.transports.Console({ colorize: process.argv[2] === 'true' }) + ] + }); +logger.info('Simply a test');