Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});