mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-04 12:07:09 +00:00
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:
1 parent
194866511f
commit
600fb71c5b
2 files changed
+23
-6
No files matched your search
@@ -360,8 +360,11 @@ func parseAVDList(output string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// pickDevice resolves which connected device to drive. A requested serial must
|
// 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
|
// be online. With no request: a single connected device is used; more than one
|
||||||
// false so the caller falls back to booting an AVD.
|
// 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) {
|
func pickDevice(requested string, connected []string) (serial string, found bool, err error) {
|
||||||
if requested != "" {
|
if requested != "" {
|
||||||
if !slices.Contains(connected, requested) {
|
if !slices.Contains(connected, requested) {
|
||||||
@@ -369,10 +372,14 @@ func pickDevice(requested string, connected []string) (serial string, found bool
|
|||||||
}
|
}
|
||||||
return requested, true, nil
|
return requested, true, nil
|
||||||
}
|
}
|
||||||
if len(connected) > 0 {
|
switch len(connected) {
|
||||||
|
case 0:
|
||||||
|
return "", false, nil
|
||||||
|
case 1:
|
||||||
return connected[0], true, nil
|
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) {
|
func pickAVD(requested string, available []string) (string, error) {
|
||||||
|
|||||||
@@ -47,13 +47,23 @@ func TestPickDevice_RequestedNotConnected(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPickDevice_NoRequestUsesFirst(t *testing.T) {
|
func TestPickDevice_NoRequestSingleDeviceUsesIt(t *testing.T) {
|
||||||
serial, found, err := pickDevice("", []string{"emulator-5554", "physical-abc"})
|
serial, found, err := pickDevice("", []string{"emulator-5554"})
|
||||||
if err != nil || !found || serial != "emulator-5554" {
|
if err != nil || !found || serial != "emulator-5554" {
|
||||||
t.Fatalf("got (%q, %v, %v), want (emulator-5554, true, nil)", serial, found, err)
|
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) {
|
func TestPickDevice_NoneConnectedFallsBackToAVD(t *testing.T) {
|
||||||
serial, found, err := pickDevice("", nil)
|
serial, found, err := pickDevice("", nil)
|
||||||
if err != nil || found || serial != "" {
|
if err != nil || found || serial != "" {
|
||||||
|
|||||||
Reference in new issue
Block a user