Skip to content
Closed
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
1 change: 1 addition & 0 deletions news/3 Code Health/3684.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed language server smoke tests.
106 changes: 0 additions & 106 deletions src/test/smoke/_run_first_msLanguageServer.smoke.test.ts

This file was deleted.

76 changes: 76 additions & 0 deletions src/test/smoke/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

// tslint:disable:no-any no-invalid-this no-default-export no-console

import * as assert from 'assert';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import * as path from 'path';
import * as vscode from 'vscode';
import { waitForCondition } from '../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST, SMOKE_TEST_EXTENSIONS_DIR } from '../constants';
import { noop, sleep } from '../core';
import { initialize } from '../initialize';

let initialized = false;
const fileDefinitions = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'smokeTests', 'definitions.py');

export async function initializeSmokeTests() {
if (!IS_SMOKE_TEST || initialized) {
return;
}
await removeLanguageServerFiles();
await enableJedi(false);
await initialize();
await openFileAndWaitForLS(fileDefinitions);
initialized = true;
}

export async function updateSetting(setting: string, value: any) {
const resource = vscode.workspace.workspaceFolders![0].uri;
await vscode.workspace.getConfiguration('python', resource).update(setting, value, vscode.ConfigurationTarget.WorkspaceFolder);
}
export async function removeLanguageServerFiles() {
const folders = await getLanaguageServerFolders();
await Promise.all(folders.map(item => fs.remove(item).catch(noop)));
}
async function getLanaguageServerFolders(): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
glob('languageServer.*', { cwd: SMOKE_TEST_EXTENSIONS_DIR }, (ex, matches) => {
ex ? reject(ex) : resolve(matches.map(item => path.join(SMOKE_TEST_EXTENSIONS_DIR, item)));
});
});
}
export function isJediEnabled() {
const resource = vscode.workspace.workspaceFolders![0].uri;
const settings = vscode.workspace.getConfiguration('python', resource);
return settings.get<boolean>('jediEnabled') === true;
}
export async function enableJedi(enable: boolean | undefined) {
if (isJediEnabled() === enable) {
return;
}
await updateSetting('jediEnabled', enable);
}
export async function openFileAndWaitForLS(file: string): Promise<vscode.TextDocument> {
const textDocument = await vscode.workspace.openTextDocument(file);
await vscode.window.showTextDocument(textDocument);
assert(vscode.window.activeTextEditor, 'No active editor');
// Make sure LS completes file loading and analysis.
// In test mode it awaits for the completion before trying
// to fetch data for completion, hover.etc.
await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', textDocument.uri, new vscode.Position(0, 0));
await waitForCondition(isLanguageServerDownloaded, 30_000, 'Language Server not downloaded');
// For for LS to get extracted.
await sleep(10_000);
return textDocument;
}

async function isLanguageServerDownloaded() {
// tslint:disable-next-line:no-unnecessary-local-variable
const downloaded = await getLanaguageServerFolders().then(items => items.length > 0);
return downloaded;
}
4 changes: 3 additions & 1 deletion src/test/smoke/debugger.smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import * as vscode from 'vscode';
import { openFile, waitForCondition } from '../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST } from '../constants';
import { closeActiveWindows, initializeTest } from '../initialize';
import { initializeSmokeTests } from './common';

suite('Smoke Test: Debug file', function () {
// Large value to allow for LS to get downloaded.
this.timeout(4 * 60_000);

suiteSetup(function () {
suiteSetup(async function () {
if (!IS_SMOKE_TEST) {
return this.skip();
}
await initializeSmokeTests();
});
setup(initializeTest);
suiteTeardown(closeActiveWindows);
Expand Down
61 changes: 61 additions & 0 deletions src/test/smoke/msLanguageServer.smoke.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

// tslint:disable:max-func-body-length no-invalid-this no-any

import * as assert from 'assert';
import { expect } from 'chai';
import * as path from 'path';
import * as vscode from 'vscode';
import { updateSetting } from '../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST } from '../constants';
import { sleep } from '../core';
import { closeActiveWindows, initializeTest } from '../initialize';
import { enableJedi, initializeSmokeTests, openFileAndWaitForLS } from './common';

const fileDefinitions = path.join(EXTENSION_ROOT_DIR_FOR_TESTS, 'src', 'testMultiRootWkspc', 'smokeTests', 'definitions.py');

suite('Smoke Test: Language Server', function () {
// Large value to allow for LS to get downloaded.
this.timeout(4 * 60000);

suiteSetup(async function () {
if (!IS_SMOKE_TEST) {
return this.skip();
}
await updateSetting('linting.ignorePatterns', ['**/dir1/**'], vscode.workspace.workspaceFolders![0].uri, vscode.ConfigurationTarget.WorkspaceFolder);
await initializeSmokeTests();
});
setup(async () => {
await initializeTest();
await closeActiveWindows();
});
suiteTeardown(async () => {
await enableJedi(undefined);
await closeActiveWindows();
await updateSetting('linting.ignorePatterns', undefined, vscode.workspace.workspaceFolders![0].uri, vscode.ConfigurationTarget.WorkspaceFolder);
});
teardown(closeActiveWindows);

test('Definitions', async () => {
const startPosition = new vscode.Position(13, 6);
const textDocument = await openFileAndWaitForLS(fileDefinitions);
let tested = false;
for (let i = 0; i < 5; i += 1) {
const locations = await vscode.commands.executeCommand<vscode.Location[]>('vscode.executeDefinitionProvider', textDocument.uri, startPosition);
if (locations && locations.length > 0) {
expect(locations![0].uri.fsPath).to.contain(path.basename(fileDefinitions));
tested = true;
break;
} else {
// Wait for LS to start.
await sleep(5_000);
}
}
if (!tested) {
assert.fail('Failled to test definitions');
}
});
});
4 changes: 3 additions & 1 deletion src/test/smoke/runInTerminal.smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import * as vscode from 'vscode';
import { openFile, waitForCondition } from '../common';
import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST } from '../constants';
import { closeActiveWindows, initializeTest } from '../initialize';
import { initializeSmokeTests } from './common';

suite('Smoke Test: Run Python File In Terminal', function () {
// Large value to allow for LS to get downloaded.
this.timeout(4 * 60_000);

suiteSetup(function () {
suiteSetup(async function () {
if (!IS_SMOKE_TEST) {
return this.skip();
}
await initializeSmokeTests();
});
setup(initializeTest);
suiteTeardown(closeActiveWindows);
Expand Down