diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 51fc79f9d1..04c545db45 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -64,9 +64,9 @@ To debug unit tests locally, press F5 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 F5 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. diff --git a/package.json b/package.json index 8906a6459d..99b92800ad 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/lsptoolshost/integrationTests/completion.integration.test.ts b/test/lsptoolshost/integrationTests/completion.integration.test.ts index fa3a150082..4924a9a99a 100644 --- a/test/lsptoolshost/integrationTests/completion.integration.test.ts +++ b/test/lsptoolshost/integrationTests/completion.integration.test.ts @@ -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 () => { @@ -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 { - 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 - ); - } }); diff --git a/test/lsptoolshost/integrationTests/integrationHelpers.ts b/test/lsptoolshost/integrationTests/integrationHelpers.ts index b4e2e8ea90..5af4b49242 100644 --- a/test/lsptoolshost/integrationTests/integrationHelpers.ts +++ b/test/lsptoolshost/integrationTests/integrationHelpers.ts @@ -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 { +export async function activateCSharpExtension(): Promise { const csharpExtension = vscode.extensions.getExtension('ms-dotnettools.csharp'); if (!csharpExtension) { throw new Error('Failed to find installation of ms-dotnettools.csharp'); @@ -53,6 +54,8 @@ export async function activateCSharpExtension(): Promise { if (shouldRestart) { await restartLanguageServer(); } + + return csharpExtension.exports; } export function usingDevKit(): boolean { @@ -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 { + 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 { const activeEditor = vscode.window.activeTextEditor; if (!activeEditor) { @@ -299,3 +321,8 @@ function isWindows() { function isLinux() { return !(isMacOS() || isWindows()); } + +export async function waitForAllAsyncOperationsAsync(exports: CSharpExtensionExports): Promise { + const source = new vscode.CancellationTokenSource(); + await exports.experimental.sendServerRequest(WaitForAsyncOperationsRequest.type, { operations: [] }, source.token); +} diff --git a/test/lsptoolshost/integrationTests/restore.integration.test.ts b/test/lsptoolshost/integrationTests/restore.integration.test.ts new file mode 100644 index 0000000000..b85c370540 --- /dev/null +++ b/test/lsptoolshost/integrationTests/restore.integration.test.ts @@ -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 Newtonsoft.Json@13.0.3'); + }); + await vscode.window.activeTextEditor!.document.save(); + await waitForAllAsyncOperationsAsync(exports); + + const position = new vscode.Position(1, 'using Newton'.length); + await waitForExpectedResult( + async () => getCompletionsAsync(position, undefined, 10), + 10 * 1000, + 100, + (completionItems) => expect(completionItems.items.map((item) => item.label)).toContain('Newtonsoft') + ); + }); +}); diff --git a/test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/scripts/Directory.Build.props b/test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/scripts/Directory.Build.props new file mode 100644 index 0000000000..028c8dee38 --- /dev/null +++ b/test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/scripts/Directory.Build.props @@ -0,0 +1,6 @@ + + + + $(MSBuildThisFileDirectory) + + \ No newline at end of file diff --git a/test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/scripts/app1.cs b/test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/scripts/app1.cs new file mode 100644 index 0000000000..7659398405 --- /dev/null +++ b/test/lsptoolshost/integrationTests/testAssets/slnWithCsproj/src/scripts/app1.cs @@ -0,0 +1,4 @@ + +using Newton; + +Console.WriteLine("Hello World!"); diff --git a/test/lsptoolshost/integrationTests/testHooks.ts b/test/lsptoolshost/integrationTests/testHooks.ts new file mode 100644 index 0000000000..3fd6936114 --- /dev/null +++ b/test/lsptoolshost/integrationTests/testHooks.ts @@ -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(method); +}