Skip to content

Commit afe9114

Browse files
authored
refactor - Extract the stacktrace parsing to util (#1704)
1 parent f343d89 commit afe9114

File tree

2 files changed

+58
-41
lines changed

2 files changed

+58
-41
lines changed

src/runners/baseRunner/RunnerResultAnalyzer.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import * as path from 'path';
5-
import { Location, MarkdownString, Range, TestItem } from 'vscode';
4+
import { Location, MarkdownString, TestItem } from 'vscode';
65
import { IRunTestContext } from '../../types';
6+
import { processStackTraceLine } from '../utils';
77

88
export abstract class RunnerResultAnalyzer {
99
constructor(protected testContext: IRunTestContext) { }
@@ -21,46 +21,14 @@ export abstract class RunnerResultAnalyzer {
2121
}
2222

2323
protected processStackTrace(data: string, traces: MarkdownString, currentItem: TestItem | undefined, projectName: string): void {
24-
const traceRegExp: RegExp = /(\s?at\s+)([\w$\\.]+\/)?((?:[\w$]+\.)+[<\w$>]+)\((.*)\)/;
25-
const traceResults: RegExpExecArray | null = traceRegExp.exec(data);
26-
if (traceResults) {
27-
const fullyQualifiedName: string = traceResults[3];
28-
if (this.isExcluded(fullyQualifiedName)) {
29-
return;
30-
}
31-
32-
const location: string = traceResults[4];
33-
let sourceName: string | undefined;
34-
let lineNumLiteral: string | undefined;
35-
const locationResult: RegExpExecArray | null = /([\w-$]+\.java):(\d+)/.exec(location);
36-
if (locationResult) {
37-
sourceName = locationResult[1];
38-
lineNumLiteral = locationResult[2];
39-
}
24+
if (this.isExcluded(data)) {
25+
return;
26+
}
4027

41-
if (!sourceName || !lineNumLiteral) {
42-
traces.appendText(data);
43-
} else {
44-
const atLiteral: string = traceResults[1];
45-
const optionalModuleName: string = traceResults[2] || '';
46-
traces.appendText(atLiteral);
47-
traces.appendMarkdown(`${optionalModuleName + fullyQualifiedName}([${sourceName}:${lineNumLiteral}](command:_java.test.openStackTrace?${encodeURIComponent(JSON.stringify([data, projectName]))}))`);
48-
if (currentItem && path.basename(currentItem.uri?.fsPath || '') === sourceName) {
49-
const lineNum: number = parseInt(lineNumLiteral, 10);
50-
if (currentItem.uri) {
51-
if (!currentItem.range || (currentItem.range.start.line + 1 < lineNum && currentItem.range.end.line + 1 > lineNum)) {
52-
this.testMessageLocation = new Location(currentItem.uri, new Range(lineNum - 1, 0, lineNum, 0));
53-
} else {
54-
this.testMessageLocation = new Location(currentItem.uri, new Range(currentItem.range.start.line, 0, currentItem.range.start.line, 0));
55-
}
56-
}
57-
}
58-
}
59-
} else {
60-
// '<' & '>' will be escaped when displaying the test message, so replacing them to '[' & ']'.
61-
traces.appendText(data.replace(/</g, '[').replace(/>/g, ']'));
28+
const location: Location | undefined = processStackTraceLine(data, traces, currentItem, projectName);
29+
if (location) {
30+
this.testMessageLocation = location;
6231
}
63-
traces.appendMarkdown('<br/>');
6432
}
6533

6634
private isExcluded(stacktrace: string): boolean {

src/runners/utils.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { Location, TestItem, TestMessage, TestRun, Uri } from 'vscode';
4+
import { Location, MarkdownString, Range, TestItem, TestMessage, TestRun, Uri } from 'vscode';
55
import { JavaTestRunnerCommands } from '../constants';
66
import { asRange } from '../controller/utils';
77
import { executeJavaLanguageServerCommand } from '../utils/commandUtils';
8+
import * as path from 'path';
89

910
export async function findTestLocation(fullName: string): Promise<Location | undefined> {
1011
const location: any | undefined = await executeJavaLanguageServerCommand<any>(
@@ -52,3 +53,51 @@ export enum TestResultState {
5253
// Test run failed for some other reason (compilation error, timeout, etc)
5354
Errored = 6,
5455
}
56+
57+
/**
58+
* Append the line of stack trace to the traces.
59+
* @param lineOfMessage line of stack trace.
60+
* @param traces stack trace in markdown string.
61+
* @param currentItem current test item.
62+
* @param projectName project name.
63+
*/
64+
export function processStackTraceLine(lineOfMessage: string, traces: MarkdownString, currentItem: TestItem | undefined, projectName: string): Location | undefined {
65+
let testMessageLocation: Location | undefined;
66+
const traceResults: RegExpExecArray | null = /(\s?at\s+)([\w$\\.]+\/)?((?:[\w$]+\.)+[<\w$>]+)\((.*)\)/.exec(lineOfMessage);
67+
if (traceResults) {
68+
const fullyQualifiedName: string = traceResults[3];
69+
const location: string = traceResults[4];
70+
let sourceName: string | undefined;
71+
let lineNumLiteral: string | undefined;
72+
const locationResult: RegExpExecArray | null = /([\w-$]+\.java):(\d+)/.exec(location);
73+
if (locationResult) {
74+
sourceName = locationResult[1];
75+
lineNumLiteral = locationResult[2];
76+
}
77+
78+
if (!sourceName || !lineNumLiteral) {
79+
traces.appendText(lineOfMessage);
80+
} else {
81+
const atLiteral: string = traceResults[1];
82+
const optionalModuleName: string = traceResults[2] || '';
83+
traces.appendText(atLiteral);
84+
traces.appendMarkdown(`${optionalModuleName + fullyQualifiedName}([${sourceName}:${lineNumLiteral}](command:_java.test.openStackTrace?${encodeURIComponent(JSON.stringify([lineOfMessage, projectName]))}))`);
85+
if (currentItem && path.basename(currentItem.uri?.fsPath || '') === sourceName) {
86+
const lineNum: number = parseInt(lineNumLiteral, 10);
87+
if (currentItem.uri) {
88+
if (!currentItem.range || (currentItem.range.start.line + 1 < lineNum && currentItem.range.end.line + 1 > lineNum)) {
89+
testMessageLocation = new Location(currentItem.uri, new Range(lineNum - 1, 0, lineNum, 0));
90+
} else {
91+
testMessageLocation = new Location(currentItem.uri, new Range(currentItem.range.start.line, 0, currentItem.range.start.line, 0));
92+
}
93+
}
94+
}
95+
}
96+
} else {
97+
// '<' & '>' will be escaped when displaying the test message, so replacing them to '[' & ']'.
98+
traces.appendText(lineOfMessage.replace(/</g, '[').replace(/>/g, ']'));
99+
}
100+
traces.appendMarkdown('<br/>');
101+
102+
return testMessageLocation
103+
}

0 commit comments

Comments
 (0)