Skip to content

Commit 4a73cb9

Browse files
authored
Use OperatingSystem APIs instead of RuntimeInformation in more places (#52548)
* Use OperatingSystem APIs instead of RuntimeInformation in more places These are recommended instead of the older APIs. * PR feedback
1 parent e426e2c commit 4a73cb9

File tree

75 files changed

+191
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+191
-352
lines changed

src/installer/tests/HostActivation.Tests/DependencyResolution/ResolveComponentDependencies.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void ComponentWithNoDependenciesCaseChangedOnAsm()
7474
// Rename
7575
File.Move(fileName, changeFile);
7676

77-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
77+
if (OperatingSystem.IsWindows())
7878
{
7979
sharedTestState.RunComponentResolutionTest(component)
8080
.Should().Pass()
@@ -84,7 +84,7 @@ public void ComponentWithNoDependenciesCaseChangedOnAsm()
8484
.And.HaveStdErrContaining($"deps='{component.DepsJson}'")
8585
.And.HaveStdErrContaining($"mgd_app='{component.AppDll}'");
8686
}
87-
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
87+
else if(OperatingSystem.IsMacOS())
8888
{
8989
sharedTestState.RunComponentResolutionTest(component)
9090
.Should().Pass()
@@ -131,7 +131,7 @@ public void ComponentWithNoDependenciesCaseChangedOnDepsAndAsm()
131131
File.Move(fileName, changeFile);
132132
File.Move(component.DepsJson, changeDepsFile);
133133

134-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
134+
if (OperatingSystem.IsWindows())
135135
{
136136
sharedTestState.RunComponentResolutionTest(component)
137137
.Should().Pass()
@@ -141,7 +141,7 @@ public void ComponentWithNoDependenciesCaseChangedOnDepsAndAsm()
141141
.And.HaveStdErrContaining($"deps='{component.DepsJson}'")
142142
.And.HaveStdErrContaining($"mgd_app='{component.AppDll}'");
143143
}
144-
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
144+
else if(OperatingSystem.IsMacOS())
145145
{
146146
sharedTestState.RunComponentResolutionTest(component)
147147
.Should().Pass()
@@ -189,7 +189,7 @@ public void ComponentWithNoDependenciesNoDepsCaseChangedOnAsm()
189189
// Delete deps
190190
File.Delete(component.DepsJson);
191191

192-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
192+
if (OperatingSystem.IsWindows())
193193
{
194194
sharedTestState.RunComponentResolutionTest(component)
195195
.Should().Pass()
@@ -199,7 +199,7 @@ public void ComponentWithNoDependenciesNoDepsCaseChangedOnAsm()
199199
.And.HaveStdErrContaining($"deps='{component.DepsJson}'")
200200
.And.HaveStdErrContaining($"mgd_app='{component.AppDll}'");
201201
}
202-
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
202+
else if(OperatingSystem.IsMacOS())
203203
{
204204
sharedTestState.RunComponentResolutionTest(component)
205205
.Should().Pass()

src/installer/tests/HostActivation.Tests/FrameworkResolution/MultipleHives.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,29 @@ public MultipleHives(SharedTestState sharedState)
2121
}
2222

2323
[Fact]
24+
[PlatformSpecific(TestPlatforms.Windows)] // Multiple hives are only supported on Windows.
2425
public void FrameworkHiveSelection_GlobalHiveWithBetterMatch()
2526
{
26-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
27-
{
28-
// Multiple hives are only supported on Windows.
29-
return;
30-
}
31-
3227
RunTest(
3328
runtimeConfig => runtimeConfig
3429
.WithFramework(MicrosoftNETCoreApp, "5.0.0"))
3530
.ShouldHaveResolvedFramework(MicrosoftNETCoreApp, "5.1.2");
3631
}
3732

3833
[Fact]
34+
[PlatformSpecific(TestPlatforms.Windows)] // Multiple hives are only supported on Windows.
3935
public void FrameworkHiveSelection_MainHiveWithBetterMatch()
4036
{
41-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
42-
{
43-
// Multiple hives are only supported on Windows.
44-
return;
45-
}
46-
4737
RunTest(
4838
runtimeConfig => runtimeConfig
4939
.WithFramework(MicrosoftNETCoreApp, "6.0.0"))
5040
.ShouldHaveResolvedFramework(MicrosoftNETCoreApp, "6.1.2");
5141
}
5242

5343
[Fact]
44+
[PlatformSpecific(TestPlatforms.Windows)] // Multiple hives are only supported on Windows.
5445
public void FrameworkHiveSelection_CurrentDirectoryIsIgnored()
5546
{
56-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
57-
{
58-
// Multiple hives are only supported on Windows.
59-
return;
60-
}
61-
6247
RunTest(
6348
SharedState.DotNetMainHive,
6449
SharedState.FrameworkReferenceApp,

src/installer/tests/HostActivation.Tests/HostVersionCompatibility.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ private static bool IsRidSupported()
138138
{
139139
// Some current Linux RIDs are not supported in 2.0\2.1; just test for Ubuntu 16.
140140
return (
141-
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
142-
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
143-
(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && RuntimeInformation.RuntimeIdentifier == "ubuntu.16.04-x64")
141+
OperatingSystem.IsWindows() ||
142+
OperatingSystem.IsMacOS() ||
143+
(OperatingSystem.IsLinux() && RuntimeInformation.RuntimeIdentifier == "ubuntu.16.04-x64")
144144
);
145145
}
146146

src/installer/tests/HostActivation.Tests/MultilevelSDKLookup.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,9 @@ public void Dispose()
8989
}
9090

9191
[Fact]
92+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
9293
public void SdkMultilevelLookup_Global_Json_Single_Digit_Patch_Rollup()
9394
{
94-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
95-
{
96-
// Multi-level lookup is only supported on Windows.
97-
return;
98-
}
99-
10095
// Set specified SDK version = 9999.3.4-global-dummy
10196
SetGlobalJsonVersion("SingleDigit-global.json");
10297

@@ -264,14 +259,9 @@ public void SdkMultilevelLookup_Global_Json_Single_Digit_Patch_Rollup()
264259
}
265260

266261
[Fact]
262+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
267263
public void SdkMultilevelLookup_Global_Json_Two_Part_Patch_Rollup()
268264
{
269-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
270-
{
271-
// Multi-level lookup is only supported on Windows.
272-
return;
273-
}
274-
275265
// Set specified SDK version = 9999.3.304-global-dummy
276266
SetGlobalJsonVersion("TwoPart-global.json");
277267

@@ -444,14 +434,9 @@ public void SdkMultilevelLookup_Global_Json_Two_Part_Patch_Rollup()
444434
}
445435

446436
[Fact]
437+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
447438
public void SdkMultilevelLookup_Precedential_Order()
448439
{
449-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
450-
{
451-
// Multi-level lookup is only supported on Windows.
452-
return;
453-
}
454-
455440
WriteEmptyGlobalJson();
456441

457442
// Add SDK versions
@@ -498,6 +483,7 @@ public void SdkMultilevelLookup_Precedential_Order()
498483
}
499484

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

514-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
515-
{
516-
// Multi-level lookup is only supported on Windows.
517-
return;
518-
}
519-
520500
WriteEmptyGlobalJson();
521501

522502
using (var registeredInstallLocationOverride = new RegisteredInstallLocationOverride(DotNet.GreatestVersionHostFxrFilePath))
@@ -547,14 +527,9 @@ public void SdkMultilevelLookup_RegistryAccess()
547527
}
548528

549529
[Fact]
530+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
550531
public void SdkMultilevelLookup_Must_Pick_The_Highest_Semantic_Version()
551532
{
552-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
553-
{
554-
// Multi-level lookup is only supported on Windows.
555-
return;
556-
}
557-
558533
WriteEmptyGlobalJson();
559534

560535
// Add SDK versions

src/installer/tests/HostActivation.Tests/MultilevelSharedFxLookup.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,9 @@ public void Dispose()
129129
}
130130

131131
[Fact]
132+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
132133
public void SharedMultilevelFxLookup_Must_Verify_Folders_in_the_Correct_Order()
133134
{
134-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
135-
{
136-
// Multi-level lookup is only supported on Windows.
137-
return;
138-
}
139-
140135
var fixture = SharedFxLookupPortableAppFixture
141136
.Copy();
142137

@@ -246,14 +241,9 @@ public void SharedMultilevelFxLookup_Must_Verify_Folders_in_the_Correct_Order()
246241
}
247242

248243
[Fact]
244+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
249245
public void SharedMultilevelFxLookup_Must_Not_Roll_Forward_If_Framework_Version_Is_Specified_Through_Argument()
250246
{
251-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
252-
{
253-
// Multi-level lookup is only supported on Windows.
254-
return;
255-
}
256-
257247
var fixture = SharedFxLookupPortableAppFixture
258248
.Copy();
259249

src/installer/tests/HostActivation.Tests/NativeHostApis.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,9 @@ public SdkResolutionFixture(SharedTestState state)
128128
}
129129

130130
[Fact]
131+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
131132
public void Hostfxr_get_available_sdks_with_multilevel_lookup()
132133
{
133-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
134-
{
135-
// multilevel lookup is not supported on non-Windows
136-
return;
137-
}
138-
139134
var f = new SdkResolutionFixture(sharedTestState);
140135

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

324319
[Fact]
320+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
325321
public void Hostfxr_get_dotnet_environment_info_with_multilevel_lookup_with_dotnet_root()
326322
{
327-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
328-
{
329-
// Only Windows supports multi-level lookup.
330-
return;
331-
}
332-
333323
var f = new SdkResolutionFixture(sharedTestState);
334324
string expectedSdkVersions = string.Join(';', new[]
335325
{
@@ -406,14 +396,9 @@ public void Hostfxr_get_dotnet_environment_info_with_multilevel_lookup_with_dotn
406396
}
407397

408398
[Fact]
399+
[PlatformSpecific(TestPlatforms.Windows)] // Multi-level lookup is only supported on Windows.
409400
public void Hostfxr_get_dotnet_environment_info_with_multilevel_lookup_only()
410401
{
411-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
412-
{
413-
// Only Windows supports multi-level lookup.
414-
return;
415-
}
416-
417402
var f = new SdkResolutionFixture(sharedTestState);
418403
string expectedSdkVersions = string.Join(';', new[]
419404
{
@@ -558,7 +543,7 @@ public SharedTestState()
558543
.EnsureRestored()
559544
.PublishProject();
560545

561-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
546+
if (!OperatingSystem.IsWindows())
562547
{
563548
BreadcrumbLocation = Path.Combine(
564549
PortableAppWithExceptionFixture.TestProject.OutputDirectory,

src/installer/tests/HostActivation.Tests/NativeHosting/ApplicationExecution.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.DotNet.Cli.Build.Framework;
5+
using System;
56
using System.Collections.Generic;
67
using System.IO;
78
using System.Linq;
@@ -101,7 +102,7 @@ public static FluentAssertions.AndConstraint<CommandResultAssertions> ExecuteApp
101102
public static FluentAssertions.AndConstraint<CommandResultAssertions> ExecuteApplicationWithException(this CommandResultAssertions assertion, string hostPath, string appPath)
102103
{
103104
var constraint = assertion.ExecuteApplication(hostPath, appPath);
104-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
105+
if (OperatingSystem.IsWindows())
105106
{
106107
return constraint.And.HaveStdOutContaining($"hostfxr_run_app threw exception: 0x{Constants.ErrorCode.COMPlusException.ToString("x")}");
107108
}

src/installer/tests/HostActivation.Tests/NativeHosting/Comhost.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using System.Reflection.Metadata;
@@ -22,17 +23,12 @@ public Comhost(SharedTestState sharedTestState)
2223
}
2324

2425
[Theory]
26+
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
2527
[InlineData(1, true)]
2628
[InlineData(10, true)]
2729
[InlineData(10, false)]
2830
public void ActivateClass(int count, bool synchronous)
2931
{
30-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
31-
{
32-
// COM activation is only supported on Windows
33-
return;
34-
}
35-
3632
string [] args = {
3733
"comhost",
3834
synchronous ? "synchronous" : "concurrent",
@@ -53,14 +49,9 @@ public void ActivateClass(int count, bool synchronous)
5349
}
5450

5551
[Fact]
52+
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
5653
public void ActivateClass_IgnoreAppLocalHostFxr()
5754
{
58-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
59-
{
60-
// COM activation is only supported on Windows
61-
return;
62-
}
63-
6455
using (var fixture = sharedState.ComLibraryFixture.Copy())
6556
{
6657
File.WriteAllText(Path.Combine(fixture.TestProject.BuiltApp.Location, "hostfxr.dll"), string.Empty);
@@ -86,14 +77,9 @@ public void ActivateClass_IgnoreAppLocalHostFxr()
8677
}
8778

8879
[Fact]
80+
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
8981
public void ActivateClass_ValidateIErrorInfoResult()
9082
{
91-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
92-
{
93-
// COM activation is only supported on Windows
94-
return;
95-
}
96-
9783
using (var fixture = sharedState.ComLibraryFixture.Copy())
9884
{
9985
string missingRuntimeConfig = Path.Combine(fixture.TestProject.BuiltApp.Location,
@@ -121,14 +107,9 @@ public void ActivateClass_ValidateIErrorInfoResult()
121107
}
122108

123109
[Fact]
110+
[PlatformSpecific(TestPlatforms.Windows)] // COM activation is only supported on Windows
124111
public void LoadTypeLibraries()
125112
{
126-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
127-
{
128-
// COM activation is only supported on Windows
129-
return;
130-
}
131-
132113
using (var fixture = sharedState.ComLibraryFixture.Copy())
133114
{
134115
var comHost = Path.Combine(
@@ -165,7 +146,7 @@ public class SharedTestState : SharedTestStateBase
165146

166147
public SharedTestState()
167148
{
168-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
149+
if (!OperatingSystem.IsWindows())
169150
{
170151
// COM activation is only supported on Windows
171152
return;

0 commit comments

Comments
 (0)