Skip to content

Commit 6989f40

Browse files
committed
switch to child process mock
1 parent 6837713 commit 6989f40

1 file changed

Lines changed: 62 additions & 8 deletions

File tree

src/test/managers/builtin/helpers.shouldUseUv.unit.test.ts

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import assert from 'assert';
22
import * as sinon from 'sinon';
33
import { LogOutputChannel } from 'vscode';
4+
import * as childProcessApis from '../../../common/childProcess.apis';
45
import * as persistentState from '../../../common/persistentState';
56
import * as workspaceApis from '../../../common/workspace.apis';
67
import { resetUvInstallationCache, shouldUseUv } from '../../../managers/builtin/helpers';
78
import * as uvEnvironments from '../../../managers/builtin/uvEnvironments';
9+
import { MockChildProcess } from '../../mocks/mockChildProcess';
810

911
interface MockWorkspaceConfig {
1012
get: sinon.SinonStub;
@@ -19,6 +21,7 @@ suite('Helpers - shouldUseUv', () => {
1921
let getWorkspacePersistentStateStub: sinon.SinonStub;
2022
let mockPersistentState: { get: sinon.SinonStub; set: sinon.SinonStub; clear: sinon.SinonStub };
2123
let getUvEnvironmentsStub: sinon.SinonStub;
24+
let spawnStub: sinon.SinonStub;
2225

2326
setup(() => {
2427
// Reset UV installation cache before each test to ensure clean state
@@ -63,6 +66,9 @@ suite('Helpers - shouldUseUv', () => {
6366
logLevel: 1,
6467
onDidChangeLogLevel: sinon.stub() as LogOutputChannel['onDidChangeLogLevel'],
6568
} as unknown as LogOutputChannel;
69+
70+
// Stub childProcess.apis spawnProcess
71+
spawnStub = sinon.stub(childProcessApis, 'spawnProcess');
6672
});
6773

6874
teardown(() => {
@@ -83,8 +89,20 @@ suite('Helpers - shouldUseUv', () => {
8389

8490
getUvEnvironmentsStub.resolves([]);
8591

86-
// Run
87-
const result = await shouldUseUv(mockLog);
92+
// Arrange - Create mock process that simulates successful uv --version
93+
const mockProcess = new MockChildProcess('uv', ['--version']);
94+
spawnStub.withArgs('uv', ['--version']).returns(mockProcess);
95+
96+
// Run - Call function and set up mock events in parallel
97+
const resultPromise = shouldUseUv(mockLog);
98+
99+
// Simulate successful uv --version command
100+
setTimeout(() => {
101+
mockProcess.stdout?.emit('data', 'uv 0.1.0\n');
102+
mockProcess.emit('exit', 0, null);
103+
}, 10);
104+
105+
const result = await resultPromise;
88106

89107
// Assert - Should return true when setting is true and UV is installed
90108
assert.strictEqual(result, true);
@@ -108,8 +126,20 @@ suite('Helpers - shouldUseUv', () => {
108126
getUvEnvironmentsStub.resolves([uvEnvPath]);
109127
mockConfig.get.withArgs('alwaysUseUv', true).returns(false);
110128

111-
// Run
112-
const result = await shouldUseUv(mockLog, uvEnvPath);
129+
// Arrange - Create mock process that simulates successful uv --version
130+
const mockProcess = new MockChildProcess('uv', ['--version']);
131+
spawnStub.withArgs('uv', ['--version']).returns(mockProcess);
132+
133+
// Run - Call function and set up mock events in parallel
134+
const resultPromise = shouldUseUv(mockLog, uvEnvPath);
135+
136+
// Simulate successful uv --version command
137+
setTimeout(() => {
138+
mockProcess.stdout?.emit('data', 'uv 0.1.0\n');
139+
mockProcess.emit('exit', 0, null);
140+
}, 10);
141+
142+
const result = await resultPromise;
113143

114144
// Assert - Should return true for UV environments when UV is installed
115145
assert.strictEqual(result, true);
@@ -134,8 +164,20 @@ suite('Helpers - shouldUseUv', () => {
134164
mockConfig.get.withArgs('alwaysUseUv', true).returns(true);
135165
getUvEnvironmentsStub.resolves([]);
136166

137-
// Run
138-
const result = await shouldUseUv(mockLog, nonUvEnvPath);
167+
// Arrange - Create mock process that simulates successful uv --version
168+
const mockProcess = new MockChildProcess('uv', ['--version']);
169+
spawnStub.withArgs('uv', ['--version']).returns(mockProcess);
170+
171+
// Run - Call function and set up mock events in parallel
172+
const resultPromise = shouldUseUv(mockLog, nonUvEnvPath);
173+
174+
// Simulate successful uv --version command
175+
setTimeout(() => {
176+
mockProcess.stdout?.emit('data', 'uv 0.1.0\n');
177+
mockProcess.emit('exit', 0, null);
178+
}, 10);
179+
180+
const result = await resultPromise;
139181

140182
// Assert - Should return true when alwaysUseUv is true and UV is installed
141183
assert.strictEqual(result, true);
@@ -146,8 +188,20 @@ suite('Helpers - shouldUseUv', () => {
146188
mockConfig.get.withArgs('alwaysUseUv', true).returns(true);
147189
getUvEnvironmentsStub.resolves([]);
148190

149-
// Run
150-
const result = await shouldUseUv(mockLog);
191+
// Arrange - Create mock process that simulates successful uv --version
192+
const mockProcess = new MockChildProcess('uv', ['--version']);
193+
spawnStub.withArgs('uv', ['--version']).returns(mockProcess);
194+
195+
// Run - Call function and set up mock events in parallel
196+
const resultPromise = shouldUseUv(mockLog);
197+
198+
// Simulate successful uv --version command
199+
setTimeout(() => {
200+
mockProcess.stdout?.emit('data', 'uv 0.1.0\n');
201+
mockProcess.emit('exit', 0, null);
202+
}, 10);
203+
204+
const result = await resultPromise;
151205

152206
// Assert - Should return true with default setting when UV is installed
153207
assert.strictEqual(result, true);

0 commit comments

Comments
 (0)