UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

148 lines (146 loc) 6.26 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _sinon = _interopRequireDefault(require("sinon")); var _cliSpinners = require("cli-spinners"); var _driverLogging = require("../driver-logging"); /* eslint-env mocha */ describe('driver-logging', function () { let sandbox; let clock; let stdoutWrite; let cursorTo; let moveCursor; let consoleLog; let originalIsTTY; let originalCursorTo; let originalMoveCursor; beforeEach(function () { sandbox = _sinon.default.createSandbox(); clock = sandbox.useFakeTimers(); stdoutWrite = sandbox.stub(process.stdout, 'write').returns(true); originalCursorTo = process.stdout.cursorTo; originalMoveCursor = process.stdout.moveCursor; process.stdout.cursorTo = function () {}; process.stdout.moveCursor = function () {}; cursorTo = sandbox.stub(process.stdout, 'cursorTo'); moveCursor = sandbox.stub(process.stdout, 'moveCursor'); consoleLog = sandbox.stub(console, 'log'); originalIsTTY = process.stdout.isTTY; process.stdout.isTTY = true; }); afterEach(function () { if ((0, _driverLogging.isDriverLogging)()) { (0, _driverLogging.stopDriverLogging)({ success: true }); } process.stdout.isTTY = originalIsTTY; sandbox.restore(); if (originalCursorTo === undefined) { delete process.stdout.cursorTo; } else { process.stdout.cursorTo = originalCursorTo; } if (originalMoveCursor === undefined) { delete process.stdout.moveCursor; } else { process.stdout.moveCursor = originalMoveCursor; } }); describe('startDriverLogging / isDriverLogging', function () { it('should mark logging as active and write the initial spinner frame', function () { (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(false); (0, _driverLogging.startDriverLogging)('Doing thing'); (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(true); (0, _chai.expect)(stdoutWrite.called).to.equal(true); const written = stdoutWrite.getCalls().map(c => c.args[0]).join(''); (0, _chai.expect)(written).to.contain('Doing thing'); }); it('should advance the spinner frame on each interval tick', function () { (0, _driverLogging.startDriverLogging)('Working'); const writesBefore = stdoutWrite.callCount; clock.tick(_cliSpinners.dots.interval); (0, _chai.expect)(stdoutWrite.callCount).to.be.greaterThan(writesBefore); }); it('should wrap the spinner frame index back to 0 after exhausting all frames', function () { (0, _driverLogging.startDriverLogging)('Wrap test'); clock.tick(_cliSpinners.dots.interval * (_cliSpinners.dots.frames.length + 1)); (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(true); }); }); describe('stopDriverLogging', function () { it('should clear the logging state and emit a final success line', function () { (0, _driverLogging.startDriverLogging)('Working'); (0, _driverLogging.stopDriverLogging)({ success: true }); (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(false); }); it('should default success to true when not provided', function () { (0, _driverLogging.startDriverLogging)('Working'); (0, _driverLogging.stopDriverLogging)(); (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(false); }); it('should mark a failure when success is false', function () { (0, _driverLogging.startDriverLogging)('Working'); (0, _driverLogging.stopDriverLogging)({ success: false }); (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(false); }); it('should be a no-op if logging was never started', function () { (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(false); (0, _driverLogging.stopDriverLogging)({ success: true }); (0, _chai.expect)(stdoutWrite.called).to.equal(false); (0, _chai.expect)(cursorTo.called).to.equal(false); (0, _chai.expect)(moveCursor.called).to.equal(false); }); }); describe('non-TTY behaviour', function () { beforeEach(function () { process.stdout.isTTY = false; }); it('should print a SUCCESS line via console.log when stopping with success', function () { (0, _driverLogging.startDriverLogging)('Working'); (0, _driverLogging.stopDriverLogging)({ success: true }); const messages = consoleLog.getCalls().map(c => c.args.join(' ')); const success = messages.find(m => m.includes('SUCCESS')); (0, _chai.expect)(success).to.be.a('string'); }); it('should print a FAILURE line via console.log when stopping with success=false', function () { (0, _driverLogging.startDriverLogging)('Working'); (0, _driverLogging.stopDriverLogging)({ success: false }); const messages = consoleLog.getCalls().map(c => c.args.join(' ')); const failure = messages.find(m => m.includes('FAILURE')); (0, _chai.expect)(failure).to.be.a('string'); }); it('should not print anything during ongoing spinner ticks while non-TTY', function () { (0, _driverLogging.startDriverLogging)('Working'); const callsAtStart = consoleLog.callCount; clock.tick(_cliSpinners.dots.interval * 3); (0, _chai.expect)(consoleLog.callCount).to.equal(callsAtStart); }); }); describe('log', function () { it('should write the dimmed message to stdout while logging is active', function () { (0, _driverLogging.startDriverLogging)('Active'); stdoutWrite.resetHistory(); (0, _driverLogging.log)('hello'); (0, _chai.expect)(stdoutWrite.calledOnce).to.equal(true); (0, _chai.expect)(stdoutWrite.firstCall.args[0]).to.contain('hello'); }); it('should be a no-op when logging is not active', function () { (0, _chai.expect)((0, _driverLogging.isDriverLogging)()).to.equal(false); stdoutWrite.resetHistory(); (0, _driverLogging.log)('ignored'); (0, _chai.expect)(stdoutWrite.called).to.equal(false); }); }); });