Use Sinon to stub the logger instead of manual stubbing

This commit is contained in:
Jérémie Astori 2018-03-20 01:54:04 -04:00
parent d1548572d4
commit c86ea9463d
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
4 changed files with 36 additions and 52 deletions

View File

@ -13,30 +13,27 @@ function timestamp() {
return colors.dim(time); return colors.dim(time);
} }
/* eslint-disable no-console */ module.exports = {
exports.error = function(...args) { /* eslint-disable no-console */
error(...args) {
console.error(timestamp(), colors.red("[ERROR]"), ...args); console.error(timestamp(), colors.red("[ERROR]"), ...args);
}; },
warn(...args) {
exports.warn = function(...args) {
console.error(timestamp(), colors.yellow("[WARN]"), ...args); console.error(timestamp(), colors.yellow("[WARN]"), ...args);
}; },
info(...args) {
exports.info = function(...args) {
console.log(timestamp(), colors.blue("[INFO]"), ...args); console.log(timestamp(), colors.blue("[INFO]"), ...args);
}; },
debug(...args) {
exports.debug = function(...args) {
console.log(timestamp(), colors.green("[DEBUG]"), ...args); console.log(timestamp(), colors.green("[DEBUG]"), ...args);
}; },
raw(...args) {
exports.raw = function(...args) {
console.log(...args); console.log(...args);
}; },
/* eslint-enable no-console */
/* eslint-enable no-console */ prompt(options, callback) {
exports.prompt = (options, callback) => {
options.prompt = [timestamp(), colors.cyan("[PROMPT]"), options.text].join(" "); options.prompt = [timestamp(), colors.cyan("[PROMPT]"), options.text].join(" ");
read(options, callback); read(options, callback);
},
}; };

View File

@ -1,24 +1,21 @@
"use strict"; "use strict";
const expect = require("chai").expect; const expect = require("chai").expect;
const stub = require("sinon").stub;
const TestUtil = require("../../util"); const TestUtil = require("../../util");
let packages; let packages;
describe("packages", function() { describe("packages", function() {
let originalLogInfo;
beforeEach(function() { beforeEach(function() {
originalLogInfo = log.info; stub(log, "info");
log.info = () => {};
delete require.cache[require.resolve("../../../src/plugins/packages")]; delete require.cache[require.resolve("../../../src/plugins/packages")];
packages = require("../../../src/plugins/packages"); packages = require("../../../src/plugins/packages");
}); });
afterEach(function() { afterEach(function() {
log.info = originalLogInfo; log.info.restore();
}); });
describe(".getStylesheets", function() { describe(".getStylesheets", function() {
@ -51,8 +48,9 @@ describe("packages", function() {
describe(".loadPackages", function() { describe(".loadPackages", function() {
it("should display report about loading packages", function() { it("should display report about loading packages", function() {
// Mock `log.info` to extract its effect into a string // Mock `log.info` to extract its effect into a string
log.info.restore();
let stdout = ""; let stdout = "";
log.info = TestUtil.mockLogger((str) => stdout += str); stub(log, "info").callsFake(TestUtil.sanitizeLog((str) => stdout += str));
packages.loadPackages(); packages.loadPackages();

View File

@ -1,25 +1,20 @@
"use strict"; "use strict";
const expect = require("chai").expect; const expect = require("chai").expect;
const stub = require("sinon").stub;
const TestUtil = require("../../util"); const TestUtil = require("../../util");
const Utils = require("../../../src/command-line/utils"); const Utils = require("../../../src/command-line/utils");
describe("Utils", function() { describe("Utils", function() {
describe(".extraHelp", function() { describe(".extraHelp", function() {
let originalRaw;
beforeEach(function() {
originalRaw = log.raw;
});
afterEach(function() { afterEach(function() {
log.raw = originalRaw; log.raw.restore();
}); });
it("should start and end with empty lines to display correctly with --help", function() { it("should start and end with empty lines to display correctly with --help", function() {
// Mock `log.raw` to extract its effect into an array // Mock `log.raw` to extract its effect into an array
const stdout = []; const stdout = [];
log.raw = TestUtil.mockLogger((str) => stdout.push(str)); stub(log, "raw").callsFake(TestUtil.sanitizeLog((str) => stdout.push(str)));
Utils.extraHelp(); Utils.extraHelp();
@ -36,7 +31,7 @@ describe("Utils", function() {
it("should contain information about THELOUNGE_HOME env var", function() { it("should contain information about THELOUNGE_HOME env var", function() {
// Mock `log.raw` to extract its effect into a concatenated string // Mock `log.raw` to extract its effect into a concatenated string
let stdout = ""; let stdout = "";
log.raw = TestUtil.mockLogger((str) => stdout += str); stub(log, "raw").callsFake(TestUtil.sanitizeLog((str) => stdout += str));
Utils.extraHelp(); Utils.extraHelp();
@ -119,18 +114,12 @@ describe("Utils", function() {
}); });
describe("when given the same key multiple times", function() { describe("when given the same key multiple times", function() {
let originalWarn;
beforeEach(function() {
originalWarn = log.warn;
});
afterEach(function() { afterEach(function() {
log.warn = originalWarn; log.warn.restore();
}); });
it("should not override options", function() { it("should not override options", function() {
log.warn = () => {}; stub(log, "warn");
expect(Utils.parseConfigOptions("foo=baz", {foo: "bar"})) expect(Utils.parseConfigOptions("foo=baz", {foo: "bar"}))
.to.deep.equal({foo: "bar"}); .to.deep.equal({foo: "bar"});
@ -138,7 +127,7 @@ describe("Utils", function() {
it("should display a warning", function() { it("should display a warning", function() {
let warning = ""; let warning = "";
log.warn = TestUtil.mockLogger((str) => warning += str); stub(log, "warn").callsFake(TestUtil.sanitizeLog((str) => warning += str));
Utils.parseConfigOptions("foo=bar", {foo: "baz"}); Utils.parseConfigOptions("foo=bar", {foo: "baz"});

View File

@ -24,7 +24,7 @@ MockClient.prototype.createMessage = function(opts) {
return message; return message;
}; };
function mockLogger(callback) { function sanitizeLog(callback) {
return function(...args) { return function(...args) {
// Concats and removes ANSI colors. See https://stackoverflow.com/a/29497680 // Concats and removes ANSI colors. See https://stackoverflow.com/a/29497680
const stdout = args.join(" ").replace( const stdout = args.join(" ").replace(
@ -51,5 +51,5 @@ module.exports = {
createWebserver() { createWebserver() {
return express(); return express();
}, },
mockLogger, sanitizeLog,
}; };