Skip to content

Commit 1094b3c

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Prepare static error test updater tool to handle web tests.
This doesn't actually run DDC to generate the web errors yet, but it changes the CLI in anticipation of that, and adds tests to verify that once web errors are reported that the updater handles them correctly. Change-Id: I31264e3d468969b07f9eb60353a9b02a93bec7ea Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/155102 Commit-Queue: Bob Nystrom <[email protected]> Reviewed-by: Srujan Gaddam <[email protected]> Auto-Submit: Bob Nystrom <[email protected]>
1 parent b258585 commit 1094b3c

File tree

3 files changed

+252
-113
lines changed

3 files changed

+252
-113
lines changed

pkg/test_runner/lib/src/update_errors.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ final _lineCommentRegExp = RegExp(r"^\s*//");
1212
/// Removes existing static error marker comments in [source] and adds markers
1313
/// for the given [errors].
1414
///
15-
/// If [removeAnalyzer] is `false`, then existing analyzer errors in [source]
16-
/// are preserved. Likewise for [removeCfe] and CFE errors.
15+
/// If [remove] is not `null`, then only removes existing errors for the given
16+
/// sources.
1717
String updateErrorExpectations(String source, List<StaticError> errors,
18-
{bool removeAnalyzer, bool removeCfe}) {
19-
removeAnalyzer ??= true;
20-
removeCfe ??= true;
18+
{Set<ErrorSource> remove}) {
19+
remove ??= {};
2120

2221
var existingErrors = StaticError.parseExpectations(source);
2322
var lines = source.split("\n");
@@ -38,13 +37,9 @@ String updateErrorExpectations(String source, List<StaticError> errors,
3837
}
3938

4039
// Re-add errors for the portions we intend to preserve.
41-
var keepAnalyzer = !removeAnalyzer && error.hasError(ErrorSource.analyzer);
42-
var keepCfe = !removeCfe && error.hasError(ErrorSource.cfe);
43-
4440
var keptErrors = {
45-
if (keepAnalyzer)
46-
ErrorSource.analyzer: error.errorFor(ErrorSource.analyzer),
47-
if (keepCfe) ErrorSource.cfe: error.errorFor(ErrorSource.cfe),
41+
for (var source in ErrorSource.all.toSet().difference(remove))
42+
if (error.hasError(source)) source: error.errorFor(source)
4843
};
4944

5045
if (keptErrors.isNotEmpty) {

pkg/test_runner/test/update_errors_test.dart

Lines changed: 183 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
import 'dart:math' as math;
45

56
import 'package:expect/expect.dart';
67

@@ -16,7 +17,7 @@ import 'utils.dart';
1617
// escapes here like `\/`.
1718

1819
void main() {
19-
// Inserts analyzer, CFE, and both errors.
20+
// Inserts single-front end errors.
2021
expectUpdate("""
2122
int i = "bad";
2223
@@ -26,32 +27,84 @@ int third = "boo";
2627
""", errors: [
2728
makeError(line: 1, column: 9, length: 5, analyzerError: "some.error"),
2829
makeError(line: 3, column: 15, length: 7, cfeError: "Bad."),
30+
makeError(line: 5, column: 13, length: 5, webError: "Web.\nError."),
31+
], expected: """
32+
int i = "bad";
33+
/\/ ^^^^^
34+
/\/ [analyzer] some.error
35+
36+
int another = "wrong";
37+
/\/ ^^^^^^^
38+
/\/ [cfe] Bad.
39+
40+
int third = "boo";
41+
/\/ ^^^^^
42+
/\/ [web] Web.
43+
/\/ Error.
44+
""");
45+
46+
// Inserts errors for multiple front ends.
47+
expectUpdate("""
48+
int i = "bad";
49+
50+
int another = "wrong";
51+
52+
int third = "boo";
53+
54+
int last = "oops";
55+
""", errors: [
56+
makeError(
57+
line: 1,
58+
column: 9,
59+
length: 5,
60+
analyzerError: "some.error",
61+
cfeError: "Bad."),
62+
makeError(
63+
line: 3,
64+
column: 15,
65+
length: 7,
66+
cfeError: "Another bad.",
67+
webError: "Web.\nError."),
2968
makeError(
3069
line: 5,
3170
column: 13,
3271
length: 5,
33-
analyzerError: "an.error",
34-
cfeError: "Wrong.\nLine.\nAnother."),
72+
analyzerError: "third.error",
73+
webError: "Web error."),
74+
makeError(
75+
line: 7,
76+
column: 12,
77+
length: 6,
78+
analyzerError: "last.error",
79+
cfeError: "Final.\nError.",
80+
webError: "Web error."),
3581
], expected: """
3682
int i = "bad";
3783
/\/ ^^^^^
3884
/\/ [analyzer] some.error
85+
/\/ [cfe] Bad.
3986
4087
int another = "wrong";
4188
/\/ ^^^^^^^
42-
/\/ [cfe] Bad.
89+
/\/ [cfe] Another bad.
90+
/\/ [web] Web.
91+
/\/ Error.
4392
4493
int third = "boo";
4594
/\/ ^^^^^
46-
/\/ [analyzer] an.error
47-
/\/ [cfe] Wrong.
48-
/\/ Line.
49-
/\/ Another.
95+
/\/ [analyzer] third.error
96+
/\/ [web] Web error.
97+
98+
int last = "oops";
99+
/\/ ^^^^^^
100+
/\/ [analyzer] last.error
101+
/\/ [cfe] Final.
102+
/\/ Error.
103+
/\/ [web] Web error.
50104
""");
51105

52106
// Removes only analyzer errors.
53-
expectUpdate(
54-
"""
107+
expectUpdate("""
55108
int i = "bad";
56109
/\/ ^^^^^
57110
/\/ [analyzer] some.error
@@ -64,9 +117,7 @@ int third = "boo";
64117
/\/ ^^^^^
65118
/\/ [analyzer] an.error
66119
/\/ [cfe] Wrong.
67-
""",
68-
removeCfe: false,
69-
expected: """
120+
""", remove: {ErrorSource.analyzer}, expected: """
70121
int i = "bad";
71122
72123
int another = "wrong";
@@ -79,8 +130,7 @@ int third = "boo";
79130
""");
80131

81132
// Removes only CFE errors.
82-
expectUpdate(
83-
"""
133+
expectUpdate("""
84134
int i = "bad";
85135
/\/ ^^^^^
86136
/\/ [analyzer] some.error
@@ -93,9 +143,7 @@ int third = "boo";
93143
/\/ ^^^^^
94144
/\/ [analyzer] an.error
95145
/\/ [cfe] Wrong.
96-
""",
97-
removeAnalyzer: false,
98-
expected: """
146+
""", remove: {ErrorSource.cfe}, expected: """
99147
int i = "bad";
100148
/\/ ^^^^^
101149
/\/ [analyzer] some.error
@@ -105,6 +153,60 @@ int another = "wrong";
105153
int third = "boo";
106154
/\/ ^^^^^
107155
/\/ [analyzer] an.error
156+
""");
157+
158+
// Removes only web errors.
159+
expectUpdate("""
160+
int i = "bad";
161+
/\/ ^^^^^
162+
/\/ [analyzer] some.error
163+
164+
int another = "wrong";
165+
/\/ ^^^^^^^
166+
/\/ [web] Bad.
167+
168+
int third = "boo";
169+
/\/ ^^^^^
170+
/\/ [cfe] Error.
171+
/\/ [web] Wrong.
172+
""", remove: {ErrorSource.web}, expected: """
173+
int i = "bad";
174+
/\/ ^^^^^
175+
/\/ [analyzer] some.error
176+
177+
int another = "wrong";
178+
179+
int third = "boo";
180+
/\/ ^^^^^
181+
/\/ [cfe] Error.
182+
""");
183+
184+
// Removes multiple error sources.
185+
expectUpdate("""
186+
int i = "bad";
187+
/\/ ^^^^^
188+
/\/ [analyzer] some.error
189+
/\/ [cfe] CFE error.
190+
191+
int another = "wrong";
192+
/\/ ^^^^^^^
193+
/\/ [web] Bad.
194+
195+
int third = "boo";
196+
/\/ ^^^^^
197+
/\/ [analyzer] another.error
198+
/\/ [cfe] Error.
199+
/\/ [web] Wrong.
200+
""", remove: {ErrorSource.analyzer, ErrorSource.web}, expected: """
201+
int i = "bad";
202+
/\/ ^^^^^
203+
/\/ [cfe] CFE error.
204+
205+
int another = "wrong";
206+
207+
int third = "boo";
208+
/\/ ^^^^^
209+
/\/ [cfe] Error.
108210
""");
109211

110212
// Preserves previous error's indentation if possible.
@@ -303,8 +405,7 @@ x
303405

304406
void regression() {
305407
// https://github.com/dart-lang/sdk/issues/37990.
306-
expectUpdate(
307-
"""
408+
expectUpdate("""
308409
int get xx => 3;
309410
int get yy => 3;
310411
@@ -325,20 +426,16 @@ class A {
325426
326427
}
327428
}
328-
""",
329-
removeAnalyzer: false,
330-
errors: [
331-
makeError(
332-
line: 6,
333-
column: 5,
334-
length: 14,
335-
cfeError: "Setter not found: 'xx'."),
336-
makeError(
337-
line: 16,
338-
column: 7,
339-
cfeError: "The method 'call' isn't defined for the class 'int'.")
340-
],
341-
expected: """
429+
""", remove: {
430+
ErrorSource.cfe
431+
}, errors: [
432+
makeError(
433+
line: 6, column: 5, length: 14, cfeError: "Setter not found: 'xx'."),
434+
makeError(
435+
line: 16,
436+
column: 7,
437+
cfeError: "The method 'call' isn't defined for the class 'int'.")
438+
], expected: """
342439
int get xx => 3;
343440
int get yy => 3;
344441
@@ -362,18 +459,62 @@ class A {
362459
}
363460

364461
void expectUpdate(String original,
365-
{List<StaticError> errors,
366-
bool removeAnalyzer = true,
367-
bool removeCfe = true,
368-
String expected}) {
462+
{List<StaticError> errors, Set<ErrorSource> remove, String expected}) {
369463
errors ??= const [];
464+
remove ??= ErrorSource.all.toSet();
370465

371-
var actual = updateErrorExpectations(original, errors,
372-
removeAnalyzer: removeAnalyzer, removeCfe: removeCfe);
466+
var actual = updateErrorExpectations(original, errors, remove: remove);
373467
if (actual != expected) {
374468
// Not using Expect.equals() because the diffs it shows aren't helpful for
375469
// strings this large.
376-
Expect.fail("Output did not match expectation. Expected:\n$expected"
377-
"\n\nWas:\n$actual");
470+
var actualLines = actual.split("\n");
471+
var expectedLines = expected.split("\n");
472+
473+
// Figure out which leading lines do match so we can ignore those and
474+
// highlight the offending ones.
475+
var matchingActual = <int>{};
476+
var matchingExpected = <int>{};
477+
for (var i = 0;
478+
i < math.min(actualLines.length, expectedLines.length);
479+
i++) {
480+
if (actualLines[i] != expectedLines[i]) break;
481+
matchingActual.add(i);
482+
matchingExpected.add(i);
483+
}
484+
485+
// Find which trailing lines are the same so we can hide those too.
486+
for (var i = 0;
487+
i < math.min(actualLines.length, expectedLines.length);
488+
i++) {
489+
// Count backwards from the ends of each list.
490+
var actualIndex = actualLines.length - i - 1;
491+
var expectedIndex = expectedLines.length - i - 1;
492+
if (actualLines[actualIndex] != expectedLines[expectedIndex]) break;
493+
matchingActual.add(actualIndex);
494+
matchingExpected.add(expectedIndex);
495+
}
496+
497+
var buffer = StringBuffer();
498+
void writeLine(int index, String line, Set<int> matchingLines) {
499+
// Only show the line if it was different from the expectation.
500+
if (matchingLines.contains(index)) {
501+
buffer.writeln(" : $line");
502+
} else {
503+
buffer.writeln("${(index + 1).toString().padLeft(4)}: $line");
504+
}
505+
}
506+
507+
buffer.writeln("Output did not match expectation. Expected:");
508+
for (var i = 0; i < expectedLines.length; i++) {
509+
writeLine(i, expectedLines[i], matchingExpected);
510+
}
511+
512+
buffer.writeln();
513+
buffer.writeln("Was:");
514+
for (var i = 0; i < actualLines.length; i++) {
515+
writeLine(i, actualLines[i], matchingActual);
516+
}
517+
518+
Expect.fail(buffer.toString());
378519
}
379520
}

0 commit comments

Comments
 (0)