Skip to content

Commit 7f69728

Browse files
committed
Add tests for RuntimeEnvironment. Respond to PR feedback.
1 parent 735f7c1 commit 7f69728

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/Cli/Microsoft.DotNet.Cli.Utils/RuntimeEnvironment.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ internal enum Platform
1818

1919
internal static class RuntimeEnvironment
2020
{
21-
private static readonly string OverrideEnvironmentVariableName = "DOTNET_RUNTIME_ID";
22-
2321
private static readonly Lazy<Platform> _platform = new Lazy<Platform>(DetermineOSPlatform);
2422
private static readonly Lazy<DistroInfo> _distroInfo = new Lazy<DistroInfo>(LoadDistroInfo);
2523

2624
public static Platform OperatingSystemPlatform { get; } = GetOSPlatform();
2725
public static string OperatingSystemVersion { get; } = GetOSVersion();
2826
public static string OperatingSystem { get; } = GetOSName();
2927

28+
// toolset-tasks.csproj needs to build for full .NET Framework, and needs to get the current
29+
// RuntimeIdentifier of the machine. Because of this, it needs to implement
30+
// it's own GetRuntimeIdentifier(). All other places should use RuntimeInformation.RuntimeIdentifier.
31+
#if TOOLSET_TASKS
32+
private static readonly string OverrideEnvironmentVariableName = "DOTNET_RUNTIME_ID";
33+
3034
public static string GetRuntimeIdentifier()
3135
{
3236
return
@@ -105,6 +109,7 @@ private static string GetRIDOS()
105109
return "unknown";
106110
}
107111
}
112+
#endif // TOOLSET_TASKS
108113

109114
private class DistroInfo
110115
{

src/Layout/toolset-tasks/toolset-tasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<TargetFrameworks>$(SdkTargetFramework);net472</TargetFrameworks>
44
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">$(SdkTargetFramework)</TargetFrameworks>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
<DefineConstants>$(DefineConstants);TOOLSET_TASKS</DefineConstants>
67
</PropertyGroup>
78

89
<ItemGroup>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using Microsoft.NET.TestFramework;
7+
using Xunit;
8+
using Xunit.Abstractions;
9+
10+
namespace Microsoft.DotNet.Cli.Utils.Tests
11+
{
12+
public class RuntimeEnvironmentTests : SdkTest
13+
{
14+
public RuntimeEnvironmentTests(ITestOutputHelper log) : base(log)
15+
{
16+
}
17+
18+
[WindowsOnlyFact]
19+
public void VerifyWindows()
20+
{
21+
Assert.Equal(Platform.Windows, RuntimeEnvironment.OperatingSystemPlatform);
22+
Assert.Equal("Windows", RuntimeEnvironment.OperatingSystem);
23+
24+
VerifyOperatingSystemVersionEqualsEnvironmentOSVersion();
25+
}
26+
27+
[MacOsOnlyFact]
28+
public void VerifyMacOs()
29+
{
30+
Assert.Equal(Platform.Darwin, RuntimeEnvironment.OperatingSystemPlatform);
31+
Assert.Equal("Mac OS X", RuntimeEnvironment.OperatingSystem);
32+
33+
VerifyOperatingSystemVersionEqualsEnvironmentOSVersion();
34+
}
35+
36+
private void VerifyOperatingSystemVersionEqualsEnvironmentOSVersion()
37+
{
38+
Version osVersion = Version.Parse(RuntimeEnvironment.OperatingSystemVersion);
39+
Version expectedOSVersion = Environment.OSVersion.Version;
40+
41+
Assert.Equal(expectedOSVersion.Major, osVersion.Major);
42+
Assert.Equal(expectedOSVersion.Minor, osVersion.Minor);
43+
Assert.Equal(expectedOSVersion.Build, osVersion.Build);
44+
}
45+
46+
[LinuxOnlyFact]
47+
public void VerifyLinux()
48+
{
49+
Assert.Equal(Platform.Linux, RuntimeEnvironment.OperatingSystemPlatform);
50+
51+
// ensure OperatingSystem and OperatingSystemVersion are aligned with the current RID
52+
Assert.StartsWith(
53+
$"{RuntimeEnvironment.OperatingSystem.ToLowerInvariant()}.{RuntimeEnvironment.OperatingSystemVersion}",
54+
RuntimeInformation.RuntimeIdentifier);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)