diff --git a/server/validate.js b/server/validate.js index 83048bb4..518e4079 100644 --- a/server/validate.js +++ b/server/validate.js @@ -12,7 +12,7 @@ function root(dir, fn) { if (dir === '/') return; - + const fs = require('fs'); fs.stat(dir, (error) => { diff --git a/test/server/validate.js b/test/server/validate.js index 1c068887..bd028f71 100644 --- a/test/server/validate.js +++ b/test/server/validate.js @@ -1,7 +1,15 @@ 'use strict'; const test = require('tape'); +const sinon = require('sinon'); + const before = require('../before'); +const dir = '../..'; + +const validatePath = `${dir}/server/validate`; +const exitPath = `${dir}/server/exit`; + +const validate = require(validatePath); test('validate: root: bad', (t) => { const config = { @@ -19,3 +27,37 @@ test('validate: root: bad', (t) => { t.end(); }); +test('validate: root: /', (t) => { + const fn = sinon.stub(); + validate.root('/', fn); + + t.notOk(fn.called, 'should not call fn'); + t.end(); +}); + +test('validate: editor: not valid', (t) => { + const fn = sinon.stub(); + + clean(); + require(exitPath); + stub(exitPath, fn); + + const {editor} = require(validatePath); + const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only'; + + editor('hello'); + + t.ok(fn.calledWith(msg), 'should call fn'); + + t.end(); +}); + +function clean() { + delete require.cache[require.resolve(validatePath)]; + delete require.cache[require.resolve(exitPath)]; +} + +function stub(name, fn) { + require.cache[require.resolve(name)].exports = fn; +} +