Skip to content

Commit 87e80ef

Browse files
msivasubramaniaandatho7561
authored andcommitted
added test cases for bundle and configuration through testhelper class
1 parent 3d6ea18 commit 87e80ef

File tree

6 files changed

+100
-48
lines changed

6 files changed

+100
-48
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
"clean": "rimraf out/server && rimraf lib",
7373
"compile": "tsc -p .",
7474
"watch": "tsc --watch -p .",
75-
"test": "mocha --require ts-node/register --require ./scripts/setup-l10n.mjs --timeout 5000 --ui bdd ./test/*.test.ts",
75+
"test": "mocha --require ts-node/register --timeout 5000 --ui bdd ./test/*.test.ts",
7676
"coverage": "nyc mocha --require ts-node/register --timeout 5000 --require source-map-support/register --recursive --ui bdd ./test/*.test.ts",
77-
"coveralls": "nyc --reporter=lcov --reporter=text mocha --timeout 5000 --require ts-node/register --require ./scripts/setup-l10n.mjs --require source-map-support/register --recursive --ui bdd ./test/*.test.ts",
77+
"coveralls": "nyc --reporter=lcov --reporter=text mocha --timeout 5000 --require ts-node/register --require source-map-support/register --recursive --ui bdd ./test/*.test.ts",
7878
"lint": "eslint --max-warnings 0 -c .eslintrc.js --ext .ts src test",
7979
"lint-ci": "eslint --max-warnings 0 -c .eslintrc.js -f @microsoft/eslint-formatter-sarif -o eslint-result.sarif --ext .ts src test",
8080
"prettier-fix": "yarn prettier --write .",

scripts/setup-l10n.mjs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/languageservice/parser/jsonParser07.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ function validate(
10751075
validationResult.problems.push({
10761076
location: { offset: node.offset, length: node.length },
10771077
severity: DiagnosticSeverity.Warning,
1078-
message: schema.patternErrorMessage || schema.errorMessage || format.errorMessage,
1078+
message: schema.patternErrorMessage || schema.errorMessage || l10n.t(format.errorMessage),
10791079
source: getSchemaSource(schema, originalSchema),
10801080
schemaUri: getSchemaUri(schema, originalSchema),
10811081
});

src/yamlServerInit.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Telemetry } from './languageservice/telemetry';
2020
import { registerCommands } from './languageservice/services/yamlCommands';
2121
import * as l10n from '@vscode/l10n';
2222
import * as path from 'path';
23+
import * as fs from 'fs';
2324

2425
export class YAMLServerInit {
2526
languageService: LanguageService;
@@ -41,19 +42,6 @@ export class YAMLServerInit {
4142
* The server receives the root path(s) of the workspace and the client capabilities.
4243
*/
4344
this.connection.onInitialize(async (params: InitializeParams): Promise<InitializeResult> => {
44-
const l10nPath: string = params.initializationOptions?.l10nPath;
45-
const locale: string = params.locale;
46-
if (l10nPath) {
47-
const bundleFile = locale === 'en' ? `bundle.l10n.json` : `bundle.l10n.${locale}.json`;
48-
const baseBundleFile = path.join(l10nPath, bundleFile);
49-
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
50-
locale: locale,
51-
_languagePackSupport: true,
52-
});
53-
await l10n.config({
54-
uri: URI.file(baseBundleFile).toString(),
55-
});
56-
}
5745
return this.connectionInitialized(params);
5846
});
5947

@@ -69,6 +57,25 @@ export class YAMLServerInit {
6957
});
7058
}
7159

60+
public async setupl10nBundle(params: InitializeParams): Promise<void> {
61+
const __dirname = path.dirname(__filename);
62+
const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../l10n');
63+
const locale: string = params.locale || 'en';
64+
if (l10nPath) {
65+
const bundleFile = !fs.existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`))
66+
? `bundle.l10n.json`
67+
: `bundle.l10n.${locale}.json`;
68+
const baseBundleFile = path.join(l10nPath, bundleFile);
69+
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
70+
locale,
71+
_languagePackSupport: true,
72+
});
73+
await l10n.config({
74+
uri: URI.file(baseBundleFile).toString(),
75+
});
76+
}
77+
}
78+
7279
// public for test setup
7380
async connectionInitialized(params: InitializeParams): Promise<InitializeResult> {
7481
this.yamlSettings.capabilities = params.capabilities;
@@ -111,7 +118,7 @@ export class YAMLServerInit {
111118
);
112119
this.registerHandlers();
113120
registerCommands(commandExecutor, this.connection);
114-
121+
await this.setupl10nBundle(params);
115122
return {
116123
capabilities: {
117124
textDocumentSync: TextDocumentSyncKind.Incremental,

test/bundlel10n.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
import * as assert from 'assert';
6+
import { SettingsState } from '../src/yamlSettings';
7+
import { TestCustomSchemaProvider, testFileSystem } from './utils/testHelper';
8+
import { createConnection, Connection } from 'vscode-languageserver/node';
9+
import { schemaRequestHandler, workspaceContext } from '../src/languageservice/services/schemaRequestHandler';
10+
import { TestTelemetry } from './utils/testsTypes';
11+
import { YAMLServerInit } from '../src/yamlServerInit';
12+
import * as l10n from '@vscode/l10n';
13+
14+
describe('Bundle l10n Test', () => {
15+
let serverInit: YAMLServerInit;
16+
17+
before(() => {
18+
const yamlSettings = new SettingsState();
19+
process.argv.push('--node-ipc');
20+
const connection = createConnection();
21+
const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promise<string> => {
22+
const testSchemaProvider = TestCustomSchemaProvider.instance();
23+
const testSchema = testSchemaProvider.getContentForSchema(uri);
24+
if (testSchema) {
25+
return Promise.resolve(testSchema);
26+
}
27+
return schemaRequestHandler(
28+
connection,
29+
uri,
30+
yamlSettings.workspaceFolders,
31+
yamlSettings.workspaceRoot,
32+
yamlSettings.useVSCodeContentRequest,
33+
testFileSystem
34+
);
35+
};
36+
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
37+
const telemetry = new TestTelemetry(connection);
38+
serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry);
39+
});
40+
41+
after(async () => {
42+
await serverInit.setupl10nBundle({
43+
locale: 'en',
44+
processId: 0,
45+
rootUri: '',
46+
capabilities: undefined,
47+
});
48+
});
49+
50+
describe('l10n bundle test', function () {
51+
it('check french locale', async () => {
52+
await serverInit.setupl10nBundle({
53+
locale: 'fr',
54+
processId: 0,
55+
rootUri: '',
56+
capabilities: undefined,
57+
});
58+
assert.equal(l10n.t('Default Value'), 'Valeur par défaut');
59+
});
60+
61+
it('un configured locale should return in english', async () => {
62+
await serverInit.setupl10nBundle({
63+
locale: 'pt-br',
64+
processId: 0,
65+
rootUri: '',
66+
capabilities: undefined,
67+
});
68+
assert.equal(l10n.t('Default Value'), 'Default value');
69+
});
70+
});
71+
});

test/utils/testHelper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ export function setupLanguageService(languageSettings: LanguageSettings): TestLa
8282
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
8383
const telemetry = new TestTelemetry(connection);
8484
const serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry);
85+
const __dirname = path.resolve(path.dirname(__filename), '..');
8586
serverInit.connectionInitialized({
8687
processId: null,
8788
capabilities: ClientCapabilities.LATEST as LSPClientCapabilities,
8889
rootUri: null,
8990
workspaceFolders: null,
91+
initializationOptions: {
92+
l10nPath: path.join(__dirname, '../l10n'),
93+
},
94+
locale: 'en',
9095
});
9196
const languageService = serverInit.languageService;
9297
const validationHandler = serverInit.validationHandler;

0 commit comments

Comments
 (0)