Skip to content

Commit bf90bf2

Browse files
committed
feature: server: validate: get rid of mock-require
1 parent 043ba29 commit bf90bf2

File tree

2 files changed

+62
-99
lines changed

2 files changed

+62
-99
lines changed

server/validate.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
'use strict';
22

3+
const {statSync: _statSync} = require('node:fs');
34
const tryCatch = require('try-catch');
45

5-
const exit = require('./exit');
6-
const {getColumns} = require('./columns');
6+
const _exit = require('./exit');
7+
const {getColumns: _getColumns} = require('./columns');
78
const isString = (a) => typeof a === 'string';
89

9-
module.exports.root = (dir, config) => {
10+
module.exports.root = (dir, config, {exit = _exit, statSync = _statSync} = {}) => {
1011
if (!isString(dir))
1112
throw Error('dir should be a string');
12-
13+
1314
if (dir === '/')
1415
return;
15-
16+
1617
if (config('dropbox'))
1718
return;
18-
19-
const {statSync} = require('node:fs');
19+
2020
const [error] = tryCatch(statSync, dir);
21-
21+
2222
if (error)
2323
return exit('cloudcmd --root: %s', error.message);
2424
};
2525

26-
module.exports.editor = (name) => {
26+
module.exports.editor = (name, {exit = _exit} = {}) => {
2727
const reg = /^(dword|edward|deepword)$/;
28-
28+
2929
if (!reg.test(name))
3030
exit('cloudcmd --editor: could be "dword", "edward" or "deepword" only');
3131
};
3232

33-
module.exports.packer = (name) => {
33+
module.exports.packer = (name, {exit = _exit} = {}) => {
3434
const reg = /^(tar|zip)$/;
35-
35+
3636
if (!reg.test(name))
3737
exit('cloudcmd --packer: could be "tar" or "zip" only');
3838
};
3939

40-
module.exports.columns = (type) => {
40+
module.exports.columns = (type, {exit = _exit, getColumns = _getColumns} = {}) => {
4141
const addQuotes = (a) => `"${a}"`;
4242
const all = Object
4343
.keys(getColumns())
4444
.concat('');
45-
45+
4646
const names = all
4747
.filter(Boolean)
4848
.map(addQuotes)
4949
.join(', ');
50-
50+
5151
if (!all.includes(type))
5252
exit(`cloudcmd --columns: can be only one of: ${names}`);
5353
};

server/validate.spec.js

Lines changed: 47 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,107 @@
11
'use strict';
22

3-
const fs = require('node:fs');
4-
53
const {test, stub} = require('supertape');
6-
74
const tryCatch = require('try-catch');
8-
const mockRequire = require('mock-require');
9-
const dir = '..';
10-
11-
const validatePath = `${dir}/server/validate`;
125

13-
const cloudcmdPath = `${dir}/server/cloudcmd`;
14-
const validate = require(validatePath);
15-
const cloudcmd = require(cloudcmdPath);
16-
const columnsPath = `${dir}/server/columns`;
17-
18-
const exitPath = `${dir}/server/exit`;
19-
const {reRequire, stopAll} = mockRequire;
6+
const validate = require('./validate');
7+
const cloudcmd = require('./cloudcmd');
208

219
test('validate: root: bad', (t) => {
2210
const config = {
2311
root: Math.random(),
2412
};
25-
13+
2614
const [e] = tryCatch(cloudcmd, {
2715
config,
2816
});
29-
17+
3018
t.equal(e.message, 'dir should be a string', 'should throw');
3119
t.end();
3220
});
3321

3422
test('validate: root: config', (t) => {
3523
const config = stub().returns(true);
36-
24+
3725
validate.root('/hello', config);
38-
26+
3927
t.calledWith(config, ['dropbox'], 'should call config');
4028
t.end();
4129
});
4230

4331
test('validate: root: /', (t) => {
4432
const fn = stub();
4533
validate.root('/', fn);
46-
34+
4735
t.notCalled(fn, 'should not call fn');
4836
t.end();
4937
});
5038

5139
test('validate: root: stat', (t) => {
52-
const fn = stub();
53-
const {statSync} = fs;
54-
40+
const config = stub();
5541
const error = 'ENOENT';
56-
57-
fs.statSync = () => {
58-
throw Error(error);
59-
};
60-
61-
mockRequire(exitPath, fn);
62-
63-
const {root} = reRequire(validatePath);
64-
65-
root('hello', fn);
66-
42+
const statSync = stub().throws(Error(error));
43+
const exit = stub();
44+
45+
validate.root('hello', config, {
46+
statSync,
47+
exit,
48+
});
49+
6750
const msg = 'cloudcmd --root: %s';
68-
69-
fs.statSync = statSync;
70-
71-
stopAll();
72-
73-
t.calledWith(fn, [msg, error], 'should call fn');
51+
52+
t.calledWith(exit, [msg, error], 'should call fn');
7453
t.end();
7554
});
7655

7756
test('validate: packer: not valid', (t) => {
78-
const fn = stub();
79-
80-
mockRequire(exitPath, fn);
81-
82-
const {packer} = reRequire(validatePath);
57+
const exit = stub();
8358
const msg = 'cloudcmd --packer: could be "tar" or "zip" only';
84-
85-
packer('hello');
86-
87-
stopAll();
88-
89-
t.calledWith(fn, [msg], 'should call fn');
59+
60+
validate.packer('hello', {
61+
exit,
62+
});
63+
64+
t.calledWith(exit, [msg], 'should call fn');
9065
t.end();
9166
});
9267

9368
test('validate: editor: not valid', (t) => {
94-
const fn = stub();
95-
96-
mockRequire(exitPath, fn);
97-
98-
const {editor} = reRequire(validatePath);
69+
const exit = stub();
9970
const msg = 'cloudcmd --editor: could be "dword", "edward" or "deepword" only';
100-
101-
editor('hello');
102-
103-
stopAll();
104-
105-
t.calledWith(fn, [msg], 'should call fn');
71+
72+
validate.editor('hello', {
73+
exit,
74+
});
75+
76+
t.calledWith(exit, [msg], 'should call fn');
10677
t.end();
10778
});
10879

10980
test('validate: columns', (t) => {
110-
const fn = stub();
111-
mockRequire(exitPath, fn);
112-
113-
const {columns} = require(validatePath);
114-
115-
columns('name-size-date');
116-
117-
stopAll();
118-
119-
t.notCalled(fn, 'should not call exit');
81+
const exit = stub();
82+
83+
validate.columns('name-size-date', {
84+
exit,
85+
});
86+
87+
t.notCalled(exit, 'should not call exit');
12088
t.end();
12189
});
12290

12391
test('validate: columns: wrong', (t) => {
124-
const fn = stub();
12592
const getColumns = stub().returns({
12693
'name-size-date': '',
12794
'name-size': '',
12895
});
129-
130-
mockRequire(exitPath, fn);
131-
mockRequire(columnsPath, {
96+
97+
const exit = stub();
98+
const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"';
99+
100+
validate.columns('hello', {
101+
exit,
132102
getColumns,
133103
});
134-
135-
const {columns} = reRequire(validatePath);
136-
const msg = 'cloudcmd --columns: can be only one of: "name-size-date", "name-size"';
137-
138-
columns('hello');
139-
140-
stopAll();
141-
142-
t.calledWith(fn, [msg], 'should call exit');
104+
105+
t.calledWith(exit, [msg], 'should call exit');
143106
t.end();
144107
});

0 commit comments

Comments
 (0)