From a4cc3c1fcdd6760599d2c75d1f63184e9945b176 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 26 Mar 2020 11:48:41 -0700 Subject: [PATCH] Add tests for EnvironmentVariableCollection --- .../api/browser/mainThreadTerminalService.ts | 2 +- .../terminal/common/environmentVariable.ts | 17 ++- .../common/environmentVariableService.ts | 1 + .../environmentVariableCollection.test.ts | 102 ++++++++++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 src/vs/workbench/contrib/terminal/test/common/environmentVariableCollection.test.ts diff --git a/src/vs/workbench/api/browser/mainThreadTerminalService.ts b/src/vs/workbench/api/browser/mainThreadTerminalService.ts index 52a57b17fdd..21589ab69f3 100644 --- a/src/vs/workbench/api/browser/mainThreadTerminalService.ts +++ b/src/vs/workbench/api/browser/mainThreadTerminalService.ts @@ -14,7 +14,7 @@ import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteA import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { TerminalDataBufferer } from 'vs/workbench/contrib/terminal/common/terminalDataBuffering'; import { IEnvironmentVariableService } from 'vs/workbench/contrib/terminal/common/environmentVariable'; -import { EnvironmentVariableCollection } from 'vs/workbench/contrib/terminal/common/environmentVariableService'; +import { EnvironmentVariableCollection } from 'vs/workbench/contrib/terminal/common/environmentVariableCollection'; @extHostNamedCustomer(MainContext.MainThreadTerminalService) export class MainThreadTerminalService implements MainThreadTerminalServiceShape { diff --git a/src/vs/workbench/contrib/terminal/common/environmentVariable.ts b/src/vs/workbench/contrib/terminal/common/environmentVariable.ts index c55f55ad04d..a10b86bd847 100644 --- a/src/vs/workbench/contrib/terminal/common/environmentVariable.ts +++ b/src/vs/workbench/contrib/terminal/common/environmentVariable.ts @@ -21,6 +21,9 @@ export interface IEnvironmentVariableMutator { } export interface IEnvironmentVariableCollection { + /** + * All entries in the collection + */ readonly entries: ReadonlyMap; /** @@ -43,12 +46,24 @@ export interface IEnvironmentVariableService { _serviceBrand: undefined; /** - * Gets a single collection constructed by merging all collections into one. + * Gets a single collection constructed by merging all environment variable collections into + * one. */ readonly mergedCollection: IEnvironmentVariableCollection; + /** + * An event that is fired when an extension's environment variable collection changes, the event + * provides the new merged collection. + */ onDidChangeCollections: Event; + /** + * Sets an extension's environment variable collection. + */ set(extensionIdentifier: string, collection: IEnvironmentVariableCollection): void; + + /** + * Deletes an extension's environment variable collection. + */ delete(extensionIdentifier: string): void; } diff --git a/src/vs/workbench/contrib/terminal/common/environmentVariableService.ts b/src/vs/workbench/contrib/terminal/common/environmentVariableService.ts index aacec8caefb..e239da17f85 100644 --- a/src/vs/workbench/contrib/terminal/common/environmentVariableService.ts +++ b/src/vs/workbench/contrib/terminal/common/environmentVariableService.ts @@ -17,6 +17,7 @@ interface ISerializableEnvironmentVariableCollection { values: string[]; types: number[]; } + interface ISerializableExtensionEnvironmentVariableCollection { extensionIdentifier: string, collection: ISerializableEnvironmentVariableCollection diff --git a/src/vs/workbench/contrib/terminal/test/common/environmentVariableCollection.test.ts b/src/vs/workbench/contrib/terminal/test/common/environmentVariableCollection.test.ts new file mode 100644 index 00000000000..5fd601596e9 --- /dev/null +++ b/src/vs/workbench/contrib/terminal/test/common/environmentVariableCollection.test.ts @@ -0,0 +1,102 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { EnvironmentVariableCollection } from 'vs/workbench/contrib/terminal/common/environmentVariableCollection'; +import { strictEqual, deepStrictEqual, throws } from 'assert'; +import { EnvironmentVariableMutatorType } from 'vs/workbench/contrib/terminal/common/environmentVariable'; +import { IProcessEnvironment } from 'vs/base/common/platform'; + +suite('EnvironmentVariable - EnvironmentVariableCollection', () => { + test('should construct correctly with no arguments', () => { + const c = new EnvironmentVariableCollection(); + strictEqual(c.entries.size, 0); + }); + + test('should construct correctly with 3 arguments', () => { + const c = new EnvironmentVariableCollection( + ['A', 'B', 'C'], + ['a', 'b', 'c'], + [1, 2, 3] + ); + const keys = [...c.entries.keys()]; + deepStrictEqual(keys, ['A', 'B', 'C']); + deepStrictEqual(c.entries.get('A'), { value: 'a', type: EnvironmentVariableMutatorType.Replace }); + deepStrictEqual(c.entries.get('B'), { value: 'b', type: EnvironmentVariableMutatorType.Append }); + deepStrictEqual(c.entries.get('C'), { value: 'c', type: EnvironmentVariableMutatorType.Prepend }); + }); + + test('should throw when ctor arguments have differing length', () => { + throws(() => new EnvironmentVariableCollection(['A'], ['a'], [])); + throws(() => new EnvironmentVariableCollection([], ['a'], [1])); + throws(() => new EnvironmentVariableCollection(['A'], [], [])); + }); + + test('getNewAdditions should return undefined when there are no new additions', () => { + const c1 = new EnvironmentVariableCollection( + ['A', 'B', 'C'], + ['a', 'b', 'c'], + [1, 2, 3] + ); + const c2 = new EnvironmentVariableCollection( + ['A', 'B', 'C'], + ['a', 'b', 'c'], + [1, 2, 3] + ); + const newAdditions = c1.getNewAdditions(c2); + strictEqual(newAdditions, undefined); + }); + + test('getNewAdditions should return only new additions in another collection', () => { + const c1 = new EnvironmentVariableCollection( + ['A', 'B', 'C'], + ['a', 'b', 'c'], + [1, 2, 3] + ); + const c2 = new EnvironmentVariableCollection( + ['B', 'D', 'C'], + ['b', 'd', 'c'], + [2, 1, 3] + ); + const newAdditions = c1.getNewAdditions(c2)!; + const keys = [...newAdditions.keys()]; + deepStrictEqual(keys, ['D']); + deepStrictEqual(newAdditions.get('D'), { value: 'd', type: EnvironmentVariableMutatorType.Replace }); + }); + + test('applyToProcessEnvironment should apply the collection to an environment', () => { + const c = new EnvironmentVariableCollection( + ['A', 'B', 'C'], + ['a', 'b', 'c'], + [1, 2, 3] + ); + const env: IProcessEnvironment = { + A: 'foo', + B: 'bar', + C: 'baz' + }; + c.applyToProcessEnvironment(env); + deepStrictEqual(env, { + A: 'a', + B: 'barb', + C: 'cbaz' + }); + }); + + test('applyToProcessEnvironment should apply the collection to environment entries with no values', () => { + const c = new EnvironmentVariableCollection( + ['A', 'B', 'C'], + ['a', 'b', 'c'], + [1, 2, 3] + ); + const env: IProcessEnvironment = { + }; + c.applyToProcessEnvironment(env); + deepStrictEqual(env, { + A: 'a', + B: 'b', + C: 'c' + }); + }); +});