@@ -32,6 +32,8 @@ suite('PythonEnvironmentManagers getLastKnownEnvironment', () => {
3232 let envManagers : PythonEnvironmentManagers ;
3333 let projectManager : typeMoq . IMock < PythonProjectManager > ;
3434 let projectsByUri : Map < string , PythonProject > ;
35+ let defaultManagerId : string ;
36+ let exactManagerSettings : Map < string , string > ;
3537
3638 function makeEnv ( id : string ) : PythonEnvironment {
3739 const envId : PythonEnvironmentId = { id, managerId : 'test-manager' } ;
@@ -61,11 +63,16 @@ suite('PythonEnvironmentManagers getLastKnownEnvironment', () => {
6163 projectManager = typeMoq . Mock . ofType < PythonProjectManager > ( ) ;
6264 setupNonThenable ( projectManager ) ;
6365 projectsByUri = new Map ( ) ;
66+ exactManagerSettings = new Map ( ) ;
6467 projectManager
6568 . setup ( ( pm ) => pm . get ( typeMoq . It . isAny ( ) ) )
6669 . returns ( ( uri ) => projectsByUri . get ( uri . toString ( ) ) ) ;
6770
6871 envManagers = new PythonEnvironmentManagers ( projectManager . object ) ;
72+ sinon . stub ( settingHelpers , 'getDefaultEnvManagerSetting' ) . callsFake ( ( ) => defaultManagerId ) ;
73+ sinon
74+ . stub ( settingHelpers , 'getProjectEnvironmentManagerSetting' )
75+ . callsFake ( ( _manager , uri ) => exactManagerSettings . get ( uri . toString ( ) ) ) ;
6976 } ) ;
7077
7178 teardown ( ( ) => {
@@ -93,10 +100,10 @@ suite('PythonEnvironmentManagers getLastKnownEnvironment', () => {
93100 refresh : async ( ) => undefined ,
94101 } as unknown as EnvironmentManager ;
95102
103+ const managerIndex = envManagers . managers . length ;
96104 envManagers . registerEnvironmentManager ( manager ) ;
97- const id = envManagers . managers [ 0 ] . id ;
98- // Force the default environment manager (used for undefined/global scope) to resolve to ours.
99- sinon . stub ( settingHelpers , 'getDefaultEnvManagerSetting' ) . returns ( id ) ;
105+ const id = envManagers . managers [ managerIndex ] . id ;
106+ defaultManagerId = id ;
100107 return id ;
101108 }
102109
@@ -250,6 +257,53 @@ suite('PythonEnvironmentManagers getLastKnownEnvironment', () => {
250257 assert . strictEqual ( envManagers . getLastKnownEnvironment ( secondUri ) , second ) ;
251258 } ) ;
252259
260+ test ( 'routes an active inline-script selection before the containing project default' , async ( ) => {
261+ const script = Uri . file ( '/workspace/project/script.py' ) ;
262+ projectsByUri . set ( script . toString ( ) , { name : 'project' , uri : Uri . file ( '/workspace/project' ) } ) ;
263+ const defaultId = registerManager ( async ( ) => makeEnv ( 'default' ) , async ( ) => undefined , 'venv' ) ;
264+ let inlineEnvironment : PythonEnvironment ;
265+ const inlineId = registerManager ( async ( ) => inlineEnvironment , async ( ) => undefined , 'inline-script' ) ;
266+ inlineEnvironment = { ...makeEnv ( 'inline' ) , envId : { id : 'inline' , managerId : inlineId } } ;
267+ defaultManagerId = defaultId ;
268+
269+ await envManagers . setEnvironment ( script , inlineEnvironment , false ) ;
270+
271+ assert . strictEqual ( envManagers . getEnvironmentManager ( script ) ?. id , inlineId ) ;
272+ assert . strictEqual ( await envManagers . getEnvironment ( script ) , inlineEnvironment ) ;
273+ } ) ;
274+
275+ test ( 'lets an exact script project setting override an active inline selection' , async ( ) => {
276+ const script = Uri . file ( '/workspace/script.py' ) ;
277+ projectsByUri . set ( script . toString ( ) , { name : 'script.py' , uri : script } ) ;
278+ const selectedId = registerManager ( async ( ) => makeEnv ( 'selected' ) , async ( ) => undefined , 'venv' ) ;
279+ let inlineEnvironment : PythonEnvironment ;
280+ const inlineId = registerManager ( async ( ) => inlineEnvironment , async ( ) => undefined , 'inline-script' ) ;
281+ inlineEnvironment = { ...makeEnv ( 'inline' ) , envId : { id : 'inline' , managerId : inlineId } } ;
282+ defaultManagerId = selectedId ;
283+
284+ await envManagers . setEnvironment ( script , inlineEnvironment , false ) ;
285+ exactManagerSettings . set ( script . toString ( ) , selectedId ) ;
286+
287+ assert . strictEqual ( envManagers . getEnvironmentManager ( script ) ?. id , selectedId ) ;
288+ } ) ;
289+
290+ test ( 'clears active inline routing after selecting a different manager' , async ( ) => {
291+ const script = Uri . file ( '/workspace/project/script.py' ) ;
292+ projectsByUri . set ( script . toString ( ) , { name : 'project' , uri : Uri . file ( '/workspace/project' ) } ) ;
293+ let selectedEnvironment : PythonEnvironment ;
294+ const selectedId = registerManager ( async ( ) => selectedEnvironment , async ( ) => undefined , 'venv' ) ;
295+ let inlineEnvironment : PythonEnvironment ;
296+ const inlineId = registerManager ( async ( ) => inlineEnvironment , async ( ) => undefined , 'inline-script' ) ;
297+ selectedEnvironment = { ...makeEnv ( 'selected' ) , envId : { id : 'selected' , managerId : selectedId } } ;
298+ inlineEnvironment = { ...makeEnv ( 'inline' ) , envId : { id : 'inline' , managerId : inlineId } } ;
299+ defaultManagerId = selectedId ;
300+
301+ await envManagers . setEnvironment ( script , inlineEnvironment , false ) ;
302+ await envManagers . setEnvironment ( script , selectedEnvironment , false ) ;
303+
304+ assert . strictEqual ( envManagers . getEnvironmentManager ( script ) ?. id , selectedId ) ;
305+ } ) ;
306+
253307 test ( 'does not persist an inline-script manager for the containing project' , async ( ) => {
254308 const script = Uri . file ( '/workspace/project/script.py' ) ;
255309 const containingProject = { name : 'project' , uri : Uri . file ( '/workspace/project' ) } ;
0 commit comments