Skip to content

Commit

Permalink
Add tests for RuntimeEnvironment. Respond to PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerhardt committed Apr 14, 2020
1 parent 735f7c1 commit 7f69728
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ internal enum Platform

internal static class RuntimeEnvironment
{
private static readonly string OverrideEnvironmentVariableName = "DOTNET_RUNTIME_ID";

private static readonly Lazy<Platform> _platform = new Lazy<Platform>(DetermineOSPlatform);
private static readonly Lazy<DistroInfo> _distroInfo = new Lazy<DistroInfo>(LoadDistroInfo);

public static Platform OperatingSystemPlatform { get; } = GetOSPlatform();
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
Expand Down Expand Up @@ -105,6 +109,7 @@ private static string GetRIDOS()
return "unknown";
}
}
#endif // TOOLSET_TASKS

private class DistroInfo
{
Expand Down
1 change: 1 addition & 0 deletions src/Layout/toolset-tasks/toolset-tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">$(SdkTargetFramework)</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);TOOLSET_TASKS</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 7f69728

Please sign in to comment.