mirror of
https://github.com/microsoft/vscode.git
synced 2026-09-26 09:45:54 +01:00
Add tests for EnvironmentVariableCollection
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -21,6 +21,9 @@ export interface IEnvironmentVariableMutator {
|
||||
}
|
||||
|
||||
export interface IEnvironmentVariableCollection {
|
||||
/**
|
||||
* All entries in the collection
|
||||
*/
|
||||
readonly entries: ReadonlyMap<string, IEnvironmentVariableMutator>;
|
||||
|
||||
/**
|
||||
@@ -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<IEnvironmentVariableCollection>;
|
||||
|
||||
/**
|
||||
* Sets an extension's environment variable collection.
|
||||
*/
|
||||
set(extensionIdentifier: string, collection: IEnvironmentVariableCollection): void;
|
||||
|
||||
/**
|
||||
* Deletes an extension's environment variable collection.
|
||||
*/
|
||||
delete(extensionIdentifier: string): void;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ interface ISerializableEnvironmentVariableCollection {
|
||||
values: string[];
|
||||
types: number[];
|
||||
}
|
||||
|
||||
interface ISerializableExtensionEnvironmentVariableCollection {
|
||||
extensionIdentifier: string,
|
||||
collection: ISerializableEnvironmentVariableCollection
|
||||
|
||||
@@ -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'
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user