Skip to content

Commit a44f745

Browse files
authored
Allow integration test helpers to work on substrings instead of whole strings (#160437)
The integration test framework that waits for transitions and (optionally) takes actions on transitions allows to match patterns. If one uses a RegExp pattern than the framework only checks whether a line contains the given RegExp pattern. If one uses a String pattern it matches it exactly. => We add a `Barrier.contains()` and `Multiple.contains()` that allow matching a line with if it contains the String (just like in RegExp) => This makes tests simpler as they don't have to know about the exact padding of progres bar etc. Those may be irrelevant for the purpose of the integration test and only complicate it.
1 parent 188d1e1 commit a44f745

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

packages/flutter_tools/test/integration.shard/isolated/native_assets_test.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void main() {
7272
<String>['run', '-d$device', '--$buildMode'],
7373
exampleDirectory.path,
7474
<Transition>[
75-
Multiple(
75+
Multiple.contains(
7676
<Pattern>['Flutter run key commands.'],
7777
handler: (String line) {
7878
if (buildMode == 'debug') {
@@ -85,31 +85,31 @@ void main() {
8585
},
8686
),
8787
if (buildMode == 'debug') ...<Transition>[
88-
Barrier('Performing hot reload...'.padRight(progressMessageWidth), logging: true),
88+
Barrier.contains('Performing hot reload...', logging: true),
8989
Multiple(
9090
<Pattern>[RegExp('Reloaded .*')],
9191
handler: (String line) {
9292
// Do a hot restart, pushing a new complete dill file.
9393
return 'R';
9494
},
9595
),
96-
Barrier('Performing hot restart...'.padRight(progressMessageWidth)),
96+
Barrier.contains('Performing hot restart...'),
9797
Multiple(
9898
<Pattern>[RegExp('Restarted application .*')],
9999
handler: (String line) {
100100
// Do another hot reload, pushing a diff to the second dill file.
101101
return 'r';
102102
},
103103
),
104-
Barrier('Performing hot reload...'.padRight(progressMessageWidth), logging: true),
104+
Barrier.contains('Performing hot reload...', logging: true),
105105
Multiple(
106106
<Pattern>[RegExp('Reloaded .*')],
107107
handler: (String line) {
108108
return 'q';
109109
},
110110
),
111111
],
112-
const Barrier('Application finished.'),
112+
Barrier.contains('Application finished.'),
113113
],
114114
logging: false,
115115
);

packages/flutter_tools/test/integration.shard/overall_experience_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void main() {
5656
return 'q';
5757
},
5858
),
59-
const Barrier('Application finished.'),
59+
Barrier('Application finished.'),
6060
],
6161
);
6262
expect(existsDuringTest, isNot(isNull));
@@ -136,7 +136,7 @@ void main() {
136136
return 'q';
137137
},
138138
),
139-
const Barrier('Application finished.'),
139+
Barrier('Application finished.'),
140140
],
141141
logging:
142142
false, // we ignore leading log lines to avoid making this test sensitive to e.g. the help message text
@@ -232,7 +232,7 @@ void main() {
232232
return 'q';
233233
},
234234
),
235-
const Barrier('Application finished.'),
235+
Barrier('Application finished.'),
236236
],
237237
logging:
238238
false, // we ignore leading log lines to avoid making this test sensitive to e.g. the help message text
@@ -391,7 +391,7 @@ void main() {
391391
return 'q';
392392
},
393393
),
394-
const Barrier('Application finished.'),
394+
Barrier('Application finished.'),
395395
],
396396
);
397397
expect(result.exitCode, 0);

packages/flutter_tools/test/integration.shard/transition_test_utils.dart

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,48 +43,56 @@ abstract class Transition {
4343
bool matches(String line);
4444

4545
@protected
46-
bool lineMatchesPattern(String line, Pattern pattern) {
47-
if (pattern is String) {
48-
return line == pattern;
46+
bool lineMatchesPattern(String line, Pattern pattern, bool contains) {
47+
if (pattern is RegExp) {
48+
// Ideally this would also distinguish between "contains" and "equals"
49+
// operation.
50+
return line.contains(pattern);
4951
}
50-
return line.contains(pattern);
52+
return contains ? line.contains(pattern) : line == pattern;
5153
}
5254

5355
@protected
54-
String describe(Pattern pattern) {
55-
if (pattern is String) {
56-
return '"$pattern"';
57-
}
56+
String describe(Pattern pattern, bool contains) {
5857
if (pattern is RegExp) {
5958
return '/${pattern.pattern}/';
6059
}
61-
return '$pattern';
60+
return contains ? '"...$pattern..."' : '"$pattern"';
6261
}
6362
}
6463

6564
class Barrier extends Transition {
66-
const Barrier(this.pattern, {super.handler, super.logging});
65+
Barrier(this.pattern, {super.handler, super.logging}) : contains = false;
66+
Barrier.contains(this.pattern, {super.handler, super.logging}) : contains = true;
67+
6768
final Pattern pattern;
69+
final bool contains;
6870

6971
@override
70-
bool matches(String line) => lineMatchesPattern(line, pattern);
72+
bool matches(String line) => lineMatchesPattern(line, pattern, contains);
7173

7274
@override
73-
String toString() => describe(pattern);
75+
String toString() => describe(pattern, contains);
7476
}
7577

7678
class Multiple extends Transition {
7779
Multiple(List<Pattern> patterns, {super.handler, super.logging})
7880
: _originalPatterns = patterns,
79-
patterns = patterns.toList();
81+
patterns = patterns.toList(),
82+
contains = false;
83+
Multiple.contains(List<Pattern> patterns, {super.handler, super.logging})
84+
: _originalPatterns = patterns,
85+
patterns = patterns.toList(),
86+
contains = true;
8087

8188
final List<Pattern> _originalPatterns;
8289
final List<Pattern> patterns;
90+
final bool contains;
8391

8492
@override
8593
bool matches(String line) {
8694
for (int index = 0; index < patterns.length; index += 1) {
87-
if (lineMatchesPattern(line, patterns[index])) {
95+
if (lineMatchesPattern(line, patterns[index], contains)) {
8896
patterns.removeAt(index);
8997
break;
9098
}
@@ -94,6 +102,7 @@ class Multiple extends Transition {
94102

95103
@override
96104
String toString() {
105+
String describe(Pattern pattern) => super.describe(pattern, contains);
97106
if (patterns.isEmpty) {
98107
return '${_originalPatterns.map(describe).join(', ')} (all matched)';
99108
}

0 commit comments

Comments
 (0)