|
| 1 | +import { Yok } from "../../yok"; |
| 2 | +import { Errors } from "../../errors"; |
| 3 | +import { CommonLoggerStub } from "./stubs"; |
| 4 | +import { assert } from "chai"; |
| 5 | +import * as path from "path"; |
| 6 | +import { LoggerLevel } from "../../../constants"; |
| 7 | +const helpers = require("../../helpers"); |
| 8 | +const originalIsInteractive = helpers.isInteractive; |
| 9 | +const originalProcessExit = process.exit; |
| 10 | + |
| 11 | +describe("errors", () => { |
| 12 | + let isInteractive = false; |
| 13 | + let processExitCode = 0; |
| 14 | + |
| 15 | + before(() => { |
| 16 | + helpers.isInteractive = () => isInteractive; |
| 17 | + }); |
| 18 | + |
| 19 | + after(() => { |
| 20 | + helpers.isInteractive = originalIsInteractive; |
| 21 | + }); |
| 22 | + |
| 23 | + const getTestInjector = (): IInjector => { |
| 24 | + const testInjector = new Yok(); |
| 25 | + testInjector.register("errors", Errors); |
| 26 | + testInjector.register("logger", CommonLoggerStub); |
| 27 | + return testInjector; |
| 28 | + }; |
| 29 | + |
| 30 | + describe("beginCommand", () => { |
| 31 | + let testInjector: IInjector; |
| 32 | + let errors: IErrors; |
| 33 | + let logger: CommonLoggerStub; |
| 34 | + const errMsg = "Error Message"; |
| 35 | + let isProcessExitCalled = false; |
| 36 | + let isPrintCommandHelpSuggestionExecuted = false; |
| 37 | + let actionResult = true; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + (<any>process).exit = (code?: number): any => { |
| 41 | + isProcessExitCalled = true; |
| 42 | + processExitCode = code; |
| 43 | + }; |
| 44 | + testInjector = getTestInjector(); |
| 45 | + errors = testInjector.resolve("errors"); |
| 46 | + logger = testInjector.resolve("logger"); |
| 47 | + isProcessExitCalled = false; |
| 48 | + isPrintCommandHelpSuggestionExecuted = false; |
| 49 | + actionResult = true; |
| 50 | + isInteractive = false; |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + process.exit = originalProcessExit; |
| 55 | + }); |
| 56 | + |
| 57 | + const setupTest = (opts?: { err?: Error }): { action: () => Promise<boolean>, printCommandHelpSuggestion: () => Promise<void> } => { |
| 58 | + const action = async () => { |
| 59 | + if (opts && opts.err) { |
| 60 | + throw opts.err; |
| 61 | + } |
| 62 | + |
| 63 | + return actionResult; |
| 64 | + }; |
| 65 | + |
| 66 | + const printCommandHelpSuggestion = async () => { |
| 67 | + isPrintCommandHelpSuggestionExecuted = true; |
| 68 | + }; |
| 69 | + |
| 70 | + return { action, printCommandHelpSuggestion }; |
| 71 | + }; |
| 72 | + |
| 73 | + const assertProcessExited = () => { |
| 74 | + assert.isTrue(isProcessExitCalled, "When the action fails, process.exit must be called."); |
| 75 | + assert.equal(processExitCode, 127, "When the action fails, process.exit must be called with 127 by default."); |
| 76 | + }; |
| 77 | + |
| 78 | + const assertPrintCommandHelpSuggestionIsNotCalled = () => { |
| 79 | + assert.isFalse(isPrintCommandHelpSuggestionExecuted, "printCommandHelpSuggestion should not be called when the suggestCommandHelp is not set to the exception."); |
| 80 | + }; |
| 81 | + |
| 82 | + it("executes the passed action and does not print anything when it is successful", async () => { |
| 83 | + const { action, printCommandHelpSuggestion } = setupTest(); |
| 84 | + let result = await errors.beginCommand(action, printCommandHelpSuggestion); |
| 85 | + assert.isTrue(result, "beginCommand must return the result of the passed action."); |
| 86 | + |
| 87 | + actionResult = false; |
| 88 | + result = await errors.beginCommand(action, printCommandHelpSuggestion); |
| 89 | + assert.isFalse(result, "beginCommand must return the result of the passed action."); |
| 90 | + assert.equal(logger.errorOutput, ""); |
| 91 | + assert.equal(logger.output, ""); |
| 92 | + assert.equal(logger.traceOutput, ""); |
| 93 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("exits the process when the action fails", async () => { |
| 97 | + const { action, printCommandHelpSuggestion } = setupTest({ err: new Error(errMsg) }); |
| 98 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 99 | + assertProcessExited(); |
| 100 | + assert.equal(logger.errorOutput, errMsg + '\n'); |
| 101 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 102 | + }); |
| 103 | + |
| 104 | + describe("prints the stack trace of the error when", () => { |
| 105 | + const assertCallStack = () => { |
| 106 | + assert.isTrue(logger.errorOutput.indexOf(errMsg) !== -1, "The error output must contain the error message"); |
| 107 | + assert.isTrue(logger.errorOutput.indexOf("at Generator.next") !== -1, "The error output must contain callstack"); |
| 108 | + assert.isTrue(logger.errorOutput.indexOf(path.join("lib", "common")) !== -1, "The error output must contain path to lib/common, as this is the location of the file"); |
| 109 | + }; |
| 110 | + |
| 111 | + it("printCallStack property is set to true", async () => { |
| 112 | + const { action, printCommandHelpSuggestion } = setupTest({ err: new Error(errMsg) }); |
| 113 | + errors.printCallStack = true; |
| 114 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 115 | + assertCallStack(); |
| 116 | + assertProcessExited(); |
| 117 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("loggerLevel is TRACE", async () => { |
| 121 | + const { action, printCommandHelpSuggestion } = setupTest({ err: new Error(errMsg) }); |
| 122 | + logger.loggerLevel = LoggerLevel.TRACE; |
| 123 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 124 | + assertCallStack(); |
| 125 | + assertProcessExited(); |
| 126 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("loggerLevel is DEBUG", async () => { |
| 130 | + const { action, printCommandHelpSuggestion } = setupTest({ err: new Error(errMsg) }); |
| 131 | + logger.loggerLevel = LoggerLevel.DEBUG; |
| 132 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 133 | + assertCallStack(); |
| 134 | + assertProcessExited(); |
| 135 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it("colorizes the error message to stderr when the terminal is interactive", async () => { |
| 140 | + const { action, printCommandHelpSuggestion } = setupTest({ err: new Error(errMsg) }); |
| 141 | + isInteractive = true; |
| 142 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 143 | + assert.equal(logger.errorOutput, `\x1B[31;1m${errMsg}\x1B[0m\n`); |
| 144 | + assertProcessExited(); |
| 145 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("prints message on stdout instead of stderr when printOnStdout is set in the Error", async () => { |
| 149 | + const errObj: any = new Error(errMsg); |
| 150 | + errObj.printOnStdout = true; |
| 151 | + const { action, printCommandHelpSuggestion } = setupTest({ err: errObj }); |
| 152 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 153 | + assert.equal(logger.errorOutput, ""); |
| 154 | + assert.equal(logger.output, `${errMsg}\n`); |
| 155 | + assertProcessExited(); |
| 156 | + assertPrintCommandHelpSuggestionIsNotCalled(); |
| 157 | + }); |
| 158 | + |
| 159 | + it("suggests how to show command help when error's suggestCommandHelp is set", async () => { |
| 160 | + const errObj: any = new Error(errMsg); |
| 161 | + errObj.suggestCommandHelp = true; |
| 162 | + const { action, printCommandHelpSuggestion } = setupTest({ err: errObj }); |
| 163 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 164 | + assert.equal(logger.errorOutput, `${errMsg}\n`); |
| 165 | + assertProcessExited(); |
| 166 | + assert.isTrue(isPrintCommandHelpSuggestionExecuted, "printCommandHelpSuggestion should be called when the action fails with an error object for which suggestCommandHelp is true."); |
| 167 | + }); |
| 168 | + |
| 169 | + it("exits with passed exit code when the error has errorCode set to number", async () => { |
| 170 | + const errObj: any = new Error(errMsg); |
| 171 | + errObj.errorCode = 222; |
| 172 | + const { action, printCommandHelpSuggestion } = setupTest({ err: errObj }); |
| 173 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 174 | + assert.equal(logger.errorOutput, `${errMsg}\n`); |
| 175 | + assert.isTrue(isProcessExitCalled, "When the action fails, process.exit must be called."); |
| 176 | + assert.equal(processExitCode, errObj.errorCode, `When the action fails, process.exit must be called with ${errObj.errorCode}.`); |
| 177 | + }); |
| 178 | + |
| 179 | + it("exits with default exit code code when the error has errorCode set to string", async () => { |
| 180 | + const errObj: any = new Error(errMsg); |
| 181 | + errObj.errorCode = "222"; |
| 182 | + const { action, printCommandHelpSuggestion } = setupTest({ err: errObj }); |
| 183 | + await errors.beginCommand(action, printCommandHelpSuggestion); |
| 184 | + assert.equal(logger.errorOutput, `${errMsg}\n`); |
| 185 | + assert.isTrue(isProcessExitCalled, "When the action fails, process.exit must be called."); |
| 186 | + assert.equal(processExitCode, 127, "When the action fails, process.exit must be called with 127 by default."); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments