Skip to content

Commit 1749b82

Browse files
committed
feature: fileop: extract: get rid of mock-require
1 parent a64eace commit 1749b82

2 files changed

Lines changed: 27 additions & 51 deletions

File tree

server/extract.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
const currify = require('currify');
44

5-
const inly = require('inly');
5+
const _inly = require('inly');
66
const {webToWin} = require('mellow');
77

8-
const isRootWin32 = currify(require('./is-root-win32'));
8+
const _isRootWin32 = currify(require('./is-root-win32'));
99
const WIN32_ROOT_MSG = 'Could not extract from root on windows!';
1010

11-
module.exports = (id, root, socket, from, to) => {
11+
module.exports = (id, root, socket, from, to, files, overrides = {}) => {
12+
const {isRootWin32 = _isRootWin32} = overrides;
13+
1214
from = webToWin(from, root);
1315
to = webToWin(to, root);
1416

1517
if (!isRootWin32(root, from))
16-
return operate(id, socket, from, to);
18+
return operate(id, socket, from, to, overrides);
1719

1820
socket.emit(`${id}#error`, WIN32_ROOT_MSG);
1921
};
2022

21-
function operate(id, socket, from, to) {
23+
function operate(id, socket, from, to, overrides) {
24+
const {inly = _inly} = overrides;
2225
const extractor = inly(from, to);
2326

2427
extractor.on('file', (name) => {

test/server/extract.js

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
const {once} = require('node:events');
44

5-
const {test} = require('supertape');
6-
const mock = require('mock-require');
7-
const clear = require('clear-module');
8-
const wait = require('@iocmd/wait');
5+
const {test, stub} = require('supertape');
96

10-
const clearFileop = require('../lib/clear');
7+
const wait = require('@iocmd/wait');
118

129
const {
1310
rawErrorEmitter,
@@ -16,17 +13,13 @@ const {
1613
endEmitter,
1714
} = require('../lib/emitters');
1815

16+
const connect = require('../lib/connect');
1917
const connectPath = '../lib/connect';
20-
const extractPath = 'inly';
21-
const isRootPath = '../../server/is-root-win32';
2218

2319
test('operate: extract: error', async (t) => {
24-
clearFileop();
25-
clear(extractPath);
26-
mock(extractPath, rawErrorEmitter);
27-
const connect = require(connectPath);
28-
29-
const {socket, done} = await connect();
20+
const {socket, done} = await connect({
21+
inly: rawErrorEmitter,
22+
});
3023

3124
const error = 'EACCES: /hello/abc';
3225
const from = '/hello/abc';
@@ -49,13 +42,9 @@ test('operate: extract: error', async (t) => {
4942
});
5043

5144
test('operate: extract: progress', async (t) => {
52-
clearFileop();
53-
clear(extractPath);
54-
mock(extractPath, progressEmitter);
55-
56-
const connect = require(connectPath);
57-
58-
const {socket, done} = await connect();
45+
const {socket, done} = await connect({
46+
inly: progressEmitter,
47+
});
5948

6049
const from = '/hello/abc';
6150
const to = '/world/abc';
@@ -77,13 +66,11 @@ test('operate: extract: progress', async (t) => {
7766
});
7867

7968
test('operate: extract: file', async (t) => {
80-
clearFileop();
81-
clear(extractPath);
82-
mock(extractPath, fileEmitter);
83-
8469
const connect = require(connectPath);
8570

86-
const {socket, done} = await connect();
71+
const {socket, done} = await connect({
72+
inly: fileEmitter,
73+
});
8774

8875
const from = '/hello/abc';
8976
const to = '/world/abc';
@@ -105,13 +92,11 @@ test('operate: extract: file', async (t) => {
10592
});
10693

10794
test('operate: extract: end', async (t) => {
108-
clearFileop();
109-
clear(extractPath);
110-
mock(extractPath, endEmitter);
111-
11295
const connect = require(connectPath);
11396

114-
const {socket, done} = await connect();
97+
const {socket, done} = await connect({
98+
inly: endEmitter,
99+
});
115100

116101
const from = '/hello/abc';
117102
const to = '/world/abc';
@@ -129,35 +114,23 @@ test('operate: extract: end', async (t) => {
129114
});
130115

131116
test('operate: extract: error: root', async (t) => {
132-
clearFileop();
133-
clear(isRootPath);
134-
135117
const from = '/hello';
136118
const to = '/world';
137119
const names = ['abc'];
138-
const truth = () => true;
120+
const truth = stub().returns(true);
121+
const isRootWin32 = stub().returns(truth);
139122

140-
Object.defineProperty(truth, 'length', {
141-
value: 2,
123+
const {socket, done} = await connect({
124+
isRootWin32,
142125
});
143126

144-
const isRoot = require(isRootPath);
145-
mock(isRootPath, truth);
146-
147-
const connect = require(connectPath);
148-
149-
mock(isRootPath, isRoot);
150-
151-
const {socket, done} = await connect();
152-
153127
socket.emit('operation', 'extract', from, to, names);
154128
const [id] = await once(socket, 'id');
155129

156130
socket.emit(`${id}#start`);
157131
const [e] = await once(socket, `${id}#error`);
158132

159133
done();
160-
clear(isRootPath);
161134

162135
const error = 'Could not extract from root on windows!';
163136

0 commit comments

Comments
 (0)