Skip to content

Commit

Permalink
Remove PlatformAbstractions from runtime tests. (#36243)
Browse files Browse the repository at this point in the history
* Remove PlatformAbstractions from runtime tests.

Contributes to #3470
  • Loading branch information
eerhardt authored May 12, 2020
1 parent 14835fb commit 7fb42d9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace System
{
Expand Down Expand Up @@ -34,14 +34,10 @@ public static partial class PlatformDetection
// OSX family
public static bool IsOSX => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
public static bool IsNotOSX => !IsOSX;
public static Version OSXVersion => IsOSX ?
ToVersion(Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion) :
throw new PlatformNotSupportedException();
private static Lazy<Version> m_osxProductVersion = new Lazy<Version>(GetOSXProductVersion);
public static bool IsMacOsHighSierraOrHigher => IsOSX && (m_osxProductVersion.Value.Major > 10 || (m_osxProductVersion.Value.Major == 10 && m_osxProductVersion.Value.Minor >= 13));
public static bool IsMacOsHighSierraOrHigher => IsOSX && Environment.OSVersion.Version >= new Version(10, 13);
public static bool IsNotMacOsHighSierraOrHigher => !IsMacOsHighSierraOrHigher;
public static bool IsMacOsMojaveOrHigher => IsOSX && (m_osxProductVersion.Value.Major > 10 || (m_osxProductVersion.Value.Major == 10 && m_osxProductVersion.Value.Minor >= 14));
public static bool IsMacOsCatalinaOrHigher => IsOSX && (m_osxProductVersion.Value.Major > 10 || (m_osxProductVersion.Value.Major == 10 && m_osxProductVersion.Value.Minor >= 15));
public static bool IsMacOsMojaveOrHigher => IsOSX && Environment.OSVersion.Version >= new Version(10, 14);
public static bool IsMacOsCatalinaOrHigher => IsOSX && Environment.OSVersion.Version >= new Version(10, 15);

// RedHat family covers RedHat and CentOS
public static bool IsRedHatFamily => IsRedHatFamilyAndVersion();
Expand Down Expand Up @@ -106,56 +102,6 @@ public static string LibcVersion
}
}

private static Version GetOSXProductVersion()
{
if (IsOSX)
{
try
{
// <plist version="1.0">
// <dict>
// <key>ProductBuildVersion</key>
// <string>17A330h</string>
// <key>ProductCopyright</key>
// <string>1983-2017 Apple Inc.</string>
// <key>ProductName</key>
// <string>Mac OS X</string>
// <key>ProductUserVisibleVersion</key>
// <string>10.13</string>
// <key>ProductVersion</key>
// <string>10.13</string>
// </dict>
// </plist>

XElement dict = XDocument.Load("/System/Library/CoreServices/SystemVersion.plist").Root.Element("dict");
if (dict != null)
{
foreach (XElement key in dict.Elements("key"))
{
if ("ProductVersion".Equals(key.Value))
{
XElement stringElement = key.NextNode as XElement;
if (stringElement != null && stringElement.Name.LocalName.Equals("string"))
{
string versionString = stringElement.Value;
if (versionString != null)
{
return Version.Parse(versionString);
}
}
}
}
}
}
catch
{
}
}

// In case of exception, couldn't get the version or non osx
return new Version(0, 0, 0);
}

private static Version s_opensslVersion;
private static Version GetOpenSslVersion()
{
Expand Down Expand Up @@ -204,11 +150,38 @@ private static Version ToVersion(string versionString)
}
}

private static DistroInfo GetDistroInfo() => new DistroInfo()
private static DistroInfo GetDistroInfo()
{
Id = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem,
VersionId = ToVersion(Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion)
};
DistroInfo result = new DistroInfo();

if (IsFreeBSD)
{
result.Id = "FreeBSD";
// example:
// FreeBSD 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016 root@releng2.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC
// What we want is major release as minor releases should be compatible.
result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split()[1].Split('.')[0]);
}
else if (File.Exists("/etc/os-release"))
{
foreach (string line in File.ReadAllLines("/etc/os-release"))
{
if (line.StartsWith("ID=", StringComparison.Ordinal))
{
result.Id = line.Substring(3).Trim('"', '\'');
}
else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
{
result.VersionId = ToVersion(line.Substring(11).Trim('"', '\''));
}
}
}

result.Id ??= "Linux";
result.VersionId ??= ToVersion(string.Empty);

return result;
}

private static bool IsRedHatFamilyAndVersion(int major = -1, int minor = -1, int build = -1, int revision = -1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static bool IsNonZeroLowerBoundArraySupported
((!IsOSX && !IsWindows) &&
(OpenSslVersion.Major >= 1 && (OpenSslVersion.Minor >= 1 || OpenSslVersion.Build >= 2)));

public static bool SupportsClientAlpn => SupportsAlpn || (IsOSX && PlatformDetection.OSXVersion > new Version(10, 12));
public static bool SupportsClientAlpn => SupportsAlpn || (IsOSX && Environment.OSVersion.Version > new Version(10, 12));

// OpenSSL 1.1.1 and above.
public static bool SupportsTls13 => GetTls13Support();
Expand Down Expand Up @@ -142,7 +142,7 @@ public static string GetDistroVersionString()
}
else if (IsOSX)
{
return "OSX Version=" + m_osxProductVersion.Value.ToString();
return "OSX Version=" + Environment.OSVersion.Version.ToString();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,5 @@
<PackageReference Include="Microsoft.DotNet.XUnitExtensions" Version="$(MicrosoftDotNetXUnitExtensionsVersion)" />
<PackageReference Include="xunit.core" Version="$(XUnitVersion)" ExcludeAssets="build" />
<PackageReference Include="xunit.assert" Version="$(XUnitVersion)" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="$(MicrosoftDotNetPlatformAbstractionsVersion)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using Xunit;
using F = Microsoft.Extensions.DependencyModel.Tests.TestLibraryFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static string JaJPAbbreviatedEraName()

public static string[] FrFRDayNames()
{
if (PlatformDetection.IsOSX && PlatformDetection.OSXVersion < new Version(10, 12))
if (PlatformDetection.IsOSX && Environment.OSVersion.Version < new Version(10, 12))
{
return new string[] { "Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi" };
}
Expand All @@ -38,7 +38,7 @@ public static string[] FrFRDayNames()

public static string[] FrFRAbbreviatedDayNames()
{
if (PlatformDetection.IsOSX && PlatformDetection.OSXVersion < new Version(10, 12))
if (PlatformDetection.IsOSX && Environment.OSVersion.Version < new Version(10, 12))
{
return new string[] { "Dim.", "Lun.", "Mar.", "Mer.", "Jeu.", "Ven.", "Sam." };
}
Expand Down

0 comments on commit 7fb42d9

Please sign in to comment.