-
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
2
commits 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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
24 changes: 1 addition & 23 deletions
24
src/test/managers/conda/condaEnvManager.findEnvironmentByPath.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
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
92 changes: 92 additions & 0 deletions
92
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,92 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import { PythonEnvironmentApi } from '../../../api'; | ||
| import { CondaEnvManager } from '../../../managers/conda/condaEnvManager'; | ||
| import * as condaUtils from '../../../managers/conda/condaUtils'; | ||
| import { NativePythonFinder } from '../../../managers/common/nativePythonFinder'; | ||
| import { makeMockPythonEnvironment as makeEnv } from '../../mocks/pythonEnvironment'; | ||
|
|
||
| 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'); | ||
| }); | ||
| }); |
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,80 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { Uri } from 'vscode'; | ||
| import { PythonEnvironment } from '../../api'; | ||
| import { PythonEnvironmentImpl } from '../../internal.api'; | ||
|
|
||
| /** | ||
| * Options for {@link createMockPythonEnvironment}. | ||
| */ | ||
| export interface MockPythonEnvironmentOptions { | ||
| /** Environment name, e.g. `myenv`. Defaults to `test-env`. */ | ||
| name?: string; | ||
| /** Filesystem path for `environmentPath`, `displayPath`, and `sysPrefix`. */ | ||
| envPath: string; | ||
| /** Version string. Defaults to `3.12.0`. */ | ||
| version?: string; | ||
| /** Manager id. Defaults to `ms-python.python:conda`. */ | ||
| managerId?: string; | ||
| /** Environment id. Defaults to `<name>-test`. */ | ||
| id?: string; | ||
| /** Optional description. */ | ||
| description?: string; | ||
| /** Optional display name. Defaults to `<name> (<version>)`. */ | ||
| displayName?: string; | ||
| /** If true, includes an `activation` entry in `execInfo`. */ | ||
| hasActivation?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Create a minimal {@link PythonEnvironment} for use in unit tests. | ||
| * | ||
| * Shared across manager and view tests so they agree on the shape of a mock | ||
| * environment. Only fields that tests commonly need are populated; extend this | ||
| * helper if additional fields become required. | ||
| */ | ||
| export function createMockPythonEnvironment(options: MockPythonEnvironmentOptions): PythonEnvironment { | ||
| const { | ||
| name = 'test-env', | ||
| envPath, | ||
| version = '3.12.0', | ||
| managerId = 'ms-python.python:conda', | ||
| id = `${name}-test`, | ||
| description, | ||
| displayName = `${name} (${version})`, | ||
| hasActivation = false, | ||
| } = options; | ||
|
|
||
| return new PythonEnvironmentImpl( | ||
| { id, managerId }, | ||
| { | ||
| name, | ||
| displayName, | ||
| displayPath: envPath, | ||
| version, | ||
| description, | ||
| environmentPath: Uri.file(envPath), | ||
| sysPrefix: envPath, | ||
| execInfo: { | ||
| run: { executable: 'python' }, | ||
| ...(hasActivation && { | ||
| activation: [{ executable: envPath.replace('python', 'activate') }], | ||
| }), | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Positional shorthand for {@link createMockPythonEnvironment} used by conda | ||
| * manager unit tests. Prefer {@link createMockPythonEnvironment} for new tests | ||
| * that need to customize additional fields. | ||
| */ | ||
| export function makeMockPythonEnvironment( | ||
| name: string, | ||
| envPath: string, | ||
| version: string = '3.12.0', | ||
| ): PythonEnvironment { | ||
| return createMockPythonEnvironment({ name, envPath, version }); | ||
| } | ||
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.
this will always be conda right? maybe update the function to specify that so there isn't confusion