Skip to content

Commit

Permalink
context.environmentVariableCollection.delete AND replace is not a fun…
Browse files Browse the repository at this point in the history
…ction???
  • Loading branch information
anthonykim1 committed Sep 21, 2024
1 parent 32edc64 commit 5ab075e
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"--extensionTestsPath=${workspaceFolder}/out/test"
],
"env": {
"VSC_PYTHON_CI_TEST_GREP": "PYTHONSTARTUP is set when setting is enabled" // Modify this to run a subset of the single workspace tests
"VSC_PYTHON_CI_TEST_GREP": "Terminal - Shell Integration with PYTHONSTARTUP" // Modify this to run a subset of the single workspace tests
},
"sourceMaps": true,
"outFiles": ["${workspaceFolder}/out/**/*.js", "!${workspaceFolder}/**/node_modules**/*"],
Expand Down
104 changes: 104 additions & 0 deletions src/test/terminals/shellIntegration/pythonStartup-ts-mockito.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as sinon from 'sinon';
import * as TypeMoq from 'typemoq';
import {
EnvironmentVariableCollection,
GlobalEnvironmentVariableCollection,
Uri,
WorkspaceConfiguration,
} from 'vscode';
import { mock, instance, when, anything, verify, reset } from 'ts-mockito';
import path from 'path';
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis';
import { registerPythonStartup } from '../../../client/terminals/pythonStartup';
import { IExtensionContext } from '../../../client/common/types';

suite.only('temporarily try with ts-mockito', () => {
// let context: IExtensionContext;
let getConfigurationStub: sinon.SinonStub;
let pythonConfig: TypeMoq.IMock<WorkspaceConfiguration>;
let editorConfig: TypeMoq.IMock<WorkspaceConfiguration>;
// let context: TypeMoq.IMock<IExtensionContext>;
let context: IExtensionContext;
let createDirectoryStub: sinon.SinonStub;
let copyStub: sinon.SinonStub;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// let environmentVariableCollection: TypeMoq.IMock<EnvironmentVariableCollection>;
// let globalEnvironmentVariableCollection: TypeMoq.IMock<GlobalEnvironmentVariableCollection>;

let collection: EnvironmentVariableCollection;
let globalCollection: GlobalEnvironmentVariableCollection;

const joinPathStub = sinon.stub(Uri, 'joinPath');
const sourcePathStub = sinon.stub(path, 'join');

setup(() => {
// context = TypeMoq.Mock.ofType<IExtensionContext>();
context = mock<IExtensionContext>();
// context.setup((c) => c).returns(() => context.object);
// environmentVariableCollection = TypeMoq.Mock.ofType<EnvironmentVariableCollection>();
// globalEnvironmentVariableCollection = TypeMoq.Mock.ofType<GlobalEnvironmentVariableCollection>();

// context.setup((c) => c.environmentVariableCollection).returns(() => globalEnvironmentVariableCollection.object);
// globalEnvironmentVariableCollection
// .setup((c) => c.getScoped(TypeMoq.It.isAny()))
// .returns(() => environmentVariableCollection.object);
// context.setup((c) => c.storageUri).returns(() => Uri.parse('a'));
// context
// .setup((c) =>
// c.environmentVariableCollection.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()),
// )
// .returns(() => Promise.resolve());

globalCollection = mock<GlobalEnvironmentVariableCollection>();
collection = mock<EnvironmentVariableCollection>();
when(context.environmentVariableCollection).thenReturn(instance(globalCollection));
when(globalCollection.getScoped(anything())).thenReturn(instance(collection));

getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
createDirectoryStub = sinon.stub(workspaceApis, 'createDirectory');
copyStub = sinon.stub(workspaceApis, 'copy');
when(context.storageUri).thenReturn(undefined);

// Stub the Uri.joinPath method
joinPathStub.returns(Uri.parse('file:///mock/path/pythonrc.py'));

pythonConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
editorConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
getConfigurationStub.callsFake((section: string) => {
if (section === 'python') {
return pythonConfig.object;
}
return editorConfig.object;
});

createDirectoryStub.callsFake((_) => Promise.resolve());
copyStub.callsFake((_, __, ___) => Promise.resolve());
});

teardown(() => {
sinon.restore();
});

test('PYTHONSTARTUP is set when setting is enabled', async () => {
pythonConfig.setup((p) => p.get('REPL.enableShellIntegration')).returns(() => false);
when(context.storageUri).thenReturn(Uri.parse('file:///a/b/c'));

// Stub the Uri.joinPath method
joinPathStub.returns(Uri.parse('file:///mock/path/pythonrc.py'));

await registerPythonStartup(context);

// Make sure context.environmentVariableCollection.replace is called once
// context.verify(
// (c) => c.environmentVariableCollection.replace(TypeMoq.It.isAny(), TypeMoq.It.isAny()),
// TypeMoq.Times.once(),
// );
when(collection.replace(anything(), anything(), anything())).thenResolve();
when(collection.delete(anything())).thenResolve();
// verify(collection.replace(anything(), anything(), anything())).once();
verify(collection.delete(anything())).once();
// context.verify((c) => c.environmentVariableCollection.delete(TypeMoq.It.isAny()), TypeMoq.Times.never());
});
});

0 comments on commit 5ab075e

Please sign in to comment.