Skip to content

Commit e7a65cd

Browse files
authored
Fixes to creation of json for Language Server (#5436)
For #5043 <!-- If an item below does not apply to you, then go ahead and check it off as "done" and strikethrough the text, e.g.: - [x] ~Has unit tests & system/integration tests~ --> - [x] Pull request represents a single change (i.e. not fixing disparate/unrelated things in a single PR) - [x] Title summarizes what is changing - [n/a] Has a [news entry](https://github.com/Microsoft/vscode-python/tree/master/news) file (remember to thank yourself!) - [n/a] Has sufficient logging. - [n/a] Has telemetry for enhancements. - [x] Unit tests & system/integration tests are added/updated - [n/a] [Test plan](https://github.com/Microsoft/vscode-python/blob/master/.github/test_plan.md) is updated as appropriate - [n/a] [`package-lock.json`](https://github.com/Microsoft/vscode-python/blob/master/package-lock.json) has been regenerated by running `npm install` (if dependencies have changed) - [n/a] The wiki is updated with any design decisions/details.
1 parent 6b9e367 commit e7a65cd

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/client/activation/languageServer/activator.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,22 @@ export class LanguageServerExtensionActivator implements ILanguageServerActivato
6565
}
6666
public async prepareLanguageServerForNoICU(languageServerFolderPath: string): Promise<void> {
6767
const targetJsonFile = path.join(languageServerFolderPath, 'Microsoft.Python.LanguageServer.runtimeconfig.json');
68+
// tslint:disable-next-line:no-any
69+
let content: any = {};
6870
if (await this.fs.fileExists(targetJsonFile)) {
69-
return;
71+
try {
72+
content = JSON.parse(await this.fs.readFile(targetJsonFile));
73+
if (content.runtimeOptions && content.runtimeOptions.configProperties &&
74+
content.runtimeOptions.configProperties['System.Globalization.Invariant'] === true) {
75+
return;
76+
}
77+
} catch {
78+
// Do nothing.
79+
}
7080
}
71-
const json = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
72-
await this.fs.writeFile(targetJsonFile, JSON.stringify(json));
81+
content.runtimeOptions = content.runtimeOptions || {};
82+
content.runtimeOptions.configProperties = content.runtimeOptions.configProperties || {};
83+
content.runtimeOptions.configProperties['System.Globalization.Invariant'] = true;
84+
await this.fs.writeFile(targetJsonFile, JSON.stringify(content));
7385
}
7486
}

src/test/activation/languageServer/activator.unit.test.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,21 +167,26 @@ suite('Language Server - Activator', () => {
167167
verify(lsDownloader.downloadLanguageServer(anything(), undefined)).once();
168168
verify(fs.fileExists(targetJsonFile)).once();
169169
});
170-
test('Do not download nor check if ICU config exists after downloading', async () => {
170+
test('Download if contents of ICU config is not as expected', async () => {
171171
const languageServerFolder = 'Some folder name';
172172
const languageServerFolderPath = path.join(EXTENSION_ROOT_DIR, languageServerFolder);
173173
const mscorlib = path.join(languageServerFolderPath, 'mscorlib.dll');
174174
const targetJsonFile = path.join(languageServerFolderPath, 'Microsoft.Python.LanguageServer.runtimeconfig.json');
175+
const jsonContents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': false } } };
175176

176177
when(settings.downloadLanguageServer).thenReturn(true);
177178
when(lsFolderService.getLanguageServerFolderName(undefined)).thenResolve(languageServerFolder);
178-
when(fs.fileExists(mscorlib)).thenResolve(true);
179+
when(fs.fileExists(mscorlib)).thenResolve(false);
180+
when(lsDownloader.downloadLanguageServer(languageServerFolderPath, undefined)).thenResolve();
181+
when(fs.fileExists(targetJsonFile)).thenResolve(true);
182+
when(fs.readFile(targetJsonFile)).thenResolve(JSON.stringify(jsonContents));
179183

180184
await activator.ensureLanguageServerIsAvailable(undefined);
181185

182186
verify(lsFolderService.getLanguageServerFolderName(undefined)).once();
183-
verify(lsDownloader.downloadLanguageServer(anything(), undefined)).never();
184-
verify(fs.fileExists(targetJsonFile)).never();
187+
verify(lsDownloader.downloadLanguageServer(anything(), undefined)).once();
188+
verify(fs.fileExists(targetJsonFile)).once();
189+
verify(fs.readFile(targetJsonFile)).once();
185190
});
186191
test('JSON file is created to ensure LS can start without ICU', async () => {
187192
const targetJsonFile = path.join('some folder', 'Microsoft.Python.LanguageServer.runtimeconfig.json');
@@ -194,14 +199,30 @@ suite('Language Server - Activator', () => {
194199
verify(fs.fileExists(targetJsonFile)).atLeast(1);
195200
verify(fs.writeFile(targetJsonFile, JSON.stringify(contents))).once();
196201
});
197-
test('JSON file is not created if it already exists', async () => {
202+
test('JSON file is not created if it already exists with the right content', async () => {
198203
const targetJsonFile = path.join('some folder', 'Microsoft.Python.LanguageServer.runtimeconfig.json');
199204
const contents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
205+
const existingContents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
200206
when(fs.fileExists(targetJsonFile)).thenResolve(true);
207+
when(fs.readFile(targetJsonFile)).thenResolve(JSON.stringify(existingContents));
201208

202209
await activator.prepareLanguageServerForNoICU('some folder');
203210

204211
verify(fs.fileExists(targetJsonFile)).atLeast(1);
205212
verify(fs.writeFile(targetJsonFile, JSON.stringify(contents))).never();
213+
verify(fs.readFile(targetJsonFile)).once();
214+
});
215+
test('JSON file is created if it already exists but with the wrong file content', async () => {
216+
const targetJsonFile = path.join('some folder', 'Microsoft.Python.LanguageServer.runtimeconfig.json');
217+
const contents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
218+
const existingContents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': false } } };
219+
when(fs.fileExists(targetJsonFile)).thenResolve(true);
220+
when(fs.readFile(targetJsonFile)).thenResolve(JSON.stringify(existingContents));
221+
222+
await activator.prepareLanguageServerForNoICU('some folder');
223+
224+
verify(fs.fileExists(targetJsonFile)).atLeast(1);
225+
verify(fs.writeFile(targetJsonFile, JSON.stringify(contents))).once();
226+
verify(fs.readFile(targetJsonFile)).once();
206227
});
207228
});

0 commit comments

Comments
 (0)