From ced48a4fe1c19a23dc8f8a213060dc53502ae88b Mon Sep 17 00:00:00 2001 From: Mateo Torres-Ruiz Date: Thu, 19 Mar 2020 00:20:54 -0700 Subject: Add the command line option `--output-pinvokes` (#992) * Add the command line option `--output-pinvokes` * Remove unnecessary memory stream * Address feedback * Sort list of pinvokes, add unreachable DllImports to the tests * Remove redundant if, change comparison order of PInvokeInfo * Use an overload of Zip that is compatible with .NET 4.7.71 * Use StringComparison.Ordinal when comparing PInvokeInfo fields Use List for storing PInvokeInfos * Copy PInvokes json to tests output directory --- test/Mono.Linker.Tests/Mono.Linker.Tests.csproj | 9 ++++-- .../Dependencies/PInvokesExpectations.json | 32 ++++++++++++++++++++++ .../Mono.Linker.Tests/TestCases/IndividualTests.cs | 28 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 test/Mono.Linker.Tests/TestCases/Dependencies/PInvokesExpectations.json (limited to 'test/Mono.Linker.Tests') diff --git a/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj b/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj index e4b13a53c..2e93533ee 100644 --- a/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj +++ b/test/Mono.Linker.Tests/Mono.Linker.Tests.csproj @@ -64,6 +64,12 @@ + + + PreserveNewest + + + @@ -72,8 +78,7 @@ - + diff --git a/test/Mono.Linker.Tests/TestCases/Dependencies/PInvokesExpectations.json b/test/Mono.Linker.Tests/TestCases/Dependencies/PInvokesExpectations.json new file mode 100644 index 000000000..872915eca --- /dev/null +++ b/test/Mono.Linker.Tests/TestCases/Dependencies/PInvokesExpectations.json @@ -0,0 +1,32 @@ +[ + { + "assembly": "copyassembly.dll", + "entryPoint": "CustomEntryPoint", + "fullName": "Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies.CanOutputPInvokes_CopyAssembly Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies.CanOutputPInvokes_CopyAssembly::CustomEntryPoint()", + "moduleName": "lib_copyassembly" + }, + { + "assembly": "copyassembly.dll", + "entryPoint": "FooEntryPoint", + "fullName": "Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies.CanOutputPInvokes_CopyAssembly Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies.CanOutputPInvokes_CopyAssembly::FooEntryPoint()", + "moduleName": "lib_copyassembly" + }, + { + "assembly": "test.exe", + "entryPoint": "CustomEntryPoint", + "fullName": "Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.CanOutputPInvokes\/Foo Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.CanOutputPInvokes::CustomEntryPoint()", + "moduleName": "lib" + }, + { + "assembly": "test.exe", + "entryPoint": "CustomEntryPoint", + "fullName": "Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.CanOutputPInvokes\/Foo Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.CanOutputPInvokes::CustomEntryPoint0()", + "moduleName": "lib" + }, + { + "assembly": "test.exe", + "entryPoint": "FooEntryPoint", + "fullName": "Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.CanOutputPInvokes\/Foo Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.CanOutputPInvokes::FooEntryPoint()", + "moduleName": "lib" + } +] \ No newline at end of file diff --git a/test/Mono.Linker.Tests/TestCases/IndividualTests.cs b/test/Mono.Linker.Tests/TestCases/IndividualTests.cs index 439caf37e..7ded3ee80 100644 --- a/test/Mono.Linker.Tests/TestCases/IndividualTests.cs +++ b/test/Mono.Linker.Tests/TestCases/IndividualTests.cs @@ -1,12 +1,18 @@ using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.Serialization.Json; using System.Xml; using Mono.Cecil; using Mono.Linker.Tests.Cases.CommandLine.Mvid; +using Mono.Linker.Tests.Cases.Interop.PInvoke.Individual; using Mono.Linker.Tests.Cases.References.Individual; using Mono.Linker.Tests.Cases.Tracing.Individual; using Mono.Linker.Tests.Extensions; using Mono.Linker.Tests.TestCasesRunner; using NUnit.Framework; +using NUnit.Framework.Internal; namespace Mono.Linker.Tests.TestCases { @@ -26,6 +32,28 @@ namespace Mono.Linker.Tests.TestCases Assert.Fail ($"The linked assembly is missing. Should have existed at {result.OutputAssemblyPath}"); } + [Test] + public void CanOutputPInvokes () + { + var testcase = CreateIndividualCase (typeof (CanOutputPInvokes)); + var result = Run (testcase); + + var outputPath = result.OutputAssemblyPath.Parent.Combine ("pinvokes.json"); + if (!outputPath.Exists ()) + Assert.Fail ($"The json file with the list of all the PInvokes found by the linker is missing. Expected it to exist at {outputPath}"); + + var jsonSerializer = new DataContractJsonSerializer (typeof (List)); + + using (var fsActual = File.Open(outputPath, FileMode.Open)) + using (var fsExpected = File.Open("TestCases/Dependencies/PInvokesExpectations.json", FileMode.Open)) { + var actual = jsonSerializer.ReadObject (fsActual) as List; + var expected = jsonSerializer.ReadObject (fsExpected) as List; + foreach (var pinvokePair in Enumerable.Zip(actual, expected, (fst, snd) => Tuple.Create(fst, snd))) { + Assert.That (pinvokePair.Item1.CompareTo (pinvokePair.Item2), Is.EqualTo (0)); + } + } + } + [Test] public void CanEnableDependenciesDump () { -- cgit v1.2.3