From ffbde444d2552c010fe11e5bb6697ecfd079a2e7 Mon Sep 17 00:00:00 2001 From: TEJA P C Date: Thu, 26 Nov 2026 01:13:18 +0000 Subject: [PATCH] fix: show views and commands when useEnvironmentsExtension is unset --- package.json | 26 +++++----- src/extension.ts | 8 +++ src/test/enabledContext.unit.test.ts | 78 ++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/test/enabledContext.unit.test.ts diff --git a/package.json b/package.json index cb9e3f9e2..baa202925 100644 --- a/package.json +++ b/package.json @@ -198,7 +198,7 @@ "title": "%python-envs.setEnvManager.title%", "category": "Python", "icon": "$(gear)", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.setPkgManager", @@ -403,7 +403,7 @@ "title": "%python-envs.runPetInTerminal.title%", "category": "Python", "icon": "$(terminal)", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" } ], "menus": { @@ -442,19 +442,19 @@ }, { "command": "python-envs.setEnvManager", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.packages", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.set", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.setPkgManager", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.removePythonProject", @@ -470,7 +470,7 @@ }, { "command": "python-envs.runAsTask", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.clearScriptEnvCache", @@ -522,15 +522,15 @@ }, { "command": "python-envs.createNewProjectFromTemplate", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.terminal.revertStartupScriptChanges", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" }, { "command": "python-envs.reportIssue", - "when": "config.python.useEnvironmentsExtension != false" + "when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension" } ], "view/item/context": [ @@ -734,7 +734,7 @@ "id": "python", "title": "Python", "icon": "files/logo.svg", - "when": "config.python.useEnvironmentsExtension != false && python-envs.workspaceHasPython" + "when": "(pythonEnvs.enabled || config.python.useEnvironmentsExtension) && python-envs.workspaceHasPython" } ] }, @@ -745,14 +745,14 @@ "name": "Python Projects", "icon": "files/logo.svg", "contextualTitle": "Python Projects", - "when": "config.python.useEnvironmentsExtension != false && python-envs.workspaceHasPython" + "when": "(pythonEnvs.enabled || config.python.useEnvironmentsExtension) && python-envs.workspaceHasPython" }, { "id": "env-managers", "name": "Environment Managers", "icon": "files/logo.svg", "contextualTitle": "Environment Managers", - "when": "config.python.useEnvironmentsExtension != false && python-envs.workspaceHasPython" + "when": "(pythonEnvs.enabled || config.python.useEnvironmentsExtension) && python-envs.workspaceHasPython" } ] }, diff --git a/src/extension.ts b/src/extension.ts index e088619c2..f555bb78b 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -141,6 +141,14 @@ export async function activate(context: ExtensionContext): Promise collectWhens(item, whens)); + } else if (value !== null && typeof value === 'object') { + for (const [key, item] of Object.entries(value)) { + if (key === 'when' && typeof item === 'string') { + whens.push(item); + } + collectWhens(item, whens); + } + } +} + +suite('Enabled context key contributions', () => { + const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'package.json'), 'utf8')); + const contributes = packageJson.contributes; + + test('no when clause uses the broken != false comparison on the setting', () => { + const whens: string[] = []; + collectWhens(contributes, whens); + assert.ok(whens.length > 0); + assert.deepStrictEqual( + whens.filter((when) => when.includes('useEnvironmentsExtension != false')), + [], + ); + }); + + test('views and view container follow the enabled context key', () => { + const container: Contribution[] = contributes.viewsContainers.activitybar; + assert.strictEqual(container.length, 1); + assert.ok(container[0].when?.includes('pythonEnvs.enabled')); + + const views: Contribution[] = contributes.views.python; + assert.deepStrictEqual( + views.map((view) => view.id), + ['python-projects', 'env-managers'], + ); + for (const view of views) { + assert.ok(view.when?.includes('pythonEnvs.enabled'), `${view.id} should use pythonEnvs.enabled`); + } + }); + + test('command contributions follow the enabled context key', () => { + const commands: Contribution[] = contributes.commands; + const gated = commands.filter((entry) => entry.when !== undefined); + assert.deepStrictEqual(gated.map((entry) => entry.command).sort(), [ + 'python-envs.runPetInTerminal', + 'python-envs.setEnvManager', + ]); + for (const entry of gated) { + assert.ok(entry.when?.includes('pythonEnvs.enabled'), `${entry.command} should use pythonEnvs.enabled`); + } + }); + + test('command palette entries follow the enabled context key', () => { + const palette: Contribution[] = contributes.menus.commandPalette; + const gated = palette.filter((entry) => entry.when?.includes('pythonEnvs.enabled')); + assert.deepStrictEqual(gated.map((entry) => entry.command).sort(), [ + 'python-envs.createNewProjectFromTemplate', + 'python-envs.packages', + 'python-envs.reportIssue', + 'python-envs.runAsTask', + 'python-envs.set', + 'python-envs.setEnvManager', + 'python-envs.setPkgManager', + 'python-envs.terminal.revertStartupScriptChanges', + ]); + }); +});