From a5005f622e664150d0af63bd9cbf89eef0e7a243 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Wed, 1 Mar 2023 04:41:37 -0800 Subject: [PATCH] Ensure picker returns array only if multi-select is enabled (#20772) Fixes https://github.com/microsoft/vscode-python/issues/20768 --- src/client/common/vscodeApis/windowApis.ts | 7 ++- .../creation/provider/condaUtils.unit.test.ts | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/pythonEnvironments/creation/provider/condaUtils.unit.test.ts diff --git a/src/client/common/vscodeApis/windowApis.ts b/src/client/common/vscodeApis/windowApis.ts index c33c14ca9875..5c279b890a9f 100644 --- a/src/client/common/vscodeApis/windowApis.ts +++ b/src/client/common/vscodeApis/windowApis.ts @@ -112,7 +112,12 @@ export async function showQuickPickWithBack( }), quickPick.onDidAccept(() => { if (!deferred.completed) { - deferred.resolve(quickPick.selectedItems.map((item) => item)); + if (quickPick.canSelectMany) { + deferred.resolve(quickPick.selectedItems.map((item) => item)); + } else { + deferred.resolve(quickPick.selectedItems[0]); + } + quickPick.hide(); } }), diff --git a/src/test/pythonEnvironments/creation/provider/condaUtils.unit.test.ts b/src/test/pythonEnvironments/creation/provider/condaUtils.unit.test.ts new file mode 100644 index 000000000000..3f115f9f58ed --- /dev/null +++ b/src/test/pythonEnvironments/creation/provider/condaUtils.unit.test.ts @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { assert } from 'chai'; +import * as sinon from 'sinon'; +import { CancellationTokenSource } from 'vscode'; +import * as windowApis from '../../../../client/common/vscodeApis/windowApis'; +import { pickPythonVersion } from '../../../../client/pythonEnvironments/creation/provider/condaUtils'; + +suite('Conda Utils test', () => { + let showQuickPickWithBackStub: sinon.SinonStub; + + setup(() => { + showQuickPickWithBackStub = sinon.stub(windowApis, 'showQuickPickWithBack'); + }); + + teardown(() => { + sinon.restore(); + }); + + test('No version selected or user pressed escape', async () => { + showQuickPickWithBackStub.resolves(undefined); + + const actual = await pickPythonVersion(); + assert.isUndefined(actual); + }); + + test('User selected a version', async () => { + showQuickPickWithBackStub.resolves({ label: 'Python', description: '3.10' }); + + const actual = await pickPythonVersion(); + assert.equal(actual, '3.10'); + }); + + test('With cancellation', async () => { + const source = new CancellationTokenSource(); + + showQuickPickWithBackStub.callsFake(() => { + source.cancel(); + }); + + const actual = await pickPythonVersion(source.token); + assert.isUndefined(actual); + }); +});