Skip to content

Commit

Permalink
Fix assembly resolution exception that would happen during every test
Browse files Browse the repository at this point in the history
The reason this issue started when the tests were moved upstream was due to a behavior change in the newer nunit.  nunit/nunit#1072

This PR uses more explicit resolution for the input & output which is more correct anyways
  • Loading branch information
mrvoorhe committed May 1, 2017
1 parent fd736b4 commit 95b16bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
35 changes: 31 additions & 4 deletions linker/Tests/Mono.Linker.Tests.Core/DefaultChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public override void Check(LinkedTestCaseResult linkResult)

int expectedNumberOfAssertionsToMake = 0;

using (var original = AssemblyDefinition.ReadAssembly(linkResult.InputAssemblyPath.ToString()))
using (var original = ReadAssembly(linkResult.InputAssemblyPath))
{
expectedNumberOfAssertionsToMake += PerformOutputAssemblyChecks(original, linkResult.LinkedAssemblyPath.Parent);
expectedNumberOfAssertionsToMake += PerformOutputAssemblyChecks(original.Definition, linkResult.LinkedAssemblyPath.Parent);

using (var linked = AssemblyDefinition.ReadAssembly(linkResult.LinkedAssemblyPath.ToString()))
using (var linked = ReadAssembly(linkResult.LinkedAssemblyPath))
{
expectedNumberOfAssertionsToMake += CompareAssemblies(original, linked);
expectedNumberOfAssertionsToMake += CompareAssemblies(original.Definition, linked.Definition);
}
}

Expand All @@ -53,6 +53,15 @@ public override void Check(LinkedTestCaseResult linkResult)
Assert.AreEqual(_assertionCounter.AssertionsMade, expectedNumberOfAssertionsToMake, $"Expected to make {expectedNumberOfAssertionsToMake} assertions, but only made {_assertionCounter.AssertionsMade}. The test may be invalid or there may be a bug in the checking logic");
}

private static AssemblyContainer ReadAssembly(NPath assemblyPath)
{
var readerParams = new ReaderParameters();
var resolver = new AssemblyResolver();
readerParams.AssemblyResolver = resolver;
resolver.AddSearchDirectory(assemblyPath.Parent.ToString());
return new AssemblyContainer(AssemblyDefinition.ReadAssembly(assemblyPath.ToString(), readerParams), resolver);
}

private int PerformOutputAssemblyChecks (AssemblyDefinition original, NPath outputDirectory)
{
var assembliesToCheck = original.MainModule.Types.SelectMany(t => t.CustomAttributes).Where(attr => attr.IsAssemblyAssertion()).ToArray();
Expand Down Expand Up @@ -253,6 +262,24 @@ public override string ToString()
}
}

private class AssemblyContainer : IDisposable
{
public readonly AssemblyResolver Resolver;
public readonly AssemblyDefinition Definition;

public AssemblyContainer(AssemblyDefinition definition, AssemblyResolver resolver)
{
Definition = definition;
Resolver = resolver;
}

public void Dispose()
{
Resolver?.Dispose();
Definition?.Dispose();
}
}

private class AssertionCounter
{
public int AssertionsMade { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
<Project>{d68133bd-1e63-496e-9ede-4fbdbf77b486}</Project>
<Name>Mono.Cecil</Name>
</ProjectReference>
<ProjectReference Include="..\..\Mono.Linker.csproj">
<Project>{dd28e2b1-057b-4b4d-a04d-b2ebd9e76e46}</Project>
<Name>Mono.Linker</Name>
</ProjectReference>
<ProjectReference Include="..\Mono.Linker.Tests.Cases.Expectations\Mono.Linker.Tests.Cases.Expectations.csproj">
<Project>{2c26601f-3e2f-45b9-a02f-58ee9296e19e}</Project>
<Name>Mono.Linker.Tests.Cases.Expectations</Name>
Expand Down

0 comments on commit 95b16bd

Please sign in to comment.