Skip to content

Commit e543098

Browse files
committed
addressing review comments
addressing review comments
1 parent e5d4b68 commit e543098

File tree

7 files changed

+139
-134
lines changed

7 files changed

+139
-134
lines changed

vscode/src/localiser.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2023-2024, Oracle and/or its affiliates.
2+
Copyright (c) 2023-2025, Oracle and/or its affiliates.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -21,9 +21,10 @@ import * as fs from 'fs';
2121
import { extConstants } from './constants';
2222
import { FileUtils } from './utils';
2323
import { LOGGER } from './logger';
24+
import path = require('path');
2425

2526
const DEFAULT_LANGUAGE = "en";
26-
const DEFAULT_BUNDLE_FILE = `l10n/bundle.l10n.${DEFAULT_LANGUAGE}.json`;
27+
const DEFAULT_BUNDLE_FILE = path.join("l10n", `bundle.l10n.${DEFAULT_LANGUAGE}.json`);
2728
const _format2Regexp = /{([^}]+)}/g;
2829

2930
export interface l10n {
@@ -35,26 +36,18 @@ export interface l10n {
3536
class l10Wrapper implements l10n {
3637
private defaultl10nMap: any;
3738
constructor(extensionId: string, defaultBundlePath: string) {
38-
let extnPath = vscode.extensions.getExtension(extensionId)?.extensionPath;
39-
let defaultBundleAbsoluteFsPath = FileUtils.toUri(`${extnPath}/${defaultBundlePath}`).fsPath;
40-
let logAndThrowError = (message: string, err: unknown) => {
41-
let errMsg = "";
42-
if (err instanceof Error) {
43-
errMsg = err?.message
44-
}
45-
LOGGER.error(message + errMsg);
46-
throw err;
47-
}
39+
let extnPath = vscode.extensions.getExtension(extensionId)!!.extensionPath;
40+
let defaultBundleAbsoluteFsPath = FileUtils.toUri(path.join(extnPath,defaultBundlePath)).fsPath;
4841
let fileContent: string = "";
4942
try {
5043
fileContent = fs.readFileSync(defaultBundleAbsoluteFsPath, 'utf-8');
5144
} catch (err) {
52-
logAndThrowError("error occured while reading bundle file : ", err);
45+
LOGGER.logAndThrowError("error occured while reading bundle file : ", err);
5346
}
5447
try {
5548
this.defaultl10nMap = JSON.parse(fileContent);
5649
} catch (err) {
57-
logAndThrowError("error occured while parsing bundle file : ", err);
50+
LOGGER.logAndThrowError("error occured while parsing bundle file : ", err);
5851
}
5952

6053
}

vscode/src/logger.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
import { OutputChannel, window } from "vscode";
1717
import { extConstants } from "./constants";
18+
import { isError } from "./utils";
1819

1920
enum LogLevel {
2021
INFO = 'INFO',
@@ -54,6 +55,15 @@ export class ExtensionLogger {
5455
}
5556
}
5657

58+
public logAndThrowError (message: string, err: unknown) {
59+
let errMsg = "";
60+
if (isError(err)) {
61+
errMsg = err?.message
62+
}
63+
LOGGER.error(message + errMsg);
64+
throw err;
65+
}
66+
5767
public logNoNL(message: string): void {
5868
this.outChannel.append(message);
5969
}

vscode/src/test/unit/general/localetest.unit.test.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Copyright (c) 2025, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
import { expect } from 'chai';
17+
import { describe, it, beforeEach, before, after } from "mocha";
18+
import * as sinon from 'sinon';
19+
import * as vscode from 'vscode';
20+
import * as assert from 'assert';
21+
import { Extension } from 'vscode';
22+
import { LOGGER } from '../../../logger';
23+
import { mock, instance, when, reset } from "ts-mockito";
24+
25+
import * as path from 'path';
26+
27+
describe('localiser tests', () => {
28+
let loggerLogStub: sinon.SinonStub;
29+
let mockedExtns: typeof vscode.extensions;
30+
let extMock: Extension<any>;
31+
let mockedEnv: typeof vscode.env;
32+
let mockedL10n: typeof vscode.l10n;
33+
let currentDir = __dirname.replace("/out/", "/src/");
34+
35+
before(() => {
36+
let vscodeObj = (vscode as typeof vscode & { mockedExtns: typeof vscode.extensions, mockedEnv: typeof vscode.env, mockedL10n: typeof vscode.l10n });
37+
mockedExtns = vscodeObj.mockedExtns;
38+
mockedEnv = vscodeObj.mockedEnv;
39+
mockedL10n = vscodeObj.mockedL10n;
40+
extMock = mock<Extension<any>>();
41+
loggerLogStub = sinon.stub(LOGGER, "error");
42+
});
43+
after(() => {
44+
sinon.reset();
45+
reset(mockedExtns);
46+
reset(extMock);
47+
reset(mockedEnv);
48+
reset(mockedL10n);
49+
});
50+
51+
beforeEach(() => {
52+
sinon.reset();
53+
reset(mockedExtns);
54+
reset(extMock);
55+
reset(mockedEnv);
56+
reset(mockedL10n);
57+
})
58+
59+
60+
61+
describe('l10n tests', () => {
62+
describe('issue while reading bundle', () => {
63+
it('file not found error', () => {
64+
let msg: string | null = null;
65+
when(extMock?.extensionPath).thenReturn(path.join(currentDir, 'doesnt-exist'));
66+
var mkInst = instance(extMock);
67+
when(mockedExtns.getExtension("oracle.oracle-java")).thenReturn(mkInst);
68+
try {
69+
require('../../../localiser');
70+
} catch (e) {
71+
msg = (e as any & { message: string }).message
72+
}
73+
assert.strictEqual(msg!!.includes("no such file or directory"), true);
74+
expect(loggerLogStub.called).to.be.true;
75+
});
76+
it('file parsing error', () => {
77+
let msg: string | null = null;
78+
when(extMock?.extensionPath).thenReturn(path.join(currentDir, 'resources', 'corrupt'));
79+
var mkInst = instance(extMock);
80+
when(mockedExtns.getExtension("oracle.oracle-java")).thenReturn(mkInst);
81+
try {
82+
require('../../../localiser');
83+
} catch (e) {
84+
msg = (e as any & { message: string }).message
85+
}
86+
assert.strictEqual(msg!!.includes("Bad control character in string literal in JSON"), true);
87+
expect(loggerLogStub.called).to.be.true;
88+
});
89+
90+
});
91+
describe('l10n initialisation tests', () => {
92+
it('l10n initialized', () => {
93+
when(extMock?.extensionPath).thenReturn(path.join(currentDir, 'resources'));
94+
var mkInst = instance(extMock);
95+
when(mockedExtns.getExtension("oracle.oracle-java")).thenReturn(mkInst);
96+
require('../../../localiser');
97+
expect(loggerLogStub.called).to.be.false;
98+
});
99+
it('get nbLocaleCode and get value', () => {
100+
when(extMock?.extensionPath).thenReturn(path.join(currentDir, 'resources'));
101+
var mkExtInst = instance(extMock);
102+
when(mockedEnv.language).thenReturn("en");
103+
when(mockedExtns.getExtension("oracle.oracle-java")).thenReturn(mkExtInst);
104+
when(mockedL10n.bundle).thenReturn(undefined);
105+
let l10n = require('../../../localiser');
106+
let l10nObj = l10n.l10n as { nbLocaleCode(): string, value(key: string, placeholderMap?: Record<string, any>): string };
107+
assert.strictEqual(l10nObj.nbLocaleCode(), "en");
108+
assert.strictEqual(l10nObj.value("label1"), "label1 description");
109+
assert.strictEqual(l10nObj.value("label2", { "placeholder1": "sample data" }), "lable2 sample data description");
110+
expect(loggerLogStub.called).to.be.false;
111+
});
112+
});
113+
});
114+
});

vscode/src/test/unit/mocks/init.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2023-2024, Oracle and/or its affiliates.
2+
Copyright (c) 2023-2025, Oracle and/or its affiliates.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -35,11 +35,11 @@ export const initMocks = () => {
3535

3636
const replaceImportsWithMocks = (mocks: any) => {
3737
Module._load = function (request: any, _parent: any) {
38-
38+
var isLocaliserUnitTest = ['localiser.unit.test.ts','localiser.unit.test.js'].find(it=>request.includes(it)||_parent?.filename.includes(it))
3939
if (request === 'vscode') {
4040
return mocks.vscode;
41-
} else if (request.includes('localiser') && !(_parent?.filename?.includes("localetest.unit.test.ts") || _parent?.filename?.includes("localetest.unit.test.js")) ) {
42-
return mocks.localiser;
41+
} else if (request.includes('localiser') && !isLocaliserUnitTest) {
42+
return mocks.localiser;
4343
}
4444

4545
if (/\.less$/.test(request)) {

vscode/src/test/unit/mocks/vscode/mockVscode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2023-2024, Oracle and/or its affiliates.
2+
Copyright (c) 2023-2025, Oracle and/or its affiliates.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@ import { mockedEnums } from './vscodeHostedTypes';
2222
import { NotebookCellOutputItem } from './notebookCellOutputItem';
2323
import { mock,instance } from "ts-mockito";
2424
type VSCode = typeof vscode;
25-
const mockedVSCode: Partial<VSCode> & { mockedExtns?:typeof vscode.extensions,mockedL10n?:typeof vscode.l10n} = {};
25+
const mockedVSCode: Partial<VSCode> & { mockedExtns?: typeof vscode.extensions, mockedL10n?: typeof vscode.l10n } = {};
2626

2727
const mockedVscodeClassesAndTypes = () => {
2828
mockedVSCode.Uri = URI as any;

vscode/src/test/unit/mocks/vscode/namespaces/env.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import { mock, when, anyString, anyOfClass, anything, instance } from "ts-mockit
1919

2020
type VSCode = typeof vscode;
2121
let mockedEnv: typeof vscode.env;
22-
export const mockEnvNamespace = (mockedVSCode: Partial<VSCode> & { mockedEnv?:typeof vscode.env}) => {
22+
export const mockEnvNamespace = (mockedVSCode: Partial<VSCode> & { mockedEnv?: typeof vscode.env }) => {
2323
mockedEnv = mock<typeof vscode.env>();
24-
mockedVSCode.mockedEnv=mockedEnv;
24+
mockedVSCode.mockedEnv = mockedEnv;
2525
mockedVSCode.env = instance(mockedEnv);
2626
mockTelemetryFields();
2727
}

0 commit comments

Comments
 (0)