-
Notifications
You must be signed in to change notification settings - Fork 42
fix: use conda.sh for Git Bash activation on Windows (Fixes #1247) #1248
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c9b09b9
fix: use conda.sh for Git Bash activation on Windows (Fixes #1247)
karthiknadig 521578b
fix: address review feedback - use two commands for conda.sh and add …
karthiknadig f2c73f8
fix: address second round of review feedback
karthiknadig bc75eb6
fix: return empty string for empty command arrays in getShellCommandA…
karthiknadig 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
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
201 changes: 201 additions & 0 deletions
201
src/test/managers/conda/condaUtils.windowsActivation.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,201 @@ | ||
| import assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import { ShellConstants } from '../../../features/common/shellConstants'; | ||
| import * as condaSourcingUtils from '../../../managers/conda/condaSourcingUtils'; | ||
| import { windowsExceptionGenerateConfig } from '../../../managers/conda/condaUtils'; | ||
|
|
||
| /** | ||
| * Tests for windowsExceptionGenerateConfig - Windows shell activation commands. | ||
| * | ||
| * Key behavior tested: | ||
| * - Git Bash uses conda.sh (initialization script) + conda activate when condaShPath is available | ||
| * - Git Bash skips activation when condaShPath is not available and sourceInitPath is .bat | ||
| * - Git Bash falls back to source <activate-script> <env> when condaShPath is not available and source is not .bat | ||
| * - PowerShell uses ps1 hook + conda activate | ||
| * - CMD uses activate.bat + conda activate | ||
| */ | ||
| suite('Conda Utils - windowsExceptionGenerateConfig', () => { | ||
| let getCondaHookPs1PathStub: sinon.SinonStub; | ||
|
|
||
| setup(() => { | ||
| // Mock getCondaHookPs1Path to avoid filesystem access | ||
| getCondaHookPs1PathStub = sinon.stub(condaSourcingUtils, 'getCondaHookPs1Path'); | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| suite('Git Bash activation with conda.sh', () => { | ||
| test('Uses source conda.sh + conda activate when condaShPath is provided', async () => { | ||
| // Arrange | ||
| getCondaHookPs1PathStub.resolves('C:\\conda\\shell\\condabin\\conda-hook.ps1'); | ||
| const sourceInitPath = 'C:\\conda\\Scripts\\activate.bat'; | ||
| const prefix = 'myenv'; | ||
| const condaFolder = 'C:\\conda'; | ||
| const condaShPath = 'C:\\conda\\etc\\profile.d\\conda.sh'; | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig(sourceInitPath, prefix, condaFolder, condaShPath); | ||
|
|
||
| // Assert | ||
| const gitBashActivation = result.shellActivation.get(ShellConstants.GITBASH); | ||
| assert.ok(gitBashActivation, 'Git Bash activation should be defined'); | ||
| assert.strictEqual(gitBashActivation.length, 2, 'Should have 2 commands: source conda.sh + conda activate'); | ||
|
|
||
| // First command: source conda.sh (no env arg - it's an initialization script) | ||
| assert.strictEqual(gitBashActivation[0].executable, 'source'); | ||
| assert.deepStrictEqual(gitBashActivation[0].args, ['C:/conda/etc/profile.d/conda.sh']); | ||
|
|
||
| // Second command: conda activate <env> | ||
| assert.strictEqual(gitBashActivation[1].executable, 'conda'); | ||
| assert.deepStrictEqual(gitBashActivation[1].args, ['activate', 'myenv']); | ||
| }); | ||
|
|
||
| test('Skips Git Bash activation when condaShPath is undefined and sourceInitPath is .bat', async () => { | ||
| // Arrange: sourceInitPath is a .bat file which Git Bash cannot source | ||
| getCondaHookPs1PathStub.resolves(undefined); | ||
| const sourceInitPath = 'C:\\conda\\Scripts\\activate.bat'; | ||
| const prefix = 'myenv'; | ||
| const condaFolder = 'C:\\conda'; | ||
| const condaShPath = undefined; // Not available | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig(sourceInitPath, prefix, condaFolder, condaShPath); | ||
|
|
||
| // Assert: Git Bash activation should be empty since .bat cannot be sourced | ||
| const gitBashActivation = result.shellActivation.get(ShellConstants.GITBASH); | ||
| assert.ok(gitBashActivation, 'Git Bash activation should be defined'); | ||
| assert.strictEqual( | ||
| gitBashActivation.length, | ||
| 0, | ||
| 'Git Bash activation should be empty when sourceInitPath is .bat', | ||
| ); | ||
| }); | ||
|
|
||
| test('Falls back to single source command when condaShPath is undefined and source is not .bat', async () => { | ||
| // Arrange: sourceInitPath is a bash-compatible script (no .bat extension) | ||
| getCondaHookPs1PathStub.resolves(undefined); | ||
| const sourceInitPath = 'C:\\conda\\Scripts\\activate'; // No .bat extension | ||
| const prefix = 'myenv'; | ||
| const condaFolder = 'C:\\conda'; | ||
| const condaShPath = undefined; // Not available | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig(sourceInitPath, prefix, condaFolder, condaShPath); | ||
|
|
||
| // Assert | ||
| const gitBashActivation = result.shellActivation.get(ShellConstants.GITBASH); | ||
| assert.ok(gitBashActivation, 'Git Bash activation should be defined'); | ||
| assert.strictEqual(gitBashActivation.length, 1, 'Should have 1 command when source is bash-compatible'); | ||
|
|
||
| // Single command: source <activate-script> <env> | ||
| assert.strictEqual(gitBashActivation[0].executable, 'source'); | ||
| assert.deepStrictEqual(gitBashActivation[0].args, ['C:/conda/Scripts/activate', 'myenv']); | ||
| }); | ||
|
|
||
| test('Converts Windows backslashes to forward slashes for bash', async () => { | ||
| // Arrange | ||
| getCondaHookPs1PathStub.resolves(undefined); | ||
| const condaShPath = 'C:\\Tools\\miniforge3\\etc\\profile.d\\conda.sh'; | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig( | ||
| 'C:\\Tools\\miniforge3\\Scripts\\activate.bat', | ||
| 'pipes', | ||
| 'C:\\Tools\\miniforge3', | ||
| condaShPath, | ||
| ); | ||
|
|
||
| // Assert | ||
| const gitBashActivation = result.shellActivation.get(ShellConstants.GITBASH); | ||
| assert.ok(gitBashActivation, 'Git Bash activation should be defined'); | ||
| // Verify forward slashes are used | ||
| const sourcePath = gitBashActivation[0].args?.[0]; | ||
| assert.ok(sourcePath, 'Source path should be defined'); | ||
| assert.ok(!sourcePath.includes('\\'), 'Path should not contain backslashes'); | ||
| assert.ok(sourcePath.includes('/'), 'Path should contain forward slashes'); | ||
| }); | ||
| }); | ||
|
|
||
| suite('PowerShell activation', () => { | ||
| test('Uses ps1 hook when available', async () => { | ||
| // Arrange | ||
| const ps1HookPath = 'C:\\conda\\shell\\condabin\\conda-hook.ps1'; | ||
| getCondaHookPs1PathStub.resolves(ps1HookPath); | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig( | ||
| 'C:\\conda\\Scripts\\activate.bat', | ||
| 'myenv', | ||
| 'C:\\conda', | ||
| undefined, | ||
| ); | ||
|
|
||
| // Assert | ||
| const pwshActivation = result.shellActivation.get(ShellConstants.PWSH); | ||
| assert.ok(pwshActivation, 'PowerShell activation should be defined'); | ||
| assert.strictEqual(pwshActivation.length, 2, 'Should have 2 commands'); | ||
| assert.strictEqual(pwshActivation[0].executable, ps1HookPath); | ||
| assert.strictEqual(pwshActivation[1].executable, 'conda'); | ||
| assert.deepStrictEqual(pwshActivation[1].args, ['activate', 'myenv']); | ||
| }); | ||
|
|
||
| test('Falls back to sourceInitPath when ps1 hook not found', async () => { | ||
| // Arrange | ||
| getCondaHookPs1PathStub.resolves(undefined); | ||
| const sourceInitPath = 'C:\\conda\\Scripts\\activate.bat'; | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig(sourceInitPath, 'myenv', 'C:\\conda', undefined); | ||
|
|
||
| // Assert | ||
| const pwshActivation = result.shellActivation.get(ShellConstants.PWSH); | ||
| assert.ok(pwshActivation, 'PowerShell activation should be defined'); | ||
| assert.strictEqual(pwshActivation[0].executable, sourceInitPath); | ||
| }); | ||
| }); | ||
|
|
||
| suite('CMD activation', () => { | ||
| test('Uses activate.bat + conda activate', async () => { | ||
| // Arrange | ||
| getCondaHookPs1PathStub.resolves(undefined); | ||
| const sourceInitPath = 'C:\\conda\\Scripts\\activate.bat'; | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig(sourceInitPath, 'myenv', 'C:\\conda', undefined); | ||
|
|
||
| // Assert | ||
| const cmdActivation = result.shellActivation.get(ShellConstants.CMD); | ||
| assert.ok(cmdActivation, 'CMD activation should be defined'); | ||
| assert.strictEqual(cmdActivation.length, 2, 'Should have 2 commands'); | ||
| assert.strictEqual(cmdActivation[0].executable, sourceInitPath); | ||
| assert.strictEqual(cmdActivation[1].executable, 'conda'); | ||
| assert.deepStrictEqual(cmdActivation[1].args, ['activate', 'myenv']); | ||
| }); | ||
| }); | ||
|
|
||
| suite('Deactivation commands', () => { | ||
| test('All shells use conda deactivate', async () => { | ||
| // Arrange | ||
| getCondaHookPs1PathStub.resolves(undefined); | ||
|
|
||
| // Act | ||
| const result = await windowsExceptionGenerateConfig( | ||
| 'C:\\conda\\Scripts\\activate.bat', | ||
| 'myenv', | ||
| 'C:\\conda', | ||
| undefined, | ||
| ); | ||
|
|
||
| // Assert: All shells should have conda deactivate | ||
| for (const shell of [ShellConstants.GITBASH, ShellConstants.CMD, ShellConstants.PWSH]) { | ||
| const deactivation = result.shellDeactivation.get(shell); | ||
| assert.ok(deactivation, `${shell} deactivation should be defined`); | ||
| assert.strictEqual(deactivation.length, 1, `${shell} should have 1 deactivation command`); | ||
| assert.strictEqual(deactivation[0].executable, 'conda'); | ||
| assert.deepStrictEqual(deactivation[0].args, ['deactivate']); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.