|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.DotNet.Cli.Build; |
| 9 | +using Microsoft.DotNet.Cli.Build.Framework; |
| 10 | +using Microsoft.DotNet.CoreSetup.Test; |
| 11 | +using Microsoft.DotNet.CoreSetup.Test.HostActivation; |
| 12 | +using Microsoft.DotNet.TestUtils; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace HostActivation.Tests |
| 16 | +{ |
| 17 | + public class HostCommands : IClassFixture<HostCommands.SharedTestState> |
| 18 | + { |
| 19 | + private SharedTestState SharedState { get; } |
| 20 | + |
| 21 | + public HostCommands(SharedTestState sharedState) |
| 22 | + { |
| 23 | + SharedState = sharedState; |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public void ListRuntimes_OtherArchitectures() |
| 28 | + { |
| 29 | + using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(SharedState.DotNet.GreatestVersionHostFxrFilePath)) |
| 30 | + { |
| 31 | + registeredInstallLocationOverride.SetInstallLocation(SharedState.OtherArchInstallLocations); |
| 32 | + foreach ((string arch, string installLocation) in SharedState.OtherArchInstallLocations) |
| 33 | + { |
| 34 | + // Verifiy exact match of command output. The output of --list-runtimes is intended to be machine-readable |
| 35 | + // and must not change in a way that breaks existing parsing. |
| 36 | + string expectedOutput = GetListRuntimesOutput(installLocation, SharedState.InstalledVersions); |
| 37 | + SharedState.DotNet.Exec("--list-runtimes", "--arch", arch) |
| 38 | + .ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride) |
| 39 | + .CaptureStdOut() |
| 40 | + .Execute() |
| 41 | + .Should().Pass() |
| 42 | + .And.HaveStdOut(expectedOutput); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void ListSdks_OtherArchitectures() |
| 49 | + { |
| 50 | + using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(SharedState.DotNet.GreatestVersionHostFxrFilePath)) |
| 51 | + { |
| 52 | + registeredInstallLocationOverride.SetInstallLocation(SharedState.OtherArchInstallLocations); |
| 53 | + foreach ((string arch, string installLocation) in SharedState.OtherArchInstallLocations) |
| 54 | + { |
| 55 | + // Verifiy exact match of command output. The output of --list-sdks is intended to be machine-readable |
| 56 | + // and must not change in a way that breaks existing parsing. |
| 57 | + string expectedOutput = GetListSdksOutput(installLocation, SharedState.InstalledVersions); |
| 58 | + SharedState.DotNet.Exec("--list-sdks", "--arch", arch) |
| 59 | + .ApplyRegisteredInstallLocationOverride(registeredInstallLocationOverride) |
| 60 | + .CaptureStdOut() |
| 61 | + .Execute() |
| 62 | + .Should().Pass() |
| 63 | + .And.HaveStdOut(expectedOutput); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static string GetListRuntimesOutput(string installLocation, string[] versions) |
| 69 | + { |
| 70 | + string runtimePath = Path.Combine(installLocation, "shared", Constants.MicrosoftNETCoreApp); |
| 71 | + return string.Join(string.Empty, versions.Select(v => $"{Constants.MicrosoftNETCoreApp} {v} [{runtimePath}]{Environment.NewLine}")); |
| 72 | + } |
| 73 | + |
| 74 | + private static string GetListSdksOutput(string installLocation, string[] versions) |
| 75 | + { |
| 76 | + string sdkPath = Path.Combine(installLocation, "sdk"); |
| 77 | + return string.Join(string.Empty, versions.Select(v => $"{v} [{sdkPath}]{Environment.NewLine}")); |
| 78 | + } |
| 79 | + |
| 80 | + public sealed class SharedTestState : IDisposable |
| 81 | + { |
| 82 | + public DotNetCli DotNet { get; } |
| 83 | + |
| 84 | + // Versions are assumed to be in ascending order. We use this property to check against the |
| 85 | + // exact expected output of --list-sdks and --list-runtimes. |
| 86 | + public string[] InstalledVersions { get; } = ["5.0.5", "10.0.0", "9999.0.1"]; |
| 87 | + |
| 88 | + public (string, string)[] OtherArchInstallLocations { get; } |
| 89 | + |
| 90 | + private TestArtifact _artifact; |
| 91 | + |
| 92 | + public SharedTestState() |
| 93 | + { |
| 94 | + _artifact = TestArtifact.Create(nameof(HostCommands)); |
| 95 | + |
| 96 | + var builder = new DotNetBuilder(_artifact.Location, TestContext.BuiltDotNet.BinPath, "exe"); |
| 97 | + foreach (string version in InstalledVersions) |
| 98 | + { |
| 99 | + builder.AddMicrosoftNETCoreAppFrameworkMockHostPolicy(version); |
| 100 | + builder.AddMockSDK(version, version); |
| 101 | + } |
| 102 | + |
| 103 | + DotNet = builder.Build(); |
| 104 | + |
| 105 | + // Add runtimes and SDKs for other architectures |
| 106 | + string[] otherArchs = new[] { "arm", "arm64", "x64", "x86" }.Where(a => a != TestContext.BuildArchitecture).ToArray(); |
| 107 | + OtherArchInstallLocations = new (string, string)[otherArchs.Length]; |
| 108 | + for (int i = 0; i < otherArchs.Length; i++) |
| 109 | + { |
| 110 | + string arch = otherArchs[i]; |
| 111 | + string installLocation = Directory.CreateDirectory(Path.Combine(_artifact.Location, arch)).FullName; |
| 112 | + foreach (string version in InstalledVersions) |
| 113 | + { |
| 114 | + DotNetBuilder.AddMicrosoftNETCoreAppFrameworkMockHostPolicy(installLocation, version); |
| 115 | + DotNetBuilder.AddMockSDK(installLocation, version, version); |
| 116 | + } |
| 117 | + |
| 118 | + OtherArchInstallLocations[i] = (arch, installLocation); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + // Enable test-only behavior for the copied .NET. We don't bother disabling the behaviour later, |
| 123 | + // as we just delete the entire copy after the tests run. |
| 124 | + _ = TestOnlyProductBehavior.Enable(DotNet.GreatestVersionHostFxrFilePath); |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + public void Dispose() |
| 129 | + { |
| 130 | + _artifact.Dispose(); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments