diff --git a/src/Adapter/PlatformServices.WinUI/PlatformServices.WinUI.csproj b/src/Adapter/PlatformServices.WinUI/PlatformServices.WinUI.csproj index edb014985b..e49ece17e3 100644 --- a/src/Adapter/PlatformServices.WinUI/PlatformServices.WinUI.csproj +++ b/src/Adapter/PlatformServices.WinUI/PlatformServices.WinUI.csproj @@ -47,11 +47,10 @@ - - + + - Services\ns10ThreadOperations.cs @@ -66,6 +65,7 @@ + Resources\Resource.Designer.cs diff --git a/src/Adapter/PlatformServices.WinUI/Services/WinUITestSourceHost.cs b/src/Adapter/PlatformServices.WinUI/Services/WinUITestSourceHost.cs new file mode 100644 index 0000000000..5744535125 --- /dev/null +++ b/src/Adapter/PlatformServices.WinUI/Services/WinUITestSourceHost.cs @@ -0,0 +1,119 @@ +// 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.MSTestAdapter.PlatformServices +{ + using System; + using System.IO; + + using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface; + using Microsoft.VisualStudio.TestPlatform.ObjectModel; + using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter; + + /// + /// A host that loads the test source + /// + public class TestSourceHost : ITestSourceHost + { + private string sourceFileName; + private string currentDirectory = null; + + /// + /// Initializes a new instance of the class. + /// + /// The source file name. + /// The run-settings provided for this session. + /// The handle to the test platform. + public TestSourceHost(string sourceFileName, IRunSettings runSettings, IFrameworkHandle frameworkHandle) + { + this.sourceFileName = sourceFileName; + + // Set the environment context. + this.SetContext(sourceFileName); + } + + /// + /// Setup the isolation host. + /// + public void SetupHost() + { + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + this.ResetContext(); + } + + /// + /// Creates an instance of a given type in the test source host. + /// + /// The type that needs to be created in the host. + /// The arguments to pass to the constructor. + /// This array of arguments must match in number, order, and type the parameters of the constructor to invoke. + /// Pass in null for a constructor with no arguments. + /// + /// An instance of the type created in the host. + /// . + /// + public object CreateInstanceForType(Type type, object[] args) + { + return Activator.CreateInstance(type, args); + } + + /// + /// Sets context required for running tests. + /// + /// + /// source parameter used for setting context + /// + private void SetContext(string source) + { + if (string.IsNullOrEmpty(source)) + { + return; + } + + Exception setWorkingDirectoryException = null; + this.currentDirectory = Directory.GetCurrentDirectory(); + try + { + var dirName = Path.GetDirectoryName(source); + if (string.IsNullOrEmpty(dirName)) + { + dirName = Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location); + } + + Directory.SetCurrentDirectory(dirName); + } + catch (IOException ex) + { + setWorkingDirectoryException = ex; + } + catch (System.Security.SecurityException ex) + { + setWorkingDirectoryException = ex; + } + + if (setWorkingDirectoryException != null) + { + EqtTrace.Error("MSTestExecutor.SetWorkingDirectory: Failed to set the working directory to '{0}'. {1}", Path.GetDirectoryName(source), setWorkingDirectoryException); + } + } + + /// + /// Resets the context as it was before calling SetContext() + /// + private void ResetContext() + { + if (!string.IsNullOrEmpty(this.currentDirectory)) + { + Directory.SetCurrentDirectory(this.currentDirectory); + } + } + } + +#pragma warning restore SA1649 // SA1649FileNameMustMatchTypeName +}