Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ UWP Reflection based discovery #1336

Merged
merged 7 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Microsoft.TestPlatform.ObjectModel/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ namespace Microsoft.VisualStudio.TestPlatform.ObjectModel
using System.IO;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.ObjectModel.Utilities;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;

/// <summary>
/// Stores information about a test case.
/// </summary>
Expand Down Expand Up @@ -221,10 +223,19 @@ private Guid GetTestId()
// For example in the database adapter case this is not a file path.
string source = this.Source;

if (File.Exists(source))
// As discussed with team, we found no scenario for netcore, & fullclr where the Source is not present where ID is generated,
// which means we would always use FileName to generate ID. In cases where somehow Source Path contained garbage character the API Path.GetFileName()
// we are simply returning original input.
// For UWP where source during discovery, & during execution can be on different machine, in such case we should always use Path.GetFileName()
try
{
// If source name is malformed, GetFileName API will throw exception, so use same input malformed string to generate ID
source = Path.GetFileName(source);
}
catch
{
// do nothing
}

string testcaseFullName = this.ExecutorUri + source + this.FullyQualifiedName;
return EqtHash.GuidFromString(testcaseFullName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.DesktopTestHostRuntimeProvider
{
using System.IO;
using System.Xml.Linq;

/// <summary> Wrapper for an appxmanifest file. </summary>
internal static class AppxManifestFile
{
/// <summary> Gets the app's exe name. </summary>
/// <param name="filePath">
/// AppxManifest filePath
/// </param>
/// <returns>ExecutableName</returns>
public static string GetApplicationExecutableName(string filePath)
{
if (File.Exists(filePath))
{
var doc = XDocument.Load(filePath);
var ns = doc.Root.Name.Namespace;

return doc.Element(ns + "Package").
Element(ns + "Applications").
Element(ns + "Application").
Attribute("Executable").Value;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.TestPlatform.TestHostProvider.Hosting;
using Microsoft.TestPlatform.TestHostProvider.Resources;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Extensions;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces;
using Microsoft.VisualStudio.TestPlatform.DesktopTestHostRuntimeProvider;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;
Expand Down Expand Up @@ -203,7 +205,21 @@ public IEnumerable<string> GetTestPlatformExtensions(IEnumerable<string> sources
/// <inheritdoc/>
public IEnumerable<string> GetTestSources(IEnumerable<string> sources)
{
// We do not have scenario where full CLR tests are deployed to remote machine, so no need to udpate sources
// We are doing this specifically for UWP, should we extract it out to some other utility?
// Why? Lets say if we have to do same for someother source extension, would we just add another if check?
var uwpSources = sources.Where(source => source.EndsWith(".appxrecipe", StringComparison.OrdinalIgnoreCase));

if (uwpSources.Any())
{
List<string> actualSources = new List<string>();
foreach (var uwpSource in uwpSources)
{
actualSources.Add(Path.Combine(Path.GetDirectoryName(uwpSource), this.GetUwpSources(uwpSource)));
}

return actualSources;
}

return sources;
}

Expand Down Expand Up @@ -380,5 +396,23 @@ private bool LaunchHost(TestProcessStartInfo testHostStartInfo, CancellationToke
this.OnHostLaunched(new HostProviderEventArgs("Test Runtime launched", 0, this.testHostProcess.Id));
return this.testHostProcess != null;
}

private string GetUwpSources(string uwpSource)
{
var doc = XDocument.Load(uwpSource);
var ns = doc.Root.Name.Namespace;

string appxManifestPath = doc.Element(ns + "Project").
Element(ns + "ItemGroup").
Element(ns + "AppXManifest").
Attribute("Include").Value;

if (!Path.IsPathRooted(appxManifestPath))
{
appxManifestPath = Path.Combine(Path.GetDirectoryName(uwpSource), appxManifestPath);
}

return AppxManifestFile.GetApplicationExecutableName(appxManifestPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace TestPlatform.TestHostProvider.UnitTests.Hosting
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Extensions;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Helpers.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Hosting;
using Microsoft.VisualStudio.TestPlatform.DesktopTestHostRuntimeProvider;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Host;
Expand Down Expand Up @@ -378,6 +380,23 @@ public void LaunchTestHostShouldUseCustomHostIfSet()
Assert.AreEqual(currentProcess.Id, this.testHostId);
}

[TestMethod]
public void GetTestSourcesShouldReturnAppropriateSourceIfAppxRecipeIsProvided()
{
var sourcePath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation()), @"..\..\..\..\TestAssets\UWPTestAssets\UnitTestApp8.build.appxrecipe");
IEnumerable<string> sources = this.testHostManager.GetTestSources(new List<string> { sourcePath });
Assert.IsTrue(sources.Any());
Assert.IsTrue(sources.FirstOrDefault().EndsWith(".exe", StringComparison.OrdinalIgnoreCase));
}

[TestMethod]
public void AppxManifestFileShouldReturnAppropriateSourceIfAppxManifestIsProvided()
{
var appxManifestPath = Path.Combine(Path.GetDirectoryName(typeof(TestableTestHostManager).GetTypeInfo().Assembly.GetAssemblyLocation()), @"..\..\..\..\TestAssets\UWPTestAssets\AppxManifest.xml");
string source = AppxManifestFile.GetApplicationExecutableName(appxManifestPath);
Assert.AreEqual("UnitTestApp8.exe", source);
}

[TestMethod]
public async Task ErrorMessageShouldBeReadAsynchronously()
{
Expand Down
75 changes: 75 additions & 0 deletions test/TestAssets/UWPTestAssets/AppxManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp build" xmlns:build="http://schemas.microsoft.com/developer/appx/2015/build">
<!--
THIS PACKAGE MANIFEST FILE IS GENERATED BY THE BUILD PROCESS.

Changes to this file will be lost when it is regenerated. To correct errors in this file, edit the source .appxmanifest file.

For more information on package manifest files, see http://go.microsoft.com/fwlink/?LinkID=241727
-->
<Identity Name="e7a3d303-9783-498c-9285-7474e1e66396" Publisher="CN=maban" Version="1.0.0.0" ProcessorArchitecture="x86" />
<mp:PhoneIdentity PhoneProductId="e7a3d303-9783-498c-9285-7474e1e66396" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
<Properties>
<DisplayName>UnitTestApp8</DisplayName>
<PublisherDisplayName>maban</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.10586.0" MaxVersionTested="10.0.16299.0" />
<PackageDependency Name="Microsoft.VCLibs.140.00" MinVersion="14.0.25426.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
</Dependencies>
<Resources>
<Resource Language="EN-US" />
</Resources>
<Applications>
<Application Id="vstest.executionengine.universal.App" Executable="UnitTestApp8.exe" EntryPoint="UnitTestApp8.App">
<uap:VisualElements DisplayName="UnitTestApp8" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="UnitTestApp8" BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png" />
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClientServer" />
</Capabilities>
<Extensions>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>CLRHost.dll</Path>
<ActivatableClass ActivatableClassId="Microsoft.VisualStudio.TestPlatform.TestExecutor.WinRTCore.UnitTestClient" ThreadingModel="both" />
</InProcessServer>
</Extension>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>CppUnitTestFramework_Executor_WindowsPhone.dll</Path>
<ActivatableClass ActivatableClassId="CppUnitTestFramework_Executor_WindowsPhone.CppTestCase" ThreadingModel="both" />
<ActivatableClass ActivatableClassId="CppUnitTestFramework_Executor_WindowsPhone.TestResult" ThreadingModel="both" />
<ActivatableClass ActivatableClassId="CppUnitTestFramework_Executor_WindowsPhone.CTestRunner" ThreadingModel="both" />
</InProcessServer>
</Extension>
<Extension Category="windows.activatableClass.inProcessServer">
<InProcessServer>
<Path>vstest_executionengine_platformbridge.dll</Path>
<ActivatableClass ActivatableClassId="vstest_executionengine_platformbridge.MessageTransmitter" ThreadingModel="both" />
<ActivatableClass ActivatableClassId="vstest_executionengine_platformbridge.ServiceMain" ThreadingModel="both" />
<ActivatableClass ActivatableClassId="vstest_executionengine_platformbridge.NativeMethods" ThreadingModel="both" />
</InProcessServer>
</Extension>
</Extensions>
<build:Metadata>
<build:Item Name="cl.exe" Version="19.13.26009.0 built by: VCTOOLSREL" />
<build:Item Name="VisualStudio" Version="15.0" />
<build:Item Name="VisualStudioEdition" Value="Microsoft Visual Studio Enterprise 2017" />
<build:Item Name="OperatingSystem" Version="10.0.16299.15 (WinBuild.160101.0800)" />
<build:Item Name="Microsoft.Build.AppxPackage.dll" Version="15.0.27215.3002" />
<build:Item Name="ProjectGUID" Value="{f0aa89fc-5ffc-4a0b-819b-0bae6b341260}" />
<build:Item Name="ilc.exe" Version="1.4.24211.00 built by: PROJECTNGDR2" />
<build:Item Name="OptimizingToolset" Value="ilc.exe" />
<build:Item Name="UseDotNetNativeSharedAssemblyFrameworkPackage" Value="True" />
<build:Item Name="UniversalGenericsOptOut" Value="false" />
<build:Item Name="Microsoft.Windows.UI.Xaml.Build.Tasks.dll" Version="15.0.27215.3002" />
<build:Item Name="CppUnitTestFramework.Universal" Version="15.0" />
<build:Item Name="TestPlatform.Universal" Version="15.0" />
<build:Item Name="MakePri.exe" Version="10.0.16299.15 (WinBuild.160101.0800)" />
</build:Metadata>
</Package>
Loading