diff options
author | Vitek Karas <vitek.karas@microsoft.com> | 2019-11-25 15:22:52 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2019-11-25 15:22:52 +0300 |
commit | ee985232b1f44f8ea55365db7e4a30118ee27d40 (patch) | |
tree | ee504fd80a4aae7e3c9ea2dbf97d45adfee82eb8 /test/Mono.Linker.Tests | |
parent | 4ca67c40817fabc17ce59e20ff73a2d875314653 (diff) |
Ignore reflection calls inside reflection implementation (#828)
* Ignore reflection calls inside reflection implementation
Some relfection methods are implemented by calling different overloads of the same method. In these cases linker should not try to analyze the internal call.
For example:
```C#
MethodInfo GetMethod(string name) => GetMethod(name, null);
```
In this case the call to `GetMethod(name, null)` should not be analyzed and should not cause "warnings".
Added new test infra to be able to capture and validate logged messages.
* PR feedback
Diffstat (limited to 'test/Mono.Linker.Tests')
6 files changed, 64 insertions, 5 deletions
diff --git a/test/Mono.Linker.Tests/TestCasesRunner/LinkedTestCaseResult.cs b/test/Mono.Linker.Tests/TestCasesRunner/LinkedTestCaseResult.cs index 821334c12..b62419fd6 100644 --- a/test/Mono.Linker.Tests/TestCasesRunner/LinkedTestCaseResult.cs +++ b/test/Mono.Linker.Tests/TestCasesRunner/LinkedTestCaseResult.cs @@ -10,8 +10,9 @@ namespace Mono.Linker.Tests.TestCasesRunner { public readonly TestCaseSandbox Sandbox; public readonly TestCaseMetadaProvider MetadataProvider; public readonly ManagedCompilationResult CompilationResult; + public readonly LinkerTestLogger Logger; - public LinkedTestCaseResult (TestCase testCase, NPath inputAssemblyPath, NPath outputAssemblyPath, NPath expectationsAssemblyPath, TestCaseSandbox sandbox, TestCaseMetadaProvider metadaProvider, ManagedCompilationResult compilationResult) + public LinkedTestCaseResult (TestCase testCase, NPath inputAssemblyPath, NPath outputAssemblyPath, NPath expectationsAssemblyPath, TestCaseSandbox sandbox, TestCaseMetadaProvider metadaProvider, ManagedCompilationResult compilationResult, LinkerTestLogger logger) { TestCase = testCase; InputAssemblyPath = inputAssemblyPath; @@ -20,6 +21,7 @@ namespace Mono.Linker.Tests.TestCasesRunner { Sandbox = sandbox; MetadataProvider = metadaProvider; CompilationResult = compilationResult; + Logger = logger; } } }
\ No newline at end of file diff --git a/test/Mono.Linker.Tests/TestCasesRunner/LinkerDriver.cs b/test/Mono.Linker.Tests/TestCasesRunner/LinkerDriver.cs index f76898873..12d15bcb4 100644 --- a/test/Mono.Linker.Tests/TestCasesRunner/LinkerDriver.cs +++ b/test/Mono.Linker.Tests/TestCasesRunner/LinkerDriver.cs @@ -1,8 +1,8 @@ namespace Mono.Linker.Tests.TestCasesRunner { public class LinkerDriver { - public virtual void Link (string [] args) + public virtual void Link (string [] args, ILogger logger) { - new Driver (args).Run (); + new Driver (args).Run (logger); } } }
\ No newline at end of file diff --git a/test/Mono.Linker.Tests/TestCasesRunner/LinkerTestLogger.cs b/test/Mono.Linker.Tests/TestCasesRunner/LinkerTestLogger.cs new file mode 100644 index 000000000..ffd9cfe67 --- /dev/null +++ b/test/Mono.Linker.Tests/TestCasesRunner/LinkerTestLogger.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; + +namespace Mono.Linker.Tests.TestCasesRunner +{ + public class LinkerTestLogger : ILogger + { + public struct MessageRecord + { + public MessageImportance Importance; + public string Message; + } + + public List<MessageRecord> Messages { get; private set; } = new List<MessageRecord>(); + + public void LogMessage(MessageImportance importance, string message, params object[] values) + { + Messages.Add(new MessageRecord + { + Importance = importance, + Message = string.Format(message, values) + }); + } + } +}
\ No newline at end of file diff --git a/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs b/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs index 1280a57f9..02814196c 100644 --- a/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs +++ b/test/Mono.Linker.Tests/TestCasesRunner/ResultChecker.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Linker.Tests.Cases.Expectations.Assertions; @@ -165,6 +166,7 @@ namespace Mono.Linker.Tests.TestCasesRunner { protected virtual void AdditionalChecking (LinkedTestCaseResult linkResult, AssemblyDefinition original, AssemblyDefinition linked) { + VerifyLoggedMessages(original, linkResult.Logger); } protected virtual void InitialChecking (LinkedTestCaseResult linkResult, AssemblyDefinition original, AssemblyDefinition linked) @@ -586,6 +588,31 @@ namespace Mono.Linker.Tests.TestCasesRunner { } } + void VerifyLoggedMessages (AssemblyDefinition original, LinkerTestLogger logger) + { + string allMessages = string.Join (Environment.NewLine, logger.Messages.Select (mc => mc.Message)); + + foreach (var typeWithRemoveInAssembly in original.AllDefinedTypes ()) { + foreach (var attr in typeWithRemoveInAssembly.CustomAttributes) { + if (attr.AttributeType.Resolve ().Name == nameof (LogContainsAttribute)) { + var expectedMessagePattern = (string)attr.ConstructorArguments [0].Value; + Assert.That ( + logger.Messages.Any (mc => Regex.IsMatch (mc.Message, expectedMessagePattern)), + $"Expected to find logged message matching `{expectedMessagePattern}`, but no such message was found.{Environment.NewLine}Logged messages:{Environment.NewLine}{allMessages}"); + } + + if (attr.AttributeType.Resolve ().Name == nameof (LogDoesNotContainAttribute)) { + var unexpectedMessagePattern = (string)attr.ConstructorArguments [0].Value; + foreach (var loggedMessage in logger.Messages) { + Assert.That ( + !Regex.IsMatch (loggedMessage.Message, unexpectedMessagePattern), + $"Expected to not find logged message matching `{unexpectedMessagePattern}`, but found:{Environment.NewLine}{loggedMessage.Message}{Environment.NewLine}Logged messages:{Environment.NewLine}{allMessages}"); + } + } + } + } + } + protected TypeDefinition GetOriginalTypeFromInAssemblyAttribute (CustomAttribute inAssemblyAttribute) { return GetOriginalTypeFromInAssemblyAttribute (inAssemblyAttribute.ConstructorArguments [0].Value.ToString (), inAssemblyAttribute.ConstructorArguments [1].Value); diff --git a/test/Mono.Linker.Tests/TestCasesRunner/TestCaseMetadaProvider.cs b/test/Mono.Linker.Tests/TestCasesRunner/TestCaseMetadaProvider.cs index 24416984c..8aeaf9c36 100644 --- a/test/Mono.Linker.Tests/TestCasesRunner/TestCaseMetadaProvider.cs +++ b/test/Mono.Linker.Tests/TestCasesRunner/TestCaseMetadaProvider.cs @@ -52,6 +52,11 @@ namespace Mono.Linker.Tests.TestCasesRunner { tclo.AdditionalArguments.Add (new KeyValuePair<string, string []> ((string)ca [0].Value, values)); } + if (_testCaseTypeDefinition.CustomAttributes.Any (attr => + attr.AttributeType.Name == nameof (LogContainsAttribute) || attr.AttributeType.Name == nameof (LogDoesNotContainAttribute))) { + tclo.AdditionalArguments.Add (new KeyValuePair<string, string []> ("--verbose", new string [] { })); + } + return tclo; } diff --git a/test/Mono.Linker.Tests/TestCasesRunner/TestRunner.cs b/test/Mono.Linker.Tests/TestCasesRunner/TestRunner.cs index 1ce4a2c68..b9a1cc71b 100644 --- a/test/Mono.Linker.Tests/TestCasesRunner/TestRunner.cs +++ b/test/Mono.Linker.Tests/TestCasesRunner/TestRunner.cs @@ -96,9 +96,10 @@ namespace Mono.Linker.Tests.TestCasesRunner { AddLinkOptions (sandbox, compilationResult, builder, metadataProvider); - linker.Link (builder.ToArgs ()); + LinkerTestLogger logger = new LinkerTestLogger(); + linker.Link (builder.ToArgs (), logger); - return new LinkedTestCaseResult (testCase, compilationResult.InputAssemblyPath, sandbox.OutputDirectory.Combine (compilationResult.InputAssemblyPath.FileName), compilationResult.ExpectationsAssemblyPath, sandbox, metadataProvider, compilationResult); + return new LinkedTestCaseResult (testCase, compilationResult.InputAssemblyPath, sandbox.OutputDirectory.Combine (compilationResult.InputAssemblyPath.FileName), compilationResult.ExpectationsAssemblyPath, sandbox, metadataProvider, compilationResult, logger); } protected virtual void AddLinkOptions (TestCaseSandbox sandbox, ManagedCompilationResult compilationResult, LinkerArgumentBuilder builder, TestCaseMetadaProvider metadataProvider) |