From c86ea9463de3b1f9e96ef9200baa43e80f041bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Tue, 20 Mar 2018 01:54:04 -0400 Subject: [PATCH] Use Sinon to stub the logger instead of manual stubbing --- src/log.js | 47 ++++++++++++++---------------- test/plugins/packages/indexTest.js | 12 ++++---- test/src/command-line/utilsTest.js | 25 +++++----------- test/util.js | 4 +-- 4 files changed, 36 insertions(+), 52 deletions(-) diff --git a/src/log.js b/src/log.js index 17608572..b627c874 100644 --- a/src/log.js +++ b/src/log.js @@ -13,30 +13,27 @@ function timestamp() { return colors.dim(time); } -/* eslint-disable no-console */ -exports.error = function(...args) { - console.error(timestamp(), colors.red("[ERROR]"), ...args); -}; +module.exports = { + /* eslint-disable no-console */ + error(...args) { + console.error(timestamp(), colors.red("[ERROR]"), ...args); + }, + warn(...args) { + console.error(timestamp(), colors.yellow("[WARN]"), ...args); + }, + info(...args) { + console.log(timestamp(), colors.blue("[INFO]"), ...args); + }, + debug(...args) { + console.log(timestamp(), colors.green("[DEBUG]"), ...args); + }, + raw(...args) { + console.log(...args); + }, + /* eslint-enable no-console */ -exports.warn = function(...args) { - console.error(timestamp(), colors.yellow("[WARN]"), ...args); -}; - -exports.info = function(...args) { - console.log(timestamp(), colors.blue("[INFO]"), ...args); -}; - -exports.debug = function(...args) { - console.log(timestamp(), colors.green("[DEBUG]"), ...args); -}; - -exports.raw = function(...args) { - console.log(...args); -}; - -/* eslint-enable no-console */ - -exports.prompt = (options, callback) => { - options.prompt = [timestamp(), colors.cyan("[PROMPT]"), options.text].join(" "); - read(options, callback); + prompt(options, callback) { + options.prompt = [timestamp(), colors.cyan("[PROMPT]"), options.text].join(" "); + read(options, callback); + }, }; diff --git a/test/plugins/packages/indexTest.js b/test/plugins/packages/indexTest.js index 417d6fa6..0777534f 100644 --- a/test/plugins/packages/indexTest.js +++ b/test/plugins/packages/indexTest.js @@ -1,24 +1,21 @@ "use strict"; const expect = require("chai").expect; +const stub = require("sinon").stub; const TestUtil = require("../../util"); let packages; describe("packages", function() { - let originalLogInfo; - beforeEach(function() { - originalLogInfo = log.info; - - log.info = () => {}; + stub(log, "info"); delete require.cache[require.resolve("../../../src/plugins/packages")]; packages = require("../../../src/plugins/packages"); }); afterEach(function() { - log.info = originalLogInfo; + log.info.restore(); }); describe(".getStylesheets", function() { @@ -51,8 +48,9 @@ describe("packages", function() { describe(".loadPackages", function() { it("should display report about loading packages", function() { // Mock `log.info` to extract its effect into a string + log.info.restore(); let stdout = ""; - log.info = TestUtil.mockLogger((str) => stdout += str); + stub(log, "info").callsFake(TestUtil.sanitizeLog((str) => stdout += str)); packages.loadPackages(); diff --git a/test/src/command-line/utilsTest.js b/test/src/command-line/utilsTest.js index ff36856f..6e1ec680 100644 --- a/test/src/command-line/utilsTest.js +++ b/test/src/command-line/utilsTest.js @@ -1,25 +1,20 @@ "use strict"; const expect = require("chai").expect; +const stub = require("sinon").stub; const TestUtil = require("../../util"); const Utils = require("../../../src/command-line/utils"); describe("Utils", function() { describe(".extraHelp", function() { - let originalRaw; - - beforeEach(function() { - originalRaw = log.raw; - }); - afterEach(function() { - log.raw = originalRaw; + log.raw.restore(); }); it("should start and end with empty lines to display correctly with --help", function() { // Mock `log.raw` to extract its effect into an array const stdout = []; - log.raw = TestUtil.mockLogger((str) => stdout.push(str)); + stub(log, "raw").callsFake(TestUtil.sanitizeLog((str) => stdout.push(str))); Utils.extraHelp(); @@ -36,7 +31,7 @@ describe("Utils", function() { it("should contain information about THELOUNGE_HOME env var", function() { // Mock `log.raw` to extract its effect into a concatenated string let stdout = ""; - log.raw = TestUtil.mockLogger((str) => stdout += str); + stub(log, "raw").callsFake(TestUtil.sanitizeLog((str) => stdout += str)); Utils.extraHelp(); @@ -119,18 +114,12 @@ describe("Utils", function() { }); describe("when given the same key multiple times", function() { - let originalWarn; - - beforeEach(function() { - originalWarn = log.warn; - }); - afterEach(function() { - log.warn = originalWarn; + log.warn.restore(); }); it("should not override options", function() { - log.warn = () => {}; + stub(log, "warn"); expect(Utils.parseConfigOptions("foo=baz", {foo: "bar"})) .to.deep.equal({foo: "bar"}); @@ -138,7 +127,7 @@ describe("Utils", function() { it("should display a warning", function() { let warning = ""; - log.warn = TestUtil.mockLogger((str) => warning += str); + stub(log, "warn").callsFake(TestUtil.sanitizeLog((str) => warning += str)); Utils.parseConfigOptions("foo=bar", {foo: "baz"}); diff --git a/test/util.js b/test/util.js index bf9f3c25..1f433d4b 100644 --- a/test/util.js +++ b/test/util.js @@ -24,7 +24,7 @@ MockClient.prototype.createMessage = function(opts) { return message; }; -function mockLogger(callback) { +function sanitizeLog(callback) { return function(...args) { // Concats and removes ANSI colors. See https://stackoverflow.com/a/29497680 const stdout = args.join(" ").replace( @@ -51,5 +51,5 @@ module.exports = { createWebserver() { return express(); }, - mockLogger, + sanitizeLog, };