Add containsCommand matcher (flutter/engine#52306)

Adds a `Matcher` that can be used against a `List<ExecutedProcess>` to check if a command that matches a predicate was executed. Fails if no such command was found.

An alternative would be to take a single regex instead of a closure.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
Chris Bracken
2024-04-23 14:23:05 -07:00
committed by GitHub
parent 4722356522
commit 2929b70726
3 changed files with 127 additions and 22 deletions

View File

@@ -360,13 +360,12 @@ void main() {
'//flutter/fml:fml_arc_unittests',
]);
expect(result, equals(0));
expect(testEnv.processHistory.length, greaterThan(6));
expect(testEnv.processHistory[6].command[0], contains('ninja'));
expect(testEnv.processHistory[6].command[2], endsWith('/host_debug'));
expect(
testEnv.processHistory[6].command[5],
equals('flutter/fml:fml_arc_unittests'),
);
expect(testEnv.processHistory, containsCommand((List<String> command) {
return command.length > 5 &&
command[0].contains('ninja') &&
command[2].endsWith('/host_debug') &&
command[5] == 'flutter/fml:fml_arc_unittests';
}));
} finally {
testEnv.cleanup();
}
@@ -389,21 +388,14 @@ void main() {
'//flutter/...',
]);
expect(result, equals(0));
expect(testEnv.processHistory.length, greaterThan(6));
expect(testEnv.processHistory[6].command[0], contains('ninja'));
expect(testEnv.processHistory[6].command[2], endsWith('/host_debug'));
expect(
testEnv.processHistory[6].command[5],
equals('flutter/display_list:display_list_unittests'),
);
expect(
testEnv.processHistory[6].command[6],
equals('flutter/flow:flow_unittests'),
);
expect(
testEnv.processHistory[6].command[7],
equals('flutter/fml:fml_arc_unittests'),
);
expect(testEnv.processHistory, containsCommand((List<String> command) {
return command.length > 7 &&
command[0].contains('ninja') &&
command[2].endsWith('/host_debug') &&
command[5] == 'flutter/display_list:display_list_unittests' &&
command[6] == 'flutter/flow:flow_unittests' &&
command[7] == 'flutter/fml:fml_arc_unittests';
}));
} finally {
testEnv.cleanup();
}

View File

@@ -8,6 +8,7 @@ import 'dart:io' as io;
import 'package:engine_repo_tools/engine_repo_tools.dart';
import 'package:engine_tool/src/environment.dart';
import 'package:engine_tool/src/logger.dart';
import 'package:litetest/litetest.dart' show Expect, Matcher;
import 'package:path/path.dart' as path;
import 'package:platform/platform.dart';
import 'package:process_fakes/process_fakes.dart';
@@ -169,3 +170,48 @@ FakeProcess _getCannedResult(
}
return FakeProcess();
}
typedef CommandMatcher = bool Function(List<String> command);
/// Returns a [Matcher] that fails the test if no process has a matching command.
///
/// Usage:
/// expect(testEnv.processHistory,
/// containsCommand((List<String> command) {
/// return command.length > 5 &&
/// command[0].contains('ninja') &&
/// command[2].endsWith('/host_debug') &&
/// command[5] == 'flutter/fml:fml_arc_unittests';
/// })
/// );
Matcher containsCommand(CommandMatcher commandMatcher) => (dynamic processes) {
Expect.type<List<ExecutedProcess>>(processes);
final List<List<String>> commands = (processes as List<ExecutedProcess>)
.map((ExecutedProcess process) => process.command)
.toList();
if (!commands.any(commandMatcher)) {
Expect.fail('No process found with matching command');
}
};
/// Returns a [Matcher] that fails the test if any process has a matching
/// command.
///
/// Usage:
/// expect(testEnv.processHistory,
/// doesNotContainCommand((List<String> command) {
/// return command.length > 5 &&
/// command[0].contains('ninja') &&
/// command[2].endsWith('/host_debug') &&
/// command[5] == 'flutter/fml:fml_arc_unittests';
/// })
/// );
Matcher doesNotContainCommand(CommandMatcher commandMatcher) => (dynamic processes) {
Expect.type<List<ExecutedProcess>>(processes);
final List<List<String>> commands = (processes as List<ExecutedProcess>)
.map((ExecutedProcess process) => process.command)
.toList();
if (commands.any(commandMatcher)) {
Expect.fail('Process found with matching command');
}
};

View File

@@ -0,0 +1,67 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:litetest/litetest.dart';
import 'utils.dart';
void main() async {
final List<CannedProcess> cannedProcesses = <CannedProcess>[
CannedProcess((List<String> command) => command.contains('ulfuls'),
stdout: 'Ashita ga aru sa'),
CannedProcess((List<String> command) => command.contains('quruli'),
stdout: 'Tokyo'),
CannedProcess((List<String> command) => command.contains('elizaveta'),
stdout: 'Moshimo ano toki'),
CannedProcess((List<String> command) => command.contains('scott_murphy'),
stdout: 'Donna toki mo'),
];
test('containsCommand passes if command matched', () async {
final TestEnvironment testEnvironment = TestEnvironment.withTestEngine(
cannedProcesses: cannedProcesses,
);
try {
await testEnvironment.environment.processRunner.runProcess(
<String>['ulfuls', '--lyrics'],
workingDirectory: testEnvironment.environment.engine.srcDir,
failOk: true);
await testEnvironment.environment.processRunner.runProcess(
<String>['quruli', '--lyrics'],
workingDirectory: testEnvironment.environment.engine.srcDir,
failOk: true);
final List<ExecutedProcess> history = testEnvironment.processHistory;
expect(history, containsCommand((List<String> command) {
return command.isNotEmpty && command[0] == 'quruli';
}));
expect(history, containsCommand((List<String> command) {
return command.length > 1 && command[1] == '--lyrics';
}));
} finally {
testEnvironment.cleanup();
}
});
test('doesNotContainCommand passes if command not matched', () async {
final TestEnvironment testEnvironment = TestEnvironment.withTestEngine(
cannedProcesses: cannedProcesses,
);
try {
await testEnvironment.environment.processRunner.runProcess(
<String>['elizaveta', '--lyrics'],
workingDirectory: testEnvironment.environment.engine.srcDir,
failOk: true);
await testEnvironment.environment.processRunner.runProcess(
<String>['scott_murphy', '--lyrics'],
workingDirectory: testEnvironment.environment.engine.srcDir,
failOk: true);
final List<ExecutedProcess> history = testEnvironment.processHistory;
expect(history, doesNotContainCommand((List<String> command) {
return command.length > 1 && command[1] == '--not-an-option';
}));
} finally {
testEnvironment.cleanup();
}
});
}