Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4fcf002
Expose OperatingSystem static methods
jrdodds Jun 22, 2023
f3e563e
Merge branch 'dotnet:main' into AvailableStaticMethodsSystemOperating…
jrdodds Jun 22, 2023
250df8e
make class internal
jrdodds Jun 22, 2023
5d00cb2
Update comment in src/Build/Resources/Constants.cs
jrdodds Jun 22, 2023
210dca4
Update comment in src/Framework/OperatingSystem.cs
jrdodds Jun 22, 2023
908aa4b
Update comment in src/Framework/OperatingSystem.cs
jrdodds Jun 22, 2023
296094b
flip #if test
jrdodds Jun 22, 2023
cc180fc
changes per review request; add missing unit test for IsOSPlatformVer…
jrdodds Jun 22, 2023
e4efcd4
add test case
jrdodds Jun 22, 2023
541c4ba
add IsOSPlatformVersionAtLeast* test methods
jrdodds Jun 22, 2023
b0b7786
consolidate tests
jrdodds Jun 22, 2023
6accf37
remove IsOSXLike()
jrdodds Jun 23, 2023
387ef70
unit tests
jrdodds Jun 23, 2023
558e8a6
expose only selected methods of System.OperatingSystem
jrdodds Jun 23, 2023
30dea8f
per review request expose all static methods of System.OperatingSystem
jrdodds Jun 27, 2023
16baa32
Merge branch 'dotnet:main' into AvailableStaticMethodsSystemOperating…
jrdodds Jun 27, 2023
f8dbc4a
Merge branch 'dotnet:main' into AvailableStaticMethodsSystemOperating…
jrdodds Jun 28, 2023
6dde4c3
Merge branch 'dotnet:main' into AvailableStaticMethodsSystemOperating…
jrdodds Jun 30, 2023
759ae6a
add comment to re-run build after unit test timeout
jrdodds Jun 30, 2023
ffeb0c3
Revert "add comment to re-run build after unit test timeout"
jrdodds Jul 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 284 additions & 0 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2839,6 +2839,290 @@ public void PropertyFunctionRuntimeInformation(string propertyFunction, string e
Assert.Equal(expectedExpansion, result);
}

[Theory]
[InlineData("windows")]
[InlineData("linux")]
[InlineData("macos")]
[InlineData("osx")]
public void IsOSPlatform(string platform)
{
string propertyFunction = $"$([System.OperatingSystem]::IsOSPlatform('{platform}'))";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsOSPlatform(platform);
#else
result = Microsoft.Build.Framework.OperatingSystem.IsOSPlatform(platform);
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData("windows", 4, 0, 0, 0)]
[InlineData("windows", 999, 0, 0, 0)]
[InlineData("linux", 0, 0, 0, 0)]
[InlineData("macos", 10, 15, 0, 0)]
[InlineData("macos", 999, 0, 0, 0)]
[InlineData("osx", 0, 0, 0, 0)]
public void IsOSPlatformVersionAtLeast(string platform, int major, int minor, int build, int revision)
{
string propertyFunction = $"$([System.OperatingSystem]::IsOSPlatformVersionAtLeast('{platform}', {major}, {minor}, {build}, {revision}))";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsOSPlatformVersionAtLeast(platform, major, minor, build, revision);
#else
result = Microsoft.Build.Framework.OperatingSystem.IsOSPlatformVersionAtLeast(platform, major, minor, build, revision);
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsLinux()
{
const string propertyFunction = "$([System.OperatingSystem]::IsLinux())";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsLinux();
#else
result = Microsoft.Build.Framework.OperatingSystem.IsLinux();
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsFreeBSD()
{
const string propertyFunction = "$([System.OperatingSystem]::IsFreeBSD())";
bool result = false;
#if NET5_0_OR_GREATER
result = System.OperatingSystem.IsFreeBSD();
#else
result = Microsoft.Build.Framework.OperatingSystem.IsFreeBSD();
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(999, 0, 0, 0)]
public void IsFreeBSDVersionAtLeast(int major, int minor, int build, int revision)
{
string propertyFunction = $"$([System.OperatingSystem]::IsFreeBSDVersionAtLeast({major}, {minor}, {build}, {revision}))";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsFreeBSDVersionAtLeast(major, minor, build, revision);
#else
result = Microsoft.Build.Framework.OperatingSystem.IsFreeBSDVersionAtLeast(major, minor, build, revision);
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsMacOS()
{
const string propertyFunction = "$([System.OperatingSystem]::IsMacOS())";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsMacOS();
#else
result = Microsoft.Build.Framework.OperatingSystem.IsMacOS();
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(10, 15, 0)]
[InlineData(999, 0, 0)]
public void IsMacOSVersionAtLeast(int major, int minor, int build)
{
string propertyFunction = $"$([System.OperatingSystem]::IsMacOSVersionAtLeast({major}, {minor}, {build}))";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsMacOSVersionAtLeast(major, minor, build);
#else
result = Microsoft.Build.Framework.OperatingSystem.IsMacOSVersionAtLeast(major, minor, build);
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsWindows()
{
const string propertyFunction = "$([System.OperatingSystem]::IsWindows())";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsWindows();
#else
result = Microsoft.Build.Framework.OperatingSystem.IsWindows();
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(4, 0, 0, 0)]
[InlineData(999, 0, 0, 0)]
public void IsWindowsVersionAtLeast(int major, int minor, int build, int revision)
{
string propertyFunction = $"$([System.OperatingSystem]::IsWindowsVersionAtLeast({major}, {minor}, {build}, {revision}))";
bool result = false;
#if NET5_0_OR_GREATER
result = OperatingSystem.IsWindowsVersionAtLeast(major, minor, build, revision);
#else
result = Microsoft.Build.Framework.OperatingSystem.IsWindowsVersionAtLeast(major, minor, build, revision);
#endif
string expected = result ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

#if NET5_0_OR_GREATER

[Fact]
public void IsAndroid()
{
const string propertyFunction = "$([System.OperatingSystem]::IsAndroid())";

string expected = OperatingSystem.IsAndroid() ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0, 0)]
[InlineData(999, 0, 0, 0)]
public void IsAndroidVersionAtLeast(int major, int minor, int build, int revision)
{
string propertyFunction = $"$([System.OperatingSystem]::IsAndroidVersionAtLeast({major}, {minor}, {build}, {revision}))";
string expected = OperatingSystem.IsAndroidVersionAtLeast(major, minor, build, revision) ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsIOS()
{
const string propertyFunction = "$([System.OperatingSystem]::IsIOS())";

string expected = OperatingSystem.IsIOS() ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(16, 5, 1)]
[InlineData(999, 0, 0)]
public void IsIOSVersionAtLeast(int major, int minor, int build)
{
string propertyFunction = $"$([System.OperatingSystem]::IsIOSVersionAtLeast({major}, {minor}, {build}))";
string expected = OperatingSystem.IsIOSVersionAtLeast(major, minor, build) ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsMacCatalyst()
{
const string propertyFunction = "$([System.OperatingSystem]::IsMacCatalyst())";

string expected = OperatingSystem.IsMacCatalyst() ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(999, 0, 0)]
public void IsMacCatalystVersionAtLeast(int major, int minor, int build)
{
string propertyFunction = $"$([System.OperatingSystem]::IsMacCatalystVersionAtLeast({major}, {minor}, {build}))";
string expected = OperatingSystem.IsMacCatalystVersionAtLeast(major, minor, build) ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsTvOS()
{
const string propertyFunction = "$([System.OperatingSystem]::IsTvOS())";

string expected = OperatingSystem.IsTvOS() ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(16, 5, 0)]
[InlineData(999, 0, 0)]
public void IsTvOSVersionAtLeast(int major, int minor, int build)
{
string propertyFunction = $"$([System.OperatingSystem]::IsTvOSVersionAtLeast({major}, {minor}, {build}))";
string expected = OperatingSystem.IsTvOSVersionAtLeast(major, minor, build) ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Fact]
public void IsWatchOS()
{
const string propertyFunction = "$([System.OperatingSystem]::IsWatchOS())";

string expected = OperatingSystem.IsWatchOS() ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

[Theory]
[InlineData(0, 0, 0)]
[InlineData(9, 5, 2)]
[InlineData(999, 0, 0)]
public void IsWatchOSVersionAtLeast(int major, int minor, int build)
{
string propertyFunction = $"$([System.OperatingSystem]::IsWatchOSVersionAtLeast({major}, {minor}, {build}))";
string expected = OperatingSystem.IsWatchOSVersionAtLeast(major, minor, build) ? "True" : "False";
var pg = new PropertyDictionary<ProjectPropertyInstance>();
var expander = new Expander<ProjectPropertyInstance, ProjectItemInstance>(pg, FileSystems.Default);
expander.ExpandIntoStringLeaveEscaped(propertyFunction, ExpanderOptions.ExpandProperties, MockElementLocation.Instance).ShouldBe(expected);
}

#endif

[Theory]
[InlineData("AString", "x12x456789x11", "$(AString.IndexOf('x', 1))", "3")]
[InlineData("AString", "x12x456789x11", "$(AString.IndexOf('x45', 1))", "3")]
Expand Down
9 changes: 9 additions & 0 deletions src/Build/Resources/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,15 @@ private static void InitializeAvailableMethods()
availableStaticMethods.TryAdd("Microsoft.Build.Utilities.ToolLocationHelper", new Tuple<string, Type>("Microsoft.Build.Utilities.ToolLocationHelper, Microsoft.Build.Utilities.Core, Version=" + MSBuildConstants.CurrentAssemblyVersion + ", Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", null));
availableStaticMethods.TryAdd("System.Runtime.InteropServices.RuntimeInformation", runtimeInformationType);
availableStaticMethods.TryAdd("System.Runtime.InteropServices.OSPlatform", osPlatformType);
#if NET5_0_OR_GREATER
var operatingSystemType = new Tuple<string, Type>(null, typeof(OperatingSystem));
availableStaticMethods.TryAdd("System.OperatingSystem", operatingSystemType);
#else
// Add alternate type for System.OperatingSystem static methods which aren't available on .NET Framework.
var operatingSystemType = new Tuple<string, Type>("Microsoft.Build.Framework.OperatingSystem, Microsoft.Build.Framework, Version=" + MSBuildConstants.CurrentAssemblyVersion + ", Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", null);
availableStaticMethods.TryAdd("System.OperatingSystem", operatingSystemType);
availableStaticMethods.TryAdd("Microsoft.Build.Framework.OperatingSystem", operatingSystemType);
#endif

s_availableStaticMethods = availableStaticMethods;
}
Expand Down
57 changes: 57 additions & 0 deletions src/Framework.UnitTests/OperatingSystem_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Shouldly;

using Xunit;
using Xunit.NetCore.Extensions;

namespace Microsoft.Build.Framework.UnitTests
{
public class OperatingSystem_Tests
{
#if !NET5_0_OR_GREATER
[WindowsFullFrameworkOnlyTheory]
[InlineData("windows", true)]
[InlineData("linux", false)]
[InlineData("macOS", false)]
public void IsOSPlatform(string platform, bool expected)
{
Microsoft.Build.Framework.OperatingSystem.IsOSPlatform(platform).ShouldBe(expected);
}

[WindowsFullFrameworkOnlyTheory]
[InlineData("windows", 4, true)]
[InlineData("windows", 999, false)]
[InlineData("linux", 0, false)]
[InlineData("macOS", 0, false)]
public void IsOSPlatformVersionAtLeast(string platform, int major, bool expected)
{
Microsoft.Build.Framework.OperatingSystem.IsOSPlatformVersionAtLeast(platform, major).ShouldBe(expected);
}

[WindowsFullFrameworkOnlyFact]
public void IsWindows()
{
Microsoft.Build.Framework.OperatingSystem.IsWindows().ShouldBeTrue();
}

[WindowsFullFrameworkOnlyFact]
public void IsWindowsVersionAtLeast()
{
Microsoft.Build.Framework.OperatingSystem.IsWindowsVersionAtLeast(4).ShouldBeTrue();
Microsoft.Build.Framework.OperatingSystem.IsWindowsVersionAtLeast(999).ShouldBeFalse();
}

[WindowsFullFrameworkOnlyFact]
public void IsOtherThanWindows()
{
Microsoft.Build.Framework.OperatingSystem.IsFreeBSD().ShouldBeFalse();
Microsoft.Build.Framework.OperatingSystem.IsFreeBSDVersionAtLeast(0).ShouldBeFalse();
Microsoft.Build.Framework.OperatingSystem.IsLinux().ShouldBeFalse();
Microsoft.Build.Framework.OperatingSystem.IsMacOS().ShouldBeFalse();
Microsoft.Build.Framework.OperatingSystem.IsMacOSVersionAtLeast(0).ShouldBeFalse();
}
#endif
}
}
Loading