ProcessResultMatcher
created and used in test (#127414)
Addresses issues: - https://github.com/flutter/flutter/issues/127135 - https://github.com/flutter/flutter/issues/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].*
This commit is contained in:
parent
8576da53c5
commit
cb3b1f8c84
@ -31,6 +31,11 @@ HostPlatform _identifyMacBinaryArch(String path) {
|
|||||||
final ProcessResult result = processManager.runSync(
|
final ProcessResult result = processManager.runSync(
|
||||||
<String>['file', _dartBinary.path],
|
<String>['file', _dartBinary.path],
|
||||||
);
|
);
|
||||||
|
expect(
|
||||||
|
result,
|
||||||
|
ProcessResultMatcher(
|
||||||
|
stdoutSubstring: '${_dartBinary.path}: Mach-O 64-bit executable',
|
||||||
|
));
|
||||||
final RegExpMatch? match = pattern.firstMatch(result.stdout as String);
|
final RegExpMatch? match = pattern.firstMatch(result.stdout as String);
|
||||||
if (match == null) {
|
if (match == null) {
|
||||||
fail('Unrecognized STDOUT from `file`: "${result.stdout}"');
|
fail('Unrecognized STDOUT from `file`: "${result.stdout}"');
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
import 'package:file/file.dart';
|
import 'package:file/file.dart';
|
||||||
import 'package:file_testing/file_testing.dart';
|
import 'package:file_testing/file_testing.dart';
|
||||||
|
import 'package:flutter_tools/src/base/io.dart';
|
||||||
|
|
||||||
import '../src/common.dart';
|
import '../src/common.dart';
|
||||||
import 'test_utils.dart';
|
import 'test_utils.dart';
|
||||||
@ -68,7 +69,7 @@ void main() {
|
|||||||
// Build example APK
|
// Build example APK
|
||||||
final Directory exampleDir = projectRoot.childDirectory('example');
|
final Directory exampleDir = projectRoot.childDirectory('example');
|
||||||
|
|
||||||
processManager.runSync(<String>[
|
final ProcessResult result = processManager.runSync(<String>[
|
||||||
flutterBin,
|
flutterBin,
|
||||||
...getLocalEngineArguments(),
|
...getLocalEngineArguments(),
|
||||||
'build',
|
'build',
|
||||||
@ -78,6 +79,8 @@ void main() {
|
|||||||
'--verbose',
|
'--verbose',
|
||||||
], workingDirectory: exampleDir.path);
|
], workingDirectory: exampleDir.path);
|
||||||
|
|
||||||
|
expect(result, ProcessResultMatcher());
|
||||||
|
|
||||||
final String exampleAppApk = fileSystem.path.join(
|
final String exampleAppApk = fileSystem.path.join(
|
||||||
exampleDir.path,
|
exampleDir.path,
|
||||||
'build',
|
'build',
|
||||||
|
@ -124,3 +124,82 @@ abstract final class AppleTestUtils {
|
|||||||
return nmOutput.isEmpty ? const <String>[] : nmOutput.split('\n');
|
return nmOutput.isEmpty ? const <String>[] : nmOutput.split('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Matcher to be used for [ProcessResult] returned
|
||||||
|
/// from a process run
|
||||||
|
///
|
||||||
|
/// The default for [expectedExitCode] will be 0 while
|
||||||
|
/// [stdoutSubstring] and [stderrSubstring] are both optional
|
||||||
|
class ProcessResultMatcher extends Matcher {
|
||||||
|
ProcessResultMatcher({
|
||||||
|
this.expectedExitCode = 0,
|
||||||
|
this.stdoutSubstring,
|
||||||
|
this.stderrSubstring,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// The expected exit code to get returned from a process run
|
||||||
|
final int expectedExitCode;
|
||||||
|
|
||||||
|
/// Substring to find in the process's stdout
|
||||||
|
final String? stdoutSubstring;
|
||||||
|
|
||||||
|
/// Substring to find in the process's stderr
|
||||||
|
final String? stderrSubstring;
|
||||||
|
|
||||||
|
bool _foundStdout = true;
|
||||||
|
bool _foundStderr = true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describe(Description description) {
|
||||||
|
description.add('a process with exit code $expectedExitCode');
|
||||||
|
if (stdoutSubstring != null) {
|
||||||
|
description.add(' and stdout: "$stdoutSubstring"');
|
||||||
|
}
|
||||||
|
if (stderrSubstring != null) {
|
||||||
|
description.add(' and stderr: "$stderrSubstring"');
|
||||||
|
}
|
||||||
|
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
|
||||||
|
final ProcessResult result = item as ProcessResult;
|
||||||
|
|
||||||
|
if (stdoutSubstring != null) {
|
||||||
|
_foundStdout = (result.stdout as String).contains(stdoutSubstring!);
|
||||||
|
matchState['stdout'] = result.stdout;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stderrSubstring != null) {
|
||||||
|
_foundStderr = (result.stderr as String).contains(stderrSubstring!);
|
||||||
|
matchState['stderr'] = result.stderr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.exitCode == expectedExitCode && _foundStdout && _foundStderr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describeMismatch(
|
||||||
|
Object? item,
|
||||||
|
Description mismatchDescription,
|
||||||
|
Map<dynamic, dynamic> matchState,
|
||||||
|
bool verbose,
|
||||||
|
) {
|
||||||
|
final ProcessResult result = item! as ProcessResult;
|
||||||
|
|
||||||
|
if (result.exitCode != expectedExitCode) {
|
||||||
|
mismatchDescription.add('Actual exitCode was ${result.exitCode}');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchState.containsKey('stdout')) {
|
||||||
|
mismatchDescription.add('Actual stdout:\n${matchState["stdout"]}');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchState.containsKey('stderr')) {
|
||||||
|
mismatchDescription.add('Actual stderr:\n${matchState["stderr"]}');
|
||||||
|
}
|
||||||
|
|
||||||
|
return mismatchDescription;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user