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
This commit is contained in:
Jérémie Astori 2017-12-09 02:11:05 -05:00
parent 6077a1ae67
commit b90c224a99
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
4 changed files with 67 additions and 1 deletions

View File

@ -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() {

View File

@ -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) => {

View File

@ -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");
});
});
});

View File

@ -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,
};