Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 6dd1c46

Browse files
[flutter_plugin_tools] Work around banner in drive-examples (#4142)
* [flutter_plugin_tools] Work around banner in drive-examples Strip off any unexpected output before parsing `flutter devices --machine` output. Works around a bug in the Flutter tool that can result in banners being printed in `--machine` mode. Fixes flutter/flutter#86052
1 parent 3f94042 commit 6dd1c46

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

script/tool/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
immediately abort the test.
1010
- Deprecated `--plugins` in favor of new `--packages`. `--plugins` continues to
1111
work for now, but will be removed in the future.
12+
- Make `drive-examples` device detection robust against Flutter tool banners.
1213

1314
## 0.3.0
1415

script/tool/lib/src/drive_examples_command.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,14 @@ class DriveExamplesCommand extends PackageLoopingCommand {
208208
return deviceIds;
209209
}
210210

211+
String output = result.stdout as String;
212+
// --machine doesn't currently prevent the tool from printing banners;
213+
// see https://github.com/flutter/flutter/issues/86055. This workaround
214+
// can be removed once that is fixed.
215+
output = output.substring(output.indexOf('['));
216+
211217
final List<Map<String, dynamic>> devices =
212-
(jsonDecode(result.stdout as String) as List<dynamic>)
213-
.cast<Map<String, dynamic>>();
218+
(jsonDecode(output) as List<dynamic>).cast<Map<String, dynamic>>();
214219
for (final Map<String, dynamic> deviceInfo in devices) {
215220
final String targetPlatform =
216221
(deviceInfo['targetPlatform'] as String?) ?? '';

script/tool/test/drive_examples_command_test.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,22 @@ void main() {
4444
void setMockFlutterDevicesOutput({
4545
bool hasIosDevice = true,
4646
bool hasAndroidDevice = true,
47+
bool includeBanner = false,
4748
}) {
49+
const String updateBanner = '''
50+
╔════════════════════════════════════════════════════════════════════════════╗
51+
║ A new version of Flutter is available! ║
52+
║ ║
53+
║ To update to the latest version, run "flutter upgrade". ║
54+
╚════════════════════════════════════════════════════════════════════════════╝
55+
''';
4856
final List<String> devices = <String>[
4957
if (hasIosDevice) '{"id": "$_fakeIosDevice", "targetPlatform": "ios"}',
5058
if (hasAndroidDevice)
5159
'{"id": "$_fakeAndroidDevice", "targetPlatform": "android-x86"}',
5260
];
53-
final String output = '''[${devices.join(',')}]''';
61+
final String output =
62+
'''${includeBanner ? updateBanner : ''}[${devices.join(',')}]''';
5463

5564
final MockProcess mockDevicesProcess = MockProcess.succeeding();
5665
mockDevicesProcess.stdoutController.close(); // ignore: unawaited_futures
@@ -113,6 +122,32 @@ void main() {
113122
);
114123
});
115124

125+
test('handles flutter tool banners when checking devices', () async {
126+
createFakePlugin(
127+
'plugin',
128+
packagesDir,
129+
extraFiles: <String>[
130+
'example/test_driver/integration_test.dart',
131+
'example/integration_test/foo_test.dart',
132+
],
133+
platformSupport: <String, PlatformSupport>{
134+
kPlatformIos: PlatformSupport.inline,
135+
},
136+
);
137+
138+
setMockFlutterDevicesOutput(includeBanner: true);
139+
final List<String> output =
140+
await runCapturingPrint(runner, <String>['drive-examples', '--ios']);
141+
142+
expect(
143+
output,
144+
containsAllInOrder(<Matcher>[
145+
contains('Running for plugin'),
146+
contains('No issues found!'),
147+
]),
148+
);
149+
});
150+
116151
test('fails for iOS if getting devices fails', () async {
117152
// Simulate failure from `flutter devices`.
118153
processRunner.mockProcessesForExecutable['flutter'] = <io.Process>[

0 commit comments

Comments
 (0)