Skip to content

Commit 3d2b22a

Browse files
ci: fix iOS Device Tests (#4447)
--------- Co-authored-by: James Crosswell <[email protected]>
1 parent b6fb323 commit 3d2b22a

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

scripts/device-test.ps1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ param(
1111

1212
Set-StrictMode -Version latest
1313
$ErrorActionPreference = 'Stop'
14+
. $PSScriptRoot/ios-simulator-utils.ps1
1415

1516
if (!$Build -and !$Run)
1617
{
@@ -57,8 +58,15 @@ try
5758
'--app', "$buildDir/Sentry.Maui.Device.TestApp.app",
5859
'--target', 'ios-simulator-64',
5960
'--launch-timeout', '00:10:00',
60-
'--set-env', 'CI=$envValue'
61+
'--set-env', "CI=$envValue"
6162
)
63+
64+
$udid = Get-IosSimulatorUdid -IosVersion '18.5' -Verbose
65+
if ($udid) {
66+
$arguments += @('--device', $udid)
67+
} else {
68+
Write-Host "No suitable simulator found; proceeding without a specific --device"
69+
}
6270
}
6371

6472
if ($Build)
@@ -76,7 +84,7 @@ try
7684
if (!(Get-Command xharness -ErrorAction SilentlyContinue))
7785
{
7886
Push-Location ($CI ? $env:RUNNER_TEMP : $IsWindows ? $env:TMP : $IsMacos ? $env:TMPDIR : '/temp')
79-
dotnet tool install Microsoft.DotNet.XHarness.CLI --global --version '10.0.0-prerelease.25330.2' `
87+
dotnet tool install Microsoft.DotNet.XHarness.CLI --global --version '10.0.0-prerelease.25412.1' `
8088
--add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json
8189
Pop-Location
8290
}

scripts/ios-simulator-utils.ps1

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
function Get-IosSimulatorUdid {
2+
[CmdletBinding()]
3+
param(
4+
[string]$IosVersion = '18.5',
5+
[string[]]$PreferredDeviceTypes = @(
6+
'com.apple.CoreSimulator.SimDeviceType.iPhone-XS',
7+
'com.apple.CoreSimulator.SimDeviceType.iPhone-16',
8+
'com.apple.CoreSimulator.SimDeviceType.iPhone-15'
9+
)
10+
)
11+
12+
try {
13+
$simDevices = & xcrun simctl list devices --json | ConvertFrom-Json
14+
} catch {
15+
Write-Verbose "Failed to query simctl: $($_.Exception.Message)"
16+
return $null
17+
}
18+
if (-not $simDevices -or -not $simDevices.devices) {
19+
Write-Verbose "No devices structure returned."
20+
return $null
21+
}
22+
23+
$devicesByRuntime = $simDevices.devices
24+
$preferredIndex = @{}
25+
for ($i = 0; $i -lt $PreferredDeviceTypes.Count; $i++) {
26+
$preferredIndex[$PreferredDeviceTypes[$i]] = $i
27+
}
28+
29+
$dashVer = $IosVersion -replace '\.','-'
30+
$exactKey = "com.apple.CoreSimulator.SimRuntime.iOS-$dashVer"
31+
$runtimeKey = $null
32+
33+
$allRuntimeNames = $devicesByRuntime.PSObject.Properties.Name
34+
if ($allRuntimeNames -contains $exactKey) {
35+
$runtimeKey = $exactKey
36+
Write-Verbose "Found exact runtime: $runtimeKey"
37+
} else {
38+
$major = ($IosVersion.Split('.')[0])
39+
$candidates = $allRuntimeNames | Where-Object { $_ -match "com\.apple\.CoreSimulator\.SimRuntime\.iOS-$major-" }
40+
if ($candidates) {
41+
$runtimeKey = $candidates |
42+
Sort-Object {
43+
$v = ($_ -replace '.*iOS-','') -replace '-','.'
44+
try { [Version]$v } catch { [Version]'0.0' }
45+
} -Descending |
46+
Select-Object -First 1
47+
Write-Verbose "Exact runtime $exactKey not found. Using fallback runtime $runtimeKey"
48+
} else {
49+
Write-Verbose "No simulator runtime found for iOS major $major"
50+
return $null
51+
}
52+
}
53+
54+
$runtimeDevices = $devicesByRuntime.PSObject.Properties |
55+
Where-Object { $_.Name -eq $runtimeKey } |
56+
Select-Object -ExpandProperty Value
57+
58+
if (-not $runtimeDevices) {
59+
Write-Verbose "Runtime key $runtimeKey present but no devices listed."
60+
return $null
61+
}
62+
63+
$usable = $runtimeDevices | Where-Object { $_.isAvailable -and $_.state -in @('Shutdown','Booted') }
64+
if (-not $usable) {
65+
Write-Verbose "No available devices in runtime $runtimeKey"
66+
return $null
67+
}
68+
69+
$ranked = $usable | ForEach-Object {
70+
$dt = $_.deviceTypeIdentifier
71+
$weightPref = if ($preferredIndex.ContainsKey($dt)) { $preferredIndex[$dt] } else { 9999 }
72+
$weightFamily = if ($dt -match 'iPhone') { 0 } else { 1 } # prefer iPhone if not explicitly listed
73+
[PSCustomObject]@{
74+
Device = $_
75+
WeightPref = $weightPref
76+
WeightFamily = $weightFamily
77+
WeightBoot = if ($_.state -eq 'Booted') { 0 } else { 1 }
78+
SortName = $_.name
79+
}
80+
}
81+
82+
$sorted = $ranked | Sort-Object WeightPref, WeightFamily, WeightBoot, SortName
83+
$sorted | Select-Object -First 5 | ForEach-Object {
84+
Write-Verbose ("Candidate: {0} | {1} | pref={2} fam={3} bootW={4}" -f $_.Device.name, $_.Device.deviceTypeIdentifier, $_.WeightPref, $_.WeightFamily, $_.WeightBoot)
85+
}
86+
87+
$selected = $sorted | Select-Object -First 1
88+
if (-not $selected) {
89+
Write-Verbose "Failed to select a simulator."
90+
return $null
91+
}
92+
93+
Write-Verbose ("Selected simulator: {0} ({1}) [{2}]" -f $selected.Device.name, $selected.Device.deviceTypeIdentifier, $selected.Device.udid)
94+
return $selected.Device.udid
95+
}

test/Sentry.Maui.Device.TestApp/Sentry.Maui.Device.TestApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
3535
<!-- Pin target iOS version so that our tests don't break when new versions of Xcode are released.
3636
'net8.0-ios' resolves the latest version of the iOS SDK otherwise. -->
37-
<TargetPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">18</TargetPlatformVersion>
37+
<TargetPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">18.0</TargetPlatformVersion>
3838

3939
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
4040

0 commit comments

Comments
 (0)