Skip to content

Commit dbf54f9

Browse files
rmarinhoCopilot
andcommitted
Fix regex to use explicit adb states and support tab separators
- Replace \s{2,} with \s+ to handle tab-separated adb output - Use explicit state list (device|offline|unauthorized|etc.) instead of \S+ to prevent false positives from non-device lines - Add ParseAdbDevicesOutput_TabSeparator test Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f8fbca3 commit dbf54f9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Xamarin.Android.Tools.AndroidSdk/Runners/AdbRunner.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ public class AdbRunner
2424

2525
// Pattern to match device lines: <serial> <state> [key:value ...]
2626
// Requires 2+ spaces between serial and state (adb pads serials).
27-
// Matches known multi-word states (e.g. "no permissions") and any single-word state
28-
// so unknown states (recovery, sideload, etc.) are not silently dropped.
27+
// Matches known adb device states. Uses \s+ to handle both space and tab separators.
28+
// Explicit state list prevents false positives from non-device lines.
2929
static readonly Regex AdbDevicesRegex = new Regex (
30-
@"^([^\s]+)\s{2,}(no permissions|\S+)\s*(.*)$", RegexOptions.Compiled);
30+
@"^([^\s]+)\s+(device|offline|unauthorized|authorizing|no permissions|recovery|sideload|bootloader|connecting|host)\s*(.*)$",
31+
RegexOptions.Compiled | RegexOptions.IgnoreCase);
3132
static readonly Regex ApiRegex = new Regex (@"\bApi\b", RegexOptions.Compiled);
3233

3334
public AdbRunner (Func<string?> getSdkPath)

tests/Xamarin.Android.Tools.AndroidSdk-Tests/AdbRunnerTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ public void ParseAdbDevicesOutput_WindowsNewlines ()
215215
Assert.IsTrue (devices [0].IsEmulator);
216216
}
217217

218+
[Test]
219+
public void ParseAdbDevicesOutput_TabSeparator ()
220+
{
221+
var output =
222+
"List of devices attached\n" +
223+
"emulator-5554\tdevice\n" +
224+
"R5CR10YZQPJ\tdevice\n";
225+
226+
var devices = AdbRunner.ParseAdbDevicesOutput (output);
227+
228+
Assert.AreEqual (2, devices.Count);
229+
Assert.AreEqual ("emulator-5554", devices [0].Serial);
230+
Assert.AreEqual ("R5CR10YZQPJ", devices [1].Serial);
231+
}
232+
218233
[Test]
219234
public void ParseAdbDevicesOutput_IpPortDevice ()
220235
{

0 commit comments

Comments
 (0)