11import * as vscode from 'vscode' ;
22
3- import { compare } from '@renovatebot/pep440' ;
3+ import { compare , Pep440Version } from '@renovatebot/pep440' ;
44import assert from 'assert' ;
55import {
66 Package ,
7+ PackageManagementOptions ,
78 PythonEnvironment ,
89 PythonEnvironmentApi ,
910 isPackageVersionLookupNotSupportedError ,
1011} from '../../api' ;
11- import { CONDA_MANAGER_ID , DEFAULT_PACKAGE_MANAGER_ID } from '../../common/constants' ;
12+ import { CONDA_MANAGER_ID , DEFAULT_PACKAGE_MANAGER_ID , VENV_MANAGER_ID } from '../../common/constants' ;
1213import { ENVS_EXTENSION_ID } from '../constants' ;
1314import { waitForCondition } from '../testUtils' ;
1415import {
@@ -102,6 +103,21 @@ for (const profile of profiles) {
102103 let fixture : EnvironmentFixture | undefined ;
103104 let previousAlwaysUseUv : boolean | undefined ;
104105 let alwaysUseUvUpdated = false ;
106+ let previousDefaultPackageManager : string | undefined ;
107+ let defaultPackageManagerUpdated = false ;
108+
109+ function getPackages ( ) : Thenable < Package [ ] | undefined > {
110+ return vscode . commands . executeCommand < Package [ ] > (
111+ 'python-envs.test.getPackagesForProject' , environment ! , fixture ! . projectUri ,
112+ ) ;
113+ }
114+
115+ function managePackages ( options : PackageManagementOptions ) : Thenable < void > {
116+ return vscode . commands . executeCommand < void > (
117+ 'python-envs.test.managePackagesForProject' , environment ! , fixture ! . projectUri , options ,
118+ ) ;
119+ }
120+
105121 suiteSetup ( async function ( ) {
106122 if ( process . env . VSC_PYTHON_PACKAGE_NETWORK_TEST !== '1' ) {
107123 this . skip ( ) ;
@@ -121,10 +137,19 @@ for (const profile of profiles) {
121137 assert . ok ( workspaceFolder , 'Integration test workspace not found' ) ;
122138 const config = vscode . workspace . getConfiguration ( 'python-envs' , workspaceFolder . uri ) ;
123139
124- if ( profile . packageManagerId === DEFAULT_PACKAGE_MANAGER_ID ) {
140+ // Fixture projects live outside the workspace, so select their manager globally.
141+ previousDefaultPackageManager = config . inspect < string > ( 'defaultPackageManager' ) ?. globalValue ;
142+ await config . update ( 'defaultPackageManager' , profile . packageManagerId , vscode . ConfigurationTarget . Global ) ;
143+ defaultPackageManagerUpdated = true ;
144+
145+ if ( profile . provider . managerId === VENV_MANAGER_ID ) {
125146 previousAlwaysUseUv = config . inspect < boolean > ( 'alwaysUseUv' ) ?. globalValue ;
126147 await config . update ( 'alwaysUseUv' , false , vscode . ConfigurationTarget . Global ) ;
127148 alwaysUseUvUpdated = true ;
149+ assert . strictEqual (
150+ vscode . workspace . getConfiguration ( 'python-envs' , workspaceFolder . uri ) . get < boolean > ( 'alwaysUseUv' ) ,
151+ false ,
152+ ) ;
128153 }
129154
130155 fixture = await createEnvironmentFixture ( api , workspaceFolder , {
@@ -133,6 +158,10 @@ for (const profile of profiles) {
133158 provider : profile . provider ,
134159 } ) ;
135160 environment = fixture . environment ;
161+ assert . strictEqual (
162+ vscode . workspace . getConfiguration ( 'python-envs' , fixture . projectUri ) . get < string > ( 'defaultPackageManager' ) ,
163+ profile . packageManagerId ,
164+ ) ;
136165 assert . strictEqual (
137166 environment . envId . managerId ,
138167 profile . provider . managerId ,
@@ -142,7 +171,7 @@ for (const profile of profiles) {
142171
143172 test ( `${ profile . name } Package Manager should install, list, and uninstall a package` , async ( ) => {
144173 const packageName = profile . packageName ;
145- const baseline = await api . getPackages ( environment ! , { skipCache : true } ) ;
174+ const baseline = await getPackages ( ) ;
146175 assert . ok ( baseline , 'Unable to list packages before installation' ) ;
147176 assert . ok (
148177 baseline . every ( ( pkg ) => pkg . pkgId . managerId === profile . packageManagerId ) ,
@@ -151,12 +180,12 @@ for (const profile of profiles) {
151180 const wasInstalled = baseline . some ( ( pkg ) => pkg . name . toLowerCase ( ) === packageName ) ;
152181
153182 if ( ! wasInstalled ) {
154- await api . managePackages ( environment ! , { install : [ packageName ] , runHeadless : true } ) ;
183+ await managePackages ( { install : [ packageName ] , runHeadless : true } ) ;
155184 }
156185 let packages : Package [ ] | undefined ;
157186 await waitForCondition (
158187 async ( ) => {
159- packages = await api . getPackages ( environment ! , { skipCache : true } ) ;
188+ packages = await getPackages ( ) ;
160189 return packages ?. some ( ( pkg ) => pkg . name . toLowerCase ( ) === packageName ) ?? false ;
161190 } ,
162191 30_000 ,
@@ -169,16 +198,17 @@ for (const profile of profiles) {
169198 const directPackageNames = await vscode . commands . executeCommand < string [ ] | undefined > (
170199 'python-envs.test.getDirectPackageNames' ,
171200 environment ! ,
201+ fixture ! . projectUri ,
172202 ) ;
173203 if ( directPackageNames !== undefined ) {
174204 assert . ok ( directPackageNames . includes ( packageName ) , 'Installed package was not reported as direct' ) ;
175205 }
176206
177207 if ( ! wasInstalled ) {
178- await api . managePackages ( environment ! , { uninstall : [ packageName ] , runHeadless : true } ) ;
208+ await managePackages ( { uninstall : [ packageName ] , runHeadless : true } ) ;
179209 await waitForCondition (
180210 async ( ) => {
181- packages = await api . getPackages ( environment ! , { skipCache : true } ) ;
211+ packages = await getPackages ( ) ;
182212 return packages !== undefined && ! packages . some ( ( pkg ) => pkg . name . toLowerCase ( ) === packageName ) ;
183213 } ,
184214 30_000 ,
@@ -191,7 +221,7 @@ for (const profile of profiles) {
191221 } ) ;
192222
193223 test ( `${ profile . name } Package Manager should list available package versions` , async function ( ) {
194- const packages = await api . getPackages ( environment ! , { skipCache : true } ) ;
224+ const packages = await getPackages ( ) ;
195225 assert . ok ( packages , 'Unable to list packages before version lookup' ) ;
196226 const supportsVersionLookup = profile . supportsVersionLookup ( packages ) ;
197227 assert . notStrictEqual (
@@ -202,9 +232,10 @@ for (const profile of profiles) {
202232
203233 let versions ;
204234 try {
205- versions = await api . getPackageAvailableVersions ( environment ! , profile . packageName , {
206- errorMode : 'throw' ,
207- } ) ;
235+ versions = await vscode . commands . executeCommand < Pep440Version [ ] > (
236+ 'python-envs.test.getPackageAvailableVersionsForProject' ,
237+ environment ! , fixture ! . projectUri , profile . packageName ,
238+ ) ;
208239 } catch ( error ) {
209240 if ( isPackageVersionLookupNotSupportedError ( error ) ) {
210241 assert . strictEqual (
@@ -233,11 +264,21 @@ for (const profile of profiles) {
233264 await fixture . dispose ( ) ;
234265 }
235266 } finally {
236- if ( alwaysUseUvUpdated ) {
267+ if ( alwaysUseUvUpdated || defaultPackageManagerUpdated ) {
237268 const workspaceFolder = vscode . workspace . workspaceFolders ?. [ 0 ] ;
238269 assert . ok ( workspaceFolder , 'Integration test workspace not found during teardown' ) ;
239270 const config = vscode . workspace . getConfiguration ( 'python-envs' , workspaceFolder . uri ) ;
240- await config . update ( 'alwaysUseUv' , previousAlwaysUseUv , vscode . ConfigurationTarget . Global ) ;
271+ try {
272+ if ( alwaysUseUvUpdated ) {
273+ await config . update ( 'alwaysUseUv' , previousAlwaysUseUv , vscode . ConfigurationTarget . Global ) ;
274+ }
275+ } finally {
276+ if ( defaultPackageManagerUpdated ) {
277+ await config . update (
278+ 'defaultPackageManager' , previousDefaultPackageManager , vscode . ConfigurationTarget . Global ,
279+ ) ;
280+ }
281+ }
241282 }
242283 }
243284 } ) ;
0 commit comments