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:
parent
6077a1ae67
commit
b90c224a99
@ -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() {
|
||||
|
@ -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) => {
|
||||
|
46
test/src/command-line/utilsTest.js
Normal file
46
test/src/command-line/utilsTest.js
Normal 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");
|
||||
});
|
||||
});
|
||||
});
|
15
test/util.js
15
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,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user