|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +/* This file has been modified for Oracle Java SE extension */ |
| 23 | + |
| 24 | +import * as assert from 'assert'; |
| 25 | +import * as fs from 'fs'; |
| 26 | +import * as path from 'path'; |
| 27 | +import * as vscode from 'vscode'; |
| 28 | + |
| 29 | +import { extensions, window } from 'vscode'; |
| 30 | +import { DEFAULT_BUNDLE_FILE_NAME, DEFAULT_PACKAGE_FILE_NAME, EXTENSION_NAME, SUPPORTED_LANGUAGES } from '../../constants'; |
| 31 | +import { checkCommandsLocalisation, checkConfigurationLocalisation, checkDebuggersLocalisation, checkL10nUsageInFiles, checkViewsLocalisation, getKeysFromJSON, matchKeys, matchValuesTemplate } from '../../testutils'; |
| 32 | + |
| 33 | + |
| 34 | +suite("Extension localisation tests", function () { |
| 35 | + window.showInformationMessage("Starting Localisation tests"); |
| 36 | + // Check the consistency of the keys and value templates across the bundle files for the supported languages |
| 37 | + test("Consistency of keys across bundle.l10n.lang.json files for supported languages", async () => { |
| 38 | + const extension = extensions.getExtension(EXTENSION_NAME); |
| 39 | + assert(extension); |
| 40 | + const enBundlePath = path.join(extension.extensionPath, "l10n", DEFAULT_BUNDLE_FILE_NAME); |
| 41 | + assert.ok(fs.existsSync(enBundlePath), `${DEFAULT_BUNDLE_FILE_NAME} doesn't exists`); |
| 42 | + for (const lang of SUPPORTED_LANGUAGES) { |
| 43 | + const langBundlePath = path.join(extension.extensionPath, "l10n", `bundle.l10n.${lang}.json`); |
| 44 | + assert.ok(fs.existsSync(langBundlePath), `bundle.l10n.${lang}.json doesn't exists`); |
| 45 | + assert.ok(matchKeys(enBundlePath, langBundlePath), `Keys of ${DEFAULT_BUNDLE_FILE_NAME} and bundle.l10n.${lang}.json don't match`); |
| 46 | + assert.ok(matchValuesTemplate(enBundlePath, langBundlePath), `Value templates don't match for of the keys of ${DEFAULT_BUNDLE_FILE_NAME} and bundle.l10n.${lang}.json `); |
| 47 | + } |
| 48 | + }).timeout(10000); |
| 49 | + |
| 50 | + test("Consistency of keys across package.nls.lang.json files for supported languages", async () => { |
| 51 | + const extension = extensions.getExtension(EXTENSION_NAME); |
| 52 | + assert(extension); |
| 53 | + const enPackagePath = path.join(extension.extensionPath, DEFAULT_PACKAGE_FILE_NAME); |
| 54 | + assert.ok(fs.existsSync(enPackagePath), `${DEFAULT_PACKAGE_FILE_NAME} doesn't exists`); |
| 55 | + for (const lang of SUPPORTED_LANGUAGES) { |
| 56 | + const langPackagePath = path.join(extension.extensionPath, `package.nls.${lang}.json`); |
| 57 | + assert.ok(fs.existsSync(langPackagePath), `package.nls.${lang}.json doesn't exists`); |
| 58 | + assert.ok(matchKeys(enPackagePath, langPackagePath), `Keys of ${DEFAULT_PACKAGE_FILE_NAME} and package.nls.${lang}.json don't match`); |
| 59 | + } |
| 60 | + }).timeout(10000); |
| 61 | + |
| 62 | + // Check localisable fields being appropriately localised for the contributes defined in package.json |
| 63 | + test("Localisable fields in package.json localised properly ", async () => { |
| 64 | + const extension = extensions.getExtension(EXTENSION_NAME); |
| 65 | + assert(extension); |
| 66 | + const packagePath = path.join(extension.extensionPath, "package.json"); |
| 67 | + assert.ok(fs.existsSync(packagePath), "package.json doesn't exists"); |
| 68 | + const enPackagePath = path.join(extension.extensionPath, DEFAULT_PACKAGE_FILE_NAME); |
| 69 | + assert.ok(fs.existsSync(enPackagePath), `${DEFAULT_PACKAGE_FILE_NAME} doesn't exists`); |
| 70 | + const validKeys: Set<string> = getKeysFromJSON(enPackagePath); |
| 71 | + const packageObj = JSON.parse(fs.readFileSync(packagePath, 'utf8')); |
| 72 | + const contributes = packageObj.contributes |
| 73 | + assert.ok(checkCommandsLocalisation(contributes.commands, validKeys), "Error some commands not localized"); |
| 74 | + assert.ok(checkViewsLocalisation(contributes.views, validKeys), "Error some views is not localized"); |
| 75 | + assert.ok(checkDebuggersLocalisation(contributes.debuggers, validKeys), "Error some debugger labels not localized"); |
| 76 | + assert.ok(checkConfigurationLocalisation(contributes.configuration, validKeys), "Error some configuration labels not localized"); |
| 77 | + }).timeout(10000); |
| 78 | + |
| 79 | + |
| 80 | + // Check if l10n.value is called with a valid key and the placeholder map has all the keys as required in the string template |
| 81 | + test("Proper usage of l10n.value for localisation in the ts/js code files", async () => { |
| 82 | + const ignoredDirEntriesNames = new Set(["test"]); // Names of folders,files( .js only),subfolders within the out directory which are not to be checked |
| 83 | + const extension = vscode.extensions.getExtension(EXTENSION_NAME); |
| 84 | + assert(extension, "extension not found"); |
| 85 | + const enBundlePath = path.join(extension.extensionPath, "l10n", DEFAULT_BUNDLE_FILE_NAME); |
| 86 | + assert(enBundlePath, `${DEFAULT_BUNDLE_FILE_NAME} not found`); |
| 87 | + const validKeyValues = JSON.parse(fs.readFileSync(enBundlePath, 'utf8')); |
| 88 | + assert(checkL10nUsageInFiles(path.join(extension.extensionPath, "out"), ignoredDirEntriesNames, validKeyValues) === 0, "Some files have invalid localisation keys used. Check the logs or error messages"); |
| 89 | + }).timeout(10000); |
| 90 | + |
| 91 | +}); |
0 commit comments