forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add create environment button to
requirements.txt
and `pyproject.to…
…ml` files
- Loading branch information
1 parent
b897300
commit f1e1044
Showing
7 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/client/pythonEnvironments/creation/pyprojectTomlCreateEnv.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { TextDocument, TextDocumentChangeEvent } from 'vscode'; | ||
import { IDisposableRegistry } from '../../common/types'; | ||
import { executeCommand } from '../../common/vscodeApis/commandApis'; | ||
import { | ||
onDidOpenTextDocument, | ||
onDidChangeTextDocument, | ||
getOpenTextDocuments, | ||
} from '../../common/vscodeApis/workspaceApis'; | ||
import { isPipInstallableToml } from './provider/venvUtils'; | ||
|
||
async function setPyProjectTomlContextKey(doc: TextDocument): Promise<void> { | ||
if (isPipInstallableToml(doc.getText())) { | ||
await executeCommand('setContext', 'pipInstallableToml', true); | ||
} else { | ||
await executeCommand('setContext', 'pipInstallableToml', false); | ||
} | ||
} | ||
|
||
export function registerPyProjectTomlCreateEnvFeatures(disposables: IDisposableRegistry): void { | ||
disposables.push( | ||
onDidOpenTextDocument(async (doc: TextDocument) => { | ||
if (doc.fileName.endsWith('pyproject.toml')) { | ||
await setPyProjectTomlContextKey(doc); | ||
} | ||
}), | ||
onDidChangeTextDocument(async (e: TextDocumentChangeEvent) => { | ||
if (e.document.fileName.endsWith('pyproject.toml')) { | ||
await setPyProjectTomlContextKey(e.document); | ||
} | ||
}), | ||
); | ||
|
||
getOpenTextDocuments().forEach(async (doc: TextDocument) => { | ||
if (doc.fileName.endsWith('pyproject.toml')) { | ||
await setPyProjectTomlContextKey(doc); | ||
} | ||
}); | ||
} |
216 changes: 216 additions & 0 deletions
216
src/test/pythonEnvironments/creation/pyprojectTomlCreateEnv.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import * as chaiAsPromised from 'chai-as-promised'; | ||
import * as sinon from 'sinon'; | ||
import * as typemoq from 'typemoq'; | ||
import { assert, use as chaiUse } from 'chai'; | ||
import { TextDocument, TextDocumentChangeEvent } from 'vscode'; | ||
import * as cmdApis from '../../../client/common/vscodeApis/commandApis'; | ||
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; | ||
import { IDisposableRegistry } from '../../../client/common/types'; | ||
import { registerPyProjectTomlCreateEnvFeatures } from '../../../client/pythonEnvironments/creation/pyprojectTomlCreateEnv'; | ||
|
||
chaiUse(chaiAsPromised); | ||
|
||
class FakeDisposable { | ||
public dispose() { | ||
// Do nothing | ||
} | ||
} | ||
|
||
function getInstallableToml(): typemoq.IMock<TextDocument> { | ||
const pyprojectTomlPath = 'pyproject.toml'; | ||
const pyprojectToml = typemoq.Mock.ofType<TextDocument>(); | ||
pyprojectToml.setup((p) => p.fileName).returns(() => pyprojectTomlPath); | ||
pyprojectToml | ||
.setup((p) => p.getText(typemoq.It.isAny())) | ||
.returns( | ||
() => | ||
'[project]\nname = "spam"\nversion = "2020.0.0"\n[build-system]\nrequires = ["setuptools ~= 58.0", "cython ~= 0.29.0"]\n[project.optional-dependencies]\ntest = ["pytest"]\ndoc = ["sphinx", "furo"]', | ||
); | ||
return pyprojectToml; | ||
} | ||
|
||
function getNonInstallableToml(): typemoq.IMock<TextDocument> { | ||
const pyprojectTomlPath = 'pyproject.toml'; | ||
const pyprojectToml = typemoq.Mock.ofType<TextDocument>(); | ||
pyprojectToml.setup((p) => p.fileName).returns(() => pyprojectTomlPath); | ||
pyprojectToml | ||
.setup((p) => p.getText(typemoq.It.isAny())) | ||
.returns(() => '[project]\nname = "spam"\nversion = "2020.0.0"\n'); | ||
return pyprojectToml; | ||
} | ||
|
||
function getSomeFile(): typemoq.IMock<TextDocument> { | ||
const someFilePath = 'something.py'; | ||
const someFile = typemoq.Mock.ofType<TextDocument>(); | ||
someFile.setup((p) => p.fileName).returns(() => someFilePath); | ||
someFile.setup((p) => p.getText(typemoq.It.isAny())).returns(() => 'print("Hello World")'); | ||
return someFile; | ||
} | ||
|
||
suite('PyProject.toml Create Env Features', () => { | ||
let executeCommandStub: sinon.SinonStub; | ||
const disposables: IDisposableRegistry = []; | ||
let getOpenTextDocumentsStub: sinon.SinonStub; | ||
let onDidOpenTextDocumentStub: sinon.SinonStub; | ||
let onDidChangeTextDocumentStub: sinon.SinonStub; | ||
|
||
setup(() => { | ||
executeCommandStub = sinon.stub(cmdApis, 'executeCommand'); | ||
getOpenTextDocumentsStub = sinon.stub(workspaceApis, 'getOpenTextDocuments'); | ||
onDidOpenTextDocumentStub = sinon.stub(workspaceApis, 'onDidOpenTextDocument'); | ||
onDidChangeTextDocumentStub = sinon.stub(workspaceApis, 'onDidChangeTextDocument'); | ||
|
||
onDidOpenTextDocumentStub.returns(new FakeDisposable()); | ||
onDidChangeTextDocumentStub.returns(new FakeDisposable()); | ||
}); | ||
|
||
teardown(() => { | ||
sinon.restore(); | ||
disposables.forEach((d) => d.dispose()); | ||
}); | ||
|
||
test('Installable pyproject.toml is already open in the editor on extension activate', async () => { | ||
const pyprojectToml = getInstallableToml(); | ||
getOpenTextDocumentsStub.returns([pyprojectToml.object]); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
|
||
assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); | ||
}); | ||
|
||
test('Non installable pyproject.toml is already open in the editor on extension activate', async () => { | ||
const pyprojectToml = getNonInstallableToml(); | ||
getOpenTextDocumentsStub.returns([pyprojectToml.object]); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
|
||
assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); | ||
}); | ||
|
||
test('Some random file open in the editor on extension activate', async () => { | ||
const someFile = getSomeFile(); | ||
getOpenTextDocumentsStub.returns([someFile.object]); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
|
||
assert.ok(executeCommandStub.notCalled); | ||
}); | ||
|
||
test('Installable pyproject.toml is opened in the editor', async () => { | ||
getOpenTextDocumentsStub.returns([]); | ||
|
||
let handler: (doc: TextDocument) => void = () => { | ||
/* do nothing */ | ||
}; | ||
onDidOpenTextDocumentStub.callsFake((callback) => { | ||
handler = callback; | ||
return new FakeDisposable(); | ||
}); | ||
|
||
const pyprojectToml = getInstallableToml(); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
handler(pyprojectToml.object); | ||
|
||
assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); | ||
}); | ||
|
||
test('Non Installable pyproject.toml is opened in the editor', async () => { | ||
getOpenTextDocumentsStub.returns([]); | ||
|
||
let handler: (doc: TextDocument) => void = () => { | ||
/* do nothing */ | ||
}; | ||
onDidOpenTextDocumentStub.callsFake((callback) => { | ||
handler = callback; | ||
return new FakeDisposable(); | ||
}); | ||
|
||
const pyprojectToml = getNonInstallableToml(); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
handler(pyprojectToml.object); | ||
|
||
assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); | ||
}); | ||
|
||
test('Some random file is opened in the editor', async () => { | ||
getOpenTextDocumentsStub.returns([]); | ||
|
||
let handler: (doc: TextDocument) => void = () => { | ||
/* do nothing */ | ||
}; | ||
onDidOpenTextDocumentStub.callsFake((callback) => { | ||
handler = callback; | ||
return new FakeDisposable(); | ||
}); | ||
|
||
const someFile = getSomeFile(); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
handler(someFile.object); | ||
|
||
assert.ok(executeCommandStub.notCalled); | ||
}); | ||
|
||
test('Installable pyproject.toml is changed', async () => { | ||
getOpenTextDocumentsStub.returns([]); | ||
|
||
let handler: (d: TextDocumentChangeEvent) => void = () => { | ||
/* do nothing */ | ||
}; | ||
onDidChangeTextDocumentStub.callsFake((callback) => { | ||
handler = callback; | ||
return new FakeDisposable(); | ||
}); | ||
|
||
const pyprojectToml = getInstallableToml(); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined }); | ||
|
||
assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); | ||
}); | ||
|
||
test('Non Installable pyproject.toml is changed', async () => { | ||
getOpenTextDocumentsStub.returns([]); | ||
|
||
let handler: (d: TextDocumentChangeEvent) => void = () => { | ||
/* do nothing */ | ||
}; | ||
onDidChangeTextDocumentStub.callsFake((callback) => { | ||
handler = callback; | ||
return new FakeDisposable(); | ||
}); | ||
|
||
const pyprojectToml = getNonInstallableToml(); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined }); | ||
|
||
assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); | ||
}); | ||
|
||
test('Some random file is changed', async () => { | ||
getOpenTextDocumentsStub.returns([]); | ||
|
||
let handler: (d: TextDocumentChangeEvent) => void = () => { | ||
/* do nothing */ | ||
}; | ||
onDidChangeTextDocumentStub.callsFake((callback) => { | ||
handler = callback; | ||
return new FakeDisposable(); | ||
}); | ||
|
||
const someFile = getSomeFile(); | ||
|
||
registerPyProjectTomlCreateEnvFeatures(disposables); | ||
handler({ contentChanges: [], document: someFile.object, reason: undefined }); | ||
|
||
assert.ok(executeCommandStub.notCalled); | ||
}); | ||
}); |