Skip to content

Commit dee8097

Browse files
committed
Fix tests
The localization tests need to initialize the language server with the correct translation loading function. Signed-off-by: David Thompson <[email protected]>
1 parent 4645931 commit dee8097

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

src/nodeTranslationSetup.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) IBM Corp. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
import * as path from 'path';
6+
import { existsSync } from 'fs';
7+
import { URI } from 'vscode-uri';
8+
import * as l10n from '@vscode/l10n';
9+
import { InitializeParams } from 'vscode-languageserver';
10+
11+
/**
12+
* Loads translations from the filesystem based on the configured locale and the folder of translations provided in hte initilaization parameters.
13+
*
14+
* This is the default implementation when running as binary, but isn't used when running as a web worker.
15+
*
16+
* @param params the language server initialization parameters
17+
*/
18+
export async function setupl10nBundle(params: InitializeParams): Promise<void> {
19+
const __dirname = path.dirname(__filename);
20+
const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../../../l10n');
21+
const locale: string = params.locale || 'en';
22+
if (l10nPath) {
23+
const bundleFile = !existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`))
24+
? `bundle.l10n.json`
25+
: `bundle.l10n.${locale}.json`;
26+
const baseBundleFile = path.join(l10nPath, bundleFile);
27+
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
28+
locale,
29+
_languagePackSupport: true,
30+
});
31+
await l10n.config({
32+
uri: URI.file(baseBundleFile).toString(),
33+
});
34+
}
35+
}

src/server.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
* Licensed under the MIT License. See License.txt in the project root for license information.
66
*--------------------------------------------------------------------------------------------*/
77

8-
import { promises as fs, existsSync } from 'fs';
9-
import { Connection, createConnection, InitializeParams, ProposedFeatures } from 'vscode-languageserver/node';
8+
import { promises as fs } from 'fs';
9+
import { Connection, createConnection, ProposedFeatures } from 'vscode-languageserver/node';
1010
import { TelemetryImpl } from './languageserver/telemetry';
1111
import { schemaRequestHandler, workspaceContext } from './languageservice/services/schemaRequestHandler';
1212
import { convertErrorToTelemetryMsg } from './languageservice/utils/objects';
13+
import { setupl10nBundle } from './nodeTranslationSetup';
1314
import { YAMLServerInit } from './yamlServerInit';
1415
import { SettingsState } from './yamlSettings';
15-
import * as path from 'path';
16-
import * as l10n from '@vscode/l10n';
17-
import { URI } from 'vscode-uri';
1816

1917
// Create a connection for the server.
2018
let connection: Connection = null;
@@ -69,23 +67,4 @@ const schemaRequestHandlerWrapper = (connection: Connection, uri: string): Promi
6967
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
7068
const telemetry = new TelemetryImpl(connection);
7169

72-
async function setupl10nBundle(params: InitializeParams): Promise<void> {
73-
const __dirname = path.dirname(__filename);
74-
const l10nPath: string = params.initializationOptions?.l10nPath || path.join(__dirname, '../../../l10n');
75-
const locale: string = params.locale || 'en';
76-
if (l10nPath) {
77-
const bundleFile = !existsSync(path.join(l10nPath, `bundle.l10n.${locale}.json`))
78-
? `bundle.l10n.json`
79-
: `bundle.l10n.${locale}.json`;
80-
const baseBundleFile = path.join(l10nPath, bundleFile);
81-
process.env.VSCODE_NLS_CONFIG = JSON.stringify({
82-
locale,
83-
_languagePackSupport: true,
84-
});
85-
await l10n.config({
86-
uri: URI.file(baseBundleFile).toString(),
87-
});
88-
}
89-
}
90-
9170
new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry, setupl10nBundle).start();

test/bundlel10n.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
* Copyright (c) Red Hat. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5+
import * as l10n from '@vscode/l10n';
56
import * as assert from 'assert';
7+
import * as path from 'path';
8+
import { Connection, createConnection } from 'vscode-languageserver/node';
9+
import { schemaRequestHandler, workspaceContext } from '../src/languageservice/services/schemaRequestHandler';
10+
import { setupl10nBundle } from '../src/nodeTranslationSetup';
11+
import { YAMLServerInit } from '../src/yamlServerInit';
612
import { SettingsState } from '../src/yamlSettings';
713
import { TestCustomSchemaProvider, testFileSystem } from './utils/testHelper';
8-
import { createConnection, Connection } from 'vscode-languageserver/node';
9-
import { schemaRequestHandler, workspaceContext } from '../src/languageservice/services/schemaRequestHandler';
1014
import { TestTelemetry } from './utils/testsTypes';
11-
import { YAMLServerInit } from '../src/yamlServerInit';
12-
import * as l10n from '@vscode/l10n';
13-
import * as path from 'path';
1415

1516
describe('Bundle l10n Test', () => {
1617
let serverInit: YAMLServerInit;
@@ -36,7 +37,7 @@ describe('Bundle l10n Test', () => {
3637
};
3738
const schemaRequestService = schemaRequestHandlerWrapper.bind(this, connection);
3839
const telemetry = new TestTelemetry(connection);
39-
serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry);
40+
serverInit = new YAMLServerInit(connection, yamlSettings, workspaceContext, schemaRequestService, telemetry, setupl10nBundle);
4041
});
4142

4243
after(async () => {

0 commit comments

Comments
 (0)