From 7f6972851bf392b729abc605b8671912afdd29e1 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Tue, 14 Apr 2020 15:02:00 -0500 Subject: [PATCH] Add tests for RuntimeEnvironment. Respond to PR feedback. --- .../RuntimeEnvironment.cs | 9 ++- src/Layout/toolset-tasks/toolset-tasks.csproj | 1 + .../RuntimeEnvironmentTests.cs | 57 +++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/Tests/Microsoft.DotNet.Cli.Utils.Tests/RuntimeEnvironmentTests.cs diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs index e15cd0661ad5..037fb5908ddc 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs @@ -18,8 +18,6 @@ internal enum Platform internal static class RuntimeEnvironment { - private static readonly string OverrideEnvironmentVariableName = "DOTNET_RUNTIME_ID"; - private static readonly Lazy _platform = new Lazy(DetermineOSPlatform); private static readonly Lazy _distroInfo = new Lazy(LoadDistroInfo); @@ -27,6 +25,12 @@ internal static class RuntimeEnvironment public static string OperatingSystemVersion { get; } = GetOSVersion(); public static string OperatingSystem { get; } = GetOSName(); + // toolset-tasks.csproj needs to build for full .NET Framework, and needs to get the current + // RuntimeIdentifier of the machine. Because of this, it needs to implement + // it's own GetRuntimeIdentifier(). All other places should use RuntimeInformation.RuntimeIdentifier. +#if TOOLSET_TASKS + private static readonly string OverrideEnvironmentVariableName = "DOTNET_RUNTIME_ID"; + public static string GetRuntimeIdentifier() { return @@ -105,6 +109,7 @@ private static string GetRIDOS() return "unknown"; } } +#endif // TOOLSET_TASKS private class DistroInfo { diff --git a/src/Layout/toolset-tasks/toolset-tasks.csproj b/src/Layout/toolset-tasks/toolset-tasks.csproj index 9b51bae96ae7..9b9e6245be6d 100644 --- a/src/Layout/toolset-tasks/toolset-tasks.csproj +++ b/src/Layout/toolset-tasks/toolset-tasks.csproj @@ -3,6 +3,7 @@ $(SdkTargetFramework);net472 $(SdkTargetFramework) true + $(DefineConstants);TOOLSET_TASKS diff --git a/src/Tests/Microsoft.DotNet.Cli.Utils.Tests/RuntimeEnvironmentTests.cs b/src/Tests/Microsoft.DotNet.Cli.Utils.Tests/RuntimeEnvironmentTests.cs new file mode 100644 index 000000000000..2984c7ef7572 --- /dev/null +++ b/src/Tests/Microsoft.DotNet.Cli.Utils.Tests/RuntimeEnvironmentTests.cs @@ -0,0 +1,57 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.NET.TestFramework; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.DotNet.Cli.Utils.Tests +{ + public class RuntimeEnvironmentTests : SdkTest + { + public RuntimeEnvironmentTests(ITestOutputHelper log) : base(log) + { + } + + [WindowsOnlyFact] + public void VerifyWindows() + { + Assert.Equal(Platform.Windows, RuntimeEnvironment.OperatingSystemPlatform); + Assert.Equal("Windows", RuntimeEnvironment.OperatingSystem); + + VerifyOperatingSystemVersionEqualsEnvironmentOSVersion(); + } + + [MacOsOnlyFact] + public void VerifyMacOs() + { + Assert.Equal(Platform.Darwin, RuntimeEnvironment.OperatingSystemPlatform); + Assert.Equal("Mac OS X", RuntimeEnvironment.OperatingSystem); + + VerifyOperatingSystemVersionEqualsEnvironmentOSVersion(); + } + + private void VerifyOperatingSystemVersionEqualsEnvironmentOSVersion() + { + Version osVersion = Version.Parse(RuntimeEnvironment.OperatingSystemVersion); + Version expectedOSVersion = Environment.OSVersion.Version; + + Assert.Equal(expectedOSVersion.Major, osVersion.Major); + Assert.Equal(expectedOSVersion.Minor, osVersion.Minor); + Assert.Equal(expectedOSVersion.Build, osVersion.Build); + } + + [LinuxOnlyFact] + public void VerifyLinux() + { + Assert.Equal(Platform.Linux, RuntimeEnvironment.OperatingSystemPlatform); + + // ensure OperatingSystem and OperatingSystemVersion are aligned with the current RID + Assert.StartsWith( + $"{RuntimeEnvironment.OperatingSystem.ToLowerInvariant()}.{RuntimeEnvironment.OperatingSystemVersion}", + RuntimeInformation.RuntimeIdentifier); + } + } +}