diff --git a/lib/index.js b/lib/index.js index db71830..11d5b71 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,14 +4,6 @@ const spawn = require('cross-spawn').spawn const path = require('path') const fs = require('fs') -process.on('uncaughtException', function (e) { - if (e.message.match(/passed/gi)) { - console.log(PrintHelp()) - } - console.log(e.message) - process.exit(1) -}) - function EnvCmd (args) { const parsedArgs = ParseArgs(args) const env = ParseEnvFile(parsedArgs.envFilePath) @@ -93,9 +85,20 @@ Options: ` } +function HandleUncaughtExceptions (e) { + if (e.message.match(/passed/gi)) { + console.log(PrintHelp()) + } + console.log(e.message) + process.exit(1) +} + +process.on('uncaughtException', HandleUncaughtExceptions) + module.exports = { EnvCmd, ParseArgs, ParseEnvFile, - PrintHelp + PrintHelp, + HandleUncaughtExceptions } diff --git a/test/test.js b/test/test.js index 7835bfa..c907b27 100644 --- a/test/test.js +++ b/test/test.js @@ -4,6 +4,7 @@ const assert = require('better-assert') const describe = require('mocha').describe const it = require('mocha').it const afterEach = require('mocha').afterEach +const beforeEach = require('mocha').beforeEach const path = require('path') const proxyquire = require('proxyquire') const sinon = require('sinon') @@ -21,6 +22,7 @@ const EnvCmd = lib.EnvCmd const ParseArgs = lib.ParseArgs const ParseEnvFile = lib.ParseEnvFile const PrintHelp = lib.PrintHelp +const HandleUncaughtExceptions = lib.HandleUncaughtExceptions describe('env-cmd', function () { describe('ParseArgs', function () { @@ -109,4 +111,25 @@ describe('env-cmd', function () { assert(helpText.match(/-e/).length !== 0) }) }) + describe('HandleUncaughtExceptions', function () { + beforeEach(function () { + this.logStub = sinon.stub(console, 'log') + this.processStub = sinon.stub(process, 'exit') + }) + afterEach(function () { + this.logStub.restore() + this.processStub.restore() + }) + it('should print help text and error if error contains \'passed\'', function () { + HandleUncaughtExceptions(new Error('print help text passed now')) + assert(this.logStub.calledTwice) + this.logStub.restore() // restore here so test success logs get printed + }) + + it('should print just there error if error does not contain \'passed\'', function () { + HandleUncaughtExceptions(new Error('do not print help text now')) + assert(this.logStub.calledOnce) + this.logStub.restore() // restore here so test success logs get printed + }) + }) })