Skip to content

Commit e080a54

Browse files
committed
feature: server: cloudcmd: get rid of mock-require
1 parent eff3421 commit e080a54

File tree

2 files changed

+45
-63
lines changed

2 files changed

+45
-63
lines changed

server/cloudcmd.js

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

3+
const fullstore = require('fullstore');
34
const process = require('process');
45
const DIR = `${__dirname}/`;
56
const DIR_COMMON = `${DIR}../common/`;
@@ -34,11 +35,12 @@ const nomine = require('nomine');
3435
const fileop = require('@cloudcmd/fileop');
3536
const DIR_ROOT = `${DIR}../`;
3637

37-
const isDev = process.env.NODE_ENV === 'development';
3838
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
3939

40+
const isDev = fullstore(process.env.NODE_ENV === 'development');
41+
4042
const getIndexPath = (isDev) => path.join(DIR, '..', `${getDist(isDev)}/index.html`);
41-
const html = fs.readFileSync(getIndexPath(isDev), 'utf8');
43+
const html = fs.readFileSync(getIndexPath(isDev()), 'utf8');
4244

4345
const initAuth = currify(_initAuth);
4446
const notEmpty = (a) => a;
@@ -255,9 +257,10 @@ function logout(req, res, next) {
255257
res.sendStatus(401);
256258
}
257259

260+
module.exports._isDev = isDev;
258261
module.exports._replaceDist = replaceDist;
259262
function replaceDist(url) {
260-
if (!isDev)
263+
if (!isDev())
261264
return url;
262265

263266
return url.replace(/^\/dist\//, '/dist-dev/');

server/cloudcmd.spec.js

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
'use strict';
22

3-
const process = require('process');
43
const path = require('path');
5-
64
const {test, stub} = require('supertape');
5+
const cloudcmd = require('./cloudcmd.js');
76

8-
const {reRequire} = require('mock-require');
9-
10-
const DIR = './';
11-
const cloudcmdPath = `${DIR}cloudcmd`;
12-
13-
const cloudcmd = require(cloudcmdPath);
147
const {request} = require('serve-once')(cloudcmd, {
158
config: {
169
auth: false,
@@ -19,40 +12,42 @@ const {request} = require('serve-once')(cloudcmd, {
1912
});
2013

2114
const {
15+
_isDev,
16+
_replaceDist,
2217
createConfigManager,
2318
_getPrefix,
2419
_initAuth,
2520
} = cloudcmd;
2621

2722
test('cloudcmd: defaults: config', (t) => {
2823
const configManager = createConfigManager();
29-
24+
3025
configManager('configDialog', false);
31-
26+
3227
cloudcmd({
3328
configManager,
3429
});
35-
30+
3631
t.notOk(configManager('configDialog'), 'should not override config with defaults');
3732
t.end();
3833
});
3934

4035
test('cloudcmd: defaults: console', (t) => {
4136
const configManager = createConfigManager();
4237
configManager('console', false);
43-
38+
4439
cloudcmd({
4540
configManager,
4641
});
47-
42+
4843
t.notOk(configManager('console'), 'should not override config with defaults');
4944
t.end();
5045
});
5146

5247
test('cloudcmd: getPrefix', (t) => {
5348
const value = 'hello';
5449
const result = _getPrefix(value);
55-
50+
5651
t.equal(result, value);
5752
t.end();
5853
});
@@ -61,7 +56,7 @@ test('cloudcmd: getPrefix: function', (t) => {
6156
const value = 'hello';
6257
const fn = () => value;
6358
const result = _getPrefix(fn);
64-
59+
6560
t.equal(result, value);
6661
t.end();
6762
});
@@ -70,130 +65,114 @@ test('cloudcmd: getPrefix: function: empty', (t) => {
7065
const value = null;
7166
const fn = () => value;
7267
const result = _getPrefix(fn);
73-
68+
7469
t.equal(result, '');
7570
t.end();
7671
});
7772

7873
test('cloudcmd: replaceDist', (t) => {
79-
const {NODE_ENV} = process.env;
80-
81-
process.env.NODE_ENV = 'development';
82-
83-
const {_replaceDist} = reRequire(cloudcmdPath);
84-
74+
const currentIsDev = _isDev();
75+
76+
_isDev(true);
8577
const url = '/dist/hello';
8678
const result = _replaceDist(url);
8779
const expected = '/dist-dev/hello';
88-
89-
process.env.NODE_ENV = NODE_ENV;
90-
80+
81+
_isDev(currentIsDev);
82+
9183
t.equal(result, expected);
9284
t.end();
9385
});
9486

9587
test('cloudcmd: replaceDist: !isDev', (t) => {
9688
const url = '/dist/hello';
97-
const cloudcmdPath = `${DIR}cloudcmd`;
98-
99-
const reset = cleanNodeEnv();
100-
const {_replaceDist} = reRequire(cloudcmdPath);
89+
90+
const currentIsDev = _isDev();
91+
_isDev(false);
10192
const result = _replaceDist(url);
102-
103-
reset();
104-
93+
94+
_isDev(currentIsDev);
95+
10596
t.equal(result, url);
10697
t.end();
10798
});
10899

109100
test('cloudcmd: auth: reject', (t) => {
110101
const accept = stub();
111102
const reject = stub();
112-
103+
113104
const config = createConfigManager();
114-
105+
115106
const username = 'root';
116107
const password = 'toor';
117-
108+
118109
config('auth', true);
119110
config('username', username);
120111
config('password', password);
121-
112+
122113
_initAuth(config, accept, reject, username, 'abc');
123-
114+
124115
t.ok(reject.called, 'should reject');
125116
t.end();
126117
});
127118

128119
test('cloudcmd: auth: accept', (t) => {
129120
const accept = stub();
130121
const reject = stub();
131-
122+
132123
const username = 'root';
133124
const password = 'toor';
134125
const auth = true;
135-
126+
136127
const config = createConfigManager();
137128
config('username', username);
138129
config('password', password);
139130
config('auth', auth);
140-
131+
141132
_initAuth(config, accept, reject, username, password);
142-
133+
143134
t.ok(accept.called, 'should accept');
144135
t.end();
145136
});
146137

147138
test('cloudcmd: auth: accept: no auth', (t) => {
148139
const accept = stub();
149140
const reject = stub();
150-
141+
151142
const auth = false;
152143
const username = 'root';
153144
const password = 'toor';
154-
145+
155146
const config = createConfigManager();
156147
config('username', username);
157148
config('password', password);
158149
config('auth', auth);
159-
150+
160151
_initAuth(config, accept, reject, username, password);
161-
152+
162153
t.ok(accept.called, 'should accept');
163154
t.end();
164155
});
165156

166157
test('cloudcmd: getIndexPath: production', (t) => {
167158
const isDev = false;
168159
const name = path.join(__dirname, '..', 'dist', 'index.html');
169-
160+
170161
t.equal(cloudcmd._getIndexPath(isDev), name);
171162
t.end();
172163
});
173164

174165
test('cloudcmd: getIndexPath: development', (t) => {
175166
const isDev = true;
176167
const name = path.join(__dirname, '..', 'dist-dev', 'index.html');
177-
168+
178169
t.equal(cloudcmd._getIndexPath(isDev), name);
179170
t.end();
180171
});
181172

182173
test('cloudcmd: sw', async (t) => {
183174
const {status} = await request.get('/sw.js');
184-
175+
185176
t.equal(status, 200, 'should return sw');
186177
t.end();
187178
});
188-
189-
function cleanNodeEnv() {
190-
const {NODE_ENV} = process.env;
191-
192-
process.env.NODE_ENV = '';
193-
194-
const reset = () => {
195-
process.env.NODE_ENV = NODE_ENV;
196-
};
197-
198-
return reset;
199-
}

0 commit comments

Comments
 (0)