diff --git a/engine/src/flutter/tools/engine_tool/test/build_command_test.dart b/engine/src/flutter/tools/engine_tool/test/build_command_test.dart index 055767abd3..18417571e9 100644 --- a/engine/src/flutter/tools/engine_tool/test/build_command_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/build_command_test.dart @@ -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 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 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(); } diff --git a/engine/src/flutter/tools/engine_tool/test/utils.dart b/engine/src/flutter/tools/engine_tool/test/utils.dart index a6955933c6..4b3aaf3721 100644 --- a/engine/src/flutter/tools/engine_tool/test/utils.dart +++ b/engine/src/flutter/tools/engine_tool/test/utils.dart @@ -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 command); + +/// Returns a [Matcher] that fails the test if no process has a matching command. +/// +/// Usage: +/// expect(testEnv.processHistory, +/// containsCommand((List 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>(processes); + final List> commands = (processes as List) + .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 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>(processes); + final List> commands = (processes as List) + .map((ExecutedProcess process) => process.command) + .toList(); + if (commands.any(commandMatcher)) { + Expect.fail('Process found with matching command'); + } + }; diff --git a/engine/src/flutter/tools/engine_tool/test/utils_test.dart b/engine/src/flutter/tools/engine_tool/test/utils_test.dart new file mode 100644 index 0000000000..1ce186ced1 --- /dev/null +++ b/engine/src/flutter/tools/engine_tool/test/utils_test.dart @@ -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 cannedProcesses = [ + CannedProcess((List command) => command.contains('ulfuls'), + stdout: 'Ashita ga aru sa'), + CannedProcess((List command) => command.contains('quruli'), + stdout: 'Tokyo'), + CannedProcess((List command) => command.contains('elizaveta'), + stdout: 'Moshimo ano toki'), + CannedProcess((List 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( + ['ulfuls', '--lyrics'], + workingDirectory: testEnvironment.environment.engine.srcDir, + failOk: true); + await testEnvironment.environment.processRunner.runProcess( + ['quruli', '--lyrics'], + workingDirectory: testEnvironment.environment.engine.srcDir, + failOk: true); + final List history = testEnvironment.processHistory; + expect(history, containsCommand((List command) { + return command.isNotEmpty && command[0] == 'quruli'; + })); + expect(history, containsCommand((List 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( + ['elizaveta', '--lyrics'], + workingDirectory: testEnvironment.environment.engine.srcDir, + failOk: true); + await testEnvironment.environment.processRunner.runProcess( + ['scott_murphy', '--lyrics'], + workingDirectory: testEnvironment.environment.engine.srcDir, + failOk: true); + final List history = testEnvironment.processHistory; + expect(history, doesNotContainCommand((List command) { + return command.length > 1 && command[1] == '--not-an-option'; + })); + } finally { + testEnvironment.cleanup(); + } + }); +}