diff options
author | Mateo Torres-Ruiz <mateoatr@users.noreply.github.com> | 2020-03-19 10:20:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-19 10:20:54 +0300 |
commit | ced48a4fe1c19a23dc8f8a213060dc53502ae88b (patch) | |
tree | b73f4453b59e20db137864f58598c3476d92f637 /test | |
parent | 461e313c839bac4ac9f53932ca5df1778a586dce (diff) |
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
Diffstat (limited to 'test')
6 files changed, 139 insertions, 2 deletions
diff --git a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/CanOutputPInvokes.cs b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/CanOutputPInvokes.cs new file mode 100644 index 000000000..7cb2ed65f --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/CanOutputPInvokes.cs @@ -0,0 +1,43 @@ +using System.Runtime.InteropServices; +using Mono.Linker.Tests.Cases.Expectations.Metadata; +using Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies; + +namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Individual +{ + [SetupLinkerAction ("copy", "copyassembly")] + [SetupLinkerAction ("link", "linkassembly")] + [SetupCompileBefore ("copyassembly.dll", new [] { typeof (CanOutputPInvokes_CopyAssembly) })] + [SetupCompileBefore ("linkassembly.dll", new [] { typeof (CanOutputPInvokes_LinkAssembly) })] + [SetupLinkerArgument ("--output-pinvokes", new [] { "pinvokes.json" })] + + public class CanOutputPInvokes + { + public static void Main () + { + var foo = FooEntryPoint (); + var bar = CustomEntryPoint (); + var baz = CustomEntryPoint0 (); + + var copyAssembly = new CanOutputPInvokes_CopyAssembly (); + } + + class Foo + { + public Foo () + { + } + } + + [DllImport ("lib")] + private static extern Foo FooEntryPoint (); + + [DllImport ("lib", EntryPoint = "CustomEntryPoint")] + private static extern Foo CustomEntryPoint (); + + [DllImport ("lib", EntryPoint = "CustomEntryPoint")] + private static extern Foo CustomEntryPoint0 (); + + [DllImport ("lib")] + private static extern Foo UnreachableDllImport (); + } +}
\ No newline at end of file diff --git a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/Dependencies/CanOutputPInvokes_CopyAssembly.cs b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/Dependencies/CanOutputPInvokes_CopyAssembly.cs new file mode 100644 index 000000000..905cd323e --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/Dependencies/CanOutputPInvokes_CopyAssembly.cs @@ -0,0 +1,19 @@ +using System.Runtime.InteropServices; + +namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies +{ + public class CanOutputPInvokes_CopyAssembly + { + public CanOutputPInvokes_CopyAssembly () + { + var foo = FooEntryPoint (); + var bar = CustomEntryPoint (); + } + + [DllImport ("lib_copyassembly")] + private static extern CanOutputPInvokes_CopyAssembly FooEntryPoint (); + + [DllImport ("lib_copyassembly", EntryPoint = "CustomEntryPoint")] + private static extern CanOutputPInvokes_CopyAssembly CustomEntryPoint (); + } +}
\ No newline at end of file diff --git a/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/Dependencies/CanOutputPInvokes_LinkAssembly.cs b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/Dependencies/CanOutputPInvokes_LinkAssembly.cs new file mode 100644 index 000000000..1bee84379 --- /dev/null +++ b/test/Mono.Linker.Tests.Cases/Interop/PInvoke/Individual/Dependencies/CanOutputPInvokes_LinkAssembly.cs @@ -0,0 +1,10 @@ +using System.Runtime.InteropServices; + +namespace Mono.Linker.Tests.Cases.Interop.PInvoke.Individual.Dependencies +{ + public class CanOutputPInvokes_LinkAssembly + { + [DllImport ("lib_linkassembly")] + private static extern CanOutputPInvokes_LinkAssembly UnreachableWhenLinked (); + } +}
\ No newline at end of file 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 @@ <PackageReference Include="NUnit3TestAdapter" Version="3.10.0" /> </ItemGroup> + <ItemGroup> + <None Update="TestCases\Dependencies\PInvokesExpectations.json"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </None> + </ItemGroup> + <Target Name="RunTestsOnMono" Condition="'$(MonoBuild)' != ''"> <Exec Command="mono $(PkgNUnit_ConsoleRunner)/tools/nunit3-console.exe --result=TestResults.xml $(OutputPath)Mono.Linker.Tests.dll" /> </Target> @@ -72,8 +78,7 @@ <!-- Arcade's custom test imports assume that we are using xunit. --> <!-- Map the Arcade "Test" target to the "VSTest" target used by "dotnet test" --> - <Target Name="Test" - Condition="'$(MonoBuild)' == ''"> + <Target Name="Test" Condition="'$(MonoBuild)' == ''"> <MSBuild Projects="$(MSBuildProjectFile)" Targets="VSTest" /> </Target> 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 { @@ -27,6 +33,28 @@ namespace Mono.Linker.Tests.TestCases } [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<PInvokeInfo>)); + + 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<PInvokeInfo>; + var expected = jsonSerializer.ReadObject (fsExpected) as List<PInvokeInfo>; + 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 () { var testcase = CreateIndividualCase (typeof (CanEnableDependenciesDump)); |