Skip to content

Commit 25a3121

Browse files
Prefer ephemeral devices from command line run (flutter#34802)
1 parent 4c56b66 commit 25a3121

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

packages/flutter_tools/lib/src/runner/flutter_command.dart

+22-5
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,8 @@ abstract class FlutterCommand extends Command<void> {
517517
}
518518

519519
devices = devices.where((Device device) => device.isSupported()).toList();
520-
// If the user has not specified all devices and has multiple connected
521-
// then filter then list by those supported in the current project. If
522-
// this ends up with a single device we can proceed as normal.
523520
if (devices.length > 1 && !deviceManager.hasSpecifiedAllDevices && !deviceManager.hasSpecifiedDeviceId) {
524-
final FlutterProject flutterProject = FlutterProject.current();
525-
devices.removeWhere((Device device) => !device.isSupportedForProject(flutterProject));
521+
devices = filterDevices(devices);
526522
}
527523

528524
if (devices.isEmpty) {
@@ -698,3 +694,24 @@ abstract class FastFlutterCommand extends FlutterCommand {
698694
);
699695
}
700696
}
697+
698+
// If the user has not specified all devices and has multiple connected
699+
// then filter the list by those supported in the current project and
700+
// remove non-ephemeral device types. If this ends up with a single
701+
// device we can proceed as normal.
702+
@visibleForTesting
703+
List<Device> filterDevices(List<Device> devices) {
704+
final FlutterProject flutterProject = FlutterProject.current();
705+
devices = devices
706+
.where((Device device) => device.isSupportedForProject(flutterProject))
707+
.toList();
708+
709+
// Note: ephemeral is nullable for device types where this is not well
710+
// defined.
711+
if (devices.any((Device device) => device.ephemeral == true)) {
712+
devices = devices
713+
.where((Device device) => device.ephemeral == true)
714+
.toList();
715+
}
716+
return devices;
717+
}

packages/flutter_tools/test/runner/flutter_command_test.dart

+51
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import 'package:flutter_tools/src/cache.dart';
66
import 'package:flutter_tools/src/base/time.dart';
7+
import 'package:flutter_tools/src/device.dart';
8+
import 'package:flutter_tools/src/project.dart';
79
import 'package:flutter_tools/src/usage.dart';
810
import 'package:flutter_tools/src/base/common.dart';
911
import 'package:flutter_tools/src/runner/flutter_command.dart';
@@ -289,6 +291,43 @@ void main() {
289291
FlutterVersion: () => betaVersion,
290292
});
291293
});
294+
295+
group('Filter devices', () {
296+
MockDevice ephemeral;
297+
MockDevice nonEphemeralOne;
298+
MockDevice nonEphemeralTwo;
299+
MockDevice unsupported;
300+
301+
setUp(() {
302+
ephemeral = MockDevice(true);
303+
nonEphemeralOne = MockDevice(false);
304+
nonEphemeralTwo = MockDevice(false);
305+
unsupported = MockDevice(true, false);
306+
});
307+
308+
test('chooses ephemeral device', () {
309+
final List<Device> filtered = filterDevices(<Device>[
310+
ephemeral,
311+
nonEphemeralOne,
312+
nonEphemeralTwo,
313+
unsupported,
314+
]);
315+
316+
expect(filtered.single, ephemeral);
317+
});
318+
319+
test('does not remove all non-ephemeral', () {
320+
final List<Device> filtered = filterDevices(<Device>[
321+
nonEphemeralOne,
322+
nonEphemeralTwo,
323+
]);
324+
325+
expect(filtered, <Device>[
326+
nonEphemeralOne,
327+
nonEphemeralTwo,
328+
]);
329+
});
330+
});
292331
}
293332

294333

@@ -309,3 +348,15 @@ class FakeCommand extends FlutterCommand {
309348
}
310349

311350
class MockVersion extends Mock implements FlutterVersion {}
351+
352+
class MockDevice extends Mock implements Device {
353+
MockDevice(this.ephemeral, [this._isSupported = true]);
354+
355+
@override
356+
final bool ephemeral;
357+
358+
bool _isSupported;
359+
360+
@override
361+
bool isSupportedForProject(FlutterProject flutterProject) => _isSupported;
362+
}

0 commit comments

Comments
 (0)