Skip to content

Commit

Permalink
[tests] Add .cctor execution to introspection
Browse files Browse the repository at this point in the history
This will spot cases like dotnet#7959
where a type (static) constructor can fail at runtime, leading to
`TypeLoadException`

Ideally it's covered by it's own tests but it's better covered twice
than never :)
  • Loading branch information
spouliot authored and monojenkins committed Feb 26, 2020
1 parent dd83fbe commit 8879f8e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/introspection/ApiTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Reflection;
using System.Runtime.CompilerServices;

using NUnit.Framework;
using Xamarin.Utils;

using Foundation;
using ObjCRuntime;

namespace Introspection {

[TestFixture]
// we want the tests to be available because we use the linker
[Preserve (AllMembers = true)]
public class ApiTypeTest : ApiBaseTest {

[Test]
public void StaticCtor ()
{
ContinueOnFailure = true;
foreach (Type t in Assembly.GetTypes ()) {
var cctor = t.GetConstructor (BindingFlags.Static | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
if (cctor == null)
continue;
// we don't skip based on availability attributes since the execution of .cctor can easily happen indirectly and
// we rather catch them all here *now* than trying to figure out how to replicate the specific conditions *later*
try {
RuntimeHelpers.RunClassConstructor (t.TypeHandle);
}
catch (TypeInitializationException e) {
ReportError ($"{t.FullName} .cctor could not execute properly: {e}");
}
}
AssertIfErrors ($"{Errors} execution failure(s)");
}
}
}
3 changes: 3 additions & 0 deletions tests/introspection/Mac/introspection-mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<Compile Include="..\ApiFrameworkTest.cs">
<Link>ApiFrameworkTest.cs</Link>
</Compile>
<Compile Include="..\ApiTypeTest.cs">
<Link>ApiTypeTest.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\external\guiunit\src\framework\GuiUnit_NET_4_5.csproj">
Expand Down
3 changes: 3 additions & 0 deletions tests/introspection/iOS/introspection-ios.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@
<Compile Include="..\ApiFrameworkTest.cs">
<Link>ApiFrameworkTest.cs</Link>
</Compile>
<Compile Include="..\ApiTypeTest.cs">
<Link>ApiTypeTest.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="Info.plist">
Expand Down

0 comments on commit 8879f8e

Please sign in to comment.