11'use strict' ;
22
33const { once} = require ( 'node:events' ) ;
4-
54const { test, stub} = require ( 'supertape' ) ;
6- const mock = require ( 'mock-require' ) ;
7- const clear = require ( 'clear-module' ) ;
85
96const {
107 endEmitter,
@@ -14,24 +11,18 @@ const {
1411 customEmitter,
1512} = require ( '../lib/emitters' ) ;
1613
17- const clearFileop = require ( '../lib/clear ' ) ;
14+ const connect = require ( '../lib/connect ' ) ;
1815
1916const connectPath = '../lib/connect' ;
20- const copyPath = 'copymitter' ;
21- const isRootPath = '../../server/is-root-win32' ;
2217
2318test ( 'operate: copy: error' , async ( t ) => {
24- clearFileop ( ) ;
25- clear ( copyPath ) ;
26-
2719 const from = '/hello' ;
2820 const to = '/world' ;
2921 const names = [ 'abc' ] ;
3022
31- mock ( copyPath , errorEmitter ) ;
32- const connect = require ( connectPath ) ;
33-
34- const { socket, done} = await connect ( ) ;
23+ const { socket, done} = await connect ( {
24+ copymitter : errorEmitter ,
25+ } ) ;
3526
3627 const error = 'EACCES: /hello/abc' ;
3728
@@ -48,19 +39,14 @@ test('operate: copy: error', async (t) => {
4839} ) ;
4940
5041test ( 'operate: copy: file' , async ( t ) => {
51- clearFileop ( ) ;
52- clear ( copyPath ) ;
53-
5442 const from = '/hello' ;
5543 const to = '/world' ;
5644 const names = [ 'abc' ] ;
57-
58- mock ( copyPath , fileEmitter ) ;
59- const connect = require ( connectPath ) ;
6045 const root = '/' ;
6146
6247 const { socket, done} = await connect ( {
6348 root,
49+ copymitter : fileEmitter ,
6450 } ) ;
6551
6652 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
@@ -76,19 +62,15 @@ test('operate: copy: file', async (t) => {
7662} ) ;
7763
7864test ( 'operate: copy: progress' , async ( t ) => {
79- clearFileop ( ) ;
80- clear ( copyPath ) ;
81-
8265 const from = '/hello' ;
8366 const to = '/world' ;
8467 const names = [ 'abc' ] ;
8568
86- mock ( copyPath , progressEmitter ) ;
87- const connect = require ( connectPath ) ;
8869 const root = '/' ;
8970
9071 const { socket, done} = await connect ( {
9172 root,
73+ copymitter : progressEmitter ,
9274 } ) ;
9375
9476 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
@@ -97,51 +79,46 @@ test('operate: copy: progress', async (t) => {
9779 socket . emit ( `${ id } #start` ) ;
9880 const [ n ] = await once ( socket , `${ id } #progress` ) ;
9981
100- t . equal ( n , 100 ) ;
10182 done ( ) ;
83+
84+ t . equal ( n , 100 ) ;
10285 t . end ( ) ;
10386} ) ;
10487
10588test ( 'operate: copy: end' , async ( t ) => {
106- clearFileop ( ) ;
107- clear ( copyPath ) ;
108-
10989 const from = '/hello' ;
11090 const to = '/world' ;
11191 const names = [ 'abc' ] ;
11292
113- mock ( copyPath , endEmitter ) ;
114- const connect = require ( connectPath ) ;
11593 const root = '/' ;
11694
11795 const { socket, done} = await connect ( {
11896 root,
97+ copymitter : endEmitter ,
11998 } ) ;
12099
121100 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
122101 const [ id ] = await once ( socket , 'id' ) ;
123102
124103 socket . emit ( `${ id } #start` ) ;
125104 await once ( socket , `${ id } #end` ) ;
126- t . pass ( 'should emit end' ) ;
127105 done ( ) ;
106+
107+ t . pass ( 'should emit end' ) ;
128108 t . end ( ) ;
129109} ) ;
130110
131111test ( 'operate: copy: abort' , async ( t ) => {
132- clearFileop ( ) ;
133- clear ( copyPath ) ;
134-
135112 const from = '/hello' ;
136113 const to = '/world' ;
137114 const names = [ 'abc' ] ;
138115
139- mock ( copyPath , errorEmitter ) ;
140116 const connect = require ( connectPath ) ;
141117 const root = '/' ;
142118
143119 const { socket, done} = await connect ( {
144120 root,
121+ copymitter : errorEmitter ,
145122 } ) ;
146123
147124 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
@@ -151,23 +128,20 @@ test('operate: copy: abort', async (t) => {
151128 await once ( socket , `${ id } #error` ) ;
152129 socket . emit ( `${ id } #abort` ) ;
153130 await once ( socket , `${ id } #end` ) ;
154- t . pass ( 'should emit abort' ) ;
155131 done ( ) ;
132+
133+ t . pass ( 'should emit abort' ) ;
156134 t . end ( ) ;
157135} ) ;
158136
159137test ( 'operate: copy: continue' , async ( t ) => {
160- clearFileop ( ) ;
161- clear ( copyPath ) ;
162-
163138 const from = '/' ;
164139 const to = '/world' ;
165140 const names = [ 'abc' ] ;
166141
167- mock ( copyPath , errorEmitter ) ;
168- const connect = require ( connectPath ) ;
169-
170- const { socket, done} = await connect ( ) ;
142+ const { socket, done} = await connect ( {
143+ copymitter : errorEmitter ,
144+ } ) ;
171145
172146 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
173147 const [ id ] = await once ( socket , 'id' ) ;
@@ -183,29 +157,28 @@ test('operate: copy: continue', async (t) => {
183157} ) ;
184158
185159test ( 'operate: copy: pause' , async ( t ) => {
186- clearFileop ( ) ;
187- clear ( copyPath ) ;
188-
189160 const from = '/' ;
190161 const to = '/world' ;
191162 const names = [ 'abc' ] ;
192163
193164 const pause = stub ( ) ;
194- mock ( copyPath , customEmitter ( {
165+ const copymitter = customEmitter ( {
195166 pause,
196167 continue : stub ( ) ,
197- } ) ) ;
198-
199- const connect = require ( connectPath ) ;
168+ } ) ;
200169
201- const { socket, done} = await connect ( ) ;
170+ const { socket, done} = await connect ( {
171+ copymitter,
172+ } ) ;
202173
203174 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
204175
205176 const [ id ] = await once ( socket , 'id' ) ;
177+
206178 socket . emit ( `${ id } #start` ) ;
207179 socket . emit ( `${ id } #pause` ) ;
208180 socket . emit ( `${ id } #continue` ) ;
181+
209182 await once ( socket , `${ id } #end` ) ;
210183
211184 done ( ) ;
@@ -215,26 +188,16 @@ test('operate: copy: pause', async (t) => {
215188} ) ;
216189
217190test ( 'operate: copy: error: root' , async ( t ) => {
218- clearFileop ( ) ;
219- clear ( isRootPath ) ;
220-
221191 const from = '/hello' ;
222192 const to = '/world' ;
223193 const names = [ 'abc' ] ;
224- const truth = ( ) => true ;
194+ const success = stub ( ) . returns ( true ) ;
195+ const isRootWin32 = stub ( ) . returns ( success ) ;
225196
226- Object . defineProperty ( truth , 'length' , {
227- value : 2 ,
197+ const { socket , done } = await connect ( {
198+ isRootWin32 ,
228199 } ) ;
229200
230- const isRoot = require ( isRootPath ) ;
231- mock ( isRootPath , truth ) ;
232-
233- const connect = require ( connectPath ) ;
234- mock ( isRootPath , isRoot ) ;
235-
236- const { socket, done} = await connect ( ) ;
237-
238201 socket . emit ( 'operation' , 'copy' , from , to , names ) ;
239202 const [ id ] = await once ( socket , 'id' ) ;
240203
@@ -243,7 +206,6 @@ test('operate: copy: error: root', async (t) => {
243206 const [ e ] = await once ( socket , `${ id } #error` ) ;
244207
245208 done ( ) ;
246- clear ( isRootPath ) ;
247209
248210 t . equal ( e , error , 'should emit error' ) ;
249211 t . end ( ) ;
0 commit comments