Skip to content

Commit 7cc62e0

Browse files
committed
feature: gritty: get rid of mock-require
1 parent 8597b1b commit 7cc62e0

3 files changed

Lines changed: 50 additions & 43 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"eslint-plugin-putout": "^30.0.2",
7373
"json-loader": "^0.5.4",
7474
"madrun": "^12.1.0",
75-
"mock-require": "^3.0.2",
7675
"nodemon": "^3.0.1",
7776
"optimize-css-assets-webpack-plugin": "^6.0.1",
7877
"putout": "^41.0.8",

server/gritty.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ const DIR_ROOT = `${__dirname}/..`;
2020
const terminalFn = currify(_terminalFn);
2121
const connectionWraped = wraptile(connection);
2222

23-
const CMD = process.platform === 'win32' ? 'cmd.exe' : 'bash';
24-
const isDev = process.env.NODE_ENV === 'development';
23+
const getCMD = (overrides = {}) => {
24+
const {platform = process.platform} = overrides;
25+
26+
return platform === 'win32' ? 'cmd.exe' : 'bash';
27+
};
28+
29+
const isDev = () => process.env.NODE_ENV === 'development';
2530

2631
const getDist = () => {
27-
if (isDev)
32+
if (isDev())
2833
return '/dist-dev';
2934

3035
return '/dist';
@@ -65,14 +70,21 @@ function _terminalFn(options, req, res, next) {
6570

6671
function staticFn(req, res) {
6772
const file = path.normalize(DIR_ROOT + req.url);
73+
6874
res.sendFile(file, {
6975
dotfiles: 'allow',
7076
});
7177
}
7278

73-
function createTerminal({command, env, cwd, cols, rows, pty = _pty}) {
74-
cols = cols || 80;
75-
rows = rows || 24;
79+
function createTerminal(overrides = {}) {
80+
const {
81+
command,
82+
env,
83+
cwd,
84+
cols = 80,
85+
rows = 24,
86+
pty = _pty,
87+
} = overrides;
7688

7789
const [cmd, ...args] = stringArgv(command);
7890
const term = pty.spawn(cmd, args, {
@@ -129,7 +141,10 @@ function connection(options, socket) {
129141
const onResize = (size) => {
130142
size = size || {};
131143

132-
const {cols = 80, rows = 25} = size;
144+
const {
145+
cols = 80,
146+
rows = 25,
147+
} = size;
133148

134149
term.resize(cols, rows);
135150
log(`Resized terminal ${term.pid} to ${cols} cols and ${rows} rows.`);
@@ -139,18 +154,20 @@ function connection(options, socket) {
139154
term.write(msg);
140155
};
141156

142-
function onTerminal(params) {
143-
params = params || {};
157+
function onTerminal(overrides = {}) {
144158
const {
145159
env,
146160
rows,
147161
cols,
148162
cwd,
149-
} = params;
163+
platform,
164+
} = overrides;
150165

151-
const command = params.command || options.command || CMD;
166+
const command = overrides.command || options.command || getCMD({
167+
platform,
168+
});
152169

153-
const autoRestart = choose(params.autoRestart, options.autoRestart, {
170+
const autoRestart = choose(overrides.autoRestart, options.autoRestart, {
154171
default: true,
155172
});
156173

test/server/gritty.js

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

3+
const {stripVTControlCharacters} = require('node:util');
34
const process = require('node:process');
45

56
const {once} = require('node:events');
@@ -8,19 +9,15 @@ const {tryCatch} = require('try-catch');
89
const {test, stub} = require('supertape');
910

1011
const currify = require('currify');
11-
const io = require('socket.io-client');
12-
const mockRequire = require('mock-require');
12+
const {io} = require('socket.io-client');
1313
const wait = require('@iocmd/wait');
1414

1515
const serveOnce = require('serve-once');
1616
const gritty = require('../../');
1717

1818
const {connect} = require('../before');
19-
2019
const {request} = serveOnce(gritty);
2120

22-
const {reRequire, stopAll} = mockRequire;
23-
2421
test('gritty: listen: args: no', (t) => {
2522
const [error] = tryCatch(gritty.listen);
2623

@@ -44,8 +41,6 @@ test('gritty: listen: args: auth', (t) => {
4441

4542
test('gritty: server: dist-dev', async (t) => {
4643
process.env.NODE_ENV = 'development';
47-
48-
const gritty = reRequire('../..');
4944
const {request} = serveOnce(gritty);
5045

5146
const {status} = await request.get('/gritty/gritty.js');
@@ -156,22 +151,36 @@ test('gritty: server: terminal: parse args', async (t) => {
156151
const {port, done} = await connect();
157152
const socket = io(`http://localhost:${port}/gritty`);
158153

159-
mockRequire('node-pty', {
160-
spawn: stub(),
154+
await once(socket, 'connect');
155+
socket.emit('terminal', {
156+
command: 'bash -c "hello world"',
161157
});
162158

159+
const [data] = await once(socket, 'data');
160+
socket.close();
161+
done();
162+
163+
t.match(data, 'bash: line 1: hello: command not found');
164+
t.end();
165+
});
166+
167+
test('gritty: server: terminal: platform', async (t) => {
168+
const platform = 'win32';
169+
const {port, done} = await connect();
170+
const socket = io(`http://localhost:${port}/gritty`);
171+
163172
await once(socket, 'connect');
164173
socket.emit('terminal', {
165-
command: 'bash -c "hello world"',
174+
platform,
166175
});
167176

168177
const [data] = await once(socket, 'data');
169178
socket.close();
170179
done();
171180

172-
stopAll();
181+
const stripped = stripVTControlCharacters(data);
173182

174-
t.match(data, 'bash: line 1: hello: command not found');
183+
t.equal(stripped, 'bash-5.2$ ');
175184
t.end();
176185
});
177186

@@ -243,24 +252,6 @@ test('gritty: server: socket: auth: reject', async (t) => {
243252
t.end();
244253
});
245254

246-
test('gritty: server: platform', (t) => {
247-
const {platform} = process;
248-
249-
Object.defineProperty(process, 'platform', {
250-
value: 'win32',
251-
});
252-
253-
reRequire('../..');
254-
255-
t.pass('set CMD');
256-
257-
Object.defineProperty(process, 'platform', {
258-
value: platform,
259-
});
260-
261-
t.end();
262-
});
263-
264255
test('gritty: server: socket: authCheck', async (t) => {
265256
const auth = (connect, reject) => ({username, password}) => {
266257
if (username !== 'hello' || password !== 'world')

0 commit comments

Comments
 (0)