From 70d0179d8589ba1b305451e34063f5a6619a98b5 Mon Sep 17 00:00:00 2001 From: Nolan Glore Date: Tue, 9 Jun 2020 15:36:38 -0700 Subject: [PATCH] Fix null ref bug when base class cleanup fails when there is no derived class cleanup method --- .../MSTest.CoreAdapter/Execution/TestClassInfo.cs | 8 +++++--- .../Execution/TestClassInfoTests.cs | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs b/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs index 433f4a8a68..4ef28aba40 100644 --- a/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs +++ b/src/Adapter/MSTest.CoreAdapter/Execution/TestClassInfo.cs @@ -345,9 +345,11 @@ public string RunClassCleanup() if (this.IsClassInitializeExecuted || this.ClassInitializeMethod is null) { + MethodInfo classCleanupMethod = null; + try { - var classCleanupMethod = this.ClassCleanupMethod; + classCleanupMethod = this.ClassCleanupMethod; classCleanupMethod?.InvokeAsSynchronousTask(null); var baseClassCleanupQueue = new Queue(this.BaseClassCleanupMethodsStack); while (baseClassCleanupQueue.Count > 0) @@ -378,8 +380,8 @@ public string RunClassCleanup() return string.Format( CultureInfo.CurrentCulture, Resource.UTA_ClassCleanupMethodWasUnsuccesful, - this.ClassType.Name, - this.ClassCleanupMethod.Name, + classCleanupMethod.DeclaringType.Name, + classCleanupMethod.Name, errorMessage, StackTraceHelper.GetStackTraceInformation(realException)?.ErrorStackTrace); } diff --git a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs index aff4f3c6e6..9142aa4d2c 100644 --- a/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/Execution/TestClassInfoTests.cs @@ -544,6 +544,20 @@ public void RunClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions() "Class Cleanup method DummyTestClass.ClassCleanupMethod failed. Error Message: System.ArgumentException: Argument Exception. Stack Trace: at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c."); } + [TestMethod] + public void RunBaseClassCleanupWithNoDerivedClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions() + { + DummyBaseTestClass.ClassCleanupMethodBody = () => { throw new ArgumentException("Argument Exception"); }; + + this.testClassInfo.ClassCleanupMethod = null; + this.testClassInfo.BaseClassInitAndCleanupMethods.Enqueue( + Tuple.Create((MethodInfo)null, typeof(DummyBaseTestClass).GetMethod("CleanupClassMethod"))); + this.testClassInfo.BaseClassCleanupMethodsStack.Push(typeof(DummyBaseTestClass).GetMethod("CleanupClassMethod")); + StringAssert.StartsWith( + this.testClassInfo.RunClassCleanup(), + "Class Cleanup method DummyBaseTestClass.CleanupClassMethod failed. Error Message: System.ArgumentException: Argument Exception. Stack Trace: at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c."); + } + [TestMethod] public void RunBaseClassCleanupEvenIfThereIsNoDerivedClassCleanup() {