Skip to content

Commit

Permalink
refactor - Extract the stacktrace parsing to util (#1704)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Jun 27, 2024
1 parent f343d89 commit afe9114
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 41 deletions.
48 changes: 8 additions & 40 deletions src/runners/baseRunner/RunnerResultAnalyzer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as path from 'path';
import { Location, MarkdownString, Range, TestItem } from 'vscode';
import { Location, MarkdownString, TestItem } from 'vscode';
import { IRunTestContext } from '../../types';
import { processStackTraceLine } from '../utils';

export abstract class RunnerResultAnalyzer {
constructor(protected testContext: IRunTestContext) { }
Expand All @@ -21,46 +21,14 @@ export abstract class RunnerResultAnalyzer {
}

protected processStackTrace(data: string, traces: MarkdownString, currentItem: TestItem | undefined, projectName: string): void {
const traceRegExp: RegExp = /(\s?at\s+)([\w$\\.]+\/)?((?:[\w$]+\.)+[<\w$>]+)\((.*)\)/;
const traceResults: RegExpExecArray | null = traceRegExp.exec(data);
if (traceResults) {
const fullyQualifiedName: string = traceResults[3];
if (this.isExcluded(fullyQualifiedName)) {
return;
}

const location: string = traceResults[4];
let sourceName: string | undefined;
let lineNumLiteral: string | undefined;
const locationResult: RegExpExecArray | null = /([\w-$]+\.java):(\d+)/.exec(location);
if (locationResult) {
sourceName = locationResult[1];
lineNumLiteral = locationResult[2];
}
if (this.isExcluded(data)) {
return;
}

if (!sourceName || !lineNumLiteral) {
traces.appendText(data);
} else {
const atLiteral: string = traceResults[1];
const optionalModuleName: string = traceResults[2] || '';
traces.appendText(atLiteral);
traces.appendMarkdown(`${optionalModuleName + fullyQualifiedName}([${sourceName}:${lineNumLiteral}](command:_java.test.openStackTrace?${encodeURIComponent(JSON.stringify([data, projectName]))}))`);
if (currentItem && path.basename(currentItem.uri?.fsPath || '') === sourceName) {
const lineNum: number = parseInt(lineNumLiteral, 10);
if (currentItem.uri) {
if (!currentItem.range || (currentItem.range.start.line + 1 < lineNum && currentItem.range.end.line + 1 > lineNum)) {
this.testMessageLocation = new Location(currentItem.uri, new Range(lineNum - 1, 0, lineNum, 0));
} else {
this.testMessageLocation = new Location(currentItem.uri, new Range(currentItem.range.start.line, 0, currentItem.range.start.line, 0));
}
}
}
}
} else {
// '<' & '>' will be escaped when displaying the test message, so replacing them to '[' & ']'.
traces.appendText(data.replace(/</g, '[').replace(/>/g, ']'));
const location: Location | undefined = processStackTraceLine(data, traces, currentItem, projectName);
if (location) {
this.testMessageLocation = location;
}
traces.appendMarkdown('<br/>');
}

private isExcluded(stacktrace: string): boolean {
Expand Down
51 changes: 50 additions & 1 deletion src/runners/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { Location, TestItem, TestMessage, TestRun, Uri } from 'vscode';
import { Location, MarkdownString, Range, TestItem, TestMessage, TestRun, Uri } from 'vscode';
import { JavaTestRunnerCommands } from '../constants';
import { asRange } from '../controller/utils';
import { executeJavaLanguageServerCommand } from '../utils/commandUtils';
import * as path from 'path';

export async function findTestLocation(fullName: string): Promise<Location | undefined> {
const location: any | undefined = await executeJavaLanguageServerCommand<any>(
Expand Down Expand Up @@ -52,3 +53,51 @@ export enum TestResultState {
// Test run failed for some other reason (compilation error, timeout, etc)
Errored = 6,
}

/**
* Append the line of stack trace to the traces.
* @param lineOfMessage line of stack trace.
* @param traces stack trace in markdown string.
* @param currentItem current test item.
* @param projectName project name.
*/
export function processStackTraceLine(lineOfMessage: string, traces: MarkdownString, currentItem: TestItem | undefined, projectName: string): Location | undefined {
let testMessageLocation: Location | undefined;
const traceResults: RegExpExecArray | null = /(\s?at\s+)([\w$\\.]+\/)?((?:[\w$]+\.)+[<\w$>]+)\((.*)\)/.exec(lineOfMessage);
if (traceResults) {
const fullyQualifiedName: string = traceResults[3];
const location: string = traceResults[4];
let sourceName: string | undefined;
let lineNumLiteral: string | undefined;
const locationResult: RegExpExecArray | null = /([\w-$]+\.java):(\d+)/.exec(location);
if (locationResult) {
sourceName = locationResult[1];
lineNumLiteral = locationResult[2];
}

if (!sourceName || !lineNumLiteral) {
traces.appendText(lineOfMessage);
} else {
const atLiteral: string = traceResults[1];
const optionalModuleName: string = traceResults[2] || '';
traces.appendText(atLiteral);
traces.appendMarkdown(`${optionalModuleName + fullyQualifiedName}([${sourceName}:${lineNumLiteral}](command:_java.test.openStackTrace?${encodeURIComponent(JSON.stringify([lineOfMessage, projectName]))}))`);
if (currentItem && path.basename(currentItem.uri?.fsPath || '') === sourceName) {
const lineNum: number = parseInt(lineNumLiteral, 10);
if (currentItem.uri) {
if (!currentItem.range || (currentItem.range.start.line + 1 < lineNum && currentItem.range.end.line + 1 > lineNum)) {
testMessageLocation = new Location(currentItem.uri, new Range(lineNum - 1, 0, lineNum, 0));
} else {
testMessageLocation = new Location(currentItem.uri, new Range(currentItem.range.start.line, 0, currentItem.range.start.line, 0));
}
}
}
}
} else {
// '<' & '>' will be escaped when displaying the test message, so replacing them to '[' & ']'.
traces.appendText(lineOfMessage.replace(/</g, '[').replace(/>/g, ']'));
}
traces.appendMarkdown('<br/>');

return testMessageLocation
}

0 comments on commit afe9114

Please sign in to comment.