Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

Commit 1d08661

Browse files
authored
Merge pull request #57 from TestCentric/issue-55
Throw ExtensibilityException when an extension constructor throws and exception
2 parents 6dc2210 + cf07229 commit 1d08661

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

GitVersion.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
next-version: 3.0.3
1+
next-version: 3.2.0
22
mode: ContinuousDelivery
33
legacy-semver-padding: 5
44
build-metadata-padding: 5

src/testcentric.extensibility.fakes/FakeExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,20 @@ public void WriteResultFile(XmlNode resultNode, TextWriter writer)
9595
throw new NotImplementedException();
9696
}
9797
}
98+
99+
[Extension(Enabled = false)]
100+
public class FakeExtension_ThrowsInConstructor : ITestEventListener
101+
{
102+
public FakeExtension_ThrowsInConstructor()
103+
{
104+
throw new NotImplementedException();
105+
}
106+
107+
public void OnTestEvent(string text)
108+
{
109+
throw new System.NotImplementedException();
110+
}
111+
}
98112
}
99113

100114
namespace NUnit.Engine.Extensibility

src/testcentric.extensibility.tests/ExtensionManagerTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ internal class ThisIsNotAnExtensionPoint
143143
[
144144
"TestCentric.Engine.Extensibility.FakeAgentLauncher",
145145
"TestCentric.Engine.Extensibility.FakeTestEventListener",
146+
"TestCentric.Engine.Extensibility.FakeExtension_ThrowsInConstructor",
146147
"NUnit.Engine.Extensibility.FakeProjectLoader"
147148
];
148149

@@ -171,6 +172,18 @@ public void DisabledExtensionMayBeEnabled()
171172
.And.Property(nameof(ExtensionNode.Enabled)).True);
172173
}
173174

175+
[Test]
176+
public void ExtensionThrowsInConstructor()
177+
{
178+
string typeName = "TestCentric.Engine.Extensibility.FakeExtension_ThrowsInConstructor";
179+
var iexNode = ExtensionManager.Extensions.Where(n => n.TypeName == typeName).Single();
180+
var exNode = iexNode as ExtensionNode;
181+
182+
// Demonstrates that the exception is caused when ExtensionObject is accessed
183+
var tiex = Assert.Throws<ExtensibilityException>(() => { var o = exNode.ExtensionObject; });
184+
Assert.That(tiex.InnerException, Is.InstanceOf<NotImplementedException>());
185+
}
186+
174187
#if NETCOREAPP
175188
[TestCase("netstandard2.0", ExpectedResult = true)]
176189
[TestCase("net462", ExpectedResult = false)]

src/testcentric.extensibility/ExtensionNode.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,23 @@ public object ExtensionObject
121121
/// </summary>
122122
public object CreateExtensionObject(params object[] args)
123123
{
124+
try
125+
{
124126
#if NETFRAMEWORK
125127
return AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap(
126128
AssemblyPath, TypeName, false, 0, null, args, null, null, null);
127129
#else
128-
var assembly = Assembly.LoadFrom(AssemblyPath);
129-
var type = assembly.GetType(TypeName, throwOnError: true)!;
130-
return Activator.CreateInstance(type, args)!;
130+
var assembly = Assembly.LoadFrom(AssemblyPath);
131+
var type = assembly.GetType(TypeName, throwOnError: true)!;
132+
return Activator.CreateInstance(type, args)!;
131133
#endif
134+
}
135+
catch (Exception ex)
136+
{
137+
if (ex is TargetInvocationException)
138+
ex = ex.InnerException;
139+
throw new ExtensibilityException("Error in constructing extension object", ex);
140+
}
132141
}
133142

134143
#endregion

0 commit comments

Comments
 (0)