-
Notifications
You must be signed in to change notification settings - Fork 63
Fix KeyNotFoundException when simulator lacks runtimeIdentifier or version #1501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ec4450
Initial plan
Copilot 267167a
Fix SimulatorsCommand to skip entries without runtimeIdentifier and a…
Copilot de9abb5
Completed fix for SimulatorsCommand runtimeIdentifier issue
Copilot 4454559
Remove accidentally committed nuget.exe
Copilot dc4f0d9
Address review feedback: check version property and improve tests
Copilot bd2e30c
Fix KeyNotFoundException when simulator lacks runtimeIdentifier or ve…
Copilot b55c26a
Remove SimulatorsCommandTests as requested
Copilot f37a3e0
Remove nuget.exe and add to .gitignore
Copilot f7c5202
Remove unnecessary changes from gitignore
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
tests/Microsoft.DotNet.XHarness.CLI.Tests/Commands/Apple/SimulatorsCommandTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Text.Json; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.DotNet.XHarness.CLI.Tests.Commands.Apple; | ||
|
|
||
| public class SimulatorsCommandTests | ||
| { | ||
| [Fact] | ||
| public void ParseSimctlRuntimeList_WithoutRuntimeIdentifier_ShouldSkipEntry() | ||
| { | ||
| // This test validates the fix for the issue where simulators without runtimeIdentifier | ||
| // (like unusable simulators) should be skipped rather than causing an exception. | ||
|
|
||
| // Sample JSON similar to what's returned by xcrun simctl runtime list -j | ||
| // This includes an entry without runtimeIdentifier (the unusable simulator) | ||
| var json = @"{ | ||
| ""1F039DDE-73B1-4BF2-9FE0-7B089D0ABE5E"": { | ||
| ""build"": ""23J352"", | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.tvOS-26-0"", | ||
| ""version"": ""26.0"", | ||
| ""state"": ""Ready"" | ||
| }, | ||
| ""8728D520-0F86-4227-AE03-716249BBB18C"": { | ||
| ""identifier"": ""8728D520-0F86-4227-AE03-716249BBB18C"", | ||
| ""kind"": ""Cryptex Disk Image"", | ||
| ""state"": ""Unusable"", | ||
| ""signatureState"": ""Other Signature Error"" | ||
| }, | ||
| ""2A448011-F93C-427C-A6F2-CF5EFA39290F"": { | ||
| ""build"": ""23A343"", | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.iOS-26-0"", | ||
| ""version"": ""26.0"", | ||
| ""state"": ""Ready"" | ||
| } | ||
| }"; | ||
|
|
||
| var result = ParseSimulatorList(json, "com.apple.CoreSimulator.SimRuntime.iOS-26-0"); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Equal("26.0", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSimctlRuntimeList_WithoutVersionProperty_ShouldReturnNull() | ||
| { | ||
| // Test scenario where runtimeIdentifier exists but version property is missing | ||
| var json = @"{ | ||
| ""2A448011-F93C-427C-A6F2-CF5EFA39290F"": { | ||
| ""build"": ""23A343"", | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.iOS-26-0"", | ||
| ""state"": ""Ready"" | ||
| } | ||
| }"; | ||
|
|
||
| var result = ParseSimulatorList(json, "com.apple.CoreSimulator.SimRuntime.iOS-26-0"); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSimctlRuntimeList_WithOnlyUnusableSimulators_ShouldReturnNull() | ||
| { | ||
| // Test scenario where all entries lack runtimeIdentifier | ||
| var json = @"{ | ||
| ""8728D520-0F86-4227-AE03-716249BBB18C"": { | ||
| ""identifier"": ""8728D520-0F86-4227-AE03-716249BBB18C"", | ||
| ""kind"": ""Cryptex Disk Image"", | ||
| ""state"": ""Unusable"" | ||
| } | ||
| }"; | ||
|
|
||
| var result = ParseSimulatorList(json, "com.apple.CoreSimulator.SimRuntime.iOS-26-0"); | ||
|
|
||
| Assert.Null(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSimctlRuntimeList_WithValidSimulator_ShouldReturnVersion() | ||
| { | ||
| // Test normal scenario with valid simulator | ||
| var json = @"{ | ||
| ""2A448011-F93C-427C-A6F2-CF5EFA39290F"": { | ||
| ""build"": ""23A343"", | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.iOS-26-0"", | ||
| ""version"": ""26.0"", | ||
| ""state"": ""Ready"" | ||
| } | ||
| }"; | ||
|
|
||
| var result = ParseSimulatorList(json, "com.apple.CoreSimulator.SimRuntime.iOS-26-0"); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Equal("26.0", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ParseSimctlRuntimeList_WithMixedEntries_ShouldHandleGracefully() | ||
| { | ||
| // Test with multiple valid and invalid entries | ||
| var json = @"{ | ||
| ""entry1"": { | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.tvOS-25-0"", | ||
| ""version"": ""25.0"" | ||
| }, | ||
| ""entry2"": { | ||
| ""kind"": ""Unusable"" | ||
| }, | ||
| ""entry3"": { | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.iOS-26-0"" | ||
| }, | ||
| ""entry4"": { | ||
| ""runtimeIdentifier"": ""com.apple.CoreSimulator.SimRuntime.iOS-26-0"", | ||
| ""version"": ""26.0"" | ||
| } | ||
| }"; | ||
|
|
||
| var result = ParseSimulatorList(json, "com.apple.CoreSimulator.SimRuntime.iOS-26-0"); | ||
|
|
||
| // Should return null for entry3 (missing version) and find entry4 | ||
| Assert.Null(result); | ||
| } | ||
|
|
||
| // Helper method that replicates the parsing logic from SimulatorsCommand.IsInstalled | ||
| // This tests the specific JSON parsing behavior that was fixed | ||
| private static string? ParseSimulatorList(string json, string runtimeIdentifier) | ||
| { | ||
| var simulators = JsonDocument.Parse(json); | ||
|
|
||
| foreach (JsonProperty sim in simulators.RootElement.EnumerateObject()) | ||
| { | ||
| // Skip entries that don't have a runtimeIdentifier property (e.g., unusable simulators) | ||
| if (!sim.Value.TryGetProperty("runtimeIdentifier", out var runtimeIdProperty)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (runtimeIdProperty.GetString() == runtimeIdentifier) | ||
| { | ||
| // Also check if version property exists | ||
| if (!sim.Value.TryGetProperty("version", out var versionProperty)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return versionProperty.GetString(); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.