|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 4 | + |
| 5 | +import * as chaiAsPromised from 'chai-as-promised'; |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import * as typemoq from 'typemoq'; |
| 8 | +import { assert, use as chaiUse } from 'chai'; |
| 9 | +import { TextDocument, TextDocumentChangeEvent } from 'vscode'; |
| 10 | +import * as cmdApis from '../../../client/common/vscodeApis/commandApis'; |
| 11 | +import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; |
| 12 | +import { IDisposableRegistry } from '../../../client/common/types'; |
| 13 | +import { registerPyProjectTomlCreateEnvFeatures } from '../../../client/pythonEnvironments/creation/pyprojectTomlCreateEnv'; |
| 14 | + |
| 15 | +chaiUse(chaiAsPromised); |
| 16 | + |
| 17 | +class FakeDisposable { |
| 18 | + public dispose() { |
| 19 | + // Do nothing |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function getInstallableToml(): typemoq.IMock<TextDocument> { |
| 24 | + const pyprojectTomlPath = 'pyproject.toml'; |
| 25 | + const pyprojectToml = typemoq.Mock.ofType<TextDocument>(); |
| 26 | + pyprojectToml.setup((p) => p.fileName).returns(() => pyprojectTomlPath); |
| 27 | + pyprojectToml |
| 28 | + .setup((p) => p.getText(typemoq.It.isAny())) |
| 29 | + .returns( |
| 30 | + () => |
| 31 | + '[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"]', |
| 32 | + ); |
| 33 | + return pyprojectToml; |
| 34 | +} |
| 35 | + |
| 36 | +function getNonInstallableToml(): typemoq.IMock<TextDocument> { |
| 37 | + const pyprojectTomlPath = 'pyproject.toml'; |
| 38 | + const pyprojectToml = typemoq.Mock.ofType<TextDocument>(); |
| 39 | + pyprojectToml.setup((p) => p.fileName).returns(() => pyprojectTomlPath); |
| 40 | + pyprojectToml |
| 41 | + .setup((p) => p.getText(typemoq.It.isAny())) |
| 42 | + .returns(() => '[project]\nname = "spam"\nversion = "2020.0.0"\n'); |
| 43 | + return pyprojectToml; |
| 44 | +} |
| 45 | + |
| 46 | +function getSomeFile(): typemoq.IMock<TextDocument> { |
| 47 | + const someFilePath = 'something.py'; |
| 48 | + const someFile = typemoq.Mock.ofType<TextDocument>(); |
| 49 | + someFile.setup((p) => p.fileName).returns(() => someFilePath); |
| 50 | + someFile.setup((p) => p.getText(typemoq.It.isAny())).returns(() => 'print("Hello World")'); |
| 51 | + return someFile; |
| 52 | +} |
| 53 | + |
| 54 | +suite('PyProject.toml Create Env Features', () => { |
| 55 | + let executeCommandStub: sinon.SinonStub; |
| 56 | + const disposables: IDisposableRegistry = []; |
| 57 | + let getOpenTextDocumentsStub: sinon.SinonStub; |
| 58 | + let onDidOpenTextDocumentStub: sinon.SinonStub; |
| 59 | + let onDidChangeTextDocumentStub: sinon.SinonStub; |
| 60 | + |
| 61 | + setup(() => { |
| 62 | + executeCommandStub = sinon.stub(cmdApis, 'executeCommand'); |
| 63 | + getOpenTextDocumentsStub = sinon.stub(workspaceApis, 'getOpenTextDocuments'); |
| 64 | + onDidOpenTextDocumentStub = sinon.stub(workspaceApis, 'onDidOpenTextDocument'); |
| 65 | + onDidChangeTextDocumentStub = sinon.stub(workspaceApis, 'onDidChangeTextDocument'); |
| 66 | + |
| 67 | + onDidOpenTextDocumentStub.returns(new FakeDisposable()); |
| 68 | + onDidChangeTextDocumentStub.returns(new FakeDisposable()); |
| 69 | + }); |
| 70 | + |
| 71 | + teardown(() => { |
| 72 | + sinon.restore(); |
| 73 | + disposables.forEach((d) => d.dispose()); |
| 74 | + }); |
| 75 | + |
| 76 | + test('Installable pyproject.toml is already open in the editor on extension activate', async () => { |
| 77 | + const pyprojectToml = getInstallableToml(); |
| 78 | + getOpenTextDocumentsStub.returns([pyprojectToml.object]); |
| 79 | + |
| 80 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 81 | + |
| 82 | + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); |
| 83 | + }); |
| 84 | + |
| 85 | + test('Non installable pyproject.toml is already open in the editor on extension activate', async () => { |
| 86 | + const pyprojectToml = getNonInstallableToml(); |
| 87 | + getOpenTextDocumentsStub.returns([pyprojectToml.object]); |
| 88 | + |
| 89 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 90 | + |
| 91 | + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); |
| 92 | + }); |
| 93 | + |
| 94 | + test('Some random file open in the editor on extension activate', async () => { |
| 95 | + const someFile = getSomeFile(); |
| 96 | + getOpenTextDocumentsStub.returns([someFile.object]); |
| 97 | + |
| 98 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 99 | + |
| 100 | + assert.ok(executeCommandStub.notCalled); |
| 101 | + }); |
| 102 | + |
| 103 | + test('Installable pyproject.toml is opened in the editor', async () => { |
| 104 | + getOpenTextDocumentsStub.returns([]); |
| 105 | + |
| 106 | + let handler: (doc: TextDocument) => void = () => { |
| 107 | + /* do nothing */ |
| 108 | + }; |
| 109 | + onDidOpenTextDocumentStub.callsFake((callback) => { |
| 110 | + handler = callback; |
| 111 | + return new FakeDisposable(); |
| 112 | + }); |
| 113 | + |
| 114 | + const pyprojectToml = getInstallableToml(); |
| 115 | + |
| 116 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 117 | + handler(pyprojectToml.object); |
| 118 | + |
| 119 | + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); |
| 120 | + }); |
| 121 | + |
| 122 | + test('Non Installable pyproject.toml is opened in the editor', async () => { |
| 123 | + getOpenTextDocumentsStub.returns([]); |
| 124 | + |
| 125 | + let handler: (doc: TextDocument) => void = () => { |
| 126 | + /* do nothing */ |
| 127 | + }; |
| 128 | + onDidOpenTextDocumentStub.callsFake((callback) => { |
| 129 | + handler = callback; |
| 130 | + return new FakeDisposable(); |
| 131 | + }); |
| 132 | + |
| 133 | + const pyprojectToml = getNonInstallableToml(); |
| 134 | + |
| 135 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 136 | + handler(pyprojectToml.object); |
| 137 | + |
| 138 | + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); |
| 139 | + }); |
| 140 | + |
| 141 | + test('Some random file is opened in the editor', async () => { |
| 142 | + getOpenTextDocumentsStub.returns([]); |
| 143 | + |
| 144 | + let handler: (doc: TextDocument) => void = () => { |
| 145 | + /* do nothing */ |
| 146 | + }; |
| 147 | + onDidOpenTextDocumentStub.callsFake((callback) => { |
| 148 | + handler = callback; |
| 149 | + return new FakeDisposable(); |
| 150 | + }); |
| 151 | + |
| 152 | + const someFile = getSomeFile(); |
| 153 | + |
| 154 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 155 | + handler(someFile.object); |
| 156 | + |
| 157 | + assert.ok(executeCommandStub.notCalled); |
| 158 | + }); |
| 159 | + |
| 160 | + test('Installable pyproject.toml is changed', async () => { |
| 161 | + getOpenTextDocumentsStub.returns([]); |
| 162 | + |
| 163 | + let handler: (d: TextDocumentChangeEvent) => void = () => { |
| 164 | + /* do nothing */ |
| 165 | + }; |
| 166 | + onDidChangeTextDocumentStub.callsFake((callback) => { |
| 167 | + handler = callback; |
| 168 | + return new FakeDisposable(); |
| 169 | + }); |
| 170 | + |
| 171 | + const pyprojectToml = getInstallableToml(); |
| 172 | + |
| 173 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 174 | + handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined }); |
| 175 | + |
| 176 | + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); |
| 177 | + }); |
| 178 | + |
| 179 | + test('Non Installable pyproject.toml is changed', async () => { |
| 180 | + getOpenTextDocumentsStub.returns([]); |
| 181 | + |
| 182 | + let handler: (d: TextDocumentChangeEvent) => void = () => { |
| 183 | + /* do nothing */ |
| 184 | + }; |
| 185 | + onDidChangeTextDocumentStub.callsFake((callback) => { |
| 186 | + handler = callback; |
| 187 | + return new FakeDisposable(); |
| 188 | + }); |
| 189 | + |
| 190 | + const pyprojectToml = getNonInstallableToml(); |
| 191 | + |
| 192 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 193 | + handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined }); |
| 194 | + |
| 195 | + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); |
| 196 | + }); |
| 197 | + |
| 198 | + test('Some random file is changed', async () => { |
| 199 | + getOpenTextDocumentsStub.returns([]); |
| 200 | + |
| 201 | + let handler: (d: TextDocumentChangeEvent) => void = () => { |
| 202 | + /* do nothing */ |
| 203 | + }; |
| 204 | + onDidChangeTextDocumentStub.callsFake((callback) => { |
| 205 | + handler = callback; |
| 206 | + return new FakeDisposable(); |
| 207 | + }); |
| 208 | + |
| 209 | + const someFile = getSomeFile(); |
| 210 | + |
| 211 | + registerPyProjectTomlCreateEnvFeatures(disposables); |
| 212 | + handler({ contentChanges: [], document: someFile.object, reason: undefined }); |
| 213 | + |
| 214 | + assert.ok(executeCommandStub.notCalled); |
| 215 | + }); |
| 216 | +}); |
0 commit comments