|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert'; |
| 3 | + |
| 4 | +import 'package:universal_io/io.dart'; |
| 5 | +import 'package:very_good_test_runner/very_good_test_runner.dart'; |
| 6 | + |
| 7 | +/// Signature for `Process.start`. |
| 8 | +typedef StartProcess = Future<Process> Function( |
| 9 | + String executable, |
| 10 | + List<String> arguments, { |
| 11 | + String? workingDirectory, |
| 12 | + Map<String, String>? environment, |
| 13 | + bool includeParentEnvironment, |
| 14 | + bool runInShell, |
| 15 | + ProcessStartMode mode, |
| 16 | +}); |
| 17 | + |
| 18 | +/// Runs `flutter test` and returns a stream of [TestEvent] |
| 19 | +/// reported by the process. |
| 20 | +/// |
| 21 | +/// ```dart |
| 22 | +/// void main() { |
| 23 | +/// // React to `TestEvent` instances. |
| 24 | +/// flutterTest().listen(print); |
| 25 | +/// } |
| 26 | +/// ``` |
| 27 | +Stream<TestEvent> flutterTest({ |
| 28 | + List<String>? arguments, |
| 29 | + String? workingDirectory, |
| 30 | + Map<String, String>? environment, |
| 31 | + bool runInShell = false, |
| 32 | + StartProcess startProcess = Process.start, |
| 33 | +}) { |
| 34 | + final controller = StreamController<TestEvent>(); |
| 35 | + late StreamSubscription testEventSubscription; |
| 36 | + late StreamSubscription errorSubscription; |
| 37 | + late Future<Process> processFuture; |
| 38 | + |
| 39 | + Future<void> _onListen() async { |
| 40 | + processFuture = startProcess( |
| 41 | + 'flutter', |
| 42 | + ['test', ...?arguments, '--reporter=json'], |
| 43 | + environment: environment, |
| 44 | + workingDirectory: workingDirectory, |
| 45 | + runInShell: runInShell, |
| 46 | + ); |
| 47 | + final process = await processFuture; |
| 48 | + final errors = process.stderr.map((e) => utf8.decode(e).trim()); |
| 49 | + final testEvents = process.stdout.mapToTestEvents(); |
| 50 | + errorSubscription = errors.listen(controller.addError); |
| 51 | + testEventSubscription = testEvents.listen( |
| 52 | + controller.add, |
| 53 | + onError: controller.addError, |
| 54 | + onDone: controller.close, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + Future<void> _onCancel() async { |
| 59 | + await controller.close(); |
| 60 | + (await processFuture).kill(); |
| 61 | + await errorSubscription.cancel(); |
| 62 | + await testEventSubscription.cancel(); |
| 63 | + } |
| 64 | + |
| 65 | + controller |
| 66 | + ..onListen = _onListen |
| 67 | + ..onCancel = _onCancel; |
| 68 | + |
| 69 | + return controller.stream; |
| 70 | +} |
| 71 | + |
| 72 | +extension on Stream<List<int>> { |
| 73 | + Stream<TestEvent> mapToTestEvents() { |
| 74 | + return map(utf8.decode) |
| 75 | + .expand<String>((msg) sync* { |
| 76 | + for (final value in msg.split('\n')) { |
| 77 | + final trimmedValue = value.trim(); |
| 78 | + if (trimmedValue.isNotEmpty) yield trimmedValue; |
| 79 | + } |
| 80 | + }) |
| 81 | + .expand<Object?>((j) { |
| 82 | + try { |
| 83 | + return [json.decode(j)]; |
| 84 | + } on FormatException { |
| 85 | + return []; |
| 86 | + } |
| 87 | + }) |
| 88 | + .cast<Map<Object?, Object?>>() |
| 89 | + .map((json) => TestEvent.fromJson(Map<String, dynamic>.from(json))); |
| 90 | + } |
| 91 | +} |
0 commit comments