From 10307291a330c50c88ae2679e9b3755a84ebea23 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 25 Apr 2017 11:56:49 +0300 Subject: [PATCH] fix(terminal) config: terminal and terminalPath checked before set --- server/cloudcmd.js | 2 +- server/terminal.js | 11 ++++++++--- test/server/terminal.js | 18 ++++++++++++------ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/server/cloudcmd.js b/server/cloudcmd.js index 5f3e7202..a4158363 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -180,7 +180,7 @@ function listen(prefix, socket) { prefix: prefix + '/console', }); - config('terminal') && terminal.listen(socket, { + config('terminal') && terminal().listen(socket, { authCheck, prefix: prefix + '/gritty', }); diff --git a/server/terminal.js b/server/terminal.js index bbd6e740..04689966 100644 --- a/server/terminal.js +++ b/server/terminal.js @@ -6,9 +6,11 @@ const config = require('./config'); const noop = () => {}; noop.listen = noop; -module.exports = getTerminal(config('terminal')); +module.exports = (arg) => { + return getTerminal(config('terminal'), arg); +}; -function getTerminal(term) { +function getTerminal(term, arg) { if (!term) return noop; @@ -18,9 +20,12 @@ function getTerminal(term) { result = require(config('terminalPath')); }); - if (!e) + if (!e && !arg) return result; + if (!e) + return result(arg); + config('terminal', false); console.log(`cloudcmd --terminal: ${e.message}`); diff --git a/test/server/terminal.js b/test/server/terminal.js index eb9b3efa..03651faa 100644 --- a/test/server/terminal.js +++ b/test/server/terminal.js @@ -7,16 +7,19 @@ const sinon = diff(require('sinon')); const configPath = '../../server/config'; const terminalPath = '../../server/terminal'; + test('cloudcmd: terminal: disabled', (t) => { clean(terminalPath); stub(configPath, () => { return false; }); - const terminal = require(terminalPath); - const fn = terminal(); + const terminal = require('../../server/terminal'); - t.notOk(fn, 'should return noop'); + const fn = terminal(); + const noop = () => {}; + + t.equal(String(fn), String(noop), 'should return noop'); clean(configPath); require(configPath); @@ -28,8 +31,9 @@ test('cloudcmd: terminal: disabled: listen', (t) => { clean(terminalPath); stub(configPath, () => false); - const terminal = require('../../server/terminal'); - const fn = terminal.listen(); + const terminal = require(terminalPath); + + const fn = terminal().listen(); t.notOk(fn, 'should return noop'); @@ -45,7 +49,9 @@ test('cloudcmd: terminal: enabled', (t) => { clean(terminalPath); stub(configPath, () => true); - require(terminalPath); + + const terminal = require(terminalPath); + terminal(); const msg = 'cloudcmd --terminal: path must be a string';