From bf90bf2295e29393e07a45143c29af0425ede58f Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 20 Mar 2024 13:25:44 +0200 Subject: [PATCH] feature: server: validate: get rid of mock-require --- server/validate.js | 30 ++++----- server/validate.spec.js | 131 ++++++++++++++-------------------------- 2 files changed, 62 insertions(+), 99 deletions(-) diff --git a/server/validate.js b/server/validate.js index 1332ce0c..e44cdfbe 100644 --- a/server/validate.js +++ b/server/validate.js @@ -1,53 +1,53 @@ 'use strict'; +const {statSync: _statSync} = require('node:fs'); const tryCatch = require('try-catch'); -const exit = require('./exit'); -const {getColumns} = require('./columns'); +const _exit = require('./exit'); +const {getColumns: _getColumns} = require('./columns'); const isString = (a) => typeof a === 'string'; -module.exports.root = (dir, config) => { +module.exports.root = (dir, config, {exit = _exit, statSync = _statSync} = {}) => { if (!isString(dir)) throw Error('dir should be a string'); - + if (dir === '/') return; - + if (config('dropbox')) return; - - const {statSync} = require('node:fs'); + const [error] = tryCatch(statSync, dir); - + if (error) return exit('cloudcmd --root: %s', error.message); }; -module.exports.editor = (name) => { +module.exports.editor = (name, {exit = _exit} = {}) => { const reg = /^(dword|edward|deepword)$/; - + if (!reg.test(name)) exit('cloudcmd --editor: could be "dword", "edward" or "deepword" only'); }; -module.exports.packer = (name) => { +module.exports.packer = (name, {exit = _exit} = {}) => { const reg = /^(tar|zip)$/; - + if (!reg.test(name)) exit('cloudcmd --packer: could be "tar" or "zip" only'); }; -module.exports.columns = (type) => { +module.exports.columns = (type, {exit = _exit, getColumns = _getColumns} = {}) => { const addQuotes = (a) => `"${a}"`; const all = Object .keys(getColumns()) .concat(''); - + const names = all .filter(Boolean) .map(addQuotes) .join(', '); - + if (!all.includes(type)) exit(`cloudcmd --columns: can be only one of: ${names}`); }; diff --git a/server/validate.spec.js b/server/validate.spec.js index b9bfa727..14869907 100644 --- a/server/validate.spec.js +++ b/server/validate.spec.js @@ -1,41 +1,29 @@ 'use strict'; -const fs = require('node:fs'); - const {test, stub} = require('supertape'); - const tryCatch = require('try-catch'); -const mockRequire = require('mock-require'); -const dir = '..'; -const validatePath = `${dir}/server/validate`; - -const cloudcmdPath = `${dir}/server/cloudcmd`; -const validate = require(validatePath); -const cloudcmd = require(cloudcmdPath); -const columnsPath = `${dir}/server/columns`; - -const exitPath = `${dir}/server/exit`; -const {reRequire, stopAll} = mockRequire; +const validate = require('./validate'); +const cloudcmd = require('./cloudcmd'); test('validate: root: bad', (t) => { const config = { root: Math.random(), }; - + const [e] = tryCatch(cloudcmd, { config, }); - + t.equal(e.message, 'dir should be a string', 'should throw'); t.end(); }); test('validate: root: config', (t) => { const config = stub().returns(true); - + validate.root('/hello', config); - + t.calledWith(config, ['dropbox'], 'should call config'); t.end(); }); @@ -43,102 +31,77 @@ test('validate: root: config', (t) => { test('validate: root: /', (t) => { const fn = stub(); validate.root('/', fn); - + t.notCalled(fn, 'should not call fn'); t.end(); }); test('validate: root: stat', (t) => { - const fn = stub(); - const {statSync} = fs; - + const config = stub(); const error = 'ENOENT'; - - fs.statSync = () => { - throw Error(error); - }; - - mockRequire(exitPath, fn); - - const {root} = reRequire(validatePath); - - root('hello', fn); - + const statSync = stub().throws(Error(error)); + const exit = stub(); + + validate.root('hello', config, { + statSync, + exit, + }); + const msg = 'cloudcmd --root: %s'; - - fs.statSync = statSync; - - stopAll(); - - t.calledWith(fn, [msg, error], 'should call fn'); + + t.calledWith(exit, [msg, error], 'should call fn'); t.end(); }); test('validate: packer: not valid', (t) => { - const fn = stub(); - - mockRequire(exitPath, fn); - - const {packer} = reRequire(validatePath); + const exit = stub(); const msg = 'cloudcmd --packer: could be "tar" or "zip" only'; - - packer('hello'); - - stopAll(); - - t.calledWith(fn, [msg], 'should call fn'); + + validate.packer('hello', { + exit, + }); + + t.calledWith(exit, [msg], 'should call fn'); t.end(); }); test('validate: editor: not valid', (t) => { - const fn = stub(); - - mockRequire(exitPath, fn); - - const {editor} = reRequire(validatePath); + const exit = stub(); const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only'; - - editor('hello'); - - stopAll(); - - t.calledWith(fn, [msg], 'should call fn'); + + validate.editor('hello', { + exit, + }); + + t.calledWith(exit, [msg], 'should call fn'); t.end(); }); test('validate: columns', (t) => { - const fn = stub(); - mockRequire(exitPath, fn); - - const {columns} = require(validatePath); - - columns('name-size-date'); - - stopAll(); - - t.notCalled(fn, 'should not call exit'); + const exit = stub(); + + validate.columns('name-size-date', { + exit, + }); + + t.notCalled(exit, 'should not call exit'); t.end(); }); test('validate: columns: wrong', (t) => { - const fn = stub(); const getColumns = stub().returns({ 'name-size-date': '', 'name-size': '', }); - - mockRequire(exitPath, fn); - mockRequire(columnsPath, { + + const exit = stub(); + const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"'; + + validate.columns('hello', { + exit, getColumns, }); - - const {columns} = reRequire(validatePath); - const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"'; - - columns('hello'); - - stopAll(); - - t.calledWith(fn, [msg], 'should call exit'); + + t.calledWith(exit, [msg], 'should call exit'); t.end(); });