From b90c224a99d3246df1d650a08ae194786966e4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Sat, 9 Dec 2017 02:11:05 -0500 Subject: [PATCH] Define a raw logger to avoid using `console.log`, use it in extra help for environment variables, and add a test for this This has multiple benefits: - Respects the "Do not mock what you do not own" principle, instead we mock `log.raw` when necessary - Lets us not re-assign `console.log`, which breaks as Mocha uses `console.log` as well - Save and restore initial `log.raw` in test hooks (before/after), otherwise this would break Mocha/Chai --- src/command-line/utils.js | 2 +- src/log.js | 5 ++++ test/src/command-line/utilsTest.js | 46 ++++++++++++++++++++++++++++++ test/util.js | 15 ++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/src/command-line/utilsTest.js diff --git a/src/command-line/utils.js b/src/command-line/utils.js index f9dd3ce8..0a54cad7 100644 --- a/src/command-line/utils.js +++ b/src/command-line/utils.js @@ -16,7 +16,7 @@ class Utils { "", ` THELOUNGE_HOME Path for all configuration files and folders. Defaults to ${colors.green(Helper.expandHome(Utils.defaultHome()))}.`, "", - ].forEach((e) => console.log(e)); // eslint-disable-line no-console + ].forEach((e) => log.raw(e)); } static defaultHome() { diff --git a/src/log.js b/src/log.js index 584d4e4f..1efbdfe5 100644 --- a/src/log.js +++ b/src/log.js @@ -32,6 +32,11 @@ exports.info = function() { exports.debug = function() { console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); }; + +exports.raw = function() { + console.log.apply(console, arguments); +}; + /* eslint-enable no-console */ exports.prompt = (options, callback) => { diff --git a/test/src/command-line/utilsTest.js b/test/src/command-line/utilsTest.js new file mode 100644 index 00000000..dad5c9a8 --- /dev/null +++ b/test/src/command-line/utilsTest.js @@ -0,0 +1,46 @@ +"use strict"; + +const expect = require("chai").expect; +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; + }); + + 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)); + + Utils.extraHelp(); + + // Starts with 2 empty lines + expect(stdout[0]).to.equal("\n"); + expect(stdout[1]).to.equal("\n"); + expect(stdout[2]).to.not.equal("\n"); + + // Ends with 1 empty line + expect(stdout[stdout.length - 2]).to.not.equal("\n"); + expect(stdout[stdout.length - 1]).to.equal("\n"); + }); + + 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); + + Utils.extraHelp(); + + expect(stdout).to.include("THELOUNGE_HOME"); + }); + }); +}); diff --git a/test/util.js b/test/util.js index 6c13d06e..cf519f73 100644 --- a/test/util.js +++ b/test/util.js @@ -23,6 +23,20 @@ MockClient.prototype.createMessage = function(opts) { return message; }; +function mockLogger(callback) { + return function() { + // TODO: Use ...args with The Lounge v3: add `...args` as function argument + // and replaced the next line with `args.join(", ")` + const stdout = Array.prototype.slice.call(arguments).join(", ") + .replace( // Removes ANSI colors. See https://stackoverflow.com/a/29497680 + /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, + "" + ); + + callback(stdout + "\n"); + }; +} + module.exports = { createClient: function() { return new MockClient(); @@ -38,4 +52,5 @@ module.exports = { createWebserver: function() { return express(); }, + mockLogger, };