Skip to content

Commit dd6e4b4

Browse files
committed
add tests & handle empty array
1 parent f944ad7 commit dd6e4b4

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

src/features/copilotTools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class GetEnvironmentInfoTool implements LanguageModelTool<IResourceRefere
7878

7979
const execInfo: PythonEnvironmentExecutionInfo = environment.execInfo;
8080
const run: PythonCommandRunConfiguration = execInfo.run;
81-
envInfo.runCommand = run.executable + (run.args ? ` ${run.args.join(' ')}` : '');
81+
envInfo.runCommand = run.executable + (run.args && run.args.length > 0 ? ` ${run.args.join(' ')}` : '');
8282
// TODO: check if this is the right way to get type
8383
envInfo.type = environment.envId.managerId.split(':')[1];
8484
envInfo.version = environment.version;

src/test/copilotTools.unit.test.ts

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,6 @@ suite('GetEnvironmentInfoTool Tests', () => {
338338
},
339339
}));
340340

341-
mockApi
342-
.setup((x) => x.getEnvironment(typeMoq.It.isAny()))
343-
.returns(async () => {
344-
return Promise.resolve(mockEnvironmentSuccess.object);
345-
});
346341
mockApi
347342
.setup((x) => x.getEnvironment(typeMoq.It.isAny()))
348343
.returns(async () => {
@@ -379,7 +374,59 @@ suite('GetEnvironmentInfoTool Tests', () => {
379374
const content = result.content as vscode.LanguageModelTextPart[];
380375
const firstPart = content[0] as vscode.MarkdownString;
381376
console.log('result', firstPart.value);
382-
assert.strictEqual(firstPart.value.includes('Python version: 3.9.1'), true);
383-
assert.strictEqual(firstPart.value, '');
377+
assert.strictEqual(firstPart.value.includes('3.9.1'), true);
378+
assert.strictEqual(firstPart.value.includes('package1 (1.0.0)'), true);
379+
assert.strictEqual(firstPart.value.includes('package2 (2.0.0)'), true);
380+
assert.strictEqual(firstPart.value.includes(`"conda run -n env_name python"`), true);
381+
assert.strictEqual(firstPart.value.includes('venv'), true);
382+
});
383+
test('should return successful with weird environment info', async () => {
384+
// create mock of PythonEnvironment
385+
const mockEnvironmentSuccess = typeMoq.Mock.ofType<PythonEnvironment>();
386+
// mockEnvironment = typeMoq.Mock.ofType<PythonEnvironment>();
387+
388+
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
389+
mockEnvironmentSuccess.setup((x: any) => x.then).returns(() => undefined);
390+
mockEnvironmentSuccess.setup((x) => x.version).returns(() => '3.12.1');
391+
const mockEnvId = typeMoq.Mock.ofType<PythonEnvironmentId>();
392+
mockEnvId.setup((x) => x.managerId).returns(() => 'ms-python.python:sys');
393+
mockEnvironmentSuccess.setup((x) => x.envId).returns(() => mockEnvId.object);
394+
mockEnvironmentSuccess
395+
.setup((x) => x.execInfo)
396+
.returns(() => ({
397+
run: {
398+
executable: 'path/to/venv/bin/python',
399+
args: [],
400+
},
401+
}));
402+
403+
mockApi
404+
.setup((x) => x.getEnvironment(typeMoq.It.isAny()))
405+
.returns(async () => {
406+
return Promise.resolve(mockEnvironmentSuccess.object);
407+
});
408+
mockApi.setup((x) => x.refreshPackages(typeMoq.It.isAny())).returns(() => Promise.resolve());
409+
410+
mockApi
411+
.setup((x) => x.getPackages(typeMoq.It.isAny()))
412+
.returns(async () => {
413+
return Promise.resolve([]);
414+
});
415+
416+
const testFile: IResourceReference = {
417+
resourcePath: 'this/is/a/test/path.ipynb',
418+
};
419+
const options = { input: testFile, toolInvocationToken: undefined };
420+
const token = new vscode.CancellationTokenSource().token;
421+
// run
422+
const result = await getEnvironmentInfoTool.invoke(options, token);
423+
// assert
424+
const content = result.content as vscode.LanguageModelTextPart[];
425+
const firstPart = content[0] as vscode.MarkdownString;
426+
console.log('result', firstPart.value);
427+
assert.strictEqual(firstPart.value.includes('3.12.1'), true);
428+
assert.strictEqual(firstPart.value.includes('"packages": []'), true);
429+
assert.strictEqual(firstPart.value.includes(`"path/to/venv/bin/python"`), true);
430+
assert.strictEqual(firstPart.value.includes('sys'), true);
384431
});
385432
});

0 commit comments

Comments
 (0)