|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using Microsoft.DotNet.Tools.Test.Utilities;
|
| 5 | +using Newtonsoft.Json.Linq; |
| 6 | +using System.Runtime.InteropServices; |
5 | 7 |
|
6 | 8 | namespace Microsoft.DotNet.Restore.Test
|
7 | 9 | {
|
@@ -167,6 +169,115 @@ public void ItAcceptsArgumentsAfterProperties()
|
167 | 169 | .Should()
|
168 | 170 | .Pass();
|
169 | 171 | }
|
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Tests for RID-specific restore options: -r/--runtime, --os, and -a/--arch |
| 175 | + /// </summary> |
| 176 | + [Theory] |
| 177 | + [InlineData("-r", "linux-x64")] |
| 178 | + [InlineData("--runtime", "win-x64")] |
| 179 | + [InlineData("--os", "linux")] |
| 180 | + [InlineData("-a", "arm64")] |
| 181 | + [InlineData("--arch", "x64")] |
| 182 | + [InlineData("--os", "linux", "-a", "arm64")] |
| 183 | + public void ItRestoresWithRidSpecificOptions(params string[] ridOptions) |
| 184 | + { |
| 185 | + // Skip test for #24251 |
| 186 | + var testProject = new TestProject() |
| 187 | + { |
| 188 | + Name = "RestoreWithRidOptions", |
| 189 | + TargetFrameworks = ToolsetInfo.CurrentTargetFramework, |
| 190 | + }; |
| 191 | + |
| 192 | + testProject.PackageReferences.Add(new TestPackageReference("Newtonsoft.Json", ToolsetInfo.GetNewtonsoftJsonPackageVersion())); |
| 193 | + |
| 194 | + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: string.Join("_", ridOptions)); |
| 195 | + |
| 196 | + var rootPath = Path.Combine(testAsset.TestRoot, testProject.Name); |
| 197 | + |
| 198 | + // Create the command with the RID-specific options |
| 199 | + var restoreCommand = new DotnetRestoreCommand(Log) |
| 200 | + .WithWorkingDirectory(rootPath) |
| 201 | + .Execute(ridOptions); |
| 202 | + |
| 203 | + // Verify that the command runs successfully |
| 204 | + restoreCommand.Should().Pass(); |
| 205 | + |
| 206 | + // Verify that assets file was created |
| 207 | + var assetsFilePath = Path.Combine(rootPath, "obj", "project.assets.json"); |
| 208 | + File.Exists(assetsFilePath).Should().BeTrue(); |
| 209 | + |
| 210 | + // Verify that the assets file contains the expected RID-specific target |
| 211 | + var assetsContents = JObject.Parse(File.ReadAllText(assetsFilePath)); |
| 212 | + var targets = assetsContents["targets"]; |
| 213 | + targets.Should().NotBeNull("assets file should contain targets section"); |
| 214 | + |
| 215 | + // Determine the expected RID based on the options provided |
| 216 | + string expectedRid = GetExpectedRid(ridOptions); |
| 217 | + string expectedTarget = $"{ToolsetInfo.CurrentTargetFramework}/{expectedRid}"; |
| 218 | + |
| 219 | + // Check that the specific target exists |
| 220 | + var specificTarget = targets[expectedTarget]; |
| 221 | + specificTarget.Should().NotBeNull($"assets file should contain target '{expectedTarget}' when using RID options: {string.Join(" ", ridOptions)}"); |
| 222 | + } |
| 223 | + |
| 224 | + private static string GetExpectedRid(string[] ridOptions) |
| 225 | + { |
| 226 | + // Check if explicit runtime is provided |
| 227 | + for (int i = 0; i < ridOptions.Length; i++) |
| 228 | + { |
| 229 | + if ((ridOptions[i] == "-r" || ridOptions[i] == "--runtime") && i + 1 < ridOptions.Length) |
| 230 | + { |
| 231 | + return ridOptions[i + 1]; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + // Get current platform defaults |
| 236 | + string currentOs = GetCurrentOsPart(); |
| 237 | + string currentArch = GetCurrentArchPart(); |
| 238 | + |
| 239 | + // Check for --os and --arch options to synthesize RID |
| 240 | + string targetOs = currentOs; |
| 241 | + string targetArch = currentArch; |
| 242 | + |
| 243 | + for (int i = 0; i < ridOptions.Length; i++) |
| 244 | + { |
| 245 | + if (ridOptions[i] == "--os" && i + 1 < ridOptions.Length) |
| 246 | + { |
| 247 | + targetOs = ridOptions[i + 1]; |
| 248 | + } |
| 249 | + else if ((ridOptions[i] == "-a" || ridOptions[i] == "--arch") && i + 1 < ridOptions.Length) |
| 250 | + { |
| 251 | + targetArch = ridOptions[i + 1]; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + return $"{targetOs}-{targetArch}"; |
| 256 | + } |
| 257 | + |
| 258 | + private static string GetCurrentOsPart() |
| 259 | + { |
| 260 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 261 | + return "win"; |
| 262 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 263 | + return "linux"; |
| 264 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |
| 265 | + return "osx"; |
| 266 | + else |
| 267 | + throw new PlatformNotSupportedException("Unsupported platform for RID determination"); |
| 268 | + } |
| 269 | + |
| 270 | + private static string GetCurrentArchPart() |
| 271 | + { |
| 272 | + return RuntimeInformation.OSArchitecture switch |
| 273 | + { |
| 274 | + Architecture.X64 => "x64", |
| 275 | + Architecture.X86 => "x86", |
| 276 | + Architecture.Arm64 => "arm64", |
| 277 | + Architecture.Arm => "arm", |
| 278 | + _ => throw new PlatformNotSupportedException($"Unsupported architecture: {RuntimeInformation.OSArchitecture}") |
| 279 | + }; |
| 280 | + } |
170 | 281 |
|
171 | 282 | private static string[] HandleStaticGraphEvaluation(bool useStaticGraphEvaluation, string[] args) =>
|
172 | 283 | useStaticGraphEvaluation ?
|
|
0 commit comments