@@ -3,8 +3,10 @@ import * as vscode from 'vscode';
33import * as sinon from 'sinon' ;
44import * as typeMoq from 'typemoq' ;
55import {
6- PythonCommandRunConfiguration ,
6+ Package ,
7+ PackageId ,
78 PythonEnvironment ,
9+ PythonEnvironmentId ,
810 PythonPackageGetterApi ,
911 PythonPackageManagementApi ,
1012 PythonProjectEnvironmentApi ,
@@ -262,29 +264,29 @@ suite('GetEnvironmentInfoTool Tests', () => {
262264 getEnvironmentInfoTool = new GetEnvironmentInfoTool ( mockApi . object ) ;
263265
264266 // runConfig valid / not valid
265- const runConfigValid : PythonCommandRunConfiguration = {
266- executable : 'conda' ,
267- args : [ 'run' , '-n' , 'env_name' , 'python' ] ,
268- } ;
269- const runConfigValidString = 'conda run -n env_name python' ;
270- const runConfigNoArgs : PythonCommandRunConfiguration = {
271- executable : '.venv/bin/python' ,
272- args : [ ] ,
273- } ;
274- const runConfigNoArgsString = '.venv/bin/python' ;
275-
276- // managerId valid / not valid
277- const managerIdValid = `'ms-python.python:venv'` ;
278- const typeValidString = 'venv' ;
279- const managerIdInvalid = `vscode-python, there is no such manager` ;
280-
281- // environment valid
282- const envInfoVersion = '3.9.1' ;
283-
284- //package valid / not valid
285- const installedPackagesValid = [ { name : 'package1' , version : '1.0.0' } , { name : 'package2' } ] ;
286- const installedPackagesValidString = 'package1 1.0.0\npackage2 2.0.0' ;
287- const installedPackagesInvalid = undefined ;
267+ // const runConfigValid: PythonCommandRunConfiguration = {
268+ // executable: 'conda',
269+ // args: ['run', '-n', 'env_name', 'python'],
270+ // };
271+ // const runConfigValidString = 'conda run -n env_name python';
272+ // const runConfigNoArgs: PythonCommandRunConfiguration = {
273+ // executable: '.venv/bin/python',
274+ // args: [],
275+ // };
276+ // const runConfigNoArgsString = '.venv/bin/python';
277+
278+ // // managerId valid / not valid
279+ // const managerIdValid = `'ms-python.python:venv'`;
280+ // const typeValidString = 'venv';
281+ // const managerIdInvalid = `vscode-python, there is no such manager`;
282+
283+ // // environment valid
284+ // const envInfoVersion = '3.9.1';
285+
286+ // // package valid / not valid
287+ // const installedPackagesValid = [{ name: 'package1', version: '1.0.0' }, { name: 'package2' }];
288+ // const installedPackagesValidString = 'package1 1.0.0\npackage2 2.0.0';
289+ // const installedPackagesInvalid = undefined;
288290 } ) ;
289291
290292 teardown ( ( ) => {
@@ -300,7 +302,84 @@ suite('GetEnvironmentInfoTool Tests', () => {
300302 message : 'Invalid input: resourcePath is required' ,
301303 } ) ;
302304 } ) ;
303- // test should throw error if environment is not found
304- //
305- // cancellation token should work if called
305+ test ( 'should throw error if environment is not found' , async ( ) => {
306+ const testFile : IResourceReference = {
307+ resourcePath : 'this/is/a/test/path.ipynb' ,
308+ } ;
309+ mockApi
310+ . setup ( ( x ) => x . getEnvironment ( typeMoq . It . isAny ( ) ) )
311+ . returns ( async ( ) => {
312+ return Promise . reject ( new Error ( 'Unable to get environment' ) ) ;
313+ } ) ;
314+
315+ const options = { input : testFile , toolInvocationToken : undefined } ;
316+ const token = new vscode . CancellationTokenSource ( ) . token ;
317+ await assert . rejects ( getEnvironmentInfoTool . invoke ( options , token ) , {
318+ message : 'Unable to get environment' ,
319+ } ) ;
320+ } ) ;
321+ test ( 'should return successful with environment info' , async ( ) => {
322+ // create mock of PythonEnvironment
323+ const mockEnvironmentSuccess = typeMoq . Mock . ofType < PythonEnvironment > ( ) ;
324+ // mockEnvironment = typeMoq.Mock.ofType<PythonEnvironment>();
325+
326+ // // eslint-disable-next-line @typescript-eslint/no-explicit-any
327+ mockEnvironmentSuccess . setup ( ( x : any ) => x . then ) . returns ( ( ) => undefined ) ;
328+ mockEnvironmentSuccess . setup ( ( x ) => x . version ) . returns ( ( ) => '3.9.1' ) ;
329+ const mockEnvId = typeMoq . Mock . ofType < PythonEnvironmentId > ( ) ;
330+ mockEnvId . setup ( ( x ) => x . managerId ) . returns ( ( ) => 'ms-python.python:venv' ) ;
331+ mockEnvironmentSuccess . setup ( ( x ) => x . envId ) . returns ( ( ) => mockEnvId . object ) ;
332+ mockEnvironmentSuccess
333+ . setup ( ( x ) => x . execInfo )
334+ . returns ( ( ) => ( {
335+ run : {
336+ executable : 'conda' ,
337+ args : [ 'run' , '-n' , 'env_name' , 'python' ] ,
338+ } ,
339+ } ) ) ;
340+
341+ mockApi
342+ . setup ( ( x ) => x . getEnvironment ( typeMoq . It . isAny ( ) ) )
343+ . returns ( async ( ) => {
344+ return Promise . resolve ( mockEnvironmentSuccess . object ) ;
345+ } ) ;
346+ mockApi
347+ . setup ( ( x ) => x . getEnvironment ( typeMoq . It . isAny ( ) ) )
348+ . returns ( async ( ) => {
349+ return Promise . resolve ( mockEnvironmentSuccess . object ) ;
350+ } ) ;
351+ mockApi . setup ( ( x ) => x . refreshPackages ( typeMoq . It . isAny ( ) ) ) . returns ( ( ) => Promise . resolve ( ) ) ;
352+
353+ const packageAId : PackageId = {
354+ id : 'package1' ,
355+ managerId : 'ms-python.python:venv' ,
356+ environmentId : 'env_id' ,
357+ } ;
358+ const packageBId : PackageId = {
359+ id : 'package2' ,
360+ managerId : 'ms-python.python:venv' ,
361+ environmentId : 'env_id' ,
362+ } ;
363+ const packageA : Package = { name : 'package1' , displayName : 'Package 1' , version : '1.0.0' , pkgId : packageAId } ;
364+ const packageB : Package = { name : 'package2' , displayName : 'Package 2' , version : '2.0.0' , pkgId : packageBId } ;
365+ mockApi
366+ . setup ( ( x ) => x . getPackages ( typeMoq . It . isAny ( ) ) )
367+ . returns ( async ( ) => {
368+ return Promise . resolve ( [ packageA , packageB ] ) ;
369+ } ) ;
370+
371+ const testFile : IResourceReference = {
372+ resourcePath : 'this/is/a/test/path.ipynb' ,
373+ } ;
374+ const options = { input : testFile , toolInvocationToken : undefined } ;
375+ const token = new vscode . CancellationTokenSource ( ) . token ;
376+ // run
377+ const result = await getEnvironmentInfoTool . invoke ( options , token ) ;
378+ // assert
379+ const content = result . content as vscode . LanguageModelTextPart [ ] ;
380+ const firstPart = content [ 0 ] as vscode . MarkdownString ;
381+ console . log ( 'result' , firstPart . value ) ;
382+ assert . strictEqual ( firstPart . value . includes ( 'Python version: 3.9.1' ) , true ) ;
383+ assert . strictEqual ( firstPart . value , '' ) ;
384+ } ) ;
306385} ) ;
0 commit comments