Skip to content

Commit

Permalink
Use OperatingSystem APIs instead of RuntimeInformation in more places (
Browse files Browse the repository at this point in the history
…#52548)

* Use OperatingSystem APIs instead of RuntimeInformation in more places

These are recommended instead of the older APIs.

* PR feedback
  • Loading branch information
akoeplinger authored May 18, 2021
1 parent e426e2c commit 4a73cb9
Show file tree
Hide file tree
Showing 75 changed files with 191 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void ComponentWithNoDependenciesCaseChangedOnAsm()
// Rename
File.Move(fileName, changeFile);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
sharedTestState.RunComponentResolutionTest(component)
.Should().Pass()
Expand All @@ -84,7 +84,7 @@ public void ComponentWithNoDependenciesCaseChangedOnAsm()
.And.HaveStdErrContaining($"deps='{component.DepsJson}'")
.And.HaveStdErrContaining($"mgd_app='{component.AppDll}'");
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if(OperatingSystem.IsMacOS())
{
sharedTestState.RunComponentResolutionTest(component)
.Should().Pass()
Expand Down Expand Up @@ -131,7 +131,7 @@ public void ComponentWithNoDependenciesCaseChangedOnDepsAndAsm()
File.Move(fileName, changeFile);
File.Move(component.DepsJson, changeDepsFile);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
sharedTestState.RunComponentResolutionTest(component)
.Should().Pass()
Expand All @@ -141,7 +141,7 @@ public void ComponentWithNoDependenciesCaseChangedOnDepsAndAsm()
.And.HaveStdErrContaining($"deps='{component.DepsJson}'")
.And.HaveStdErrContaining($"mgd_app='{component.AppDll}'");
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if(OperatingSystem.IsMacOS())
{
sharedTestState.RunComponentResolutionTest(component)
.Should().Pass()
Expand Down Expand Up @@ -189,7 +189,7 @@ public void ComponentWithNoDependenciesNoDepsCaseChangedOnAsm()
// Delete deps
File.Delete(component.DepsJson);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
sharedTestState.RunComponentResolutionTest(component)
.Should().Pass()
Expand All @@ -199,7 +199,7 @@ public void ComponentWithNoDependenciesNoDepsCaseChangedOnAsm()
.And.HaveStdErrContaining($"deps='{component.DepsJson}'")
.And.HaveStdErrContaining($"mgd_app='{component.AppDll}'");
}
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if(OperatingSystem.IsMacOS())
{
sharedTestState.RunComponentResolutionTest(component)
.Should().Pass()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,29 @@ public MultipleHives(SharedTestState sharedState)
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multiple hives are only supported on Windows.
public void FrameworkHiveSelection_GlobalHiveWithBetterMatch()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multiple hives are only supported on Windows.
return;
}

RunTest(
runtimeConfig => runtimeConfig
.WithFramework(MicrosoftNETCoreApp, "5.0.0"))
.ShouldHaveResolvedFramework(MicrosoftNETCoreApp, "5.1.2");
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multiple hives are only supported on Windows.
public void FrameworkHiveSelection_MainHiveWithBetterMatch()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multiple hives are only supported on Windows.
return;
}

RunTest(
runtimeConfig => runtimeConfig
.WithFramework(MicrosoftNETCoreApp, "6.0.0"))
.ShouldHaveResolvedFramework(MicrosoftNETCoreApp, "6.1.2");
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multiple hives are only supported on Windows.
public void FrameworkHiveSelection_CurrentDirectoryIsIgnored()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multiple hives are only supported on Windows.
return;
}

RunTest(
SharedState.DotNetMainHive,
SharedState.FrameworkReferenceApp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ private static bool IsRidSupported()
{
// Some current Linux RIDs are not supported in 2.0\2.1; just test for Ubuntu 16.
return (
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && RuntimeInformation.RuntimeIdentifier == "ubuntu.16.04-x64")
OperatingSystem.IsWindows() ||
OperatingSystem.IsMacOS() ||
(OperatingSystem.IsLinux() && RuntimeInformation.RuntimeIdentifier == "ubuntu.16.04-x64")
);
}

Expand Down
35 changes: 5 additions & 30 deletions src/installer/tests/HostActivation.Tests/MultilevelSDKLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,9 @@ public void Dispose()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SdkMultilevelLookup_Global_Json_Single_Digit_Patch_Rollup()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

// Set specified SDK version = 9999.3.4-global-dummy
SetGlobalJsonVersion("SingleDigit-global.json");

Expand Down Expand Up @@ -264,14 +259,9 @@ public void SdkMultilevelLookup_Global_Json_Single_Digit_Patch_Rollup()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SdkMultilevelLookup_Global_Json_Two_Part_Patch_Rollup()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

// Set specified SDK version = 9999.3.304-global-dummy
SetGlobalJsonVersion("TwoPart-global.json");

Expand Down Expand Up @@ -444,14 +434,9 @@ public void SdkMultilevelLookup_Global_Json_Two_Part_Patch_Rollup()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SdkMultilevelLookup_Precedential_Order()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

WriteEmptyGlobalJson();

// Add SDK versions
Expand Down Expand Up @@ -498,6 +483,7 @@ public void SdkMultilevelLookup_Precedential_Order()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SdkMultilevelLookup_RegistryAccess()
{
// The purpose of this test is to verify that the product uses correct code to access
Expand All @@ -511,12 +497,6 @@ public void SdkMultilevelLookup_RegistryAccess()
// different registry key, inside the HKEY_CURRENT_USER hive which is writable without admin.
// Note that the test creates a unique key (based on PID) for every run, to avoid collisions between parallel running tests.

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

WriteEmptyGlobalJson();

using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(DotNet.GreatestVersionHostFxrFilePath))
Expand Down Expand Up @@ -547,14 +527,9 @@ public void SdkMultilevelLookup_RegistryAccess()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SdkMultilevelLookup_Must_Pick_The_Highest_Semantic_Version()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

WriteEmptyGlobalJson();

// Add SDK versions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,9 @@ public void Dispose()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SharedMultilevelFxLookup_Must_Verify_Folders_in_the_Correct_Order()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

var fixture = SharedFxLookupPortableAppFixture
.Copy();

Expand Down Expand Up @@ -246,14 +241,9 @@ public void SharedMultilevelFxLookup_Must_Verify_Folders_in_the_Correct_Order()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void SharedMultilevelFxLookup_Must_Not_Roll_Forward_If_Framework_Version_Is_Specified_Through_Argument()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Multi-level lookup is only supported on Windows.
return;
}

var fixture = SharedFxLookupPortableAppFixture
.Copy();

Expand Down
23 changes: 4 additions & 19 deletions src/installer/tests/HostActivation.Tests/NativeHostApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,9 @@ public SdkResolutionFixture(SharedTestState state)
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void Hostfxr_get_available_sdks_with_multilevel_lookup()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// multilevel lookup is not supported on non-Windows
return;
}

var f = new SdkResolutionFixture(sharedTestState);

// With multi-level lookup (windows only): get local and global sdks sorted by ascending version,
Expand Down Expand Up @@ -322,14 +317,9 @@ public void Hostfxr_get_dotnet_environment_info_dotnet_root_only()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void Hostfxr_get_dotnet_environment_info_with_multilevel_lookup_with_dotnet_root()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Only Windows supports multi-level lookup.
return;
}

var f = new SdkResolutionFixture(sharedTestState);
string expectedSdkVersions = string.Join(';', new[]
{
Expand Down Expand Up @@ -406,14 +396,9 @@ public void Hostfxr_get_dotnet_environment_info_with_multilevel_lookup_with_dotn
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
public void Hostfxr_get_dotnet_environment_info_with_multilevel_lookup_only()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Only Windows supports multi-level lookup.
return;
}

var f = new SdkResolutionFixture(sharedTestState);
string expectedSdkVersions = string.Join(';', new[]
{
Expand Down Expand Up @@ -558,7 +543,7 @@ public SharedTestState()
.EnsureRestored()
.PublishProject();

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
BreadcrumbLocation = Path.Combine(
PortableAppWithExceptionFixture.TestProject.OutputDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.Cli.Build.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -101,7 +102,7 @@ public static FluentAssertions.AndConstraint<CommandResultAssertions> ExecuteApp
public static FluentAssertions.AndConstraint<CommandResultAssertions> ExecuteApplicationWithException(this CommandResultAssertions assertion, string hostPath, string appPath)
{
var constraint = assertion.ExecuteApplication(hostPath, appPath);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (OperatingSystem.IsWindows())
{
return constraint.And.HaveStdOutContaining($"hostfxr_run_app threw exception: 0x{Constants.ErrorCode.COMPlusException.ToString("x")}");
}
Expand Down
31 changes: 6 additions & 25 deletions src/installer/tests/HostActivation.Tests/NativeHosting/Comhost.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
Expand All @@ -22,17 +23,12 @@ public Comhost(SharedTestState sharedTestState)
}

[Theory]
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
[InlineData(1, true)]
[InlineData(10, true)]
[InlineData(10, false)]
public void ActivateClass(int count, bool synchronous)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// COM activation is only supported on Windows
return;
}

string [] args = {
"comhost",
synchronous ? "synchronous" : "concurrent",
Expand All @@ -53,14 +49,9 @@ public void ActivateClass(int count, bool synchronous)
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
public void ActivateClass_IgnoreAppLocalHostFxr()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// COM activation is only supported on Windows
return;
}

using (var fixture = sharedState.ComLibraryFixture.Copy())
{
File.WriteAllText(Path.Combine(fixture.TestProject.BuiltApp.Location, "hostfxr.dll"), string.Empty);
Expand All @@ -86,14 +77,9 @@ public void ActivateClass_IgnoreAppLocalHostFxr()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
public void ActivateClass_ValidateIErrorInfoResult()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// COM activation is only supported on Windows
return;
}

using (var fixture = sharedState.ComLibraryFixture.Copy())
{
string missingRuntimeConfig = Path.Combine(fixture.TestProject.BuiltApp.Location,
Expand Down Expand Up @@ -121,14 +107,9 @@ public void ActivateClass_ValidateIErrorInfoResult()
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
public void LoadTypeLibraries()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// COM activation is only supported on Windows
return;
}

using (var fixture = sharedState.ComLibraryFixture.Copy())
{
var comHost = Path.Combine(
Expand Down Expand Up @@ -165,7 +146,7 @@ public class SharedTestState : SharedTestStateBase

public SharedTestState()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!OperatingSystem.IsWindows())
{
// COM activation is only supported on Windows
return;
Expand Down
Loading

0 comments on commit 4a73cb9

Please sign in to comment.