-
Notifications
You must be signed in to change notification settings - Fork 41
Fix set() for global scope not updating globalEnv in memory
#1457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
StellaHuang95
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
StellaHuang95:1-fix-set-global-not-updating-globalEnv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/test/managers/conda/condaEnvManager.setGlobal.unit.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import { Uri } from 'vscode'; | ||
| import { PythonEnvironment, PythonEnvironmentApi } from '../../../api'; | ||
| import { PythonEnvironmentImpl } from '../../../internal.api'; | ||
| import { CondaEnvManager } from '../../../managers/conda/condaEnvManager'; | ||
| import * as condaUtils from '../../../managers/conda/condaUtils'; | ||
| import { NativePythonFinder } from '../../../managers/common/nativePythonFinder'; | ||
|
|
||
| function makeEnv(name: string, envPath: string, version: string = '3.12.0'): PythonEnvironment { | ||
| return new PythonEnvironmentImpl( | ||
| { id: `${name}-test`, managerId: 'ms-python.python:conda' }, | ||
| { | ||
| name, | ||
| displayName: `${name} (${version})`, | ||
| displayPath: envPath, | ||
| version, | ||
| environmentPath: Uri.file(envPath), | ||
| sysPrefix: envPath, | ||
| execInfo: { | ||
| run: { executable: 'python' }, | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| function createManager(): CondaEnvManager { | ||
| const manager = new CondaEnvManager( | ||
| {} as NativePythonFinder, | ||
| {} as PythonEnvironmentApi, | ||
| { info: sinon.stub(), error: sinon.stub(), warn: sinon.stub() } as any, | ||
| ); | ||
| // Bypass initialization | ||
| (manager as any)._initialized = { completed: true, promise: Promise.resolve() }; | ||
| (manager as any).collection = []; | ||
| return manager; | ||
| } | ||
|
|
||
| suite('CondaEnvManager.set - globalEnv update', () => { | ||
| let setCondaForGlobalStub: sinon.SinonStub; | ||
| let checkNoPythonStub: sinon.SinonStub; | ||
|
|
||
| setup(() => { | ||
| setCondaForGlobalStub = sinon.stub(condaUtils, 'setCondaForGlobal').resolves(); | ||
| checkNoPythonStub = sinon.stub(condaUtils, 'checkForNoPythonCondaEnvironment'); | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| test('set(undefined, env) updates globalEnv in memory', async () => { | ||
| const manager = createManager(); | ||
| const oldEnv = makeEnv('base', '/miniconda3', '3.11.0'); | ||
| const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0'); | ||
| (manager as any).globalEnv = oldEnv; | ||
|
|
||
| // checkForNoPythonCondaEnvironment returns the env as-is (has Python) | ||
| checkNoPythonStub.resolves(newEnv); | ||
|
|
||
| await manager.set(undefined, newEnv); | ||
|
|
||
| // globalEnv should now be updated in memory | ||
| const result = await manager.get(undefined); | ||
| assert.strictEqual(result, newEnv, 'get(undefined) should return the newly set environment'); | ||
| assert.notStrictEqual(result, oldEnv, 'get(undefined) should NOT return the old environment'); | ||
| }); | ||
|
|
||
| test('set(undefined, env) persists to disk', async () => { | ||
| const manager = createManager(); | ||
| const newEnv = makeEnv('myenv', '/miniconda3/envs/myenv', '3.12.0'); | ||
| checkNoPythonStub.resolves(newEnv); | ||
|
|
||
| await manager.set(undefined, newEnv); | ||
|
|
||
| assert.ok(setCondaForGlobalStub.calledOnce, 'setCondaForGlobal should be called'); | ||
| assert.strictEqual( | ||
| setCondaForGlobalStub.firstCall.args[0], | ||
| newEnv.environmentPath.fsPath, | ||
| 'should persist the correct path', | ||
| ); | ||
| }); | ||
|
|
||
| test('set(undefined, undefined) clears globalEnv', async () => { | ||
| const manager = createManager(); | ||
| const oldEnv = makeEnv('base', '/miniconda3', '3.11.0'); | ||
| (manager as any).globalEnv = oldEnv; | ||
|
|
||
| await manager.set(undefined, undefined); | ||
|
|
||
| const result = await manager.get(undefined); | ||
| assert.strictEqual(result, undefined, 'get(undefined) should return undefined after clearing'); | ||
| }); | ||
|
|
||
| test('set(undefined, noPythonEnv) where user declines install clears globalEnv', async () => { | ||
| const manager = createManager(); | ||
| const oldEnv = makeEnv('base', '/miniconda3', '3.11.0'); | ||
| const noPythonEnv = makeEnv('nopy', '/miniconda3/envs/nopy', 'no-python'); | ||
| (manager as any).globalEnv = oldEnv; | ||
|
|
||
| // User declined to install Python | ||
| checkNoPythonStub.resolves(undefined); | ||
|
|
||
| await manager.set(undefined, noPythonEnv); | ||
|
|
||
| const result = await manager.get(undefined); | ||
| assert.strictEqual(result, undefined, 'globalEnv should be cleared when checkedEnv is undefined'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you see if we have a helper function like this elsewhere and if not move this to a common place which many testing files could use