Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Sep 13, 2023
1 parent cd1b06e commit 51575f4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src/client/pythonEnvironments/creation/provider/venvUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import { isWindows } from '../../../common/platform/platformService';
import { getVenvPath, hasVenv } from '../common/commonUtils';
import { deleteEnvironmentNonWindows, deleteEnvironmentWindows } from './venvDeleteUtils';

export const OPEN_REQUIREMENTS_BUTTON = {
iconPath: new ThemeIcon('go-to-file'),
tooltip: CreateEnv.Venv.openRequirementsFile,
};
const exclude = '**/{.venv*,.git,.nox,.tox,.conda,site-packages,__pypackages__}/**';
async function getPipRequirementsFiles(
workspaceFolder: WorkspaceFolder,
Expand Down Expand Up @@ -108,12 +112,7 @@ async function pickRequirementsFiles(
})
.map((e) => ({
label: e,
buttons: [
{
iconPath: new ThemeIcon('go-to-file'),
tooltip: CreateEnv.Venv.openRequirementsFile,
},
],
buttons: [OPEN_REQUIREMENTS_BUTTON],
}));

const selection = await showQuickPickWithBack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import * as windowApis from '../../../../client/common/vscodeApis/windowApis';
import * as workspaceApis from '../../../../client/common/vscodeApis/workspaceApis';
import {
ExistingVenvAction,
OPEN_REQUIREMENTS_BUTTON,
pickExistingVenvAction,
pickPackagesToInstall,
} from '../../../../client/pythonEnvironments/creation/provider/venvUtils';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants';
import { CreateEnv } from '../../../../client/common/utils/localize';
import { createDeferred } from '../../../../client/common/utils/async';

chaiUse(chaiAsPromised);

Expand All @@ -23,6 +25,7 @@ suite('Venv Utils test', () => {
let showQuickPickWithBackStub: sinon.SinonStub;
let pathExistsStub: sinon.SinonStub;
let readFileStub: sinon.SinonStub;
let showTextDocumentStub: sinon.SinonStub;

const workspace1 = {
uri: Uri.file(path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'workspace1')),
Expand All @@ -35,6 +38,7 @@ suite('Venv Utils test', () => {
showQuickPickWithBackStub = sinon.stub(windowApis, 'showQuickPickWithBack');
pathExistsStub = sinon.stub(fs, 'pathExists');
readFileStub = sinon.stub(fs, 'readFile');
showTextDocumentStub = sinon.stub(windowApis, 'showTextDocument');
});

teardown(() => {
Expand Down Expand Up @@ -224,13 +228,18 @@ suite('Venv Utils test', () => {
await assert.isRejected(pickPackagesToInstall(workspace1));
assert.isTrue(
showQuickPickWithBackStub.calledWithExactly(
[{ label: 'requirements.txt' }, { label: 'dev-requirements.txt' }, { label: 'test-requirements.txt' }],
[
{ label: 'requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'dev-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'test-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
],
{
placeHolder: CreateEnv.Venv.requirementsQuickPickTitle,
ignoreFocusOut: true,
canPickMany: true,
},
undefined,
sinon.match.func,
),
);
assert.isTrue(readFileStub.calledOnce);
Expand All @@ -257,13 +266,18 @@ suite('Venv Utils test', () => {
const actual = await pickPackagesToInstall(workspace1);
assert.isTrue(
showQuickPickWithBackStub.calledWithExactly(
[{ label: 'requirements.txt' }, { label: 'dev-requirements.txt' }, { label: 'test-requirements.txt' }],
[
{ label: 'requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'dev-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'test-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
],
{
placeHolder: CreateEnv.Venv.requirementsQuickPickTitle,
ignoreFocusOut: true,
canPickMany: true,
},
undefined,
sinon.match.func,
),
);
assert.deepStrictEqual(actual, []);
Expand All @@ -290,13 +304,18 @@ suite('Venv Utils test', () => {
const actual = await pickPackagesToInstall(workspace1);
assert.isTrue(
showQuickPickWithBackStub.calledWithExactly(
[{ label: 'requirements.txt' }, { label: 'dev-requirements.txt' }, { label: 'test-requirements.txt' }],
[
{ label: 'requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'dev-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'test-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
],
{
placeHolder: CreateEnv.Venv.requirementsQuickPickTitle,
ignoreFocusOut: true,
canPickMany: true,
},
undefined,
sinon.match.func,
),
);
assert.deepStrictEqual(actual, [
Expand Down Expand Up @@ -328,13 +347,18 @@ suite('Venv Utils test', () => {
const actual = await pickPackagesToInstall(workspace1);
assert.isTrue(
showQuickPickWithBackStub.calledWithExactly(
[{ label: 'requirements.txt' }, { label: 'dev-requirements.txt' }, { label: 'test-requirements.txt' }],
[
{ label: 'requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'dev-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
{ label: 'test-requirements.txt', buttons: [OPEN_REQUIREMENTS_BUTTON] },
],
{
placeHolder: CreateEnv.Venv.requirementsQuickPickTitle,
ignoreFocusOut: true,
canPickMany: true,
},
undefined,
sinon.match.func,
),
);
assert.deepStrictEqual(actual, [
Expand All @@ -349,6 +373,45 @@ suite('Venv Utils test', () => {
]);
assert.isTrue(readFileStub.notCalled);
});

test('User clicks button to open requirements.txt', async () => {
let allow = true;
findFilesStub.callsFake(() => {
if (allow) {
allow = false;
return Promise.resolve([
Uri.file(path.join(workspace1.uri.fsPath, 'requirements.txt')),
Uri.file(path.join(workspace1.uri.fsPath, 'dev-requirements.txt')),
Uri.file(path.join(workspace1.uri.fsPath, 'test-requirements.txt')),
]);
}
return Promise.resolve([]);
});
pathExistsStub.resolves(false);

const deferred = createDeferred();
showQuickPickWithBackStub.callsFake(async (_items, _options, _token, callback) => {
callback({
button: OPEN_REQUIREMENTS_BUTTON,
item: { label: 'requirements.txt' },
});
await deferred.promise;
return [{ label: 'requirements.txt' }];
});

let uri: Uri | undefined;
showTextDocumentStub.callsFake((arg: Uri) => {
uri = arg;
deferred.resolve();
return Promise.resolve();
});

await pickPackagesToInstall(workspace1);
assert.deepStrictEqual(
uri?.toString(),
Uri.file(path.join(workspace1.uri.fsPath, 'requirements.txt')).toString(),
);
});
});

suite('Test pick existing venv action', () => {
Expand Down

0 comments on commit 51575f4

Please sign in to comment.