From 9013447cafcdc3cd882795837a7fe7dac7bf50da Mon Sep 17 00:00:00 2001 From: Arun Mahapatra Date: Mon, 12 Jun 2017 07:46:36 +0530 Subject: [PATCH] Add platform abstraction for OS name and architecture. Related #820 #679 --- .../Helpers/DotnetHostHelper.cs | 17 ++--- .../Utilities/XmlRunSettingsUtilities.cs | 68 ++---------------- .../Interfaces/System/IEnvironment.cs | 23 ++++++ .../{Process => System}/IProcessHelper.cs | 0 .../Interfaces/System/PlatformArchitecture.cs | 18 +++++ .../System/PlatformOperationSystem.cs | 16 +++++ ...t.TestPlatform.PlatformAbstractions.csproj | Bin 4256 -> 4578 bytes .../{Process => System}/ProcessHelper.cs | 0 .../net46/System/PlatformEnvironment.cs | 42 +++++++++++ .../netcore/System/PlatformEnvironment.cs | 47 ++++++++++++ .../System/PlatformEnvironment.cs | 29 ++++++++ .../{Process => System}/ProcessHelper.cs | 0 12 files changed, 186 insertions(+), 74 deletions(-) create mode 100644 src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/IEnvironment.cs rename src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/{Process => System}/IProcessHelper.cs (100%) create mode 100644 src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformArchitecture.cs create mode 100644 src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformOperationSystem.cs rename src/Microsoft.TestPlatform.PlatformAbstractions/common/{Process => System}/ProcessHelper.cs (100%) create mode 100644 src/Microsoft.TestPlatform.PlatformAbstractions/net46/System/PlatformEnvironment.cs create mode 100644 src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformEnvironment.cs create mode 100644 src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/System/PlatformEnvironment.cs rename src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/{Process => System}/ProcessHelper.cs (100%) diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs index 4bf0dfdd16..c24ed0824e 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs @@ -10,17 +10,19 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Resources; using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces; using Microsoft.VisualStudio.TestPlatform.ObjectModel; + using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers; using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces; public class DotnetHostHelper : IDotnetHostHelper { private readonly IFileHelper fileHelper; + private readonly IEnvironment environment; /// /// Initializes a new instance of the class. /// - public DotnetHostHelper() : this(new FileHelper()) + public DotnetHostHelper() : this(new FileHelper(), new PlatformEnvironment()) { } @@ -28,29 +30,24 @@ public DotnetHostHelper() : this(new FileHelper()) /// Initializes a new instance of the class. /// /// File Helper - public DotnetHostHelper(IFileHelper fileHelper) + public DotnetHostHelper(IFileHelper fileHelper, IEnvironment environment) { this.fileHelper = fileHelper; + this.environment = environment; } /// public string GetDotnetHostFullPath() { - char separator = ';'; var dotnetExeName = "dotnet.exe"; -#if !NET46 - // Use semicolon(;) as path separator for windows - // colon(:) for Linux and OSX - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if (!this.environment.OperatingSystem.Equals(PlatformOperatingSystem.Windows)) { - separator = ':'; dotnetExeName = "dotnet"; } -#endif var pathString = Environment.GetEnvironmentVariable("PATH"); - foreach (string path in pathString.Split(separator)) + foreach (string path in pathString.Split(Path.PathSeparator)) { string exeFullPath = Path.Combine(path.Trim(), dotnetExeName); if (this.fileHelper.Exists(exeFullPath)) diff --git a/src/Microsoft.TestPlatform.ObjectModel/Utilities/XmlRunSettingsUtilities.cs b/src/Microsoft.TestPlatform.ObjectModel/Utilities/XmlRunSettingsUtilities.cs index 8bb8f8bbf9..7323c96cca 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/Utilities/XmlRunSettingsUtilities.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/Utilities/XmlRunSettingsUtilities.cs @@ -12,6 +12,7 @@ namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities using System.Xml; using System.Xml.XPath; + using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions; using ObjectModelResources = Microsoft.VisualStudio.TestPlatform.ObjectModel.Resources.Resources; /// @@ -26,22 +27,17 @@ public static ObjectModel.Architecture OSArchitecture { get { -#if NET46 - // This is a workaround for https://github.com/dotnet/corefx/issues/13566 - return WindowsSystemInformation.GetArchitecture(); -#else - var arch = RuntimeInformation.OSArchitecture; + var arch = PlatformEnvironment.Architecture; switch (arch) { - case Architecture.X64: + case PlatformArchitecture.X64: return ObjectModel.Architecture.X64; - case Architecture.X86: + case PlatformArchitecture.X86: return ObjectModel.Architecture.X86; default: return ObjectModel.Architecture.ARM; } -#endif } } @@ -390,60 +386,4 @@ public static DataCollectionRunSettings GetInProcDataCollectionRunSettings(strin return null; } } - -#if NET46 - internal static class WindowsSystemInformation - { - internal const ushort PROCESSOR_ARCHITECTURE_INTEL = 0; - internal const ushort PROCESSOR_ARCHITECTURE_ARM = 5; - internal const ushort PROCESSOR_ARCHITECTURE_IA64 = 6; - internal const ushort PROCESSOR_ARCHITECTURE_AMD64 = 9; - internal const ushort PROCESSOR_ARCHITECTURE_UNKNOWN = 0xFFFF; - - [StructLayout(LayoutKind.Sequential)] - internal struct SYSTEM_INFO - { - public ushort wProcessorArchitecture; - public ushort wReserved; - public uint dwPageSize; - public IntPtr lpMinimumApplicationAddress; - public IntPtr lpMaximumApplicationAddress; - public UIntPtr dwActiveProcessorMask; - public uint dwNumberOfProcessors; - public uint dwProcessorType; - public uint dwAllocationGranularity; - public ushort wProcessorLevel; - public ushort wProcessorRevision; - }; - - [DllImport("kernel32.dll")] - internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo); - - public static ObjectModel.Architecture GetArchitecture() - { - SYSTEM_INFO sysInfo = new SYSTEM_INFO(); - - // GetNativeSystemInfo is supported from Windows XP onwards. Since test platform - // requires Windows 7 OS at the minimum, we don't require a fallback. - GetNativeSystemInfo(ref sysInfo); - - switch (sysInfo.wProcessorArchitecture) - { - case PROCESSOR_ARCHITECTURE_INTEL: - return ObjectModel.Architecture.X86; - case PROCESSOR_ARCHITECTURE_ARM: - return ObjectModel.Architecture.ARM; - case PROCESSOR_ARCHITECTURE_IA64: - return ObjectModel.Architecture.X64; - case PROCESSOR_ARCHITECTURE_AMD64: - return ObjectModel.Architecture.X64; - case PROCESSOR_ARCHITECTURE_UNKNOWN: - EqtTrace.Error("WindowsSystemInformation.GetArchitecture: Unknown architecture found, will use default."); - break; - } - - return ObjectModel.Architecture.Default; - } - } -#endif } diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/IEnvironment.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/IEnvironment.cs new file mode 100644 index 0000000000..50289c3b3c --- /dev/null +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/IEnvironment.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions +{ + using System; + + /// + /// Operating system environment abstractions. + /// + public interface IEnvironment + { + /// + /// Operating System architecture. + /// + PlatformArchitecture Architecture { get; } + + /// + /// Operating System name. + /// + PlatformOperatingSystem OperatingSystem { get; } + } +} diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/Process/IProcessHelper.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/IProcessHelper.cs similarity index 100% rename from src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/Process/IProcessHelper.cs rename to src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/IProcessHelper.cs diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformArchitecture.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformArchitecture.cs new file mode 100644 index 0000000000..315ce99c15 --- /dev/null +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformArchitecture.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions +{ + using System; + + /// + /// Available architectures for test platform. + /// + public enum PlatformArchitecture + { + X86, + X64, + ARM, + ARM64 + } +} diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformOperationSystem.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformOperationSystem.cs new file mode 100644 index 0000000000..34b93b7309 --- /dev/null +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/Interfaces/System/PlatformOperationSystem.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions +{ + using System; + + /// + /// Available operating systems. + /// + public enum PlatformOperatingSystem + { + Windows, + Unix + } +} diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/Microsoft.TestPlatform.PlatformAbstractions.csproj b/src/Microsoft.TestPlatform.PlatformAbstractions/Microsoft.TestPlatform.PlatformAbstractions.csproj index 59eb92501f09a8f8b2b4820a2ea86306d03d715a..9ca3c3477c387bad89a699756e1e96cf14f4feeb 100644 GIT binary patch delta 214 zcmZ3W_(*w!0}HzX0~l_0WYK4yoWRO5IgXEQG9Rnb_FllKw?Wx{=hY9@*W;5kiOs`hEj$+h7yKMhFpeJ20aE(FfWy%h#{Y$ zfFT$tR>V*SluZV*ih=T|>Ord0fGUfCx)Z^sG9KaDWxsLk{vRgM#;1OV9 J;bq`r000lLIn4k7 delta 90 zcmaE)yg+e-0}C@JgU#e_7SqjjEI*hSIVTIUs!l$~n>M+DpJ%cN#~h%@MRwQ8e>fJv bSrU`DRv + public class PlatformEnvironment : IEnvironment + { + /// + public PlatformArchitecture Architecture + { + get + { + // On Mono System.Runtime.InteropServices.RuntimeInformation breaks + // See https://github.com/dotnet/corefx/issues/15112 + // Support just x86 and x64 for now, likely our solution for ARM is going to be + // netcore based. + return Environment.Is64BitOperatingSystem ? PlatformArchitecture.X64 : PlatformArchitecture.X86; + } + } + + /// + public PlatformOperatingSystem OperatingSystem + { + get + { + // Ensure the value is detected appropriately for Desktop CLR, Mono CLR 1.x and Mono + // CLR 2.x. See below link for more information: + // http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform + int p = (int) System.Environment.OSVersion.Platform; + if ((p == 4) || (p == 6) || (p == 128)) + { + return PlatformOperatingSystem.Unix; + } + + return PlatformOperatingSystem.Windows; + } + } + } +} diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformEnvironment.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformEnvironment.cs new file mode 100644 index 0000000000..bf923d60a3 --- /dev/null +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/netcore/System/PlatformEnvironment.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions +{ + using System; + using System.Runtime.InteropServices; + + /// + public class PlatformEnvironment : IEnvironment + { + /// + public PlatformArchitecture Architecture + { + get + { + switch (RuntimeInformation.OSArchitecture) + { + case System.Runtime.InteropServices.Architecture.X86: + return PlatformArchitecture.X86; + case System.Runtime.InteropServices.Architecture.X64: + return PlatformArchitecture.X64; + case System.Runtime.InteropServices.Architecture.Arm: + return PlatformArchitecture.ARM; + case System.Runtime.InteropServices.Architecture.Arm64: + return PlatformArchitecture.ARM64; + default: + throw new NotSupportedException(); + } + } + } + + /// + public PlatformOperatingSystem OperatingSystem + { + get + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return PlatformOperatingSystem.Windows; + } + + return PlatformOperatingSystem.Unix; + } + } + } +} diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/System/PlatformEnvironment.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/System/PlatformEnvironment.cs new file mode 100644 index 0000000000..dc2d2b3f95 --- /dev/null +++ b/src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/System/PlatformEnvironment.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.VisualStudio.TestPlatform.PlatformAbstractions +{ + using System; + + /// + public class PlatformEnvironment : IEnvironment + { + /// + public PlatformArchitecture Architecture + { + get + { + throw new NotImplementedException(); + } + } + + /// + public PlatformOperatingSystem OperatingSystem + { + get + { + throw new NotImplementedException(); + } + } + } +} diff --git a/src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/Process/ProcessHelper.cs b/src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/System/ProcessHelper.cs similarity index 100% rename from src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/Process/ProcessHelper.cs rename to src/Microsoft.TestPlatform.PlatformAbstractions/netstandard1.0/System/ProcessHelper.cs