Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions .github/instructions/testing-workflow.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,25 @@ interface FunctionAnalysis {
import * as sinon from 'sinon';
import * as workspaceApis from '../../common/workspace.apis'; // Wrapper functions

// Stub wrapper functions, not VS Code APIs directly
// Always mock wrapper functions (e.g., workspaceApis.getConfiguration()) instead of
// VS Code APIs directly to avoid stubbing issues
// ❌ WRONG - stubbing raw VS Code modules causes errors
import { commands } from 'vscode';
const mockExecuteCommand = sinon.stub(commands, 'executeCommand').resolves();
// Error: TypeError: Cannot stub non-existent property executeCommand

// ✅ CORRECT - stub the wrapper function from common API layer
import * as commandApi from '../../common/command.api';
const mockExecuteCommand = sinon.stub(commandApi, 'executeCommand').resolves();

// CRITICAL: Always check the implementation's imports first
// If the code uses wrapper functions like these, stub the wrapper:
// - commandApi.executeCommand() → stub commandApi.executeCommand
// - workspaceApis.getConfiguration() → stub workspaceApis.getConfiguration
// - windowApis.showInformationMessage() → stub windowApis.showInformationMessage
// - workspaceApis.getWorkspaceFolder() → stub workspaceApis.getWorkspaceFolder
//
// Why? Raw VS Code modules aren't exported in the test environment, causing
// "Cannot stub non-existent property" errors when trying to stub them directly.

const mockGetConfiguration = sinon.stub(workspaceApis, 'getConfiguration');
```

Expand Down Expand Up @@ -574,6 +590,7 @@ envConfig.inspect
- Untestable Node.js APIs → Create proxy abstraction functions (use function overloads to preserve intelligent typing while making functions mockable)

## 🧠 Agent Learnings

- Avoid testing exact error messages or log output - assert only that errors are thrown or rejection occurs to prevent brittle tests (1)
- Create shared mock helpers (e.g., `createMockLogOutputChannel()`) instead of duplicating mock setup across multiple test files (1)

- When stubbing VS Code API functions in unit tests, always check the implementation imports first - if the code uses wrapper functions (like `commandApi.executeCommand()`, `workspaceApis.getConfiguration()`, `windowApis.showInformationMessage()`), stub the wrapper function instead of the raw VS Code module. Stubbing raw VS Code modules (e.g., `sinon.stub(commands, 'executeCommand')`) causes "Cannot stub non-existent property" errors because those modules aren't exported in the test environment (2)
2 changes: 2 additions & 0 deletions src/common/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export namespace Common {
export const ok = l10n.t('Ok');
export const quickCreate = l10n.t('Quick Create');
export const installPython = l10n.t('Install Python');
export const dontShowAgain = l10n.t("Don't Show Again");
export const openSettings = l10n.t('Open Settings');
}

export namespace WorkbenchStrings {
Expand Down
42 changes: 38 additions & 4 deletions src/features/terminal/terminalEnvVarInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import {
Disposable,
EnvironmentVariableScope,
GlobalEnvironmentVariableCollection,
window,
workspace,
WorkspaceFolder,
} from 'vscode';
import { executeCommand } from '../../common/command.api';
import { Common } from '../../common/localize';
import { traceError, traceVerbose } from '../../common/logging';
import { getGlobalPersistentState } from '../../common/persistentState';
import { resolveVariables } from '../../common/utils/internalVariables';
import * as windowApis from '../../common/window.apis';
import { getConfiguration, getWorkspaceFolder } from '../../common/workspace.apis';
import { EnvVarManager } from '../execution/envVariableManager';

Expand All @@ -22,6 +25,7 @@ import { EnvVarManager } from '../execution/envVariableManager';
*/
export class TerminalEnvVarInjector implements Disposable {
private disposables: Disposable[] = [];
private static readonly DONT_SHOW_ENV_FILE_NOTIFICATION_KEY = 'dontShowEnvFileNotification';

constructor(
private readonly envVarCollection: GlobalEnvironmentVariableCollection,
Expand Down Expand Up @@ -63,9 +67,9 @@ export class TerminalEnvVarInjector implements Disposable {

// Only show notification when env vars change and we have an env file but injection is disabled
if (!useEnvFile && envFilePath) {
window.showInformationMessage(
'An environment file is configured but terminal environment injection is disabled. Enable "python.terminal.useEnvFile" to use environment variables from .env files in terminals.',
);
this.showEnvFileNotification().catch((error) => {
traceError('Failed to show environment file notification:', error);
});
}

if (args.changeType === 2) {
Expand Down Expand Up @@ -184,6 +188,36 @@ export class TerminalEnvVarInjector implements Disposable {
}
}

/**
* Show notification about environment file configuration with "Don't Show Again" option.
*/
private async showEnvFileNotification(): Promise<void> {
const state = await getGlobalPersistentState();
const dontShowAgain = await state.get<boolean>(
TerminalEnvVarInjector.DONT_SHOW_ENV_FILE_NOTIFICATION_KEY,
false,
);

if (dontShowAgain) {
traceVerbose('TerminalEnvVarInjector: Env file notification suppressed by user preference');
return;
}

const result = await windowApis.showInformationMessage(
'An environment file is configured but terminal environment injection is disabled. Enable "python.terminal.useEnvFile" to use environment variables from .env files in terminals.',
Common.openSettings,
Common.dontShowAgain,
);

if (result === Common.openSettings) {
await executeCommand('workbench.action.openSettings', 'python.terminal.useEnvFile');
traceVerbose('TerminalEnvVarInjector: User opened settings for useEnvFile');
} else if (result === Common.dontShowAgain) {
await state.set(TerminalEnvVarInjector.DONT_SHOW_ENV_FILE_NOTIFICATION_KEY, true);
traceVerbose('TerminalEnvVarInjector: User chose to not show env file notification again');
}
}

/**
* Dispose of the injector and clean up resources.
*/
Expand Down
Loading
Loading