Skip to content
Open
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
26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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",
Expand All @@ -470,7 +470,7 @@
},
{
"command": "python-envs.runAsTask",
"when": "config.python.useEnvironmentsExtension != false"
"when": "pythonEnvs.enabled || config.python.useEnvironmentsExtension"
},
{
"command": "python-envs.clearScriptEnvCache",
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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"
}
]
},
Expand All @@ -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"
}
]
},
Expand Down
8 changes: 8 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
}
const start = new StopWatch();

// Publish the enabled state as a context key for the `when` clauses in package.json.
// The clauses cannot rely on the setting alone: `config.python.useEnvironmentsExtension`
// is unregistered (undefined) without the Python extension, and a `!= false` comparison
// is evaluated as a truthiness check, so an unset setting would hide the UI of an
// extension that did activate. The clauses OR this key with the raw setting value so
// commands stay visible before activation when the setting is explicitly `true`.
await commands.executeCommand('setContext', 'pythonEnvs.enabled', true);

// Logging should be set up before anything else.
const outputChannel: LogOutputChannel = createLogOutputChannel('Python Environments');
context.subscriptions.push(outputChannel, registerLogger(outputChannel));
Expand Down
78 changes: 78 additions & 0 deletions src/test/enabledContext.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';

type Contribution = { command?: string; id?: string; when?: string };

function collectWhens(value: unknown, whens: string[]): void {
if (Array.isArray(value)) {
value.forEach((item) => 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',
]);
});
});