Skip to content

Commit 47206e3

Browse files
authored
add testing for runInBackground (#877)
1 parent 55b8efd commit 47206e3

File tree

3 files changed

+455
-2
lines changed

3 files changed

+455
-2
lines changed

src/common/childProcess.apis.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as cp from 'child_process';
2+
3+
/**
4+
* Spawns a new process using the specified command and arguments.
5+
* This function abstracts cp.spawn to make it easier to mock in tests.
6+
*
7+
* When stdio: 'pipe' is used, returns ChildProcessWithoutNullStreams.
8+
* Otherwise returns the standard ChildProcess.
9+
*/
10+
11+
// Overload for stdio: 'pipe' - guarantees non-null streams
12+
export function spawnProcess(
13+
command: string,
14+
args: string[],
15+
options: cp.SpawnOptions & { stdio: 'pipe' },
16+
): cp.ChildProcessWithoutNullStreams;
17+
18+
// Overload for general case
19+
export function spawnProcess(command: string, args: string[], options?: cp.SpawnOptions): cp.ChildProcess;
20+
21+
// Implementation - delegates to cp.spawn to preserve its typing magic
22+
export function spawnProcess(
23+
command: string,
24+
args: string[],
25+
options?: cp.SpawnOptions,
26+
): cp.ChildProcess | cp.ChildProcessWithoutNullStreams {
27+
return cp.spawn(command, args, options ?? {});
28+
}

src/features/execution/runInBackground.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as cp from 'child_process';
21
import { PythonBackgroundRunOptions, PythonEnvironment, PythonProcess } from '../../api';
2+
import { spawnProcess } from '../../common/childProcess.apis';
33
import { traceError, traceInfo, traceWarn } from '../../common/logging';
44
import { quoteStringIfNecessary } from './execUtils';
55

@@ -39,7 +39,11 @@ export async function runInBackground(
3939
traceWarn(`Error checking if executable exists: ${err instanceof Error ? err.message : String(err)}`);
4040
}
4141

42-
const proc = cp.spawn(executable, allArgs, { stdio: 'pipe', cwd: options.cwd, env: options.env });
42+
const proc = spawnProcess(executable, allArgs, {
43+
stdio: 'pipe',
44+
cwd: options.cwd,
45+
env: options.env,
46+
});
4347

4448
return {
4549
pid: proc.pid,

0 commit comments

Comments
 (0)