Skip to content

Commit 1cf8a39

Browse files
authored
Splitting test expectations between expect, expectAndGetLine, and expectAndGetBlock. (#4270)
* Splitting test expectations between expect, expectAndGetLine, and expectAndGetBlock. * bumping ver * updating changelog * removing clog entry * simplifying method * updating clog version
1 parent 4e14d92 commit 1cf8a39

File tree

7 files changed

+77
-45
lines changed

7 files changed

+77
-45
lines changed

build_runner/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 2.10.2-wip
1+
## 2.10.3-wip
22

33
- In "serve" and "watch" modes, retry failed compiles instead of exiting.
44

build_runner/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: build_runner
2-
version: 2.10.2-wip
2+
version: 2.10.3-wip
33
description: A build system for Dart code generation and modular compilation.
44
repository: https://github.com/dart-lang/build/tree/master/build_runner
55
resolution: workspace

build_runner/test/common/build_runner_tester.dart

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -239,62 +239,83 @@ class BuildRunnerProcess {
239239
/// defaults to [BuildLog.failurePattern] so that `expect` will stop if the
240240
/// process reports a build failure.
241241
///
242-
/// if [expectFailure] is set, then both [pattern] and [failOn] must be
243-
/// encountered for the test to pass.
242+
/// If the process exits instead, the test fails immediately.
243+
///
244+
/// Otherwise, waits until [pattern] appears, then completes.
245+
///
246+
/// Throws if the process appears to be stuck or done: if it outputs nothing
247+
/// for 30s.
248+
Future<void> expect(Pattern pattern, {Pattern? failOn}) async =>
249+
expectAndGetLine(pattern, failOn: failOn);
250+
251+
/// Expects [pattern] to appear in the process's stdout or stderr.
252+
///
253+
/// If [failOn] is encountered instead, the test fails immediately. It
254+
/// defaults to [BuildLog.failurePattern] so that `expect` will stop if the
255+
/// process reports a build failure.
244256
///
245257
/// If the process exits instead, the test fails immediately.
246258
///
247259
/// Otherwise, waits until [pattern] appears, returns the matching line.
248260
///
249261
/// Throws if the process appears to be stuck or done: if it outputs nothing
250262
/// for 30s.
251-
Future<String> expect(
252-
Pattern pattern, {
253-
Pattern? failOn,
254-
bool expectFailure = false,
255-
}) async {
263+
Future<String> expectAndGetLine(Pattern pattern, {Pattern? failOn}) async {
256264
printOnFailure(
257265
'--- $_testLine expects `$pattern`'
258-
'${failOn == null ? '' : ', failOn: `$failOn`'}'
259-
'${expectFailure ? ', expectFailure: true' : ''}',
266+
'${failOn == null ? '' : ', failOn: `$failOn`'}',
260267
);
261268
failOn ??= BuildLog.failurePattern;
262-
263-
final expectsMessage =
264-
expectFailure
265-
? '`$pattern` with failure matching (`$failOn`)'
266-
: '`$pattern`';
267-
268-
var failureSeen = false;
269-
var patternSeen = false;
270269
while (true) {
271270
String? line;
272271
try {
273272
line = await _outputs.next.timeout(const Duration(seconds: 30));
274273
} on TimeoutException catch (_) {
275-
throw fail('While expecting $expectsMessage, timed out after 30s.');
274+
throw fail('While expecting `$pattern`, timed out after 30s.');
276275
} catch (_) {
277-
throw fail('While expecting $expectsMessage, process exited.');
276+
throw fail('While expecting `$pattern`, process exited.');
278277
}
279278
printOnFailure(line);
279+
if (line.contains(pattern)) return line;
280280
if (line.contains(failOn)) {
281-
failureSeen = true;
282-
}
283-
if (line.contains(pattern)) {
284-
patternSeen = true;
281+
fail('While expecting `$pattern`, got `$failOn`.');
285282
}
283+
}
284+
}
286285

287-
if (expectFailure) {
288-
if (patternSeen && failureSeen) {
289-
return line;
290-
}
291-
} else {
292-
if (failureSeen) {
293-
fail('While expecting $expectsMessage, got `$failOn`.');
294-
}
295-
if (patternSeen) {
296-
return line;
297-
}
286+
/// Expects [pattern] to appear in the process's stdout or stderr.
287+
///
288+
/// If [failOn] is encountered instead, the test fails immediately. It
289+
/// defaults to [BuildLog.failurePattern] so that `expect` will stop if the
290+
/// process reports a build failure.
291+
///
292+
/// If the process exits instead, the test fails immediately.
293+
///
294+
/// Otherwise, waits until [pattern] appears, returns all text seen.
295+
///
296+
/// Throws if the process appears to be stuck or done: if it outputs nothing
297+
/// for 30s.
298+
Future<String> expectAndGetBlock(Pattern pattern, {Pattern? failOn}) async {
299+
printOnFailure(
300+
'--- $_testLine expects `$pattern`'
301+
'${failOn == null ? '' : ', failOn: `$failOn`'}',
302+
);
303+
failOn ??= BuildLog.failurePattern;
304+
final lines = StringBuffer();
305+
while (true) {
306+
String? line;
307+
try {
308+
line = await _outputs.next.timeout(const Duration(seconds: 30));
309+
lines.writeln(line);
310+
} on TimeoutException catch (_) {
311+
throw fail('While expecting `$pattern`, timed out after 30s.');
312+
} catch (_) {
313+
throw fail('While expecting `$pattern`, process exited.');
314+
}
315+
printOnFailure(line);
316+
if (line.contains(pattern)) return lines.toString();
317+
if (line.contains(failOn)) {
318+
fail('While expecting `$pattern`, got `$failOn`.');
298319
}
299320
}
300321
}
@@ -329,7 +350,7 @@ class BuildRunnerProcess {
329350
// Expects the server to log that it is serving, records the port.
330351
Future<void> expectServing() async {
331352
final regexp = RegExp('Serving `web` on http://localhost:([0-9]+)');
332-
final line = await expect(regexp);
353+
final line = await expectAndGetLine(regexp);
333354
final port = int.parse(regexp.firstMatch(line)!.group(1)!);
334355
_port = port;
335356
}

build_runner/test/integration_tests/processes_test.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,13 @@ void main() async {
9393
}
9494
''');
9595
var process = await tester.start('root_pkg', 'dart run root_pkg:parent');
96-
var parentLine = await process.expect(RegExp(r'Parent is running\. \d+'));
96+
var parentLine = await process.expectAndGetLine(
97+
RegExp(r'Parent is running\. \d+'),
98+
);
9799
var parentPid = int.parse(parentLine.split(' ').last);
98-
final childLine = await process.expect(RegExp(r'Child is waiting\. \d+'));
100+
final childLine = await process.expectAndGetLine(
101+
RegExp(r'Child is waiting\. \d+'),
102+
);
99103
var childPid = int.parse(childLine.split(' ').last);
100104

101105
expect(processIsRunning(parentPid), true);
@@ -128,7 +132,9 @@ void main() async {
128132
}
129133
''');
130134
process = await tester.start('root_pkg', 'dart run root_pkg:parent');
131-
parentLine = await process.expect(RegExp(r'Parent is running\. \d+'));
135+
parentLine = await process.expectAndGetLine(
136+
RegExp(r'Parent is running\. \d+'),
137+
);
132138
parentPid = int.parse(parentLine.split(' ').last);
133139

134140
while (tester.read('root_pkg/pid.txt') == null) {

build_runner/test/integration_tests/web_compilers_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,10 @@ String helloWorld = 'Hello Dash!';
461461
class Foo<T>{}
462462
String helloWorld = 'Hello Dash!';
463463
''');
464-
await watch.expect(
465-
'Hot reload rejected due to unsupported changes',
466-
expectFailure: true,
464+
final errorText = await watch.expectAndGetBlock(BuildLog.failurePattern);
465+
expect(
466+
errorText,
467+
contains('Hot reload rejected due to unsupported changes'),
467468
);
468469

469470
// Revert the invalid edit, rebuild succeeds.

build_test/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.5.3-wip
2+
3+
- Use `build_runner` 2.10.3.
4+
15
## 3.5.2-wip
26

37
- Use `build_runner` 2.10.2.

build_test/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: build_test
22
description: Utilities for writing unit tests of Builders.
3-
version: 3.5.2-wip
3+
version: 3.5.3-wip
44
repository: https://github.com/dart-lang/build/tree/master/build_test
55
resolution: workspace
66

@@ -10,7 +10,7 @@ environment:
1010
dependencies:
1111
build: ^4.0.0
1212
build_config: ^1.0.0
13-
build_runner: '2.10.2-wip'
13+
build_runner: '2.10.3-wip'
1414
built_collection: ^5.1.1
1515
crypto: ^3.0.0
1616
glob: ^2.0.0

0 commit comments

Comments
 (0)