Path: blob/main/src/vs/workbench/contrib/extensions/test/electron-browser/extension.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { ExtensionState } from '../../common/extensions.js';7import { Extension } from '../../browser/extensionsWorkbenchService.js';8import { IGalleryExtension, IGalleryExtensionProperties, ILocalExtension } from '../../../../../platform/extensionManagement/common/extensionManagement.js';9import { ExtensionType, IExtensionManifest, TargetPlatform } from '../../../../../platform/extensions/common/extensions.js';10import { URI } from '../../../../../base/common/uri.js';11import { getGalleryExtensionId } from '../../../../../platform/extensionManagement/common/extensionManagementUtil.js';12import { generateUuid } from '../../../../../base/common/uuid.js';13import { TestInstantiationService } from '../../../../../platform/instantiation/test/common/instantiationServiceMock.js';14import { IProductService } from '../../../../../platform/product/common/productService.js';15import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';1617suite('Extension Test', () => {1819const disposables = ensureNoDisposablesAreLeakedInTestSuite();2021let instantiationService: TestInstantiationService;2223setup(() => {24instantiationService = disposables.add(new TestInstantiationService());25instantiationService.stub(IProductService, { quality: 'insiders' });26});2728test('extension is not outdated when there is no local and gallery', () => {29const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, undefined, undefined, undefined);30assert.strictEqual(extension.outdated, false);31});3233test('extension is not outdated when there is local and no gallery', () => {34const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension(), undefined, undefined);35assert.strictEqual(extension.outdated, false);36});3738test('extension is not outdated when there is no local and has gallery', () => {39const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, undefined, aGalleryExtension(), undefined);40assert.strictEqual(extension.outdated, false);41});4243test('extension is not outdated when local and gallery are on same version', () => {44const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension(), aGalleryExtension(), undefined);45assert.strictEqual(extension.outdated, false);46});4748test('extension is outdated when local is older than gallery', () => {49const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }), aGalleryExtension('somext', { version: '1.0.1' }), undefined);50assert.strictEqual(extension.outdated, true);51});5253test('extension is outdated when local is built in and older than gallery', () => {54const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }, { type: ExtensionType.System }), aGalleryExtension('somext', { version: '1.0.1' }), undefined);55assert.strictEqual(extension.outdated, true);56});5758test('extension is not outdated when local is built in and older than gallery but product quality is stable', () => {59instantiationService.stub(IProductService, { quality: 'stable' });60const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }, { type: ExtensionType.System }), aGalleryExtension('somext', { version: '1.0.1' }), undefined);61assert.strictEqual(extension.outdated, false);62});6364test('extension is outdated when local and gallery are on same version but on different target platforms', () => {65const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', {}, { targetPlatform: TargetPlatform.WIN32_ARM64 }), aGalleryExtension('somext', {}, { targetPlatform: TargetPlatform.WIN32_X64 }), undefined);66assert.strictEqual(extension.outdated, true);67});6869test('extension is not outdated when local and gallery are on same version and local is on web', () => {70const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', {}, { targetPlatform: TargetPlatform.WEB }), aGalleryExtension('somext'), undefined);71assert.strictEqual(extension.outdated, false);72});7374test('extension is not outdated when local and gallery are on same version and gallery is on web', () => {75const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext'), aGalleryExtension('somext', {}, { targetPlatform: TargetPlatform.WEB }), undefined);76assert.strictEqual(extension.outdated, false);77});7879test('extension is not outdated when local is not pre-release but gallery is pre-release', () => {80const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }), aGalleryExtension('somext', { version: '1.0.1' }, { isPreReleaseVersion: true }), undefined);81assert.strictEqual(extension.outdated, false);82});8384test('extension is outdated when local and gallery are pre-releases', () => {85const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }, { preRelease: true, isPreReleaseVersion: true }), aGalleryExtension('somext', { version: '1.0.1' }, { isPreReleaseVersion: true }), undefined);86assert.strictEqual(extension.outdated, true);87});8889test('extension is outdated when local was opted to pre-release but current version is not pre-release', () => {90const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }, { preRelease: true, isPreReleaseVersion: false }), aGalleryExtension('somext', { version: '1.0.1' }, { isPreReleaseVersion: true }), undefined);91assert.strictEqual(extension.outdated, true);92});9394test('extension is outdated when local is pre-release but gallery is not', () => {95const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }, { preRelease: true, isPreReleaseVersion: true }), aGalleryExtension('somext', { version: '1.0.1' }), undefined);96assert.strictEqual(extension.outdated, true);97});9899test('extension is outdated when local was opted pre-release but current version is not and gallery is not', () => {100const extension = instantiationService.createInstance(Extension, () => ExtensionState.Installed, () => undefined, undefined, aLocalExtension('somext', { version: '1.0.0' }, { preRelease: true, isPreReleaseVersion: false }), aGalleryExtension('somext', { version: '1.0.1' }), undefined);101assert.strictEqual(extension.outdated, true);102});103104function aLocalExtension(name: string = 'someext', manifest: Partial<IExtensionManifest> = {}, properties: Partial<ILocalExtension> = {}): ILocalExtension {105manifest = { name, publisher: 'pub', version: '1.0.0', ...manifest };106properties = {107type: ExtensionType.User,108location: URI.file(`pub.${name}`),109identifier: { id: getGalleryExtensionId(manifest.publisher, manifest.name!) },110targetPlatform: TargetPlatform.UNDEFINED,111...properties112};113return <ILocalExtension>Object.create({ manifest, ...properties });114}115116function aGalleryExtension(name: string = 'somext', properties: Partial<IGalleryExtension> = {}, galleryExtensionProperties: Partial<IGalleryExtensionProperties> = {}): IGalleryExtension {117const targetPlatform = galleryExtensionProperties.targetPlatform ?? TargetPlatform.UNDEFINED;118const galleryExtension = <IGalleryExtension>Object.create({ name, publisher: 'pub', version: '1.0.0', allTargetPlatforms: [targetPlatform], properties: {}, assets: {}, ...properties });119galleryExtension.properties = { ...galleryExtension.properties, dependencies: [], targetPlatform, ...galleryExtensionProperties };120galleryExtension.identifier = { id: getGalleryExtensionId(galleryExtension.publisher, galleryExtension.name), uuid: generateUuid() };121return <IGalleryExtension>galleryExtension;122}123124});125126127