Skip to content

Commit 0f70ef1

Browse files
munificentcommit-bot@chromium.org
authored andcommitted
Tests for TestFile class.
Also a few minor tweaks to TestFile to make it more testable and pin down some edge case behavior. Change-Id: Icf56be74b68b1e9073b892bc441f04a433e437a8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107366 Commit-Queue: Bob Nystrom <rnystrom@google.com> Reviewed-by: Nate Bosch <nbosch@google.com>
1 parent 496345e commit 0f70ef1

2 files changed

Lines changed: 343 additions & 8 deletions

File tree

pkg/test_runner/lib/src/test_file.dart

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,33 @@ abstract class _TestFileBase {
129129
/// the test output).
130130
class TestFile extends _TestFileBase {
131131
/// Read the test file from the given [filePath].
132-
factory TestFile.read(Path suiteDirectory, String filePath) {
132+
factory TestFile.read(Path suiteDirectory, String filePath) => TestFile.parse(
133+
suiteDirectory, filePath, File(filePath).readAsStringSync());
134+
135+
/// Parse a test file with [contents].
136+
factory TestFile.parse(
137+
Path suiteDirectory, String filePath, String contents) {
133138
if (filePath.endsWith('.dill')) {
134139
return TestFile._(suiteDirectory, Path(filePath),
135140
vmOptions: [[]],
136141
sharedOptions: [],
137142
dart2jsOptions: [],
143+
ddcOptions: [],
138144
dartOptions: [],
139145
packageRoot: null,
140146
packages: null,
141147
hasSyntaxError: false,
142148
hasCompileError: false,
143149
hasRuntimeError: false,
144150
hasStaticWarning: false,
151+
hasCrash: false,
145152
isMultitest: false,
146153
isMultiHtmlTest: false,
147-
subtestNames: []);
154+
subtestNames: [],
155+
sharedObjects: [],
156+
otherResources: []);
148157
}
149158

150-
var contents = File(filePath).readAsStringSync();
151-
152159
// VM options.
153160
var vmOptions = <List<String>>[];
154161
var matches = _vmOptionsRegExp.allMatches(contents);
@@ -352,10 +359,10 @@ class TestFile extends _TestFileBase {
352359
bool hasStaticWarning,
353360
bool hasSyntaxError}) =>
354361
_MultitestFile(this, path, multitestKey,
355-
hasCompileError: hasCompileError,
356-
hasRuntimeError: hasRuntimeError,
357-
hasStaticWarning: hasStaticWarning,
358-
hasSyntaxError: hasSyntaxError);
362+
hasCompileError: hasCompileError ?? false,
363+
hasRuntimeError: hasRuntimeError ?? false,
364+
hasStaticWarning: hasStaticWarning ?? false,
365+
hasSyntaxError: hasSyntaxError ?? false);
359366

360367
String toString() => """TestFile(
361368
packageRoot: $packageRoot
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:expect/expect.dart';
6+
7+
import 'package:test_runner/src/path.dart';
8+
import 'package:test_runner/src/test_file.dart';
9+
10+
// Note: This test file validates how some of the special markers used by the
11+
// test runner are parsed. But this test is also run *by* that same test
12+
// runner, and we don't want it to see the markers inside the string literals
13+
// here as significant, so we obfuscate them using seemingly-pointless string
14+
// interpolations here, like `{'//'}`.
15+
16+
void main() {
17+
testParseDill();
18+
testParseVMOptions();
19+
testParseOtherOptions();
20+
testParseEnvironment();
21+
testParsePackages();
22+
testParseMultitest();
23+
testParseMultiHtmltest();
24+
testParseErrorFlags();
25+
testName();
26+
testMultitest();
27+
}
28+
29+
void testParseDill() {
30+
// Handles ".dill" files.
31+
var file = parse("", path: "test.dill");
32+
Expect.isNotNull(file.vmOptions);
33+
Expect.equals(1, file.vmOptions.length);
34+
Expect.listEquals(<String>[], file.vmOptions.first);
35+
36+
Expect.listEquals(<String>[], file.dartOptions);
37+
Expect.listEquals(<String>[], file.sharedOptions);
38+
Expect.listEquals(<String>[], file.dart2jsOptions);
39+
Expect.listEquals(<String>[], file.ddcOptions);
40+
Expect.listEquals(<String>[], file.otherResources);
41+
Expect.listEquals(<String>[], file.sharedObjects);
42+
43+
Expect.isNull(file.environment);
44+
Expect.isNull(file.packages);
45+
46+
Expect.isFalse(file.isMultitest);
47+
48+
Expect.isFalse(file.hasSyntaxError);
49+
Expect.isFalse(file.hasCompileError);
50+
Expect.isFalse(file.hasRuntimeError);
51+
Expect.isFalse(file.hasStaticWarning);
52+
Expect.isFalse(file.hasCrash);
53+
}
54+
55+
void testParseVMOptions() {
56+
expectVMOptions(String source, List<List<String>> expected) {
57+
var file = parse(source);
58+
Expect.isNotNull(file.vmOptions);
59+
Expect.equals(expected.length, file.vmOptions.length);
60+
for (var i = 0; i < expected.length; i++) {
61+
Expect.listEquals(expected[i], file.vmOptions[i]);
62+
}
63+
}
64+
65+
// No options.
66+
expectVMOptions("", [[]]);
67+
68+
// Splits words.
69+
expectVMOptions("${'//'} VMOptions=--verbose --async", [
70+
["--verbose", "--async"]
71+
]);
72+
73+
// Allows multiple.
74+
expectVMOptions("""
75+
${'//'} VMOptions=--first one
76+
${'//'} VMOptions=--second two
77+
""", [
78+
["--first", "one"],
79+
["--second", "two"]
80+
]);
81+
}
82+
83+
void testParseOtherOptions() {
84+
// No options.
85+
var file = parse("");
86+
Expect.listEquals(<String>[], file.dartOptions);
87+
Expect.listEquals(<String>[], file.sharedOptions);
88+
Expect.listEquals(<String>[], file.dart2jsOptions);
89+
Expect.listEquals(<String>[], file.ddcOptions);
90+
Expect.listEquals(<String>[], file.otherResources);
91+
Expect.listEquals(<String>[], file.sharedObjects);
92+
93+
// Single options split into words.
94+
file = parse("""
95+
${'//'} DartOptions=dart options
96+
${'//'} SharedOptions=shared options
97+
${'//'} dart2jsOptions=dart2js options
98+
${'//'} dartdevcOptions=ddc options
99+
${'//'} OtherResources=other resources
100+
${'//'} SharedObjects=shared objects
101+
""");
102+
Expect.listEquals(["dart", "options"], file.dartOptions);
103+
Expect.listEquals(["shared", "options"], file.sharedOptions);
104+
Expect.listEquals(["dart2js", "options"], file.dart2jsOptions);
105+
Expect.listEquals(["ddc", "options"], file.ddcOptions);
106+
Expect.listEquals(["other", "resources"], file.otherResources);
107+
Expect.listEquals(["shared", "objects"], file.sharedObjects);
108+
109+
// Disallows multiple lines for some options.
110+
expectParseThrows("""
111+
${'//'} DartOptions=first
112+
${'//'} DartOptions=second
113+
""");
114+
expectParseThrows("""
115+
${'//'} SharedOptions=first
116+
${'//'} SharedOptions=second
117+
""");
118+
expectParseThrows("""
119+
${'//'} dart2jsOptions=first
120+
${'//'} dart2jsOptions=second
121+
""");
122+
expectParseThrows("""
123+
${'//'} dartdevcOptions=first
124+
${'//'} dartdevcOptions=second
125+
""");
126+
127+
// Merges multiple lines for others.
128+
file = parse("""
129+
${'//'} OtherResources=other resources
130+
${'//'} OtherResources=even more
131+
${'//'} SharedObjects=shared objects
132+
${'//'} SharedObjects=many more
133+
""");
134+
Expect.listEquals(
135+
["other", "resources", "even", "more"], file.otherResources);
136+
Expect.listEquals(["shared", "objects", "many", "more"], file.sharedObjects);
137+
}
138+
139+
void testParseEnvironment() {
140+
// No environment.
141+
var file = parse("");
142+
Expect.isNull(file.environment);
143+
144+
// Without values.
145+
file = parse("""
146+
${'//'} Environment=some value
147+
${'//'} Environment=another one
148+
""");
149+
Expect.mapEquals({"some value": "", "another one": ""}, file.environment);
150+
151+
// With values.
152+
file = parse("""
153+
${'//'} Environment=some value=its value
154+
${'//'} Environment=another one = also value
155+
""");
156+
Expect.mapEquals(
157+
{"some value": "its value", "another one ": " also value"},
158+
file.environment);
159+
}
160+
161+
void testParsePackages() {
162+
// No option.
163+
var file = parse("");
164+
Expect.isNull(file.packages);
165+
166+
// Single option is converted to a path.
167+
file = parse("""
168+
${'//'} Packages=packages thing
169+
""");
170+
Expect.isTrue(file.packages.endsWith("/packages thing"));
171+
172+
// "none" is left alone.
173+
file = parse("""
174+
${'//'} Packages=none
175+
""");
176+
Expect.equals("none", file.packages);
177+
178+
// Cannot appear more than once.
179+
expectParseThrows("""
180+
${'//'} Packages=first
181+
${'//'} Packages=second
182+
""");
183+
}
184+
185+
void testParseMultitest() {
186+
// Not present.
187+
var file = parse("");
188+
Expect.isFalse(file.isMultitest);
189+
190+
// Present.
191+
file = parse("""
192+
main() {} ${'//'}# 01: compile-time error
193+
""");
194+
Expect.isTrue(file.isMultitest);
195+
}
196+
197+
void testParseMultiHtmltest() {
198+
// Not present.
199+
var file = parse("");
200+
Expect.isFalse(file.isMultiHtmlTest);
201+
Expect.listEquals(<String>[], file.subtestNames);
202+
203+
// Present.
204+
// Note: the "${''}" is to prevent the test runner running *this* test file
205+
// from parsing it as a multi-HTML test.
206+
file = parse("""
207+
main() {
208+
useHtml${''}IndividualConfiguration();
209+
group('pixel_manipulation', () {
210+
});
211+
group('arc', () {
212+
});
213+
group('drawImage_image_element', () {
214+
});
215+
}
216+
""");
217+
Expect.isTrue(file.isMultiHtmlTest);
218+
Expect.listEquals(["pixel_manipulation", "arc", "drawImage_image_element"],
219+
file.subtestNames);
220+
}
221+
222+
void testParseErrorFlags() {
223+
// Not present.
224+
var file = parse("");
225+
Expect.isFalse(file.hasSyntaxError);
226+
Expect.isFalse(file.hasCompileError);
227+
Expect.isFalse(file.hasRuntimeError);
228+
Expect.isFalse(file.hasStaticWarning);
229+
Expect.isFalse(file.hasCrash);
230+
231+
file = parse("@syntax${'-'}error");
232+
Expect.isTrue(file.hasSyntaxError);
233+
Expect.isTrue(file.hasCompileError); // Note: true.
234+
Expect.isFalse(file.hasRuntimeError);
235+
Expect.isFalse(file.hasStaticWarning);
236+
Expect.isFalse(file.hasCrash);
237+
238+
file = parse("@compile${'-'}error");
239+
Expect.isFalse(file.hasSyntaxError);
240+
Expect.isTrue(file.hasCompileError);
241+
Expect.isFalse(file.hasRuntimeError);
242+
Expect.isFalse(file.hasStaticWarning);
243+
Expect.isFalse(file.hasCrash);
244+
245+
file = parse("@runtime${'-'}error");
246+
Expect.isFalse(file.hasSyntaxError);
247+
Expect.isFalse(file.hasCompileError);
248+
Expect.isTrue(file.hasRuntimeError);
249+
Expect.isFalse(file.hasStaticWarning);
250+
Expect.isFalse(file.hasCrash);
251+
252+
file = parse("@static${'-'}warning");
253+
Expect.isFalse(file.hasSyntaxError);
254+
Expect.isFalse(file.hasCompileError);
255+
Expect.isFalse(file.hasRuntimeError);
256+
Expect.isTrue(file.hasStaticWarning);
257+
Expect.isFalse(file.hasCrash);
258+
}
259+
260+
void testName() {
261+
// Immediately inside suite.
262+
var file = TestFile.parse(Path("suite").absolute,
263+
Path("suite/a_test.dart").absolute.toNativePath(), "");
264+
Expect.equals("a_test", file.name);
265+
266+
// Inside subdirectory.
267+
file = TestFile.parse(Path("suite").absolute,
268+
Path("suite/a/b/c_test.dart").absolute.toNativePath(), "");
269+
Expect.equals("a/b/c_test", file.name);
270+
271+
// Multitest.
272+
file = file.split(Path("suite/a/b/c_test_00.dart").absolute, "00");
273+
Expect.equals("a/b/c_test/00", file.name);
274+
}
275+
276+
void testMultitest() {
277+
var file = parse("", path: "origin.dart");
278+
Expect.isFalse(file.hasSyntaxError);
279+
Expect.isFalse(file.hasCompileError);
280+
Expect.isFalse(file.hasRuntimeError);
281+
Expect.isFalse(file.hasStaticWarning);
282+
283+
var a = file.split(Path("a.dart").absolute, "a", hasSyntaxError: true);
284+
Expect.isTrue(a.originPath.toNativePath().endsWith("origin.dart"));
285+
Expect.isTrue(a.path.toNativePath().endsWith("a.dart"));
286+
Expect.isTrue(a.hasSyntaxError);
287+
Expect.isFalse(a.hasCompileError);
288+
Expect.isFalse(a.hasRuntimeError);
289+
Expect.isFalse(a.hasStaticWarning);
290+
291+
var b = file.split(
292+
Path("b.dart").absolute,
293+
"b",
294+
hasCompileError: true,
295+
);
296+
Expect.isTrue(b.originPath.toNativePath().endsWith("origin.dart"));
297+
Expect.isTrue(b.path.toNativePath().endsWith("b.dart"));
298+
Expect.isFalse(b.hasSyntaxError);
299+
Expect.isTrue(b.hasCompileError);
300+
Expect.isFalse(b.hasRuntimeError);
301+
Expect.isFalse(b.hasStaticWarning);
302+
303+
var c = file.split(Path("c.dart").absolute, "c", hasRuntimeError: true);
304+
Expect.isTrue(c.originPath.toNativePath().endsWith("origin.dart"));
305+
Expect.isTrue(c.path.toNativePath().endsWith("c.dart"));
306+
Expect.isFalse(c.hasSyntaxError);
307+
Expect.isFalse(c.hasCompileError);
308+
Expect.isTrue(c.hasRuntimeError);
309+
Expect.isFalse(c.hasStaticWarning);
310+
311+
var d = file.split(Path("d.dart").absolute, "d", hasStaticWarning: true);
312+
Expect.isTrue(d.originPath.toNativePath().endsWith("origin.dart"));
313+
Expect.isTrue(d.path.toNativePath().endsWith("d.dart"));
314+
Expect.isFalse(d.hasSyntaxError);
315+
Expect.isFalse(d.hasCompileError);
316+
Expect.isFalse(d.hasRuntimeError);
317+
Expect.isTrue(d.hasStaticWarning);
318+
}
319+
320+
TestFile parse(String source, {String path = "some_test.dart"}) {
321+
path = Path(path).absolute.toNativePath();
322+
var suiteDirectory = Path(path).directoryPath;
323+
return TestFile.parse(suiteDirectory, path, source);
324+
}
325+
326+
void expectParseThrows(String source) {
327+
Expect.throws(() => parse(source));
328+
}

0 commit comments

Comments
 (0)