Skip to content

Commit 2e35bad

Browse files
authored
Added WinUITestSourceHost (#822)
Small fix that was preventing tests from running from VS TestExplorer.
1 parent faf5270 commit 2e35bad

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

src/Adapter/PlatformServices.WinUI/PlatformServices.WinUI.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@
4747
<Compile Include="..\PlatformServices.Shared\netstandard1.0\Services\ns10MSTestSettingsProvider.cs" Link="Services\ns10MSTestSettingsProvider.cs" />
4848
<Compile Include="..\PlatformServices.Shared\netstandard1.0\ns10RecursiveDirectoryPath.cs" Link="ns10RecursiveDirectoryPath.cs" />
4949
<Compile Include="..\PlatformServices.Shared\netstandard1.0\Services\ns10TestContextPropertyStrings.cs" Link="Services\ns10TestContextPropertyStrings.cs" />
50-
<Compile Include="..\PlatformServices.NetCore\Utilities\NetCoreReflectionUtility.cs" Link="Utilities\NetCoreReflectionUtility.cs"/>
51-
<Compile Include="..\PlatformServices.NetCore\Utilities\NetCoreDeploymentUtility.cs" Link="Utilities\NetCoreDeploymentUtility.cs"/>
50+
<Compile Include="..\PlatformServices.NetCore\Utilities\NetCoreReflectionUtility.cs" Link="Utilities\NetCoreReflectionUtility.cs" />
51+
<Compile Include="..\PlatformServices.NetCore\Utilities\NetCoreDeploymentUtility.cs" Link="Utilities\NetCoreDeploymentUtility.cs" />
5252
<Compile Include="..\PlatformServices.NetCore\Deployment\NetCoreTestRunDirectories.cs" Link="Deployment\NetCoreTestRunDirectories.cs" />
5353
<Compile Include="..\PlatformServices.NetCore\Utilities\NetCoreAssemblyUtility.cs" Link="Utilities\NetCoreAssemblyUtility.cs" />
54-
<Compile Include="..\PlatformServices.NetCore\Services\NetCoreTestSourceHost.cs" Link="Services\NetCoreTestSourceHost.cs" />
5554
<Compile Include="..\PlatformServices.Shared\netstandard1.0\Services\ns10ThreadOperations.cs">
5655
<Link>Services\ns10ThreadOperations.cs</Link>
5756
</Compile>
@@ -66,6 +65,7 @@
6665
<Compile Include="Services\WinUIFileOperations.cs" />
6766
<Compile Include="Services\WinUITestSource.cs" />
6867
<Compile Include="Properties\AssemblyInfo.cs" />
68+
<Compile Include="Services\WinUITestSourceHost.cs" />
6969
<Folder Include="Resources\" />
7070
<Compile Include="..\PlatformServices.Shared\netstandard1.3\Resources\Resource.Designer.cs">
7171
<Link>Resources\Resource.Designer.cs</Link>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
5+
{
6+
using System;
7+
using System.IO;
8+
9+
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
10+
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
11+
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;
12+
13+
/// <summary>
14+
/// A host that loads the test source
15+
/// </summary>
16+
public class TestSourceHost : ITestSourceHost
17+
{
18+
private string sourceFileName;
19+
private string currentDirectory = null;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="TestSourceHost"/> class.
23+
/// </summary>
24+
/// <param name="sourceFileName"> The source file name. </param>
25+
/// <param name="runSettings"> The run-settings provided for this session. </param>
26+
/// <param name="frameworkHandle"> The handle to the test platform. </param>
27+
public TestSourceHost(string sourceFileName, IRunSettings runSettings, IFrameworkHandle frameworkHandle)
28+
{
29+
this.sourceFileName = sourceFileName;
30+
31+
// Set the environment context.
32+
this.SetContext(sourceFileName);
33+
}
34+
35+
/// <summary>
36+
/// Setup the isolation host.
37+
/// </summary>
38+
public void SetupHost()
39+
{
40+
}
41+
42+
/// <summary>
43+
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
44+
/// </summary>
45+
public void Dispose()
46+
{
47+
this.ResetContext();
48+
}
49+
50+
/// <summary>
51+
/// Creates an instance of a given type in the test source host.
52+
/// </summary>
53+
/// <param name="type"> The type that needs to be created in the host. </param>
54+
/// <param name="args">The arguments to pass to the constructor.
55+
/// This array of arguments must match in number, order, and type the parameters of the constructor to invoke.
56+
/// Pass in null for a constructor with no arguments.
57+
/// </param>
58+
/// <returns> An instance of the type created in the host.
59+
/// <see cref="object"/>.
60+
/// </returns>
61+
public object CreateInstanceForType(Type type, object[] args)
62+
{
63+
return Activator.CreateInstance(type, args);
64+
}
65+
66+
/// <summary>
67+
/// Sets context required for running tests.
68+
/// </summary>
69+
/// <param name="source">
70+
/// source parameter used for setting context
71+
/// </param>
72+
private void SetContext(string source)
73+
{
74+
if (string.IsNullOrEmpty(source))
75+
{
76+
return;
77+
}
78+
79+
Exception setWorkingDirectoryException = null;
80+
this.currentDirectory = Directory.GetCurrentDirectory();
81+
try
82+
{
83+
var dirName = Path.GetDirectoryName(source);
84+
if (string.IsNullOrEmpty(dirName))
85+
{
86+
dirName = Path.GetDirectoryName(typeof(TestSourceHost).Assembly.Location);
87+
}
88+
89+
Directory.SetCurrentDirectory(dirName);
90+
}
91+
catch (IOException ex)
92+
{
93+
setWorkingDirectoryException = ex;
94+
}
95+
catch (System.Security.SecurityException ex)
96+
{
97+
setWorkingDirectoryException = ex;
98+
}
99+
100+
if (setWorkingDirectoryException != null)
101+
{
102+
EqtTrace.Error("MSTestExecutor.SetWorkingDirectory: Failed to set the working directory to '{0}'. {1}", Path.GetDirectoryName(source), setWorkingDirectoryException);
103+
}
104+
}
105+
106+
/// <summary>
107+
/// Resets the context as it was before calling SetContext()
108+
/// </summary>
109+
private void ResetContext()
110+
{
111+
if (!string.IsNullOrEmpty(this.currentDirectory))
112+
{
113+
Directory.SetCurrentDirectory(this.currentDirectory);
114+
}
115+
}
116+
}
117+
118+
#pragma warning restore SA1649 // SA1649FileNameMustMatchTypeName
119+
}

0 commit comments

Comments
 (0)