Skip to content

Commit

Permalink
Merge pull request #5965 from dibarbet/debug_tests
Browse files Browse the repository at this point in the history
Implement client side of test debugging
  • Loading branch information
dibarbet authored Aug 1, 2023
2 parents 70d7e85 + b02fdf6 commit e233682
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"defaults": {
"roslyn": "4.8.0-1.23372.1",
"roslyn": "4.8.0-1.23381.8",
"omniSharp": "1.39.7",
"razor": "7.0.0-preview.23328.2",
"razorOmnisharp": "7.0.0-preview.23363.1"
Expand Down
21 changes: 21 additions & 0 deletions src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ export class RoslynLanguageServer {

// Register Razor dynamic file info handling
this.registerDynamicFileInfo(this._languageClient);

this.registerDebuggerAttach(this._languageClient);
}

public async stop(): Promise<void> {
Expand Down Expand Up @@ -488,6 +490,25 @@ export class RoslynLanguageServer {
);
}

private registerDebuggerAttach(client: RoslynLanguageClient) {
client.onRequest<RoslynProtocol.DebugAttachParams, RoslynProtocol.DebugAttachResult, void>(
RoslynProtocol.DebugAttachRequest.type,
async (request) => {
const debugConfiguration: vscode.DebugConfiguration = {
name: '.NET Core Attach',
type: 'coreclr',
request: 'attach',
processId: request.processId,
};

const result = await vscode.debug.startDebugging(undefined, debugConfiguration, undefined);
return {
didAttach: result,
};
}
);
}

private registerExtensionsChanged(languageClient: RoslynLanguageClient) {
// subscribe to extension change events so that we can get notified if C# Dev Kit is added/removed later.
languageClient.addDisposable(
Expand Down
19 changes: 19 additions & 0 deletions src/lsptoolshost/roslynProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export interface RunTestsParams extends lsp.WorkDoneProgressParams, lsp.PartialR
* Note that this does not have to only include tests, for example this could be a range representing a class.
*/
range: lsp.Range;

/**
* Whether the request should attempt to call back to the client to attach a debugger before running the tests.
*/
attachDebugger: boolean;
}

export interface TestProgress {
Expand Down Expand Up @@ -103,6 +108,14 @@ export interface RunTestsPartialResult {
progress?: TestProgress;
}

export interface DebugAttachParams {
processId: number;
}

export interface DebugAttachResult {
didAttach: boolean;
}

export namespace WorkspaceDebugConfigurationRequest {
export const method = 'workspace/debugConfiguration';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.clientToServer;
Expand Down Expand Up @@ -140,3 +153,9 @@ export namespace RunTestsRequest {
void
>(method);
}

export namespace DebugAttachRequest {
export const method = 'workspace/attachDebugger';
export const messageDirection: lsp.MessageDirection = lsp.MessageDirection.serverToClient;
export const type = new lsp.RequestType<DebugAttachParams, DebugAttachResult, void>(method);
}
16 changes: 14 additions & 2 deletions src/lsptoolshost/unitTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,31 @@ export function registerUnitTestingCommands(
context.subscriptions.push(
vscode.commands.registerTextEditorCommand(
'dotnet.test.runTestsInContext',
async (textEditor: vscode.TextEditor) => runTestsInContext(textEditor, languageServer, dotnetTestChannel)
async (textEditor: vscode.TextEditor) => {
return runTestsInContext(false, textEditor, languageServer, dotnetTestChannel);
}
)
);

context.subscriptions.push(
vscode.commands.registerTextEditorCommand(
'dotnet.test.debugTestsInContext',
async (textEditor: vscode.TextEditor) => {
return runTestsInContext(true, textEditor, languageServer, dotnetTestChannel);
}
)
);
}

async function runTestsInContext(
debug: boolean,
textEditor: vscode.TextEditor,
languageServer: RoslynLanguageServer,
dotnetTestChannel: vscode.OutputChannel
) {
const contextRange: languageClient.Range = { start: textEditor.selection.active, end: textEditor.selection.active };
const textDocument: languageClient.TextDocumentIdentifier = { uri: textEditor.document.fileName };
const request: RunTestsParams = { textDocument: textDocument, range: contextRange };
const request: RunTestsParams = { textDocument: textDocument, range: contextRange, attachDebugger: debug };
await runTests(request, languageServer, dotnetTestChannel);
}

Expand Down

0 comments on commit e233682

Please sign in to comment.