|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const fs = require('node:fs'); |
4 |
| - |
5 | 3 | const {test, stub} = require('supertape');
|
6 |
| - |
7 | 4 | const tryCatch = require('try-catch');
|
8 |
| -const mockRequire = require('mock-require'); |
9 |
| -const dir = '..'; |
10 |
| - |
11 |
| -const validatePath = `${dir}/server/validate`; |
12 | 5 |
|
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'); |
20 | 8 |
|
21 | 9 | test('validate: root: bad', (t) => {
|
22 | 10 | const config = {
|
23 | 11 | root: Math.random(),
|
24 | 12 | };
|
25 |
| - |
| 13 | + |
26 | 14 | const [e] = tryCatch(cloudcmd, {
|
27 | 15 | config,
|
28 | 16 | });
|
29 |
| - |
| 17 | + |
30 | 18 | t.equal(e.message, 'dir should be a string', 'should throw');
|
31 | 19 | t.end();
|
32 | 20 | });
|
33 | 21 |
|
34 | 22 | test('validate: root: config', (t) => {
|
35 | 23 | const config = stub().returns(true);
|
36 |
| - |
| 24 | + |
37 | 25 | validate.root('/hello', config);
|
38 |
| - |
| 26 | + |
39 | 27 | t.calledWith(config, ['dropbox'], 'should call config');
|
40 | 28 | t.end();
|
41 | 29 | });
|
42 | 30 |
|
43 | 31 | test('validate: root: /', (t) => {
|
44 | 32 | const fn = stub();
|
45 | 33 | validate.root('/', fn);
|
46 |
| - |
| 34 | + |
47 | 35 | t.notCalled(fn, 'should not call fn');
|
48 | 36 | t.end();
|
49 | 37 | });
|
50 | 38 |
|
51 | 39 | test('validate: root: stat', (t) => {
|
52 |
| - const fn = stub(); |
53 |
| - const {statSync} = fs; |
54 |
| - |
| 40 | + const config = stub(); |
55 | 41 | 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 | + |
67 | 50 | 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'); |
74 | 53 | t.end();
|
75 | 54 | });
|
76 | 55 |
|
77 | 56 | 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(); |
83 | 58 | 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'); |
90 | 65 | t.end();
|
91 | 66 | });
|
92 | 67 |
|
93 | 68 | 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(); |
99 | 70 | 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'); |
106 | 77 | t.end();
|
107 | 78 | });
|
108 | 79 |
|
109 | 80 | 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'); |
120 | 88 | t.end();
|
121 | 89 | });
|
122 | 90 |
|
123 | 91 | test('validate: columns: wrong', (t) => {
|
124 |
| - const fn = stub(); |
125 | 92 | const getColumns = stub().returns({
|
126 | 93 | 'name-size-date': '',
|
127 | 94 | 'name-size': '',
|
128 | 95 | });
|
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, |
132 | 102 | getColumns,
|
133 | 103 | });
|
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'); |
143 | 106 | t.end();
|
144 | 107 | });
|
0 commit comments