Skip to content

Commit 1002d01

Browse files
committed
test: setup test for proc manager
1 parent fc35a51 commit 1002d01

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

src/utils/proc_manager.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import { AnsiParser } from 'ansi-parser/interfaces/ansi-parser';
22
import { decodeAnsiBytes, defaultAnsiParser } from 'ansi-parser/src';
3-
import type childProcess from 'child_process';
43
import cp from 'cross-spawn';
5-
import tty from 'tty';
4+
import ttyGlobal from 'tty';
65
import { envVarNames } from '../constants/ipc';
7-
import crossKill from './cross_kill';
6+
import crossKillGlobal from './cross_kill';
87
import { applyActionToSty, defaultStyContext, restoreSty, StyContext } from './sty';
98

9+
interface CrossSpawnParams {
10+
command: string;
11+
args: string[];
12+
cwd?: string;
13+
env?: NodeJS.ProcessEnv | undefined;
14+
}
15+
type CrossProcessReadable = {
16+
readonly on: (name: 'data', handler: (buf: Buffer) => void) => void;
17+
readonly off: (name: 'data', handler: (buf: Buffer) => void) => void;
18+
};
19+
type CrossProcess = {
20+
readonly pid?: number | undefined;
21+
readonly once: (name: 'exit', handler: (exitCode: number | null) => void) => void;
22+
readonly stdout: CrossProcessReadable;
23+
readonly stderr: CrossProcessReadable;
24+
};
25+
type CrossSpawn = (params: CrossSpawnParams) => CrossProcess;
26+
1027
export type ProcStatus = 'waiting' | 'running' | 'killed' | 'finished';
1128

1229
export interface ProcOwn {
@@ -19,7 +36,8 @@ export interface ProcOwnInternal {
1936
command: string;
2037
cwd: string;
2138
npmPath: string;
22-
$raw?: childProcess.ChildProcessWithoutNullStreams;
39+
// $raw?: childProcess.ChildProcessWithoutNullStreams;
40+
$raw?: CrossProcess;
2341
}
2442

2543
export interface LogOwn {
@@ -173,13 +191,24 @@ export interface CreateProcManagerParams {
173191
enableUnreadMarker: boolean;
174192
historyAlwaysKeepHeadSize: number;
175193
historyCacheSize: number;
194+
195+
// for test
196+
tty?: typeof ttyGlobal;
197+
crossSpawn?: CrossSpawn;
198+
crossKill?: typeof crossKillGlobal;
176199
}
177200
export const createProcManager = ({
178201
forceNoColor,
179202
enableUnreadMarker,
180203
historyAlwaysKeepHeadSize,
181204
historyCacheSize,
205+
tty: tty0,
206+
crossKill: crossKill0,
207+
crossSpawn: crossSpawn0,
182208
}: CreateProcManagerParams): ProcManager => {
209+
const tty = tty0 ?? ttyGlobal;
210+
const crossSpawn: CrossSpawn = crossSpawn0 ?? cp.spawn;
211+
const crossKill = crossKill0 ?? crossKillGlobal;
183212
const isColorSupported =
184213
!('NO_COLOR' in process.env || forceNoColor) &&
185214
('FORCE_COLOR' in process.env ||
@@ -496,8 +525,10 @@ export const createProcManager = ({
496525
nodeStderr.parent = node;
497526
$notifyUpdate();
498527

499-
const p = cp.spawn(node.procOwn.npmPath, ['run', node.name], {
500-
stdio: ['pipe', 'pipe', 'pipe'],
528+
const p = crossSpawn({
529+
command: node.procOwn.npmPath,
530+
args: ['run', node.name],
531+
// stdio: ['pipe', 'pipe', 'pipe'],
501532
cwd: node.procOwn.cwd,
502533
env: {
503534
...process.env,

test/proc_manager/test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, test } from 'vitest';
2+
import { createProcManager } from '../../src/utils/proc_manager';
3+
4+
const f = () => {};
5+
6+
describe('todo', () => {
7+
test('todo', () => {
8+
const pm = createProcManager({
9+
historyCacheSize: 30,
10+
historyAlwaysKeepHeadSize: 30,
11+
enableUnreadMarker: true,
12+
forceNoColor: false,
13+
});
14+
});
15+
});

0 commit comments

Comments
 (0)