1
1
'use strict' ;
2
2
3
- const process = require ( 'process' ) ;
4
3
const path = require ( 'path' ) ;
5
-
6
4
const { test, stub} = require ( 'supertape' ) ;
5
+ const cloudcmd = require ( './cloudcmd.js' ) ;
7
6
8
- const { reRequire} = require ( 'mock-require' ) ;
9
-
10
- const DIR = './' ;
11
- const cloudcmdPath = `${ DIR } cloudcmd` ;
12
-
13
- const cloudcmd = require ( cloudcmdPath ) ;
14
7
const { request} = require ( 'serve-once' ) ( cloudcmd , {
15
8
config : {
16
9
auth : false ,
@@ -19,40 +12,42 @@ const {request} = require('serve-once')(cloudcmd, {
19
12
} ) ;
20
13
21
14
const {
15
+ _isDev,
16
+ _replaceDist,
22
17
createConfigManager,
23
18
_getPrefix,
24
19
_initAuth,
25
20
} = cloudcmd ;
26
21
27
22
test ( 'cloudcmd: defaults: config' , ( t ) => {
28
23
const configManager = createConfigManager ( ) ;
29
-
24
+
30
25
configManager ( 'configDialog' , false ) ;
31
-
26
+
32
27
cloudcmd ( {
33
28
configManager,
34
29
} ) ;
35
-
30
+
36
31
t . notOk ( configManager ( 'configDialog' ) , 'should not override config with defaults' ) ;
37
32
t . end ( ) ;
38
33
} ) ;
39
34
40
35
test ( 'cloudcmd: defaults: console' , ( t ) => {
41
36
const configManager = createConfigManager ( ) ;
42
37
configManager ( 'console' , false ) ;
43
-
38
+
44
39
cloudcmd ( {
45
40
configManager,
46
41
} ) ;
47
-
42
+
48
43
t . notOk ( configManager ( 'console' ) , 'should not override config with defaults' ) ;
49
44
t . end ( ) ;
50
45
} ) ;
51
46
52
47
test ( 'cloudcmd: getPrefix' , ( t ) => {
53
48
const value = 'hello' ;
54
49
const result = _getPrefix ( value ) ;
55
-
50
+
56
51
t . equal ( result , value ) ;
57
52
t . end ( ) ;
58
53
} ) ;
@@ -61,7 +56,7 @@ test('cloudcmd: getPrefix: function', (t) => {
61
56
const value = 'hello' ;
62
57
const fn = ( ) => value ;
63
58
const result = _getPrefix ( fn ) ;
64
-
59
+
65
60
t . equal ( result , value ) ;
66
61
t . end ( ) ;
67
62
} ) ;
@@ -70,130 +65,114 @@ test('cloudcmd: getPrefix: function: empty', (t) => {
70
65
const value = null ;
71
66
const fn = ( ) => value ;
72
67
const result = _getPrefix ( fn ) ;
73
-
68
+
74
69
t . equal ( result , '' ) ;
75
70
t . end ( ) ;
76
71
} ) ;
77
72
78
73
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 ) ;
85
77
const url = '/dist/hello' ;
86
78
const result = _replaceDist ( url ) ;
87
79
const expected = '/dist-dev/hello' ;
88
-
89
- process . env . NODE_ENV = NODE_ENV ;
90
-
80
+
81
+ _isDev ( currentIsDev ) ;
82
+
91
83
t . equal ( result , expected ) ;
92
84
t . end ( ) ;
93
85
} ) ;
94
86
95
87
test ( 'cloudcmd: replaceDist: !isDev' , ( t ) => {
96
88
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 ) ;
101
92
const result = _replaceDist ( url ) ;
102
-
103
- reset ( ) ;
104
-
93
+
94
+ _isDev ( currentIsDev ) ;
95
+
105
96
t . equal ( result , url ) ;
106
97
t . end ( ) ;
107
98
} ) ;
108
99
109
100
test ( 'cloudcmd: auth: reject' , ( t ) => {
110
101
const accept = stub ( ) ;
111
102
const reject = stub ( ) ;
112
-
103
+
113
104
const config = createConfigManager ( ) ;
114
-
105
+
115
106
const username = 'root' ;
116
107
const password = 'toor' ;
117
-
108
+
118
109
config ( 'auth' , true ) ;
119
110
config ( 'username' , username ) ;
120
111
config ( 'password' , password ) ;
121
-
112
+
122
113
_initAuth ( config , accept , reject , username , 'abc' ) ;
123
-
114
+
124
115
t . ok ( reject . called , 'should reject' ) ;
125
116
t . end ( ) ;
126
117
} ) ;
127
118
128
119
test ( 'cloudcmd: auth: accept' , ( t ) => {
129
120
const accept = stub ( ) ;
130
121
const reject = stub ( ) ;
131
-
122
+
132
123
const username = 'root' ;
133
124
const password = 'toor' ;
134
125
const auth = true ;
135
-
126
+
136
127
const config = createConfigManager ( ) ;
137
128
config ( 'username' , username ) ;
138
129
config ( 'password' , password ) ;
139
130
config ( 'auth' , auth ) ;
140
-
131
+
141
132
_initAuth ( config , accept , reject , username , password ) ;
142
-
133
+
143
134
t . ok ( accept . called , 'should accept' ) ;
144
135
t . end ( ) ;
145
136
} ) ;
146
137
147
138
test ( 'cloudcmd: auth: accept: no auth' , ( t ) => {
148
139
const accept = stub ( ) ;
149
140
const reject = stub ( ) ;
150
-
141
+
151
142
const auth = false ;
152
143
const username = 'root' ;
153
144
const password = 'toor' ;
154
-
145
+
155
146
const config = createConfigManager ( ) ;
156
147
config ( 'username' , username ) ;
157
148
config ( 'password' , password ) ;
158
149
config ( 'auth' , auth ) ;
159
-
150
+
160
151
_initAuth ( config , accept , reject , username , password ) ;
161
-
152
+
162
153
t . ok ( accept . called , 'should accept' ) ;
163
154
t . end ( ) ;
164
155
} ) ;
165
156
166
157
test ( 'cloudcmd: getIndexPath: production' , ( t ) => {
167
158
const isDev = false ;
168
159
const name = path . join ( __dirname , '..' , 'dist' , 'index.html' ) ;
169
-
160
+
170
161
t . equal ( cloudcmd . _getIndexPath ( isDev ) , name ) ;
171
162
t . end ( ) ;
172
163
} ) ;
173
164
174
165
test ( 'cloudcmd: getIndexPath: development' , ( t ) => {
175
166
const isDev = true ;
176
167
const name = path . join ( __dirname , '..' , 'dist-dev' , 'index.html' ) ;
177
-
168
+
178
169
t . equal ( cloudcmd . _getIndexPath ( isDev ) , name ) ;
179
170
t . end ( ) ;
180
171
} ) ;
181
172
182
173
test ( 'cloudcmd: sw' , async ( t ) => {
183
174
const { status} = await request . get ( '/sw.js' ) ;
184
-
175
+
185
176
t . equal ( status , 200 , 'should return sw' ) ;
186
177
t . end ( ) ;
187
178
} ) ;
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