提交 e982cfb1 编写于 作者: S Sandeep Somavarapu

#14841 Tests for enablement service

上级 dbf3ba74
......@@ -19,7 +19,6 @@ export class ExtensionEnablementService implements IExtensionEnablementService {
_serviceBrand: any;
private workspace: IWorkspace;
private disposables: IDisposable[] = [];
private _onEnablementChanged = new Emitter<string>();
......@@ -27,24 +26,36 @@ export class ExtensionEnablementService implements IExtensionEnablementService {
constructor(
@IStorageService private storageService: IStorageService,
@IWorkspaceContextService contextService: IWorkspaceContextService,
@IWorkspaceContextService private contextService: IWorkspaceContextService,
@IEnvironmentService private environmentService: IEnvironmentService,
@IExtensionManagementService private extensionManagementService: IExtensionManagementService
) {
this.workspace = contextService.getWorkspace();
extensionManagementService.onDidUninstallExtension(this.onDidUninstallExtension, this, this.disposables);
}
private get workspace(): IWorkspace {
return this.contextService.getWorkspace();
}
public getGloballyDisabledExtensions(): string[] {
return this.getDisabledExtensionsFromStorage(StorageScope.GLOBAL);
return this.getDisabledExtensions(StorageScope.GLOBAL);
}
public getWorkspaceDisabledExtensions(): string[] {
return this.getDisabledExtensionsFromStorage(StorageScope.WORKSPACE);
return this.getDisabledExtensions(StorageScope.WORKSPACE);
}
public canEnable(identifier: string): boolean {
return !this.environmentService.disableExtensions && this.isDisabled(identifier);
if (this.environmentService.disableExtensions) {
return false;
}
if (this.getGloballyDisabledExtensions().indexOf(identifier) !== -1) {
return true;
}
if (this.getWorkspaceDisabledExtensions().indexOf(identifier) !== -1) {
return true;
}
return false;
}
public setEnablement(identifier: string, enable: boolean, workspace: boolean = false): TPromise<boolean> {
......@@ -71,54 +82,37 @@ export class ExtensionEnablementService implements IExtensionEnablementService {
}
}
private getDisabledExtensions(): string[] {
const globalDisabledExtensions = this.getGloballyDisabledExtensions();
const workspaceDisabledExtensions = this.getWorkspaceDisabledExtensions();
return distinct([...workspaceDisabledExtensions, ...globalDisabledExtensions]);
}
private isDisabled(identifier: string): boolean {
return this.getDisabledExtensions().indexOf(identifier) !== -1;
}
private getDisabledExtensionsFromStorage(scope?: StorageScope): string[] {
if (scope !== void 0) {
return this._getDisabledExtensions(scope);
}
const globallyDisabled = this._getDisabledExtensions(StorageScope.GLOBAL);
const workspaceDisabled = this._getDisabledExtensions(StorageScope.WORKSPACE);
return [...globallyDisabled, ...workspaceDisabled];
}
private disableExtension(identifier: string, scope: StorageScope): TPromise<boolean> {
let disabledExtensions = this._getDisabledExtensions(scope);
let disabledExtensions = this.getDisabledExtensions(scope);
const index = disabledExtensions.indexOf(identifier);
if (index === -1) {
disabledExtensions.push(identifier);
this._setDisabledExtensions(disabledExtensions, scope, identifier);
this.setDisabledExtensions(disabledExtensions, scope, identifier);
return TPromise.wrap(true);
}
return TPromise.wrap(false);
}
private enableExtension(identifier: string, scope: StorageScope): TPromise<boolean> {
let disabledExtensions = this._getDisabledExtensions(scope);
let disabledExtensions = this.getDisabledExtensions(scope);
const index = disabledExtensions.indexOf(identifier);
if (index !== -1) {
disabledExtensions.splice(index, 1);
this._setDisabledExtensions(disabledExtensions, scope, identifier);
this.setDisabledExtensions(disabledExtensions, scope, identifier);
return TPromise.wrap(true);
}
return TPromise.wrap(false);
}
private _getDisabledExtensions(scope: StorageScope): string[] {
private getDisabledExtensions(scope: StorageScope): string[] {
if (scope === StorageScope.WORKSPACE && !this.workspace) {
return [];
}
const value = this.storageService.get(DISABLED_EXTENSIONS_STORAGE_PATH, scope, '');
return value ? distinct(value.split(',')) : [];
}
private _setDisabledExtensions(disabledExtensions: string[], scope: StorageScope, extension: string): void {
private setDisabledExtensions(disabledExtensions: string[], scope: StorageScope, extension: string): void {
if (disabledExtensions.length) {
this.storageService.store(DISABLED_EXTENSIONS_STORAGE_PATH, disabledExtensions.join(','), scope);
} else {
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as sinon from 'sinon';
import * as assert from 'assert';
import { IExtensionManagementService, IExtensionEnablementService, DidUninstallExtensionEvent } from 'vs/platform/extensionManagement/common/extensionManagement';
import { ExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionEnablementService';
import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils';
import { TestEnvironmentService, TestWorkspace } from 'vs/test/utils/servicesTestUtils';
import { Emitter } from 'vs/base/common/event';
import { StorageService, InMemoryLocalStorage } from 'vs/workbench/services/storage/common/storageService';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService, WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
function storageService(instantiationService: TestInstantiationService): IStorageService {
let service = instantiationService.get(IStorageService);
if (!service) {
let workspaceContextService = instantiationService.get(IWorkspaceContextService);
if (!workspaceContextService) {
workspaceContextService = instantiationService.stub(IWorkspaceContextService, WorkspaceContextService);
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', TestWorkspace);
}
service = instantiationService.stub(IStorageService, instantiationService.createInstance(StorageService, new InMemoryLocalStorage(), new InMemoryLocalStorage()));
}
return service;
}
export class TestExtensionEnablementService extends ExtensionEnablementService {
constructor(instantiationService: TestInstantiationService) {
super(storageService(instantiationService), instantiationService.get(IWorkspaceContextService),
instantiationService.get(IEnvironmentService) || instantiationService.stub(IEnvironmentService, TestEnvironmentService),
instantiationService.get(IExtensionManagementService) || instantiationService.stub(IExtensionManagementService, { onDidUninstallExtension: new Emitter() }));
}
}
suite('ExtensionEnablementService Test', () => {
let instantiationService: TestInstantiationService;
let testObject: IExtensionEnablementService;
const didUninstallEvent: Emitter<DidUninstallExtensionEvent> = new Emitter();
setup(() => {
instantiationService = new TestInstantiationService();
instantiationService.stub(IExtensionManagementService, { onDidUninstallExtension: didUninstallEvent.event, });
testObject = new TestExtensionEnablementService(instantiationService);
});
teardown(() => {
(<ExtensionEnablementService>testObject).dispose();
});
test('test when no extensions are disabled globally', () => {
assert.deepEqual([], testObject.getGloballyDisabledExtensions());
});
test('test when no extensions are disabled for workspace', () => {
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
});
test('test when no extensions are disabled for workspace when there is no workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => {
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', null);
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
})
.then(done, done);
});
test('test disable an extension globally', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions()))
.then(done, done);
});
test('test disable an extension globally should return truthy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension globally triggers the change event', (done) => {
const target = sinon.spy();
testObject.onEnablementChanged(target);
testObject.setEnablement('pub.a', false)
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test disable an extension globally again should return a falsy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', false))
.then(value => assert.ok(!value))
.then(done, done);
});
test('test disable an extension for workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions()))
.then(done, done);
});
test('test disable an extension for workspace returns a truthy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension for workspace again should return a falsy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false, true))
.then(value => assert.ok(!value))
.then(done, done);
});
test('test disable an extension for workspace and then globally', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
test('test disable an extension for workspace and then globally return a truthy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension for workspace and then globally triggers the change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.a', false))
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test disable an extension globally and then for workspace', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', false, true))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
test('test disable an extension globally and then for workspace return a truthy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', false, true))
.then(value => assert.ok(value))
.then(done, done);
});
test('test disable an extension globally and then for workspace triggers the change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.a', false)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.a', false, true))
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test disable an extension for workspace when there is no workspace throws error', (done) => {
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', null);
testObject.setEnablement('pub.a', false, true)
.then(() => assert.fail('should throw an error'), error => assert.ok(error))
.then(done, done);
});
test('test enable an extension globally', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', true))
.then(() => assert.deepEqual([], testObject.getGloballyDisabledExtensions()))
.then(done, done);
});
test('test enable an extension globally return truthy promise', (done) => {
testObject.setEnablement('pub.a', false)
.then(() => testObject.setEnablement('pub.a', true))
.then(value => assert.ok(value))
.then(done, done);
});
test('test enable an extension globally triggers change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.a', false)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.a', true))
.then(() => assert.ok(target.calledWithExactly('pub.a')))
.then(done, done);
});
test('test enable an extension globally when already enabled return falsy promise', (done) => {
testObject.setEnablement('pub.a', true)
.then(value => assert.ok(!value))
.then(done, done);
});
test('test enable an extension for workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', true, true))
.then(() => assert.deepEqual([], testObject.getWorkspaceDisabledExtensions()))
.then(done, done);
});
test('test enable an extension for workspace return truthy promise', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', true, true))
.then(value => assert.ok(value))
.then(done, done);
});
test('test enable an extension for workspace triggers change event', (done) => {
const target = sinon.spy();
testObject.setEnablement('pub.b', false, true)
.then(() => testObject.onEnablementChanged(target))
.then(() => testObject.setEnablement('pub.b', true, true))
.then(() => assert.ok(target.calledWithExactly('pub.b')))
.then(done, done);
});
test('test enable an extension for workspace when already enabled return falsy promise', (done) => {
testObject.setEnablement('pub.a', true, true)
.then(value => assert.ok(!value))
.then(done, done);
});
test('test enable an extension for workspace when disabled in workspace and gloablly', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => testObject.setEnablement('pub.a', true, true))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getGloballyDisabledExtensions());
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
})
.then(done, done);
});
test('test enable an extension globally when disabled in workspace and gloablly', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => testObject.setEnablement('pub.a', true))
.then(() => {
assert.deepEqual(['pub.a'], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual([], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
test('test remove an extension from disablement list when uninstalled', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => testObject.setEnablement('pub.a', false))
.then(() => didUninstallEvent.fire({ id: 'pub.a' }))
.then(() => {
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
assert.deepEqual([], testObject.getGloballyDisabledExtensions());
})
.then(done, done);
});
});
\ No newline at end of file
......@@ -17,18 +17,13 @@ import {
DidInstallExtensionEvent, DidUninstallExtensionEvent, InstallExtensionEvent
} from 'vs/platform/extensionManagement/common/extensionManagement';
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
import { ExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionEnablementService';
import { TestExtensionEnablementService } from 'vs/platform/extensionManagement/test/common/extensionEnablementService.test';
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/node/extensionGalleryService';
import { IURLService } from 'vs/platform/url/common/url';
import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils';
import { TestWorkspace } from 'vs/test/utils/servicesTestUtils';
import { Emitter } from 'vs/base/common/event';
import { IPager } from 'vs/base/common/paging';
import { ITelemetryService, NullTelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { StorageService, InMemoryLocalStorage } from 'vs/workbench/services/storage/common/storageService';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService, WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
suite('ExtensionsActions Test => ', () => {
......@@ -55,14 +50,8 @@ suite('ExtensionsActions Test => ', () => {
instantiationService.stub(IExtensionManagementService, 'onDidUninstallExtension', didUninstallEvent.event);
instantiationService.stubPromise(IExtensionManagementService, 'getInstalled', []);
instantiationService.stub(IWorkspaceContextService, WorkspaceContextService);
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', TestWorkspace);
instantiationService.stub(IEnvironmentService, { disableExtensions: false });
instantiationService.stub(IStorageService, instantiationService.createInstance(StorageService, new InMemoryLocalStorage(), new InMemoryLocalStorage()));
instantiationService.stub(IExtensionEnablementService, instantiationService.createInstance(ExtensionEnablementService));
instantiationService.stub(IExtensionEnablementService, new TestExtensionEnablementService(instantiationService));
instantiationService.set(IExtensionsWorkbenchService, instantiationService.createInstance(ExtensionsWorkbenchService));
instantiationService.stub(IExtensionService, { getExtensions: () => TPromise.wrap([]) });
});
......
......@@ -17,18 +17,13 @@ import {
DidInstallExtensionEvent, DidUninstallExtensionEvent, InstallExtensionEvent
} from 'vs/platform/extensionManagement/common/extensionManagement';
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
import { ExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionEnablementService';
import { TestExtensionEnablementService } from 'vs/platform/extensionManagement/test/common/extensionEnablementService.test';
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/node/extensionGalleryService';
import { IURLService } from 'vs/platform/url/common/url';
import { TestInstantiationService } from 'vs/test/utils/instantiationTestUtils';
import { TestEnvironmentService, TestWorkspace } from 'vs/test/utils/servicesTestUtils';
import Event, { Emitter } from 'vs/base/common/event';
import { IPager } from 'vs/base/common/paging';
import { ITelemetryService, NullTelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { StorageService, InMemoryLocalStorage } from 'vs/workbench/services/storage/common/storageService';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService, WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
suite('ExtensionsWorkbenchService Test', () => {
......@@ -55,11 +50,7 @@ suite('ExtensionsWorkbenchService Test', () => {
instantiationService.stub(IExtensionManagementService, 'onDidUninstallExtension', didUninstallEvent.event);
instantiationService.stubPromise(IExtensionManagementService, 'getInstalled', []);
instantiationService.stub(IWorkspaceContextService, WorkspaceContextService);
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', TestWorkspace);
instantiationService.stub(IEnvironmentService, TestEnvironmentService);
instantiationService.stub(IStorageService, instantiationService.createInstance(StorageService, new InMemoryLocalStorage(), new InMemoryLocalStorage()));
instantiationService.stub(IExtensionEnablementService, instantiationService.createInstance(ExtensionEnablementService));
instantiationService.stub(IExtensionEnablementService, new TestExtensionEnablementService(instantiationService));
});
teardown(() => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册