Skip to content

Commit 48512db

Browse files
committed
first draft to deduplicate diff test messages
1 parent 85840f6 commit 48512db

File tree

5 files changed

+31
-34
lines changed

5 files changed

+31
-34
lines changed

src/runners/baseRunner/RunnerResultAnalyzer.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
// Licensed under the MIT license.
33

44
import * as path from 'path';
5-
import { Location, MarkdownString, Range, TestItem, TestMessage } from 'vscode';
5+
import { Location, MarkdownString, Range, TestItem } from 'vscode';
66
import { IRunTestContext } from '../../types';
7-
import { setTestState, TestResultState } from '../utils';
87

98
export abstract class RunnerResultAnalyzer {
109
constructor(protected testContext: IRunTestContext) { }
@@ -21,7 +20,7 @@ export abstract class RunnerResultAnalyzer {
2120
return [];
2221
}
2322

24-
protected processStackTrace(data: string, traces: MarkdownString, assertionFailure: TestMessage | undefined, currentItem: TestItem | undefined, projectName: string): void {
23+
protected processStackTrace(data: string, traces: MarkdownString, currentItem: TestItem | undefined, projectName: string): void {
2524
const traceRegExp: RegExp = /(\s?at\s+)([\w$\\.]+\/)?((?:[\w$]+\.)+[<\w$>]+)\((.*)\)/;
2625
const traceResults: RegExpExecArray | null = traceRegExp.exec(data);
2726
if (traceResults) {
@@ -55,10 +54,6 @@ export abstract class RunnerResultAnalyzer {
5554
this.testMessageLocation = new Location(currentItem.uri, new Range(currentItem.range.start.line, 0, currentItem.range.start.line, 0));
5655
}
5756
}
58-
if (assertionFailure) {
59-
assertionFailure.location = this.testMessageLocation;
60-
setTestState(this.testContext.testRun, currentItem, TestResultState.Failed, assertionFailure);
61-
}
6257
}
6358
}
6459
} else {

src/runners/junitRunner/JUnitRunnerResultAnalyzer.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,17 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
107107
if (!this.tracingItem) {
108108
return;
109109
}
110-
const testMessage: TestMessage = new TestMessage(this.traces);
111110
const currentResultState: TestResultState = this.getCurrentState(this.tracingItem).resultState;
112-
this.tryAppendMessage(this.tracingItem, testMessage, currentResultState);
113-
this.recordingType = RecordingType.None;
111+
if (this.assertionFailure) {
112+
this.tryAppendMessage(this.tracingItem, this.assertionFailure, currentResultState);
113+
}
114+
if (this.traces?.value) {
115+
this.tryAppendMessage(this.tracingItem, new TestMessage(this.traces), currentResultState);
116+
}
114117
if (currentResultState === TestResultState.Errored) {
115118
setTestState(this.testContext.testRun, this.tracingItem, currentResultState);
116119
}
120+
this.recordingType = RecordingType.None;
117121
} else if (data.startsWith(MessageId.ExpectStart)) {
118122
this.recordingType = RecordingType.ExpectMessage;
119123
} else if (data.startsWith(MessageId.ExpectEnd)) {
@@ -139,8 +143,7 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
139143
this.assertionFailure = TestMessage.diff(`Expected [${assertionResults[1]}] but was [${assertionResults[2]}]`, assertionResults[1], assertionResults[2]);
140144
}
141145
}
142-
143-
this.processStackTrace(data, this.traces, this.assertionFailure, this.tracingItem, this.projectName);
146+
this.processStackTrace(data, this.traces, this.tracingItem, this.projectName);
144147
}
145148
}
146149

@@ -218,6 +221,7 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
218221
this.expectString = '';
219222
this.actualString = '';
220223
this.recordingType = RecordingType.None;
224+
this.testMessageLocation = undefined;
221225
}
222226

223227
protected getStacktraceFilter(): string[] {
@@ -335,7 +339,6 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
335339
private async tryAppendMessage(item: TestItem, testMessage: TestMessage, testState: TestResultState): Promise<void> {
336340
if (this.testMessageLocation) {
337341
testMessage.location = this.testMessageLocation;
338-
this.testMessageLocation = undefined;
339342
} else if (item.uri && item.range) {
340343
testMessage.location = new Location(item.uri, item.range);
341344
} else {

src/runners/testngRunner/TestNGRunnerResultAnalyzer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class TestNGRunnerResultAnalyzer extends RunnerResultAnalyzer {
4545
// tslint:disable-next-line: no-conditional-assignment
4646
while ((match = this.regex.exec(data)) !== null) {
4747
try {
48-
this.processData(match[1]);
48+
this.processData(match[1]);
4949
} catch (error) {
5050
this.testContext.testRun.appendOutput(`[ERROR] Failed to parse output data: ${match[1]}\n`);
5151
}
@@ -80,7 +80,7 @@ export class TestNGRunnerResultAnalyzer extends RunnerResultAnalyzer {
8080
markdownTrace.supportHtml = true;
8181

8282
for (const line of outputData.attributes.trace.split(/\r?\n/)) {
83-
this.processStackTrace(line, markdownTrace, undefined, this.currentItem, this.projectName);
83+
this.processStackTrace(line, markdownTrace, this.currentItem, this.projectName);
8484
}
8585

8686
const testMessage: TestMessage = new TestMessage(markdownTrace);

test/suite/JUnitAnalyzer.test.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import * as assert from 'assert';
77
import * as sinon from 'sinon';
8-
import { IRunTestContext, TestKind } from '../../src/types';
98
import { MarkdownString, Range, TestController, TestMessage, TestRunRequest, tests, workspace } from 'vscode';
109
import { JUnitRunnerResultAnalyzer } from '../../src/runners/junitRunner/JUnitRunnerResultAnalyzer';
10+
import { IRunTestContext, TestKind } from '../../src/types';
1111
import { generateTestItem } from './utils';
1212

1313
// tslint:disable: only-arrow-functions
@@ -146,7 +146,7 @@ java.lang.AssertionError: expected:<1> but was:<2>
146146
analyzer.analyzeData(testRunnerOutput);
147147

148148
sinon.assert.calledWith(failedSpy, testItem, sinon.match.any, sinon.match.number);
149-
const testMessage = failedSpy.getCall(0).args[1] as TestMessage;
149+
const testMessage = failedSpy.getCall(1).args[1] as TestMessage;
150150
const stringLiteral = (testMessage.message as MarkdownString).value;
151151
assert.ok(stringLiteral.split('<br/>').length === 3);
152152
});
@@ -338,7 +338,8 @@ org.junit.ComparisonFailure: expected:<hello
338338
});
339339

340340
test("test diff is not duplicated when failing assertion is extracted", () => {
341-
const testItem = generateTestItem(testController, 'junit@junit5.TestWithExtractedEqualityAssertion#test', TestKind.JUnit5, new Range(2, 0, 2, 0), undefined, 'TestWithExtractedEqualityAssertion.java');
341+
const range = new Range(9, 0, 11, 0);
342+
const testItem = generateTestItem(testController, 'junit@junit5.TestWithExtractedEqualityAssertion#test', TestKind.JUnit5, range, undefined, 'TestWithExtractedEqualityAssertion.java');
342343
const testRunRequest = new TestRunRequest([testItem], []);
343344
const testRun = testController.createTestRun(testRunRequest);
344345
const startedSpy = sinon.spy(testRun, 'started');
@@ -356,16 +357,9 @@ org.junit.ComparisonFailure: expected:<hello
356357
%ACTUALE
357358
%TRACES
358359
org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
359-
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
360-
at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
361-
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
362-
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
363-
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:510)
364360
at junit5.TestWithExtractedEqualityAssertion.extracted2(TestWithExtractedEqualityAssertion.java:18)
365361
at junit5.TestWithExtractedEqualityAssertion.extracted1(TestWithExtractedEqualityAssertion.java:14)
366-
at junit5.TestWithExtractedEqualityAssertion.test(TestWithExtractedEqualityAssertion.java:10)
367-
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
368-
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
362+
at junit5.TestWithExtractedEqualityAssertion.test(TestWithExtractedEqualityAssertion.java:11)
369363
%TRACEE
370364
%TESTE 3,test(junit5.TestWithExtractedEqualityAssertion)
371365
%RUNTIME55`;
@@ -384,16 +378,12 @@ org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
384378
sinon.assert.calledWith(startedSpy, testItem);
385379
sinon.assert.calledWith(failedSpy, testItem, sinon.match.any);
386380

387-
const testMessage = failedSpy.getCall(0).args[1] as TestMessage;
381+
const diffTestMessages = failedSpy.getCalls().map(call => call.args[1] as TestMessage).filter(v => v.actualOutput || v.expectedOutput);
382+
assert.strictEqual(diffTestMessages.length, 1, "not more than one diff-message");
383+
const testMessage = diffTestMessages[0];
388384
assert.strictEqual(testMessage.expectedOutput, '1');
389385
assert.strictEqual(testMessage.actualOutput, '2');
390-
assert.strictEqual(testMessage.location?.range.start.line, 2);
391-
392-
const testMessages = failedSpy.getCalls().map(call => call.args[1] as TestMessage).filter(v => v.actualOutput || v.expectedOutput);
393-
394-
assert.strictEqual(testMessages.length, 1, "not more than one diff-message");
395-
// assert.strictEqual(testMessages[0].location?.range.start, 10); // todo
386+
assert.strictEqual(testMessage.location?.range.start.line, 10); // =11 - 1, (most precise info we get from the stack trace)
396387
});
397388

398-
399389
});

test/test-projects/junit/src/test/java/junit5/TestWithExtractedEqualityAssertion.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@ class TestWithExtractedEqualityAssertion {
77

88
@Test
99
void test() {
10+
}
11+
12+
@Test
13+
void test1() {
1014
extracted1();
1115
}
1216

17+
@Test
18+
void test2() {
19+
extracted2();
20+
}
21+
1322
private void extracted1() {
1423
extracted2();
1524
}

0 commit comments

Comments
 (0)