Skip to content

Commit

Permalink
Add platform abstraction for OS name and architecture.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun Mahapatra committed Jun 12, 2017
1 parent d66ac59 commit 9013447
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,44 @@ 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;

/// <summary>
/// Initializes a new instance of the <see cref="DotnetHostHelper"/> class.
/// </summary>
public DotnetHostHelper() : this(new FileHelper())
public DotnetHostHelper() : this(new FileHelper(), new PlatformEnvironment())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DotnetHostHelper"/> class.
/// </summary>
/// <param name="fileHelper">File Helper</param>
public DotnetHostHelper(IFileHelper fileHelper)
public DotnetHostHelper(IFileHelper fileHelper, IEnvironment environment)
{
this.fileHelper = fileHelper;
this.environment = environment;
}

/// <inheritdoc />
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Operating system environment abstractions.
/// </summary>
public interface IEnvironment
{
/// <summary>
/// Operating System architecture.
/// </summary>
PlatformArchitecture Architecture { get; }

/// <summary>
/// Operating System name.
/// </summary>
PlatformOperatingSystem OperatingSystem { get; }
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Available architectures for test platform.
/// </summary>
public enum PlatformArchitecture
{
X86,
X64,
ARM,
ARM64
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Available operating systems.
/// </summary>
public enum PlatformOperatingSystem
{
Windows,
Unix
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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;

/// <inheritdoc />
public class PlatformEnvironment : IEnvironment
{
/// <inheritdoc />
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;
}
}

/// <inheritdoc />
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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <inheritdoc />
public class PlatformEnvironment : IEnvironment
{
/// <inheritdoc />
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();
}
}
}

/// <inheritdoc />
public PlatformOperatingSystem OperatingSystem
{
get
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return PlatformOperatingSystem.Windows;
}

return PlatformOperatingSystem.Unix;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

/// <inheritdoc />
public class PlatformEnvironment : IEnvironment
{
/// <inheritdoc />
public PlatformArchitecture Architecture
{
get
{
throw new NotImplementedException();
}
}

/// <inheritdoc />
public PlatformOperatingSystem OperatingSystem
{
get
{
throw new NotImplementedException();
}
}
}
}

0 comments on commit 9013447

Please sign in to comment.