Skip to content

Commit f77957c

Browse files
Copilotnkolev92
andcommitted
Fix RuntimeEnvironmentHelper macOS detection to avoid first-chance exception on Windows
Co-authored-by: nkolev92 <[email protected]>
1 parent 7b70c15 commit f77957c

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

src/NuGet.Core/NuGet.Common/RuntimeEnvironmentHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ private static bool GetIsMacOSX()
104104

105105
return false;
106106
#else
107+
// On Windows, return false immediately to avoid first-chance exception from uname() P/Invoke
108+
if (IsWindows)
109+
{
110+
return false;
111+
}
112+
107113
var buf = IntPtr.Zero;
108114

109115
try
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using NuGet.Test.Utility;
5+
using Xunit;
6+
7+
namespace NuGet.Common.Test
8+
{
9+
public class RuntimeEnvironmentHelperTests
10+
{
11+
[Fact]
12+
public void IsWindows_ShouldReturnConsistentValue()
13+
{
14+
// Arrange & Act
15+
var result1 = RuntimeEnvironmentHelper.IsWindows;
16+
var result2 = RuntimeEnvironmentHelper.IsWindows;
17+
18+
// Assert
19+
Assert.Equal(result1, result2);
20+
}
21+
22+
[Fact]
23+
public void IsMacOSX_ShouldReturnConsistentValue()
24+
{
25+
// Arrange & Act
26+
var result1 = RuntimeEnvironmentHelper.IsMacOSX;
27+
var result2 = RuntimeEnvironmentHelper.IsMacOSX;
28+
29+
// Assert
30+
Assert.Equal(result1, result2);
31+
}
32+
33+
[Fact]
34+
public void IsLinux_ShouldReturnConsistentValue()
35+
{
36+
// Arrange & Act
37+
var result1 = RuntimeEnvironmentHelper.IsLinux;
38+
var result2 = RuntimeEnvironmentHelper.IsLinux;
39+
40+
// Assert
41+
Assert.Equal(result1, result2);
42+
}
43+
44+
[Fact]
45+
public void PlatformDetection_ShouldBeExclusive()
46+
{
47+
// Arrange & Act
48+
var isWindows = RuntimeEnvironmentHelper.IsWindows;
49+
var isMacOS = RuntimeEnvironmentHelper.IsMacOSX;
50+
var isLinux = RuntimeEnvironmentHelper.IsLinux;
51+
52+
// Assert - Only one platform should be true
53+
var trueCount = (isWindows ? 1 : 0) + (isMacOS ? 1 : 0) + (isLinux ? 1 : 0);
54+
Assert.True(trueCount <= 1, $"Multiple platforms detected as true: Windows={isWindows}, macOS={isMacOS}, Linux={isLinux}");
55+
}
56+
57+
[PlatformFact(Platform.Windows)]
58+
public void IsMacOSX_OnWindows_ShouldReturnFalse()
59+
{
60+
// Arrange & Act
61+
var result = RuntimeEnvironmentHelper.IsMacOSX;
62+
63+
// Assert
64+
Assert.False(result);
65+
}
66+
67+
[PlatformFact(Platform.Windows)]
68+
public void IsWindows_OnWindows_ShouldReturnTrue()
69+
{
70+
// Arrange & Act
71+
var result = RuntimeEnvironmentHelper.IsWindows;
72+
73+
// Assert
74+
Assert.True(result);
75+
}
76+
77+
[PlatformFact(Platform.Darwin)]
78+
public void IsMacOSX_OnMacOS_ShouldReturnTrue()
79+
{
80+
// Arrange & Act
81+
var result = RuntimeEnvironmentHelper.IsMacOSX;
82+
83+
// Assert
84+
Assert.True(result);
85+
}
86+
87+
[PlatformFact(Platform.Darwin)]
88+
public void IsWindows_OnMacOS_ShouldReturnFalse()
89+
{
90+
// Arrange & Act
91+
var result = RuntimeEnvironmentHelper.IsWindows;
92+
93+
// Assert
94+
Assert.False(result);
95+
}
96+
97+
[PlatformFact(Platform.Linux)]
98+
public void IsLinux_OnLinux_ShouldReturnTrue()
99+
{
100+
// Arrange & Act
101+
var result = RuntimeEnvironmentHelper.IsLinux;
102+
103+
// Assert
104+
Assert.True(result);
105+
}
106+
107+
[PlatformFact(Platform.Linux)]
108+
public void IsWindows_OnLinux_ShouldReturnFalse()
109+
{
110+
// Arrange & Act
111+
var result = RuntimeEnvironmentHelper.IsWindows;
112+
113+
// Assert
114+
Assert.False(result);
115+
}
116+
117+
[PlatformFact(Platform.Linux)]
118+
public void IsMacOSX_OnLinux_ShouldReturnFalse()
119+
{
120+
// Arrange & Act
121+
var result = RuntimeEnvironmentHelper.IsMacOSX;
122+
123+
// Assert
124+
Assert.False(result);
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)