88import { expect } from 'chai' ;
99import * as path from 'path' ;
1010import * as typemoq from 'typemoq' ;
11+ import { Uri } from 'vscode' ;
1112import { InvalidPythonPathInDebuggerService } from '../../../../client/application/diagnostics/checks/invalidPythonPathInDebugger' ;
1213import { CommandOption , IDiagnosticsCommandFactory } from '../../../../client/application/diagnostics/commands/types' ;
1314import { DiagnosticCodes } from '../../../../client/application/diagnostics/constants' ;
1415import { DiagnosticCommandPromptHandlerServiceId , MessageCommandPrompt } from '../../../../client/application/diagnostics/promptHandler' ;
1516import { IDiagnostic , IDiagnosticCommand , IDiagnosticHandlerService , IInvalidPythonPathInDebuggerService } from '../../../../client/application/diagnostics/types' ;
17+ import { IWorkspaceService } from '../../../../client/common/application/types' ;
1618import { IConfigurationService , IPythonSettings } from '../../../../client/common/types' ;
1719import { IInterpreterHelper } from '../../../../client/interpreter/contracts' ;
1820import { IServiceContainer } from '../../../../client/ioc/types' ;
@@ -23,6 +25,7 @@ suite('Application Diagnostics - Checks Python Path in debugger', () => {
2325 let commandFactory : typemoq . IMock < IDiagnosticsCommandFactory > ;
2426 let configService : typemoq . IMock < IConfigurationService > ;
2527 let helper : typemoq . IMock < IInterpreterHelper > ;
28+ let workspaceService : typemoq . IMock < IWorkspaceService > ;
2629 setup ( ( ) => {
2730 const serviceContainer = typemoq . Mock . ofType < IServiceContainer > ( ) ;
2831 messageHandler = typemoq . Mock . ofType < IDiagnosticHandlerService < MessageCommandPrompt > > ( ) ;
@@ -37,8 +40,11 @@ suite('Application Diagnostics - Checks Python Path in debugger', () => {
3740 helper = typemoq . Mock . ofType < IInterpreterHelper > ( ) ;
3841 serviceContainer . setup ( s => s . get ( typemoq . It . isValue ( IInterpreterHelper ) ) )
3942 . returns ( ( ) => helper . object ) ;
43+ workspaceService = typemoq . Mock . ofType < IWorkspaceService > ( ) ;
44+ serviceContainer . setup ( s => s . get ( typemoq . It . isValue ( IWorkspaceService ) ) )
45+ . returns ( ( ) => workspaceService . object ) ;
4046
41- diagnosticService = new InvalidPythonPathInDebuggerService ( serviceContainer . object ) ;
47+ diagnosticService = new InvalidPythonPathInDebuggerService ( serviceContainer . object , workspaceService . object , commandFactory . object , helper . object , configService . object ) ;
4248 } ) ;
4349
4450 test ( 'Can handle InvalidPythonPathInDebugger diagnostics' , async ( ) => {
@@ -108,6 +114,64 @@ suite('Application Diagnostics - Checks Python Path in debugger', () => {
108114 helper . verifyAll ( ) ;
109115 expect ( valid ) . to . be . equal ( true , 'not valid' ) ;
110116 } ) ;
117+ test ( 'Ensure ${workspaceFolder} is not expanded when a resource is not passed' , async ( ) => {
118+ const pythonPath = '${workspaceFolder}/venv/bin/python' ;
119+
120+ workspaceService
121+ . setup ( c => c . getWorkspaceFolder ( typemoq . It . isAny ( ) ) )
122+ . returns ( ( ) => undefined )
123+ . verifiable ( typemoq . Times . never ( ) ) ;
124+ helper
125+ . setup ( h => h . getInterpreterInformation ( typemoq . It . isAny ( ) ) )
126+ . returns ( ( ) => Promise . resolve ( { } ) )
127+ . verifiable ( typemoq . Times . once ( ) ) ;
128+
129+ await diagnosticService . validatePythonPath ( pythonPath ) ;
130+
131+ configService . verifyAll ( ) ;
132+ helper . verifyAll ( ) ;
133+ } ) ;
134+ test ( 'Ensure ${workspaceFolder} is expanded' , async ( ) => {
135+ const pythonPath = '${workspaceFolder}/venv/bin/python' ;
136+
137+ const workspaceFolder = { uri : Uri . parse ( 'full/path/to/workspace' ) , name : '' , index : 0 } ;
138+ const expectedPath = `${ workspaceFolder . uri . fsPath } /venv/bin/python` ;
139+
140+ workspaceService
141+ . setup ( c => c . getWorkspaceFolder ( typemoq . It . isAny ( ) ) )
142+ . returns ( ( ) => workspaceFolder )
143+ . verifiable ( typemoq . Times . once ( ) ) ;
144+ helper
145+ . setup ( h => h . getInterpreterInformation ( typemoq . It . isValue ( expectedPath ) ) )
146+ . returns ( ( ) => Promise . resolve ( { } ) )
147+ . verifiable ( typemoq . Times . once ( ) ) ;
148+
149+ const valid = await diagnosticService . validatePythonPath ( pythonPath , Uri . parse ( 'something' ) ) ;
150+
151+ configService . verifyAll ( ) ;
152+ helper . verifyAll ( ) ;
153+ expect ( valid ) . to . be . equal ( true , 'not valid' ) ;
154+ } ) ;
155+ test ( 'Ensure ${env:XYZ123} is expanded' , async ( ) => {
156+ const pythonPath = '${env:XYZ123}/venv/bin/python' ;
157+
158+ process . env . XYZ123 = 'something/else' ;
159+ const expectedPath = `${ process . env . XYZ123 } /venv/bin/python` ;
160+ workspaceService
161+ . setup ( c => c . getWorkspaceFolder ( typemoq . It . isAny ( ) ) )
162+ . returns ( ( ) => undefined )
163+ . verifiable ( typemoq . Times . once ( ) ) ;
164+ helper
165+ . setup ( h => h . getInterpreterInformation ( typemoq . It . isValue ( expectedPath ) ) )
166+ . returns ( ( ) => Promise . resolve ( { } ) )
167+ . verifiable ( typemoq . Times . once ( ) ) ;
168+
169+ const valid = await diagnosticService . validatePythonPath ( pythonPath ) ;
170+
171+ configService . verifyAll ( ) ;
172+ helper . verifyAll ( ) ;
173+ expect ( valid ) . to . be . equal ( true , 'not valid' ) ;
174+ } ) ;
111175 test ( 'Ensure we get python path from config when path = undefined' , async ( ) => {
112176 const pythonPath = undefined ;
113177
0 commit comments