Skip to content

Commit

Permalink
Add containsCommand matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cbracken committed Apr 20, 2024
1 parent f5fde03 commit b2ad2e8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
36 changes: 14 additions & 22 deletions tools/engine_tool/test/build_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,12 @@ void main() {
'//flutter/fml:fml_arc_unittests',
]);
expect(result, equals(0));
expect(testEnv.processHistory.length, greaterThanOrEqualTo(2));
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 > 4 &&
command[0].contains('ninja') &&
command[2].endsWith('/host_debug') &&
command[5] == 'flutter/fml:fml_arc_unittests';
}));
} finally {
testEnv.cleanup();
}
Expand All @@ -389,21 +388,14 @@ void main() {
'//flutter/...',
]);
expect(result, equals(0));
expect(testEnv.processHistory.length, greaterThanOrEqualTo(2));
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();
}
Expand Down
24 changes: 24 additions & 0 deletions tools/engine_tool/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -169,3 +170,26 @@ 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");
}
};

0 comments on commit b2ad2e8

Please sign in to comment.