Skip to content

Commit 611c7ed

Browse files
fix(apple): parse parenthesized xctrace physical device format (#1360)
* fix(apple): parse parenthesized xctrace physical device format Accept the new 'Device Name (OS Version) (device-id)' output from xctrace list devices while preserving the legacy bracket format. Relates to #1355 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(apple): split xctrace parser to reduce cyclomatic complexity Extract device line parsing and DeviceInfo construction into focused helpers so the discovery loop stays under fallow thresholds. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(apple): avoid interpreting bracket-format device names as having OS version Only treat 'Name (version) (id)' as the new parenthesized xctrace format; preserve the full name for legacy 'Name [id]' lines, including names that contain parentheses. Addresses review feedback in #1360. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(apple): add route-specific xctrace fallback test When devicectl reports no devices, listAppleDevices must source the physical device from the parenthesized xctrace output. Addresses review feedback in #1360. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Michał Pierzchała <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent c84b449 commit 611c7ed

2 files changed

Lines changed: 147 additions & 19 deletions

File tree

‎src/platforms/apple/core/__tests__/devices.test.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,74 @@ test('parseXctracePhysicalAppleDevices tags Apple Vision devices as visionOS', (
231231
]);
232232
});
233233

234+
test('parseXctracePhysicalAppleDevices parses the parenthesized physical device format', () => {
235+
const parsed = parseXctracePhysicalAppleDevices(
236+
[
237+
'== Devices ==',
238+
'iPhone 8 Plus (16.7.16) (00008020-001C2D2234567890)',
239+
'Studio iPad Pro (17.0) (ipad-udid-1)',
240+
'Living Room Apple TV (16.0) (tv-udid-1)',
241+
].join('\n'),
242+
);
243+
244+
assert.deepEqual(parsed, [
245+
{
246+
platform: 'apple',
247+
id: '00008020-001C2D2234567890',
248+
name: 'iPhone 8 Plus',
249+
kind: 'device',
250+
target: 'mobile',
251+
appleOs: 'ios',
252+
booted: true,
253+
},
254+
{
255+
platform: 'apple',
256+
id: 'ipad-udid-1',
257+
name: 'Studio iPad Pro',
258+
kind: 'device',
259+
target: 'mobile',
260+
appleOs: 'ipados',
261+
booted: true,
262+
},
263+
{
264+
platform: 'apple',
265+
id: 'tv-udid-1',
266+
name: 'Living Room Apple TV',
267+
kind: 'device',
268+
target: 'tv',
269+
appleOs: 'tvos',
270+
booted: true,
271+
},
272+
]);
273+
});
274+
275+
test('parseXctracePhysicalAppleDevices preserves parentheses in bracket-format names', () => {
276+
const parsed = parseXctracePhysicalAppleDevices(
277+
['== Devices ==', "Alex's (iPhone) [alex-udid]", 'Office iPhone (2) [office-udid]'].join('\n'),
278+
);
279+
280+
assert.deepEqual(parsed, [
281+
{
282+
platform: 'apple',
283+
id: 'alex-udid',
284+
name: "Alex's (iPhone)",
285+
kind: 'device',
286+
target: 'mobile',
287+
appleOs: 'ios',
288+
booted: true,
289+
},
290+
{
291+
platform: 'apple',
292+
id: 'office-udid',
293+
name: 'Office iPhone (2)',
294+
kind: 'device',
295+
target: 'mobile',
296+
appleOs: 'ios',
297+
booted: true,
298+
},
299+
]);
300+
});
301+
234302
test('listAppleDevices tags devicectl iPad product types as iPadOS', async () => {
235303
mockRunCommand = async (_cmd, args) => {
236304
if (args.join(' ') === 'simctl list devices -j') {
@@ -499,6 +567,43 @@ test('listAppleDevices prefers devicectl metadata when xctrace reports the same
499567
assert.equal(physicalDevices[0]?.name, 'Primary Name');
500568
});
501569

570+
test('listAppleDevices falls back to xctrace parenthesized devices when devicectl reports none', async () => {
571+
mockRunCommand = async (_cmd, args) => {
572+
if (args.join(' ') === 'simctl list devices -j') {
573+
return { stdout: createSimctlDevicesPayload(), stderr: '', exitCode: 0 };
574+
}
575+
576+
if (args[0] === 'devicectl' && args[1] === 'list' && args[2] === 'devices') {
577+
const jsonPath = String(args[4]);
578+
await fs.writeFile(jsonPath, JSON.stringify({ result: { devices: [] } }), 'utf8');
579+
return { stdout: '', stderr: '', exitCode: 0 };
580+
}
581+
582+
if (args.join(' ') === 'xctrace list devices') {
583+
return {
584+
stdout: ['== Devices ==', 'iPhone 8 Plus (16.7.16) (00008020-001C2D2234567890)'].join('\n'),
585+
stderr: '',
586+
exitCode: 0,
587+
};
588+
}
589+
590+
throw new Error(`unexpected xcrun args: ${args.join(' ')}`);
591+
};
592+
593+
const devices = await withMockedPlatform(
594+
'darwin',
595+
async () => await withMockedAppleTools(async () => await listAppleDevices()),
596+
);
597+
598+
const physicalDevices = devices.filter(
599+
(device) => device.kind === 'device' && isIosFamily(device),
600+
);
601+
602+
assert.equal(physicalDevices.length, 1);
603+
assert.equal(physicalDevices[0]?.id, '00008020-001C2D2234567890');
604+
assert.equal(physicalDevices[0]?.name, 'iPhone 8 Plus');
605+
});
606+
502607
test('listAppleDevices keeps physical discovery disabled for simulator-set scoped runs', async () => {
503608
mockRunCommand = async (_cmd, args) => {
504609
if (args.includes('simctl') && args.includes('list') && args.includes('devices')) {

‎src/platforms/apple/core/devices.ts‎

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ type IosDeviceDiscoveryOptions = {
5858
};
5959

6060
const XCTRACE_SECTION_HEADER_PATTERN = /^==\s*(.+?)\s*==$/;
61-
const XCTRACE_DEVICE_LINE_PATTERN = /^(?<name>.+?)\s+\[(?<id>[^[\]]+)\]\s*$/;
61+
const XCTRACE_DEVICE_LINE_PATTERN =
62+
/^(?<name>.+?)\s+(?:\[(?<idBracket>[^[\]]+)\]|\((?<osVersion>[^)]+)\)\s+\((?<idParen>[^)]+)\))\s*$/;
6263

6364
function normalizeAppleDescriptor(value: string | undefined): string {
6465
return (value ?? '').trim().toLowerCase();
@@ -254,6 +255,42 @@ function mapDevicectlAppleDevices(payload: DevicectlListDevicesPayload): DeviceI
254255
return devices;
255256
}
256257

258+
function parseXctraceDeviceLine(
259+
line: string,
260+
): { name: string; id: string; osVersion?: string } | null {
261+
const match = XCTRACE_DEVICE_LINE_PATTERN.exec(line);
262+
if (!match?.groups) return null;
263+
264+
const name = match.groups.name?.trim() ?? '';
265+
const id = match.groups.idBracket?.trim() ?? match.groups.idParen?.trim() ?? '';
266+
const osVersion = match.groups.osVersion?.trim();
267+
if (!name || !id) return null;
268+
269+
return { name, id, osVersion };
270+
}
271+
272+
function buildXctracePhysicalDevice(
273+
name: string,
274+
id: string,
275+
osVersion: string | undefined,
276+
): DeviceInfo | null {
277+
const target = resolveAppleTargetFromLabel(name);
278+
if (!target) return null;
279+
280+
return {
281+
platform: 'apple',
282+
id,
283+
name,
284+
kind: 'device',
285+
target,
286+
appleOs: resolveAppleOs(target, osVersion ? [name, osVersion] : [name]),
287+
// xctrace lists currently connected devices in the "Devices" section.
288+
// The "Devices Offline" section is excluded above, so treating these as
289+
// booted preserves the existing physical-device selection semantics.
290+
booted: true,
291+
};
292+
}
293+
257294
export function parseXctracePhysicalAppleDevices(output: string): DeviceInfo[] {
258295
const devices: DeviceInfo[] = [];
259296
let section: string | null = null;
@@ -270,25 +307,11 @@ export function parseXctracePhysicalAppleDevices(output: string): DeviceInfo[] {
270307

271308
if (section !== 'Devices') continue;
272309

273-
const deviceMatch = XCTRACE_DEVICE_LINE_PATTERN.exec(line);
274-
const id = deviceMatch?.groups?.id?.trim() ?? '';
275-
const name = deviceMatch?.groups?.name?.trim() ?? '';
276-
if (!id || !name) continue;
277-
const target = resolveAppleTargetFromLabel(name);
278-
if (!target) continue;
310+
const parsedLine = parseXctraceDeviceLine(line);
311+
if (!parsedLine) continue;
279312

280-
devices.push({
281-
platform: 'apple',
282-
id,
283-
name,
284-
kind: 'device',
285-
target,
286-
appleOs: resolveAppleOs(target, [name]),
287-
// xctrace lists currently connected devices in the "Devices" section.
288-
// The "Devices Offline" section is excluded above, so treating these as
289-
// booted preserves the existing physical-device selection semantics.
290-
booted: true,
291-
});
313+
const device = buildXctracePhysicalDevice(parsedLine.name, parsedLine.id, parsedLine.osVersion);
314+
if (device) devices.push(device);
292315
}
293316

294317
return devices;

0 commit comments

Comments
 (0)