|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// tslint:disable:no-any no-invalid-this no-default-export no-console |
| 7 | + |
| 8 | +import * as assert from 'assert'; |
| 9 | +import * as fs from 'fs-extra'; |
| 10 | +import * as glob from 'glob'; |
| 11 | +import * as path from 'path'; |
| 12 | +import * as vscode from 'vscode'; |
| 13 | +import { waitForCondition } from '../common'; |
| 14 | +import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST, SMOKE_TEST_EXTENSIONS_DIR } from '../constants'; |
| 15 | +import { noop, sleep } from '../core'; |
| 16 | +import { initialize } from '../initialize'; |
| 17 | + |
| 18 | +let initialized = false; |
| 19 | +const fileDefinitions = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'smokeTests', 'definitions.py'); |
| 20 | + |
| 21 | +export async function initializeSmokeTests() { |
| 22 | + if (!IS_SMOKE_TEST || initialized) { |
| 23 | + return; |
| 24 | + } |
| 25 | + await removeLanguageServerFiles(); |
| 26 | + await enableJedi(false); |
| 27 | + await initialize(); |
| 28 | + await openFileAndWaitForLS(fileDefinitions); |
| 29 | + initialized = true; |
| 30 | +} |
| 31 | + |
| 32 | +export async function updateSetting(setting: string, value: any) { |
| 33 | + const resource = vscode.workspace.workspaceFolders![0].uri; |
| 34 | + await vscode.workspace.getConfiguration('python', resource).update(setting, value, vscode.ConfigurationTarget.WorkspaceFolder); |
| 35 | +} |
| 36 | +export async function removeLanguageServerFiles() { |
| 37 | + const folders = await getLanaguageServerFolders(); |
| 38 | + await Promise.all(folders.map(item => fs.remove(item).catch(noop))); |
| 39 | +} |
| 40 | +async function getLanaguageServerFolders(): Promise<string[]> { |
| 41 | + return new Promise<string[]>((resolve, reject) => { |
| 42 | + glob('languageServer.*', { cwd: SMOKE_TEST_EXTENSIONS_DIR }, (ex, matches) => { |
| 43 | + ex ? reject(ex) : resolve(matches.map(item => path.join(SMOKE_TEST_EXTENSIONS_DIR, item))); |
| 44 | + }); |
| 45 | + }); |
| 46 | +} |
| 47 | +export function isJediEnabled() { |
| 48 | + const resource = vscode.workspace.workspaceFolders![0].uri; |
| 49 | + const settings = vscode.workspace.getConfiguration('python', resource); |
| 50 | + return settings.get<boolean>('jediEnabled') === true; |
| 51 | +} |
| 52 | +export async function enableJedi(enable: boolean | undefined) { |
| 53 | + if (isJediEnabled() === enable) { |
| 54 | + return; |
| 55 | + } |
| 56 | + await updateSetting('jediEnabled', enable); |
| 57 | +} |
| 58 | +export async function openFileAndWaitForLS(file: string): Promise<vscode.TextDocument> { |
| 59 | + const textDocument = await vscode.workspace.openTextDocument(file); |
| 60 | + await vscode.window.showTextDocument(textDocument); |
| 61 | + assert(vscode.window.activeTextEditor, 'No active editor'); |
| 62 | + // Make sure LS completes file loading and analysis. |
| 63 | + // In test mode it awaits for the completion before trying |
| 64 | + // to fetch data for completion, hover.etc. |
| 65 | + await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, new vscode.Position(0, 0)); |
| 66 | + await waitForCondition(isLanguageServerDownloaded, 30_000, 'Language Server not downloaded'); |
| 67 | + // For for LS to get extracted. |
| 68 | + await sleep(10_000); |
| 69 | + return textDocument; |
| 70 | +} |
| 71 | + |
| 72 | +async function isLanguageServerDownloaded() { |
| 73 | + // tslint:disable-next-line:no-unnecessary-local-variable |
| 74 | + const downloaded = await getLanaguageServerFolders().then(items => items.length > 0); |
| 75 | + return downloaded; |
| 76 | +} |
0 commit comments