fix(android): require --device when several devices are connected

With no serial requested and more than one device online, pickDevice silently returned connected[0], but that serial is never threaded into the per-step adb calls, so every later bare adb command failed with "more than one device". Error instead and ask for --device, mirroring pickAVD; a single device stays unambiguous.
This commit is contained in:
pj committed 2026-06-11 09:26:01 +05:30
1 parent 194866511f
commit 600fb71c5b
2 files changed
+23 -6

No files matched your search

+11 -4
View File
@@ -360,8 +360,11 @@ func parseAVDList(output string) []string {
}
// pickDevice resolves which connected device to drive. A requested serial must
// be online; with no request the first connected device is used, else found is
// false so the caller falls back to booting an AVD.
// be online. With no request: a single connected device is used; more than one
// is ambiguous and errors asking for --device, because the chosen serial is not
// threaded into the per-step adb calls, so silently picking one would leave
// every later bare `adb` command failing with "more than one device". No device
// connected returns found=false so the caller falls back to booting an AVD.
func pickDevice(requested string, connected []string) (serial string, found bool, err error) {
if requested != "" {
if !slices.Contains(connected, requested) {
@@ -369,10 +372,14 @@ func pickDevice(requested string, connected []string) (serial string, found bool
}
return requested, true, nil
}
if len(connected) > 0 {
switch len(connected) {
case 0:
return "", false, nil
case 1:
return connected[0], true, nil
default:
return "", false, fmt.Errorf("%d devices connected (%s); select one with --device", len(connected), strings.Join(connected, ", "))
}
return "", false, nil
}
func pickAVD(requested string, available []string) (string, error) {
+12 -2
View File
@@ -47,13 +47,23 @@ func TestPickDevice_RequestedNotConnected(t *testing.T) {
}
}
func TestPickDevice_NoRequestUsesFirst(t *testing.T) {
serial, found, err := pickDevice("", []string{"emulator-5554", "physical-abc"})
func TestPickDevice_NoRequestSingleDeviceUsesIt(t *testing.T) {
serial, found, err := pickDevice("", []string{"emulator-5554"})
if err != nil || !found || serial != "emulator-5554" {
t.Fatalf("got (%q, %v, %v), want (emulator-5554, true, nil)", serial, found, err)
}
}
func TestPickDevice_NoRequestMultipleDevicesErrors(t *testing.T) {
serial, found, err := pickDevice("", []string{"emulator-5554", "physical-abc"})
if err == nil {
t.Fatal("expected an error asking for --device when several devices are connected")
}
if found || serial != "" {
t.Fatalf("ambiguous selection must not pick a device, got (%q, %v)", serial, found)
}
}
func TestPickDevice_NoneConnectedFallsBackToAVD(t *testing.T) {
serial, found, err := pickDevice("", nil)
if err != nil || found || serial != "" {