Skip to content

Add integration test for restore of file-based programs #8470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ To debug unit tests locally, press <kbd>F5</kbd> in VS Code with the "Launch Tes
To debug integration tests
1. Import the `csharp-test-profile.code-profile` in VSCode to setup a clean profile in which to run integration tests. This must be imported at least once to use the launch configurations (ensure the extensions are updated in the profile).
2. Open any integration test file and <kbd>F5</kbd> launch with the correct launch configuration selected.
- For integration tests inside `test/lsptoolshost`, use either `Launch Current File slnWithCsproj Integration Tests` or `[DevKit] Launch Current File slnWithCsproj Integration Tests` (to run tests using C# + C# Dev Kit)
- For integration tests inside `test/lsptoolshost`, use either `[Roslyn] Run Current File Integration Test` or `[DevKit] Launch Current File Integration Tests` (to run tests using C# + C# Dev Kit)
- For integration tests inside `test/razor`, use `[Razor] Run Current File Integration Test`
- For integration tests inside `test/omnisharp`, use one of the `Omnisharp:` current file profiles
- For integration tests inside `test/omnisharp`, use one of the `[O#] Run Current File Integration Test` current file profiles

These will allow you to actually debug the test, but the 'Razor integration tests' configuration does not.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"workspace"
],
"defaults": {
"roslyn": "5.0.0-2.25405.5",
"roslyn": "5.0.0-2.25408.10",
"omniSharp": "1.39.14",
"razor": "10.0.0-preview.25403.1",
"razorOmnisharp": "7.0.0-preview.23363.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import * as vscode from 'vscode';
import * as path from 'path';
import { describe, beforeAll, beforeEach, afterAll, test, expect, afterEach } from '@jest/globals';
import testAssetWorkspace from './testAssets/testAssetWorkspace';
import { activateCSharpExtension, closeAllEditorsAsync, openFileInWorkspaceAsync } from './integrationHelpers';
import {
activateCSharpExtension,
closeAllEditorsAsync,
getCompletionsAsync,
openFileInWorkspaceAsync,
} from './integrationHelpers';

describe(`Completion Tests`, () => {
beforeAll(async () => {
Expand Down Expand Up @@ -70,23 +75,4 @@ describe(`Completion Tests`, () => {
expect(methodOverrideLine).toContain('override void Method(NeedsImport n)');
expect(methodOverrideImplLine).toContain('base.Method(n);');
});

async function getCompletionsAsync(
position: vscode.Position,
triggerCharacter: string | undefined,
completionsToResolve: number
): Promise<vscode.CompletionList> {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
throw new Error('No active editor');
}

return await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
activeEditor.document.uri,
position,
triggerCharacter,
completionsToResolve
);
}
});
29 changes: 28 additions & 1 deletion test/lsptoolshost/integrationTests/integrationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import { ServerState } from '../../../src/lsptoolshost/server/languageServerEven
import testAssetWorkspace from './testAssets/testAssetWorkspace';
import { EOL, platform } from 'os';
import { describe, expect, test } from '@jest/globals';
import { WaitForAsyncOperationsRequest } from './testHooks';

export async function activateCSharpExtension(): Promise<void> {
export async function activateCSharpExtension(): Promise<CSharpExtensionExports> {
const csharpExtension = vscode.extensions.getExtension<CSharpExtensionExports>('ms-dotnettools.csharp');
if (!csharpExtension) {
throw new Error('Failed to find installation of ms-dotnettools.csharp');
Expand Down Expand Up @@ -53,6 +54,8 @@ export async function activateCSharpExtension(): Promise<void> {
if (shouldRestart) {
await restartLanguageServer();
}

return csharpExtension.exports;
}

export function usingDevKit(): boolean {
Expand Down Expand Up @@ -113,6 +116,25 @@ export function isSlnWithGenerator(workspace: typeof vscode.workspace) {
return isGivenSln(workspace, 'slnWithGenerator');
}

export async function getCompletionsAsync(
position: vscode.Position,
triggerCharacter: string | undefined,
completionsToResolve: number
): Promise<vscode.CompletionList> {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
throw new Error('No active editor');
}

return await vscode.commands.executeCommand(
'vscode.executeCompletionItemProvider',
activeEditor.document.uri,
position,
triggerCharacter,
completionsToResolve
);
}

export async function getCodeLensesAsync(): Promise<vscode.CodeLens[]> {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
Expand Down Expand Up @@ -299,3 +321,8 @@ function isWindows() {
function isLinux() {
return !(isMacOS() || isWindows());
}

export async function waitForAllAsyncOperationsAsync(exports: CSharpExtensionExports): Promise<void> {
const source = new vscode.CancellationTokenSource();
await exports.experimental.sendServerRequest(WaitForAsyncOperationsRequest.type, { operations: [] }, source.token);
}
58 changes: 58 additions & 0 deletions test/lsptoolshost/integrationTests/restore.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import * as path from 'path';
import testAssetWorkspace from './testAssets/testAssetWorkspace';
import {
activateCSharpExtension,
closeAllEditorsAsync,
getCompletionsAsync,
openFileInWorkspaceAsync,
revertActiveFile,
sleep,
waitForAllAsyncOperationsAsync,
waitForExpectedResult,
} from './integrationHelpers';
import { describe, beforeAll, beforeEach, afterAll, test, expect, afterEach } from '@jest/globals';
import { CSharpExtensionExports } from '../../../src/csharpExtensionExports';

describe(`Restore Tests`, () => {
let exports: CSharpExtensionExports;

beforeAll(async () => {
exports = await activateCSharpExtension();
});

beforeEach(async () => {
await openFileInWorkspaceAsync(path.join('src', 'scripts', 'app1.cs'));
});

afterEach(async () => {
await revertActiveFile();
await closeAllEditorsAsync();
});

afterAll(async () => {
await testAssetWorkspace.cleanupWorkspace();
});

test('Inserting package directive triggers a restore', async () => {
await sleep(1);
await vscode.window.activeTextEditor!.edit((editBuilder) => {
editBuilder.insert(new vscode.Position(0, 0), '#:package [email protected]');
});
await vscode.window.activeTextEditor!.document.save();
await waitForAllAsyncOperationsAsync(exports);

const position = new vscode.Position(1, 'using Newton'.length);
await waitForExpectedResult<vscode.CompletionList>(
async () => getCompletionsAsync(position, undefined, 10),
10 * 1000,
100,
(completionItems) => expect(completionItems.items.map((item) => item.label)).toContain('Newtonsoft')
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup>
<!-- Integration tests need to cleanup artifacts between runs. Putting the artifacts under 'testAssets' will ensure this happens when assets are overwritten after test run. -->
<ArtifactsPath>$(MSBuildThisFileDirectory)</ArtifactsPath>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

using Newton;

Console.WriteLine("Hello World!");
21 changes: 21 additions & 0 deletions test/lsptoolshost/integrationTests/testHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as lsp from 'vscode-languageserver-protocol';

export interface WaitForAsyncOperationsParams {
/**
* The operations to wait for.
*/
operations: string[];
}

export interface WaitForAsyncOperationsResponse {} // eslint-disable-line @typescript-eslint/no-empty-object-type

export namespace WaitForAsyncOperationsRequest {
export const method = 'workspace/waitForAsyncOperations';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
export const type = new lsp.RequestType<WaitForAsyncOperationsParams, WaitForAsyncOperationsResponse, void>(method);
}
Loading