diff --git a/server/terminal.js b/server/terminal.js index 5dae26bc..7a291b31 100644 --- a/server/terminal.js +++ b/server/terminal.js @@ -8,11 +8,15 @@ const noop = (req, res, next) => { noop.listen = noop; -module.exports = (config, arg) => { +function _getModule(a) { + return require(a); +} + +module.exports = (config, arg, {getModule = _getModule} = {}) => { if (!config('terminal')) return noop; - const [e, terminalModule] = tryCatch(require, config('terminalPath')); + const [e, terminalModule] = tryCatch(getModule, config('terminalPath')); if (!e && !arg) return terminalModule; diff --git a/server/terminal.spec.js b/server/terminal.spec.js index 71abca17..e857aaab 100644 --- a/server/terminal.spec.js +++ b/server/terminal.spec.js @@ -2,13 +2,8 @@ const {test, stub} = require('supertape'); -const mockRequire = require('mock-require'); - const terminal = require('./terminal'); const {createConfigManager} = require('./cloudcmd'); -const terminalPath = './terminal'; - -const {stopAll} = mockRequire; test('cloudcmd: terminal: disabled', (t) => { const config = createConfigManager(); @@ -33,13 +28,12 @@ test('cloudcmd: terminal: disabled: listen', (t) => { test('cloudcmd: terminal: enabled', (t) => { const term = stub(); const arg = 'hello'; + const config = stub().returns(true); + const getModule = stub().returns(term); - mockRequire(terminalPath, term); - - const terminal = require(terminalPath); - terminal(arg); - - stopAll(); + terminal(config, arg, { + getModule, + }); t.calledWith(term, [arg], 'should call terminal'); t.end(); @@ -67,16 +61,15 @@ test('cloudcmd: terminal: enabled: no string', (t) => { test('cloudcmd: terminal: no arg', (t) => { const gritty = {}; - - mockRequire('gritty', gritty); + const getModule = stub().returns(gritty); const config = createConfigManager(); config('terminal', true); config('terminalPath', 'gritty'); - const result = terminal(config); - - stopAll(); + const result = terminal(config, '', { + getModule, + }); t.equal(result, gritty); t.end();