Skip to content

Commit 153a671

Browse files
committed
feature: server: pack: get rid of mock-require
1 parent 3e4b72a commit 153a671

3 files changed

Lines changed: 19 additions & 55 deletions

File tree

server/listen.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ function connection(rootRaw, socket, overrides) {
5959
socket.emit(`${id()}started`);
6060

6161
const operation = getOperation(name, overrides);
62-
6362
const root = getValue(rootRaw);
6463

65-
operation(id(), root, socket, from, to, files);
64+
operation(id(), root, socket, from, to, files, overrides);
6665
});
6766
});
6867
}

server/pack.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
'use strict';
22

33
const currify = require('currify');
4-
const onezip = require('onezip');
4+
const {onezip} = require('onezip');
55
const jaguar = require('jaguar');
66
const {webToWin} = require('mellow');
77

88
const isRootWin32 = currify(require('./is-root-win32'));
99
const WIN32_ROOT_MSG = 'Could not pack from/to root on windows!';
1010

11-
module.exports = (type, id, root, socket, from, to, files) => {
11+
module.exports = (type, id, root, socket, from, to, files, overrides) => {
1212
from = webToWin(from, root);
1313
to = webToWin(to, root);
1414

1515
if (![from, to].some(isRootWin32(root)))
16-
return operate(type, id, socket, from, to, files);
16+
return operate(type, id, socket, from, to, files, overrides);
1717

1818
socket.emit(`${id}#error`, WIN32_ROOT_MSG);
1919
};
2020

21-
function getOperation(type) {
21+
function getOperation(type, overrides = {}) {
22+
const {pack} = overrides;
23+
2224
if (type === 'tar')
23-
return jaguar.pack;
25+
return (pack || jaguar.pack);
2426

25-
return onezip.pack;
27+
return (pack || onezip.pack);
2628
}
2729

28-
function operate(type, id, socket, from, to, files) {
29-
const operate = getOperation(type);
30+
function operate(type, id, socket, from, to, files, overrides) {
31+
const operate = getOperation(type, overrides);
3032
const operator = operate(from, to, files);
3133

3234
operator.on('file', (name) => {

test/server/zip.js

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,28 @@
33
const {once} = require('node:events');
44

55
const {test} = require('supertape');
6-
const mock = require('mock-require');
7-
const clear = require('clear-module');
86
const wait = require('@iocmd/wait');
97

10-
const clearFileop = require('../lib/clear');
11-
128
const {
139
endEmitter,
1410
errorEmitter,
1511
progressEmitter,
1612
fileEmitter,
1713
} = require('../lib/emitters');
1814

15+
const connect = require('../lib/connect');
1916
const connectPath = '../lib/connect';
20-
const zipPath = 'onezip';
2117

2218
test('operate: zip: error', async (t) => {
23-
clearFileop();
24-
clear(zipPath);
25-
2619
const from = '/hello';
2720
const to = '/world';
2821
const names = ['abc'];
2922

30-
mock(zipPath, {
31-
pack: errorEmitter,
32-
});
33-
3423
const connect = require(connectPath);
3524

36-
const {socket, done} = await connect();
25+
const {socket, done} = await connect({
26+
pack: errorEmitter,
27+
});
3728

3829
socket.emit('operation', 'zip', from, to, names);
3930
const [id] = await once(socket, 'id');
@@ -50,21 +41,14 @@ test('operate: zip: error', async (t) => {
5041
});
5142

5243
test('operate: zip: progress', async (t) => {
53-
clearFileop();
54-
clear(zipPath);
55-
5644
const from = '/hello';
5745
const to = '/world';
5846
const names = ['abc'];
5947

60-
mock(zipPath, {
48+
const {socket, done} = await connect({
6149
pack: progressEmitter,
6250
});
6351

64-
const connect = require(connectPath);
65-
66-
const {socket, done} = await connect();
67-
6852
socket.emit('operation', 'zip', from, to, names);
6953
const [id] = await once(socket, 'id');
7054

@@ -78,21 +62,14 @@ test('operate: zip: progress', async (t) => {
7862
});
7963

8064
test('operate: zip: file', async (t) => {
81-
clearFileop();
82-
clear(zipPath);
83-
8465
const from = '/hello';
8566
const to = '/world';
8667
const names = ['abc'];
8768

88-
mock(zipPath, {
69+
const {socket, done} = await connect({
8970
pack: fileEmitter,
9071
});
9172

92-
const connect = require(connectPath);
93-
94-
const {socket, done} = await connect();
95-
9673
socket.emit('operation', 'zip', from, to, names);
9774
const [id] = await once(socket, 'id');
9875

@@ -106,21 +83,14 @@ test('operate: zip: file', async (t) => {
10683
});
10784

10885
test('operate: zip: end', async (t) => {
109-
clearFileop();
110-
clear(zipPath);
111-
11286
const from = '/hello';
11387
const to = '/world';
11488
const names = ['abc'];
11589

116-
mock(zipPath, {
90+
const {socket, done} = await connect({
11791
pack: endEmitter,
11892
});
11993

120-
const connect = require(connectPath);
121-
122-
const {socket, done} = await connect();
123-
12494
socket.emit('operation', 'zip', from, to, names);
12595
const [id] = await once(socket, 'id');
12696

@@ -134,21 +104,14 @@ test('operate: zip: end', async (t) => {
134104
});
135105

136106
test('operate: zip: abort', async (t) => {
137-
clearFileop();
138-
clear(zipPath);
139-
140107
const from = '/hello';
141108
const to = '/world';
142109
const names = ['abc'];
143110

144-
mock(zipPath, {
111+
const {socket, done} = await connect({
145112
pack: errorEmitter,
146113
});
147114

148-
const connect = require(connectPath);
149-
150-
const {socket, done} = await connect();
151-
152115
socket.emit('operation', 'zip', from, to, names);
153116
const [id] = await once(socket, 'id');
154117

0 commit comments

Comments
 (0)