Skip to content

Commit

Permalink
Fixes to creation of json for Language Server (#5436)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DonJayamanne authored Apr 23, 2019
1 parent 6b9e367 commit e7a65cd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/client/activation/languageServer/activator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,22 @@ export class LanguageServerExtensionActivator implements ILanguageServerActivato
}
public async prepareLanguageServerForNoICU(languageServerFolderPath: string): Promise<void> {
const targetJsonFile = path.join(languageServerFolderPath, 'Microsoft.Python.LanguageServer.runtimeconfig.json');
// tslint:disable-next-line:no-any
let content: any = {};
if (await this.fs.fileExists(targetJsonFile)) {
return;
try {
content = JSON.parse(await this.fs.readFile(targetJsonFile));
if (content.runtimeOptions && content.runtimeOptions.configProperties &&
content.runtimeOptions.configProperties['System.Globalization.Invariant'] === true) {
return;
}
} catch {
// Do nothing.
}
}
const json = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
await this.fs.writeFile(targetJsonFile, JSON.stringify(json));
content.runtimeOptions = content.runtimeOptions || {};
content.runtimeOptions.configProperties = content.runtimeOptions.configProperties || {};
content.runtimeOptions.configProperties['System.Globalization.Invariant'] = true;
await this.fs.writeFile(targetJsonFile, JSON.stringify(content));
}
}
31 changes: 26 additions & 5 deletions src/test/activation/languageServer/activator.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,26 @@ suite('Language Server - Activator', () => {
verify(lsDownloader.downloadLanguageServer(anything(), undefined)).once();
verify(fs.fileExists(targetJsonFile)).once();
});
test('Do not download nor check if ICU config exists after downloading', async () => {
test('Download if contents of ICU config is not as expected', async () => {
const languageServerFolder = 'Some folder name';
const languageServerFolderPath = path.join(EXTENSION_ROOT_DIR, languageServerFolder);
const mscorlib = path.join(languageServerFolderPath, 'mscorlib.dll');
const targetJsonFile = path.join(languageServerFolderPath, 'Microsoft.Python.LanguageServer.runtimeconfig.json');
const jsonContents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': false } } };

when(settings.downloadLanguageServer).thenReturn(true);
when(lsFolderService.getLanguageServerFolderName(undefined)).thenResolve(languageServerFolder);
when(fs.fileExists(mscorlib)).thenResolve(true);
when(fs.fileExists(mscorlib)).thenResolve(false);
when(lsDownloader.downloadLanguageServer(languageServerFolderPath, undefined)).thenResolve();
when(fs.fileExists(targetJsonFile)).thenResolve(true);
when(fs.readFile(targetJsonFile)).thenResolve(JSON.stringify(jsonContents));

await activator.ensureLanguageServerIsAvailable(undefined);

verify(lsFolderService.getLanguageServerFolderName(undefined)).once();
verify(lsDownloader.downloadLanguageServer(anything(), undefined)).never();
verify(fs.fileExists(targetJsonFile)).never();
verify(lsDownloader.downloadLanguageServer(anything(), undefined)).once();
verify(fs.fileExists(targetJsonFile)).once();
verify(fs.readFile(targetJsonFile)).once();
});
test('JSON file is created to ensure LS can start without ICU', async () => {
const targetJsonFile = path.join('some folder', 'Microsoft.Python.LanguageServer.runtimeconfig.json');
Expand All @@ -194,14 +199,30 @@ suite('Language Server - Activator', () => {
verify(fs.fileExists(targetJsonFile)).atLeast(1);
verify(fs.writeFile(targetJsonFile, JSON.stringify(contents))).once();
});
test('JSON file is not created if it already exists', async () => {
test('JSON file is not created if it already exists with the right content', async () => {
const targetJsonFile = path.join('some folder', 'Microsoft.Python.LanguageServer.runtimeconfig.json');
const contents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
const existingContents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
when(fs.fileExists(targetJsonFile)).thenResolve(true);
when(fs.readFile(targetJsonFile)).thenResolve(JSON.stringify(existingContents));

await activator.prepareLanguageServerForNoICU('some folder');

verify(fs.fileExists(targetJsonFile)).atLeast(1);
verify(fs.writeFile(targetJsonFile, JSON.stringify(contents))).never();
verify(fs.readFile(targetJsonFile)).once();
});
test('JSON file is created if it already exists but with the wrong file content', async () => {
const targetJsonFile = path.join('some folder', 'Microsoft.Python.LanguageServer.runtimeconfig.json');
const contents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': true } } };
const existingContents = { runtimeOptions: { configProperties: { 'System.Globalization.Invariant': false } } };
when(fs.fileExists(targetJsonFile)).thenResolve(true);
when(fs.readFile(targetJsonFile)).thenResolve(JSON.stringify(existingContents));

await activator.prepareLanguageServerForNoICU('some folder');

verify(fs.fileExists(targetJsonFile)).atLeast(1);
verify(fs.writeFile(targetJsonFile, JSON.stringify(contents))).once();
verify(fs.readFile(targetJsonFile)).once();
});
});

0 comments on commit e7a65cd

Please sign in to comment.