diff options
author | Mike Voorhees <michaelv@unity3d.com> | 2017-05-12 15:55:42 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2017-05-12 18:00:08 +0300 |
commit | c07e471e1008198eadf833164f46c2ef3b40c4fa (patch) | |
tree | 7f4cf02e772bd33402add8de8c64221c3ca84671 /linker/Tests | |
parent | a5f27ed4fe21b4299aad63364475e615afe2dd3d (diff) |
Improve assembly resolution in ResultChecker
Add support for string type name in *InAssemblyAttributes so that non-public types can be checked
Diffstat (limited to 'linker/Tests')
5 files changed, 103 insertions, 47 deletions
diff --git a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptMemberInAssemblyAttribute.cs b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptMemberInAssemblyAttribute.cs index 31dd5131d..9d658219a 100644 --- a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptMemberInAssemblyAttribute.cs +++ b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptMemberInAssemblyAttribute.cs @@ -5,13 +5,20 @@ namespace Mono.Linker.Tests.Cases.Expectations.Assertions { [AttributeUsage (AttributeTargets.Class | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false)] public class KeptMemberInAssemblyAttribute : BaseExpectedLinkedBehaviorAttribute { public readonly string AssemblyFileName; - public readonly Type Type; + public readonly string TypeName; public readonly string [] MemberNames; public KeptMemberInAssemblyAttribute (string assemblyFileName, Type type, params string [] memberNames) { AssemblyFileName = assemblyFileName; - Type = type; + TypeName = type.ToString (); + MemberNames = memberNames; + } + + public KeptMemberInAssemblyAttribute (string assemblyFileName, string typeName, params string [] memberNames) + { + AssemblyFileName = assemblyFileName; + TypeName = typeName; MemberNames = memberNames; } } diff --git a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptTypeInAssemblyAttribute.cs b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptTypeInAssemblyAttribute.cs index f4725f9b9..2e0171a83 100644 --- a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptTypeInAssemblyAttribute.cs +++ b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/KeptTypeInAssemblyAttribute.cs @@ -6,12 +6,18 @@ namespace Mono.Linker.Tests.Cases.Expectations.Assertions public class KeptTypeInAssemblyAttribute : BaseExpectedLinkedBehaviorAttribute { public readonly string AssemblyFileName; - public readonly Type Type; + public readonly string TypeName; - public KeptTypeInAssemblyAttribute(string assemblyFileName, Type type) + public KeptTypeInAssemblyAttribute (string assemblyFileName, Type type) { AssemblyFileName = assemblyFileName; - Type = type; + TypeName = type.ToString (); + } + + public KeptTypeInAssemblyAttribute (string assemblyFileName, string typeName) + { + AssemblyFileName = assemblyFileName; + TypeName = typeName; } } } diff --git a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedMemberInAssemblyAttribute.cs b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedMemberInAssemblyAttribute.cs index 3bcd63b9d..c19794184 100644 --- a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedMemberInAssemblyAttribute.cs +++ b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedMemberInAssemblyAttribute.cs @@ -4,13 +4,20 @@ namespace Mono.Linker.Tests.Cases.Expectations.Assertions { [AttributeUsage (AttributeTargets.Class | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false)] public class RemovedMemberInAssemblyAttribute : BaseExpectedLinkedBehaviorAttribute { public readonly string AssemblyFileName; - public readonly Type Type; + public readonly string TypeName; public readonly string [] MemberNames; public RemovedMemberInAssemblyAttribute (string assemblyFileName, Type type, params string [] memberNames) { AssemblyFileName = assemblyFileName; - Type = type; + TypeName = type.ToString (); + MemberNames = memberNames; + } + + public RemovedMemberInAssemblyAttribute (string assemblyFileName, string typeName, params string [] memberNames) + { + AssemblyFileName = assemblyFileName; + TypeName = typeName; MemberNames = memberNames; } } diff --git a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedTypeInAssemblyAttribute.cs b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedTypeInAssemblyAttribute.cs index 04bc9c132..a56eee32a 100644 --- a/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedTypeInAssemblyAttribute.cs +++ b/linker/Tests/Mono.Linker.Tests.Cases.Expectations/Assertions/RemovedTypeInAssemblyAttribute.cs @@ -5,12 +5,18 @@ namespace Mono.Linker.Tests.Cases.Expectations.Assertions [AttributeUsage(AttributeTargets.Class | AttributeTargets.Delegate, AllowMultiple = true, Inherited = false)] public class RemovedTypeInAssemblyAttribute : BaseExpectedLinkedBehaviorAttribute { public readonly string AssemblyFileName; - public readonly Type Type; + public readonly string TypeName; public RemovedTypeInAssemblyAttribute (string assemblyFileName, Type type) { AssemblyFileName = assemblyFileName; - Type = type; + TypeName = type.ToString (); + } + + public RemovedTypeInAssemblyAttribute (string assemblyFileName, string typeName) + { + AssemblyFileName = assemblyFileName; + TypeName = typeName; } } } diff --git a/linker/Tests/TestCasesRunner/ResultChecker.cs b/linker/Tests/TestCasesRunner/ResultChecker.cs index 8ced27196..c61935ba1 100644 --- a/linker/Tests/TestCasesRunner/ResultChecker.cs +++ b/linker/Tests/TestCasesRunner/ResultChecker.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Mono.Cecil; using Mono.Linker.Tests.Cases.Expectations.Assertions; @@ -7,31 +8,68 @@ using Mono.Linker.Tests.Extensions; using NUnit.Framework; namespace Mono.Linker.Tests.TestCasesRunner { - public class ResultChecker { + public class ResultChecker + { + readonly BaseAssemblyResolver _originalsResolver; + readonly BaseAssemblyResolver _linkedResolver; + + public ResultChecker () + : this(new DefaultAssemblyResolver (), new DefaultAssemblyResolver ()) + { + } + + public ResultChecker (BaseAssemblyResolver originalsResolver, BaseAssemblyResolver linkedResolver) + { + _originalsResolver = originalsResolver; + _linkedResolver = linkedResolver; + } public virtual void Check (LinkedTestCaseResult linkResult) { Assert.IsTrue (linkResult.OutputAssemblyPath.FileExists (), $"The linked output assembly was not found. Expected at {linkResult.OutputAssemblyPath}"); - using (var original = ReadAssembly (linkResult.ExpectationsAssemblyPath)) { - PerformOutputAssemblyChecks (original.Definition, linkResult.OutputAssemblyPath.Parent); + InitializeResolvers (linkResult); - using (var linked = ReadAssembly (linkResult.OutputAssemblyPath)) { - var checker = new AssemblyChecker (original.Definition, linked.Definition); - checker.Verify (); - } + try + { + var original = ResolveOriginalsAssembly (linkResult.ExpectationsAssemblyPath); + PerformOutputAssemblyChecks (original, linkResult.OutputAssemblyPath.Parent); + + var linked = ResolveLinkedAssembly (linkResult.OutputAssemblyPath); - VerifyLinkingOfOtherAssemblies (original.Definition, linkResult.OutputAssemblyPath.Parent); + new AssemblyChecker (original, linked).Verify (); + + VerifyLinkingOfOtherAssemblies (original, linkResult.OutputAssemblyPath.Parent); + } + finally + { + _originalsResolver.Dispose (); + _linkedResolver.Dispose (); } } - static AssemblyContainer ReadAssembly (NPath assemblyPath) + void InitializeResolvers (LinkedTestCaseResult linkedResult) + { + _originalsResolver.AddSearchDirectory (linkedResult.ExpectationsAssemblyPath.Parent.ToString ()); + _linkedResolver.AddSearchDirectory (linkedResult.OutputAssemblyPath.Parent.ToString ()); + } + + AssemblyDefinition ResolveLinkedAssembly (NPath assemblyPath) + { + return _linkedResolver.Resolve (new AssemblyNameReference (assemblyPath.FileNameWithoutExtension, null)); + } + + AssemblyDefinition ResolveOriginalsAssembly (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); + return _originalsResolver.Resolve (new AssemblyNameReference (assemblyPath.FileNameWithoutExtension, null)); + } + + AssemblyDefinition ResolveOriginalsAssembly (string assemblyName) + { + var cleanAssemblyName = assemblyName; + if (assemblyName.EndsWith (".exe") || assemblyName.EndsWith (".dll")) + cleanAssemblyName = Path.GetFileNameWithoutExtension (assemblyName); + return _originalsResolver.Resolve (new AssemblyNameReference (cleanAssemblyName, null)); } void PerformOutputAssemblyChecks (AssemblyDefinition original, NPath outputDirectory) @@ -50,10 +88,10 @@ namespace Mono.Linker.Tests.TestCasesRunner { var checks = BuildOtherAssemblyCheckTable (original); foreach (var assemblyName in checks.Keys) { - using (var linkedAssembly = ReadAssembly (outputDirectory.Combine (assemblyName))) { + using (var linkedAssembly = ResolveLinkedAssembly (outputDirectory.Combine (assemblyName))) { foreach (var checkAttrInAssembly in checks [assemblyName]) { var expectedTypeName = checkAttrInAssembly.ConstructorArguments [1].Value.ToString (); - var linkedType = linkedAssembly.Definition.MainModule.GetType (expectedTypeName); + var linkedType = linkedAssembly.MainModule.GetType (expectedTypeName); if (checkAttrInAssembly.AttributeType.Name == nameof (RemovedTypeInAssemblyAttribute)) { if (linkedType != null) @@ -79,9 +117,9 @@ namespace Mono.Linker.Tests.TestCasesRunner { } } - static void VerifyRemovedMemberInAssembly (CustomAttribute inAssemblyAttribute, TypeDefinition linkedType) + void VerifyRemovedMemberInAssembly (CustomAttribute inAssemblyAttribute, TypeDefinition linkedType) { - var originalType = ((TypeReference) inAssemblyAttribute.ConstructorArguments [1].Value).Resolve (); + var originalType = GetOriginalTypeFromInAssemblyAttribute (inAssemblyAttribute); foreach (var memberNameAttr in (CustomAttributeArgument[]) inAssemblyAttribute.ConstructorArguments [2].Value) { string memberName = (string) memberNameAttr.Value; @@ -118,9 +156,9 @@ namespace Mono.Linker.Tests.TestCasesRunner { } } - static void VerifyKeptMemberInAssembly (CustomAttribute inAssemblyAttribute, TypeDefinition linkedType) + void VerifyKeptMemberInAssembly (CustomAttribute inAssemblyAttribute, TypeDefinition linkedType) { - var originalType = ((TypeReference) inAssemblyAttribute.ConstructorArguments [1].Value).Resolve (); + var originalType = GetOriginalTypeFromInAssemblyAttribute (inAssemblyAttribute); foreach (var memberNameAttr in (CustomAttributeArgument[]) inAssemblyAttribute.ConstructorArguments [2].Value) { string memberName = (string) memberNameAttr.Value; @@ -157,6 +195,16 @@ namespace Mono.Linker.Tests.TestCasesRunner { } } + TypeDefinition GetOriginalTypeFromInAssemblyAttribute (CustomAttribute inAssemblyAttribute) + { + var attributeValueAsTypeReference = inAssemblyAttribute.ConstructorArguments [1].Value as TypeReference; + if (attributeValueAsTypeReference != null) + return attributeValueAsTypeReference.Resolve (); + + var assembly = ResolveOriginalsAssembly (inAssemblyAttribute.ConstructorArguments [0].Value.ToString ()); + return assembly.MainModule.GetType (inAssemblyAttribute.ConstructorArguments [1].Value.ToString ()); + } + static Dictionary<string, List<CustomAttribute>> BuildOtherAssemblyCheckTable (AssemblyDefinition original) { var checks = new Dictionary<string, List<CustomAttribute>> (); @@ -182,23 +230,5 @@ namespace Mono.Linker.Tests.TestCasesRunner { || attr.AttributeType.Name == nameof (RemovedMemberInAssemblyAttribute) || attr.AttributeType.Name == nameof (KeptMemberInAssemblyAttribute); } - - struct 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 (); - } - } } }
\ No newline at end of file |