Skip to content

Commit

Permalink
Fix assembly loading in symbol loader (#340)
Browse files Browse the repository at this point in the history
* Fix assembly loading in symbol loader. (#332)

Instead of attempting to load the test assembly, try to query for it. It should
have been already loaded by the test adapter.

In netcoreapp1.1, attempting to load the same executable within itself throws.

* Add error tracing for symbol parse failure.

* Fix platform tests for objectmodel.

* Fix debug type. Copy manifest file and comcomponents to output directory.
  • Loading branch information
codito authored Jan 19, 2017
1 parent 221b75b commit dc71b0a
Show file tree
Hide file tree
Showing 8 changed files with 30,170 additions and 108 deletions.
123 changes: 69 additions & 54 deletions TestPlatform.sln

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ private void PopulateCacheForTypeAndMethodSymbols(string binaryPath)
var pdbFilePath = Path.ChangeExtension(binaryPath, ".pdb");
using (var pdbReader = new PortablePdbReader(new FileHelper().GetStream(pdbFilePath, FileMode.Open, FileAccess.Read)))
{
// Load assembly
var asm = AssemblyLoadContext.Default.LoadFromAssemblyPath(binaryPath);
// At this point, the assembly should be already loaded into the load context. We query for a reference to
// find the types and cache the symbol information. Let the loader follow default lookup order instead of
// forcing load from a specific path.
var asm = Assembly.Load(AssemblyLoadContext.GetAssemblyName(binaryPath));

foreach (var type in asm.GetTypes())
{
Expand Down Expand Up @@ -126,12 +128,14 @@ private void PopulateCacheForTypeAndMethodSymbols(string binaryPath)
}
}
}
catch (Exception)
catch (Exception ex)
{
EqtTrace.Error("PortableSymbolReader: Failed to load symbols for binary: {0}", binaryPath);
EqtTrace.Error(ex);
this.Dispose();
throw;
}
}
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

namespace Microsoft.TestPlatform.ObjectModel.PlatformTests
{
using System;
using System.Diagnostics;

using Microsoft.TestPlatform.TestUtilities;
using Microsoft.TestPlatform.TestUtilities.PerfInstrumentation;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -32,16 +30,16 @@ public static string GetAndSetTargetFrameWork(IntegrationTestEnvironment testEnv
public void GetNavigationDataShouldReturnCorrectFileNameAndLineNumber()
{
var currentTargetFrameWork = GetAndSetTargetFrameWork(this.testEnvironment);
var assemblyPath = this.GetSampleTestAssembly();
var assemblyPath = GetAssetFullPath("SimpleClassLibrary.dll");

DiaSession diaSession = new DiaSession(assemblyPath);
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SampleUnitTestProject.UnitTest1", "PassingTest");
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SimpleClassLibrary.Class1", "PassingTest");

Assert.IsNotNull(diaNavigationData, "Failed to get navigation data");
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleTestProject\UnitTest1.cs");
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleClassLibrary\Class1.cs");

Assert.AreEqual(23, diaNavigationData.MinLineNumber, "Incorrect min line number");
Assert.AreEqual(25, diaNavigationData.MaxLineNumber, "Incorrect max line number");
Assert.AreEqual(12, diaNavigationData.MinLineNumber, "Incorrect min line number");
Assert.AreEqual(14, diaNavigationData.MaxLineNumber, "Incorrect max line number");

this.testEnvironment.TargetFramework = currentTargetFrameWork;
}
Expand All @@ -50,16 +48,16 @@ public void GetNavigationDataShouldReturnCorrectFileNameAndLineNumber()
public void GetNavigationDataShouldReturnCorrectDataForAsyncMethod()
{
var currentTargetFrameWork = GetAndSetTargetFrameWork(this.testEnvironment);
var assemblyPath = this.GetAssetFullPath("SimpleTestProject3.dll");
var assemblyPath = this.GetAssetFullPath("SimpleClassLibrary.dll");

DiaSession diaSession = new DiaSession(assemblyPath);
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SampleUnitTestProject3.UnitTest1+<AsyncTestMethod>d__1", "MoveNext");
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SimpleClassLibrary.Class1+<AsyncTestMethod>d__1", "MoveNext");

Assert.IsNotNull(diaNavigationData, "Failed to get navigation data");
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleTestProject3\UnitTest1.cs");
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleClassLibrary\Class1.cs");

Assert.AreEqual(20, diaNavigationData.MinLineNumber, "Incorrect min line number");
Assert.AreEqual(22, diaNavigationData.MaxLineNumber, "Incorrect max line number");
Assert.AreEqual(17, diaNavigationData.MinLineNumber, "Incorrect min line number");
Assert.AreEqual(19, diaNavigationData.MaxLineNumber, "Incorrect max line number");

this.testEnvironment.TargetFramework = currentTargetFrameWork;
}
Expand All @@ -68,16 +66,16 @@ public void GetNavigationDataShouldReturnCorrectDataForAsyncMethod()
public void GetNavigationDataShouldReturnCorrectDataForOverLoadedMethod()
{
var currentTargetFrameWork = GetAndSetTargetFrameWork(this.testEnvironment);
var assemblyPath = this.GetAssetFullPath("SimpleTestProject3.dll");
var assemblyPath = this.GetAssetFullPath("SimpleClassLibrary.dll");

DiaSession diaSession = new DiaSession(assemblyPath);
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SampleUnitTestProject3.Class1", "OverLoadededMethod");
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SimpleClassLibrary.Class1", "OverLoadedMethod");

Assert.IsNotNull(diaNavigationData, "Failed to get navigation data");
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleTestProject3\UnitTest1.cs");
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleClassLibrary\Class1.cs");

Assert.AreEqual(32, diaNavigationData.MinLineNumber, "Incorrect min line number");
Assert.AreEqual(33, diaNavigationData.MaxLineNumber, "Incorrect max line number");
Assert.AreEqual(26, diaNavigationData.MinLineNumber, "Incorrect min line number");
Assert.AreEqual(27, diaNavigationData.MaxLineNumber, "Incorrect max line number");

this.testEnvironment.TargetFramework = currentTargetFrameWork;
}
Expand All @@ -86,16 +84,16 @@ public void GetNavigationDataShouldReturnCorrectDataForOverLoadedMethod()
public void GetNavigationDataShouldReturnNullForNotExistMethodNameOrNotExistTypeName()
{
var currentTargetFrameWork = GetAndSetTargetFrameWork(this.testEnvironment);
var assemblyPath = this.GetSampleTestAssembly();
var assemblyPath = GetAssetFullPath("SimpleClassLibrary.dll");

DiaSession diaSession = new DiaSession(assemblyPath);

// Not exist method name
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SampleUnitTestProject.UnitTest1", "NotExistMethod");
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SimpleClassLibrary.Class1", "NotExistMethod");
Assert.IsNull(diaNavigationData);

// Not Exist Type name
diaNavigationData = diaSession.GetNavigationData("SampleUnitTestProject.NotExistType", "PassingTest");
diaNavigationData = diaSession.GetNavigationData("SimpleClassLibrary.NotExistType", "PassingTest");
Assert.IsNull(diaNavigationData);

this.testEnvironment.TargetFramework = currentTargetFrameWork;
Expand All @@ -105,21 +103,21 @@ public void GetNavigationDataShouldReturnNullForNotExistMethodNameOrNotExistType
public void DiaSessionPerfTest()
{
var currentTargetFrameWork = GetAndSetTargetFrameWork(this.testEnvironment);
var assemblyPath = this.GetAssetFullPath("PerfTestProject.dll");
var assemblyPath = this.GetAssetFullPath("SimpleClassLibrary.dll");

var watch = Stopwatch.StartNew();
DiaSession diaSession = new DiaSession(assemblyPath);
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("PerfTestProject.UnitTest1", "MSTest_D1_01");
DiaNavigationData diaNavigationData = diaSession.GetNavigationData("SimpleClassLibrary.HugeMethodSet", "MSTest_D1_01");
watch.Stop();

Assert.IsNotNull(diaNavigationData, "Failed to get navigation data");
StringAssert.EndsWith(diaNavigationData.FileName, @"\PerfTestProject\UnitTest1.cs");
Assert.AreEqual(diaNavigationData.MinLineNumber, 17);
Assert.AreEqual(diaNavigationData.MaxLineNumber, 20);
StringAssert.EndsWith(diaNavigationData.FileName, @"\SimpleClassLibrary\HugeMethodSet.cs");
Assert.AreEqual(9, diaNavigationData.MinLineNumber);
Assert.AreEqual(10, diaNavigationData.MaxLineNumber);
var expectedTime = 150;
Assert.IsTrue(watch.Elapsed.Milliseconds < expectedTime, string.Format("DiaSession Perf test Actual time:{0} ms Expected time:{1} ms", watch.Elapsed.Milliseconds, expectedTime));

this.testEnvironment.TargetFramework = currentTargetFrameWork;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TestPlatformRoot Condition="$(TestPlatformRoot) == ''">..\..\</TestPlatformRoot>
<TestProject>true</TestProject>
Expand All @@ -18,10 +17,25 @@
<ProjectReference Include="..\Microsoft.TestPlatform.TestUtilities\Microsoft.TestPlatform.TestUtilities.csproj" />
<ProjectReference Include="..\..\src\Microsoft.TestPlatform.ObjectModel\Microsoft.TestPlatform.ObjectModel.csproj" />
<ProjectReference Include="..\..\src\Microsoft.TestPlatform.CoreUtilities\Microsoft.TestPlatform.CoreUtilities.csproj" />

<!-- We need SimpleClassLibrary is dependency bucket of this test suite to allow loading symbols -->
<ProjectReference Include="..\TestAssets\SimpleClassLibrary\SimpleClassLibrary.csproj">
<P2P>true</P2P>
</ProjectReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />

<PackageReference Include="Microsoft.Internal.Dia" Version="14.0.0" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\src\package\TestPlatform.ObjectModel.manifest">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\..\src\package\TestPlatform.ObjectModel.x86.manifest">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
Expand Down
29 changes: 29 additions & 0 deletions test/TestAssets/SimpleClassLibrary/Class1.cs
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 SimpleClassLibrary
{
using System.Diagnostics;
using System.Threading.Tasks;

public class Class1
{
public void PassingTest()
{
Debug.Assert(2 == 2);
}

public async Task AsyncTestMethod()
{
await Task.CompletedTask;
}

public void OverLoadedMethod()
{
}

public void OverLoadedMethod(string name)
{
}
}
}
Loading

0 comments on commit dc71b0a

Please sign in to comment.