Skip to content

Commit cb3b1f8

Browse files
authored
ProcessResultMatcher created and used in test (#127414)
Addresses issues: - flutter/flutter#127135 - flutter/flutter#99767 Creates a matcher class that we can use for `ProcessResult` to check for the exit code. *List which issues are fixed by this PR. You must list at least one issue.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent 8576da5 commit cb3b1f8

3 files changed

Lines changed: 88 additions & 1 deletion

File tree

‎packages/flutter_tools/test/host_cross_arch.shard/cache_test.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ HostPlatform _identifyMacBinaryArch(String path) {
3131
final ProcessResult result = processManager.runSync(
3232
<String>['file', _dartBinary.path],
3333
);
34+
expect(
35+
result,
36+
ProcessResultMatcher(
37+
stdoutSubstring: '${_dartBinary.path}: Mach-O 64-bit executable',
38+
));
3439
final RegExpMatch? match = pattern.firstMatch(result.stdout as String);
3540
if (match == null) {
3641
fail('Unrecognized STDOUT from `file`: "${result.stdout}"');

‎packages/flutter_tools/test/integration.shard/gradle_non_android_plugin_test.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:file/file.dart';
66
import 'package:file_testing/file_testing.dart';
7+
import 'package:flutter_tools/src/base/io.dart';
78

89
import '../src/common.dart';
910
import 'test_utils.dart';
@@ -68,7 +69,7 @@ void main() {
6869
// Build example APK
6970
final Directory exampleDir = projectRoot.childDirectory('example');
7071

71-
processManager.runSync(<String>[
72+
final ProcessResult result = processManager.runSync(<String>[
7273
flutterBin,
7374
...getLocalEngineArguments(),
7475
'build',
@@ -78,6 +79,8 @@ void main() {
7879
'--verbose',
7980
], workingDirectory: exampleDir.path);
8081

82+
expect(result, ProcessResultMatcher());
83+
8184
final String exampleAppApk = fileSystem.path.join(
8285
exampleDir.path,
8386
'build',

‎packages/flutter_tools/test/integration.shard/test_utils.dart‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,82 @@ abstract final class AppleTestUtils {
124124
return nmOutput.isEmpty ? const <String>[] : nmOutput.split('\n');
125125
}
126126
}
127+
128+
/// Matcher to be used for [ProcessResult] returned
129+
/// from a process run
130+
///
131+
/// The default for [expectedExitCode] will be 0 while
132+
/// [stdoutSubstring] and [stderrSubstring] are both optional
133+
class ProcessResultMatcher extends Matcher {
134+
ProcessResultMatcher({
135+
this.expectedExitCode = 0,
136+
this.stdoutSubstring,
137+
this.stderrSubstring,
138+
});
139+
140+
/// The expected exit code to get returned from a process run
141+
final int expectedExitCode;
142+
143+
/// Substring to find in the process's stdout
144+
final String? stdoutSubstring;
145+
146+
/// Substring to find in the process's stderr
147+
final String? stderrSubstring;
148+
149+
bool _foundStdout = true;
150+
bool _foundStderr = true;
151+
152+
@override
153+
Description describe(Description description) {
154+
description.add('a process with exit code $expectedExitCode');
155+
if (stdoutSubstring != null) {
156+
description.add(' and stdout: "$stdoutSubstring"');
157+
}
158+
if (stderrSubstring != null) {
159+
description.add(' and stderr: "$stderrSubstring"');
160+
}
161+
162+
return description;
163+
}
164+
165+
@override
166+
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
167+
final ProcessResult result = item as ProcessResult;
168+
169+
if (stdoutSubstring != null) {
170+
_foundStdout = (result.stdout as String).contains(stdoutSubstring!);
171+
matchState['stdout'] = result.stdout;
172+
}
173+
174+
if (stderrSubstring != null) {
175+
_foundStderr = (result.stderr as String).contains(stderrSubstring!);
176+
matchState['stderr'] = result.stderr;
177+
}
178+
179+
return result.exitCode == expectedExitCode && _foundStdout && _foundStderr;
180+
}
181+
182+
@override
183+
Description describeMismatch(
184+
Object? item,
185+
Description mismatchDescription,
186+
Map<dynamic, dynamic> matchState,
187+
bool verbose,
188+
) {
189+
final ProcessResult result = item! as ProcessResult;
190+
191+
if (result.exitCode != expectedExitCode) {
192+
mismatchDescription.add('Actual exitCode was ${result.exitCode}');
193+
}
194+
195+
if (matchState.containsKey('stdout')) {
196+
mismatchDescription.add('Actual stdout:\n${matchState["stdout"]}');
197+
}
198+
199+
if (matchState.containsKey('stderr')) {
200+
mismatchDescription.add('Actual stderr:\n${matchState["stderr"]}');
201+
}
202+
203+
return mismatchDescription;
204+
}
205+
}

0 commit comments

Comments
 (0)