Path: blob/main/src/vs/platform/extensionManagement/test/common/extensionNls.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 { deepClone } from '../../../../base/common/objects.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';8import { ILocalizedString } from '../../../action/common/action.js';9import { IConfigurationNode } from '../../../configuration/common/configurationRegistry.js';10import { localizeManifest } from '../../common/extensionNls.js';11import { IExtensionManifest } from '../../../extensions/common/extensions.js';12import { NullLogger } from '../../../log/common/log.js';1314const manifest: IExtensionManifest = {15name: 'test',16publisher: 'test',17version: '1.0.0',18engines: {19vscode: '*'20},21contributes: {22commands: [23{24command: 'test.command',25title: '%test.command.title%',26category: '%test.command.category%'27},28],29authentication: [30{31id: 'test.authentication',32label: '%test.authentication.label%',33}34],35configuration: {36// to ensure we test another "title" property37title: '%test.configuration.title%',38properties: {39'test.configuration': {40type: 'string',41description: 'not important',42}43}44}45}46};4748suite('Localize Manifest', () => {49const store = ensureNoDisposablesAreLeakedInTestSuite();50test('replaces template strings', function () {51const localizedManifest = localizeManifest(52store.add(new NullLogger()),53deepClone(manifest),54{55'test.command.title': 'Test Command',56'test.command.category': 'Test Category',57'test.authentication.label': 'Test Authentication',58'test.configuration.title': 'Test Configuration',59}60);6162assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');63assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');64assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Test Authentication');65assert.strictEqual((localizedManifest.contributes?.configuration as IConfigurationNode).title, 'Test Configuration');66});6768test('replaces template strings with fallback if not found in translations', function () {69const localizedManifest = localizeManifest(70store.add(new NullLogger()),71deepClone(manifest),72{},73{74'test.command.title': 'Test Command',75'test.command.category': 'Test Category',76'test.authentication.label': 'Test Authentication',77'test.configuration.title': 'Test Configuration',78}79);8081assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');82assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');83assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Test Authentication');84assert.strictEqual((localizedManifest.contributes?.configuration as IConfigurationNode).title, 'Test Configuration');85});8687test('replaces template strings - command title & categories become ILocalizedString', function () {88const localizedManifest = localizeManifest(89store.add(new NullLogger()),90deepClone(manifest),91{92'test.command.title': 'Befehl test',93'test.command.category': 'Testkategorie',94'test.authentication.label': 'Testauthentifizierung',95'test.configuration.title': 'Testkonfiguration',96},97{98'test.command.title': 'Test Command',99'test.command.category': 'Test Category',100'test.authentication.label': 'Test Authentication',101'test.configuration.title': 'Test Configuration',102}103);104105const title = localizedManifest.contributes?.commands?.[0].title as ILocalizedString;106const category = localizedManifest.contributes?.commands?.[0].category as ILocalizedString;107assert.strictEqual(title.value, 'Befehl test');108assert.strictEqual(title.original, 'Test Command');109assert.strictEqual(category.value, 'Testkategorie');110assert.strictEqual(category.original, 'Test Category');111112// Everything else stays as a string.113assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, 'Testauthentifizierung');114assert.strictEqual((localizedManifest.contributes?.configuration as IConfigurationNode).title, 'Testkonfiguration');115});116117test('replaces template strings - is best effort #164630', function () {118const manifestWithTypo: IExtensionManifest = {119name: 'test',120publisher: 'test',121version: '1.0.0',122engines: {123vscode: '*'124},125contributes: {126authentication: [127{128id: 'test.authentication',129// This not existing in the bundle shouldn't cause an error.130label: '%doesnotexist%',131}132],133commands: [134{135command: 'test.command',136title: '%test.command.title%',137category: '%test.command.category%'138},139],140}141};142143const localizedManifest = localizeManifest(144store.add(new NullLogger()),145deepClone(manifestWithTypo),146{147'test.command.title': 'Test Command',148'test.command.category': 'Test Category'149});150151assert.strictEqual(localizedManifest.contributes?.commands?.[0].title, 'Test Command');152assert.strictEqual(localizedManifest.contributes?.commands?.[0].category, 'Test Category');153assert.strictEqual(localizedManifest.contributes?.authentication?.[0].label, '%doesnotexist%');154});155});156157158