Skip to content

Commit e7f6995

Browse files
committed
update mocking
1 parent 66f599b commit e7f6995

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

src/managers/builtin/helpers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as ch from 'child_process';
21
import { CancellationError, CancellationToken, LogOutputChannel } from 'vscode';
2+
import { spawnProcess } from '../../common/childProcess.apis';
33
import { EventNames } from '../../common/telemetry/constants';
44
import { sendTelemetryEvent } from '../../common/telemetry/sender';
55
import { createDeferred } from '../../common/utils/deferred';
@@ -22,11 +22,11 @@ export async function isUvInstalled(log?: LogOutputChannel): Promise<boolean> {
2222
return available.promise;
2323
}
2424
log?.info(`Running: uv --version`);
25-
const proc = ch.spawn('uv', ['--version']);
25+
const proc = spawnProcess('uv', ['--version']);
2626
proc.on('error', () => {
2727
available.resolve(false);
2828
});
29-
proc.stdout.on('data', (d) => log?.info(d.toString()));
29+
proc.stdout?.on('data', (d) => log?.info(d.toString()));
3030
proc.on('exit', (code) => {
3131
if (code === 0) {
3232
sendTelemetryEvent(EventNames.VENV_USING_UV);
@@ -77,7 +77,7 @@ export async function runUV(
7777
): Promise<string> {
7878
log?.info(`Running: uv ${args.join(' ')}`);
7979
return new Promise<string>((resolve, reject) => {
80-
const proc = ch.spawn('uv', args, { cwd: cwd });
80+
const proc = spawnProcess('uv', args, { cwd: cwd });
8181
token?.onCancellationRequested(() => {
8282
proc.kill();
8383
reject(new CancellationError());
@@ -112,7 +112,7 @@ export async function runPython(
112112
): Promise<string> {
113113
log?.info(`Running: ${python} ${args.join(' ')}`);
114114
return new Promise<string>((resolve, reject) => {
115-
const proc = ch.spawn(python, args, { cwd: cwd });
115+
const proc = spawnProcess(python, args, { cwd: cwd });
116116
token?.onCancellationRequested(() => {
117117
proc.kill();
118118
reject(new CancellationError());

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as sinon from 'sinon';
33
import { LogOutputChannel } from 'vscode';
44
import * as persistentState from '../../../common/persistentState';
55
import * as workspaceApis from '../../../common/workspace.apis';
6-
import * as helpers from '../../../managers/builtin/helpers';
6+
import { resetUvInstallationCache, shouldUseUv } from '../../../managers/builtin/helpers';
77
import * as uvEnvironments from '../../../managers/builtin/uvEnvironments';
88

99
interface MockWorkspaceConfig {
@@ -18,12 +18,11 @@ suite('Helpers - shouldUseUv', () => {
1818
let mockLog: LogOutputChannel;
1919
let getWorkspacePersistentStateStub: sinon.SinonStub;
2020
let mockPersistentState: { get: sinon.SinonStub; set: sinon.SinonStub; clear: sinon.SinonStub };
21-
let isUvInstalledStub: sinon.SinonStub;
2221
let getUvEnvironmentsStub: sinon.SinonStub;
2322

2423
setup(() => {
2524
// Reset UV installation cache before each test to ensure clean state
26-
helpers.resetUvInstallationCache();
25+
resetUvInstallationCache();
2726

2827
mockGetConfiguration = sinon.stub(workspaceApis, 'getConfiguration');
2928
mockConfig = {
@@ -43,7 +42,6 @@ suite('Helpers - shouldUseUv', () => {
4342
getWorkspacePersistentStateStub.returns(Promise.resolve(mockPersistentState));
4443

4544
// Mock UV-related functions
46-
isUvInstalledStub = sinon.stub(helpers, 'isUvInstalled');
4745
getUvEnvironmentsStub = sinon.stub(uvEnvironments, 'getUvEnvironments');
4846

4947
// No default behaviors set - each test configures what it needs
@@ -82,11 +80,11 @@ suite('Helpers - shouldUseUv', () => {
8280
mockConfig.get.withArgs('alwaysUseUv', true).returns(true);
8381
mockConfig.get.withArgs('alwaysUseUv').returns(true);
8482
mockConfig.inspect.withArgs('alwaysUseUv').returns(mockInspectResult);
85-
isUvInstalledStub.resolves(true);
83+
8684
getUvEnvironmentsStub.resolves([]);
8785

8886
// Run
89-
const result = await helpers.shouldUseUv(mockLog);
87+
const result = await shouldUseUv(mockLog);
9088

9189
// Assert - Should return true when setting is true and UV is installed
9290
assert.strictEqual(result, true);
@@ -98,7 +96,7 @@ suite('Helpers - shouldUseUv', () => {
9896
getUvEnvironmentsStub.resolves([]);
9997

10098
// Run
101-
const result = await helpers.shouldUseUv(mockLog);
99+
const result = await shouldUseUv(mockLog);
102100

103101
// Assert - Should not use UV when setting is false
104102
assert.strictEqual(result, false);
@@ -108,11 +106,10 @@ suite('Helpers - shouldUseUv', () => {
108106
// Mock - UV environments list with test path and UV is installed
109107
const uvEnvPath = '/path/to/uv/env';
110108
getUvEnvironmentsStub.resolves([uvEnvPath]);
111-
isUvInstalledStub.resolves(true);
112109
mockConfig.get.withArgs('alwaysUseUv', true).returns(false);
113110

114111
// Run
115-
const result = await helpers.shouldUseUv(mockLog, uvEnvPath);
112+
const result = await shouldUseUv(mockLog, uvEnvPath);
116113

117114
// Assert - Should return true for UV environments when UV is installed
118115
assert.strictEqual(result, true);
@@ -125,7 +122,7 @@ suite('Helpers - shouldUseUv', () => {
125122
getUvEnvironmentsStub.resolves([]);
126123

127124
// Run
128-
const result = await helpers.shouldUseUv(mockLog, nonUvEnvPath);
125+
const result = await shouldUseUv(mockLog, nonUvEnvPath);
129126

130127
// Assert - Should not use UV for non-UV environments when setting is false
131128
assert.strictEqual(result, false);
@@ -135,11 +132,10 @@ suite('Helpers - shouldUseUv', () => {
135132
// Mock - Non-UV environment, alwaysUseUv is true, UV is installed
136133
const nonUvEnvPath = '/path/to/regular/env';
137134
mockConfig.get.withArgs('alwaysUseUv', true).returns(true);
138-
isUvInstalledStub.resolves(true);
139135
getUvEnvironmentsStub.resolves([]);
140136

141137
// Run
142-
const result = await helpers.shouldUseUv(mockLog, nonUvEnvPath);
138+
const result = await shouldUseUv(mockLog, nonUvEnvPath);
143139

144140
// Assert - Should return true when alwaysUseUv is true and UV is installed
145141
assert.strictEqual(result, true);
@@ -148,11 +144,10 @@ suite('Helpers - shouldUseUv', () => {
148144
test('should use default value true when alwaysUseUv setting is not configured', async () => {
149145
// Mock - Setting not configured, should use default of true, UV is installed
150146
mockConfig.get.withArgs('alwaysUseUv', true).returns(true);
151-
isUvInstalledStub.resolves(true);
152147
getUvEnvironmentsStub.resolves([]);
153148

154149
// Run
155-
const result = await helpers.shouldUseUv(mockLog);
150+
const result = await shouldUseUv(mockLog);
156151

157152
// Assert - Should return true with default setting when UV is installed
158153
assert.strictEqual(result, true);
@@ -164,7 +159,7 @@ suite('Helpers - shouldUseUv', () => {
164159
getUvEnvironmentsStub.resolves([]);
165160

166161
// Run
167-
const result = await helpers.shouldUseUv(mockLog);
162+
const result = await shouldUseUv(mockLog);
168163

169164
// Assert - Should not use UV when setting is false
170165
assert.strictEqual(result, false);

0 commit comments

Comments
 (0)