Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestPlatform.Common.ExtensionDecorators;
Expand Down Expand Up @@ -218,7 +219,14 @@ public void RunTests()
{
EqtTrace.Error("BaseRunTests.RunTests: Failed to run the tests. Reason: {0}.", ex);

exception = new Exception(ex.Message, ex.InnerException);
Comment thread
nohwnd marked this conversation as resolved.
// RunTestsInternal can surface a TargetInvocationException when a test extension is
// instantiated via reflection and its constructor throws. Unwrap that wrapper to the
// real exception so callers don't see the reflection noise. Any other exception is
// preserved as-is so its concrete type and stack trace are not lost on the way out.
Exception realException = ex is TargetInvocationException tie && tie.InnerException is not null
? tie.InnerException
: ex;
exception = new Exception(realException.Message, realException);
isAborted = true;
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;

using Microsoft.TestPlatform.TestUtilities;
using Microsoft.VisualStudio.TestPlatform.Common.ExtensionFramework;
Expand Down Expand Up @@ -159,6 +160,72 @@ public void RunTestsShouldRaiseTestRunCompleteWithAbortedAsTrueOnException()
Assert.IsTrue(receivedCompleteArgs.IsAborted);
}

[TestMethod]
public void RunTestsShouldPreserveOriginalExceptionAsInnerException()
{
TestRunCompleteEventArgs? receivedCompleteArgs = null;
var originalException = new NotImplementedException("original message");

// Setup mocks.
_runTestsInstance.GetExecutorUriExtensionMapCallback = (fh, rc) => throw originalException;
_mockTestRunEventsHandler.Setup(
treh =>
treh.HandleTestRunComplete(
It.IsAny<TestRunCompleteEventArgs>(),
It.IsAny<TestRunChangedEventArgs>(),
It.IsAny<ICollection<AttachmentSet>>(),
It.IsAny<ICollection<string>>()))
.Callback(
(
TestRunCompleteEventArgs complete,
TestRunChangedEventArgs stats,
ICollection<AttachmentSet> attachments,
ICollection<string> executorUris) => receivedCompleteArgs = complete);

_runTestsInstance.RunTests();

Assert.IsNotNull(receivedCompleteArgs);
Assert.IsTrue(receivedCompleteArgs.IsAborted);
Assert.IsNotNull(receivedCompleteArgs.Error);
Assert.AreSame(originalException, receivedCompleteArgs.Error!.InnerException,
"The original exception should be preserved as the inner exception of the wrapper.");
}

[TestMethod]
public void RunTestsShouldUnwrapTargetInvocationExceptionToTheRealException()
{
TestRunCompleteEventArgs? receivedCompleteArgs = null;
var realException = new NotImplementedException("real message");

// A TargetInvocationException is what reflection-based extension instantiation throws when
// a constructor fails; the wrapper itself is noise, the real exception is the inner one.
var reflectionWrapper = new TargetInvocationException(realException);

// Setup mocks.
_runTestsInstance.GetExecutorUriExtensionMapCallback = (fh, rc) => throw reflectionWrapper;
_mockTestRunEventsHandler.Setup(
treh =>
treh.HandleTestRunComplete(
It.IsAny<TestRunCompleteEventArgs>(),
It.IsAny<TestRunChangedEventArgs>(),
It.IsAny<ICollection<AttachmentSet>>(),
It.IsAny<ICollection<string>>()))
.Callback(
(
TestRunCompleteEventArgs complete,
TestRunChangedEventArgs stats,
ICollection<AttachmentSet> attachments,
ICollection<string> executorUris) => receivedCompleteArgs = complete);

_runTestsInstance.RunTests();

Assert.IsNotNull(receivedCompleteArgs);
Assert.IsTrue(receivedCompleteArgs.IsAborted);
Assert.IsNotNull(receivedCompleteArgs.Error);
Assert.AreSame(realException, receivedCompleteArgs.Error!.InnerException,
"A TargetInvocationException should be unwrapped to its real inner exception, not surfaced as-is.");
}

[TestMethod]
public void RunTestsShouldNotThrowIfExceptionIsAFileNotFoundException()
{
Expand Down
Loading