diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs index f97005e844..bd5077fd38 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/FileUtility.cs @@ -20,7 +20,7 @@ public virtual void CreateDirectoryIfNotExists(string directory) { DebugEx.Assert(!StringEx.IsNullOrEmpty(directory), "directory"); - if (!Directory.Exists(directory)) + if (!DoesDirectoryExist(directory)) { Directory.CreateDirectory(directory); // Creates subdir chain if necessary. } @@ -59,7 +59,7 @@ public virtual string GetNextIterationDirectoryName(string parentDirectoryName, : string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", originalDirectoryName, iteration.ToString(CultureInfo.InvariantCulture)); string tryMePath = Path.Combine(parentDirectoryName, tryMe); - if (!File.Exists(tryMePath) && !Directory.Exists(tryMePath)) + if (!DoesFileExist(tryMePath) && !DoesDirectoryExist(tryMePath)) { return tryMePath; } @@ -92,7 +92,7 @@ public virtual string CopyFileOverwrite(string source, string destination, out s try { string? destinationDirectory = Path.GetDirectoryName(destination); - if (!StringEx.IsNullOrEmpty(destinationDirectory) && File.Exists(source) && !Directory.Exists(destinationDirectory)) + if (!StringEx.IsNullOrEmpty(destinationDirectory) && DoesFileExist(source) && !DoesDirectoryExist(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } @@ -253,7 +253,7 @@ public virtual void DeleteDirectories(string filePath) /// path to symbols file. /// Pdb file name or null if non-existent. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Requirement is to handle all kinds of user exceptions and message appropriately.")] - private static string? GetSymbolsFileName(string? path) + private string? GetSymbolsFileName(string? path) { if (StringEx.IsNullOrEmpty(path) || path.IndexOfAny(Path.GetInvalidPathChars()) != -1) { @@ -266,7 +266,7 @@ public virtual void DeleteDirectories(string filePath) } string pdbFile = Path.ChangeExtension(path, ".pdb"); - if (File.Exists(pdbFile)) + if (DoesFileExist(pdbFile)) { if (EqtTrace.IsInfoEnabled) { diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs index e95b9c01cc..ff794699e3 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestAssemblyInfoTests.cs @@ -147,11 +147,16 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionOnAssertionFailur Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.Fail failed. Test failure. Aborting test execution."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } } public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAssertInconclusive() @@ -164,11 +169,16 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithInconclusiveO Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException: Assert.Inconclusive failed. Test Inconclusive. Aborting test execution."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertInconclusiveException)); + } } public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExceptions() @@ -182,12 +192,17 @@ public void RunAssemblyInitializeShouldThrowTestFailedExceptionWithNonAssertExce Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. System.ArgumentException: Some actualErrorMessage message. Aborting test execution."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.<>c.", StringComparison.Ordinal)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } } public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultipleNestedTypeInitializationExceptions() @@ -205,11 +220,16 @@ public void RunAssemblyInitializeShouldThrowTheInnerMostExceptionWhenThereAreMul Verify( exception.Message == "Assembly Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests+DummyTestClass.AssemblyInitializeMethod threw exception. System.InvalidOperationException: I fail.. Aborting test execution."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestAssemblyInfoTests.FailingStaticHelper..cctor()", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(InvalidOperationException)); + } } public void RunAssemblyInitializeShouldThrowForAlreadyExecutedTestAssemblyInitWithException() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs index 9242df3d9f..827fb5039d 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestClassInfoTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using AwesomeAssertions; + using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices; @@ -338,12 +340,17 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnBaseInitializeMeth Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.InitBaseClassMethod threw exception. System.ArgumentException: Some exception message."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } } public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure() @@ -358,11 +365,16 @@ public void RunClassInitializeShouldThrowTestFailedExceptionOnAssertionFailure() Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException: Assert.Fail failed. Test failure."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } } public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAssertInconclusive() @@ -377,11 +389,16 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithInconclusiveOnAs Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException: Assert.Inconclusive failed. Test Inconclusive."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertInconclusiveException)); + } } public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExceptions() @@ -396,10 +413,11 @@ public void RunClassInitializeShouldThrowTestFailedExceptionWithNonAssertExcepti Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. System.ArgumentException: Argument exception."); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); -#endif + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.<>c.", StringComparison.Ordinal)); + } } public void RunClassInitializeShouldThrowForAlreadyExecutedTestClassInitWithException() @@ -438,12 +456,16 @@ public void RunClassInitializeShouldThrowTheInnerMostExceptionWhenThereAreMultip Verify( exception.Message == "Class Initialization method Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests+DummyTestClass.ClassInitializeMethod threw exception. System.InvalidOperationException: I fail.."); -#if DEBUG - Verify( - exception.StackTraceInformation!.ErrorStackTrace.StartsWith( + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestClassInfoTests.FailingStaticHelper..cctor()", StringComparison.Ordinal)); -#endif - Verify(exception.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(InvalidOperationException)); + } } private TestResult GetResultOrRunClassInitialize() @@ -500,12 +522,9 @@ public void RunClassCleanupShouldReturnAssertFailureExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Fail failed. Test Failure.")); -#if DEBUG - Verify( - classCleanupException.Message.Contains( - $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>"), - $"Value: {classCleanupException.Message}"); -#endif + classCleanupException.Message.Should().Contain( + $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertFailureExceptionDetails)}>", + $"Value: {classCleanupException.Message}"); } public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails() @@ -522,11 +541,9 @@ public void RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails() Verify(classCleanupException is not null); Verify(classCleanupException.Message.StartsWith("Class Cleanup method DummyTestClass.ClassCleanupMethod failed.", StringComparison.Ordinal)); Verify(classCleanupException.Message.Contains("Error Message: Assert.Inconclusive failed. Test Inconclusive.")); -#if DEBUG - Verify( - classCleanupException.Message.Contains($"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>"), + classCleanupException.Message.Should().Contain( + $"{typeof(TestClassInfoTests).FullName}.<>c.<{nameof(this.RunClassCleanupShouldReturnAssertInconclusiveExceptionDetails)}>", $"Value: {classCleanupException.Message}"); -#endif } public void RunClassCleanupShouldReturnExceptionDetailsOfNonAssertExceptions() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs index 73f68819ac..25afa1abf2 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Execution/TestMethodInfoTests.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using AwesomeAssertions; + using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; @@ -575,12 +577,17 @@ public async Task TestMethodInfoInvokeWhenTestThrowsReturnsExpectedResult() Verify(exception is not null); Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + } } public async Task TestInitialize_WhenTestReturnsTaskFromException_DisplayProperException() @@ -604,8 +611,11 @@ public async Task TestInitialize_WhenTestReturnsTaskFromException_DisplayProperE var exception = result.TestFailureException as TestFailedException; Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(Exception)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(Exception)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } string expectedErrorMessage = string.Format( CultureInfo.InvariantCulture, @@ -646,11 +656,16 @@ public async Task TestMethodInfoInvokeWhenTestThrowsAssertFailReturnsExpectedRes Verify(exception is not null); Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } public async Task TestMethodInfoInvokeWhenTestThrowsAssertInconclusiveReturnsExpectedResult() @@ -682,11 +697,16 @@ public async Task TestMethodInfoInvokeWhenTestThrowsAssertInconclusiveReturnsExp Verify(exception is not null); Verify(errorMessage == exception.Message); Verify(exception.Outcome == UTF.UnitTestOutcome.Inconclusive); - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertInconclusiveException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } #endregion @@ -713,8 +733,11 @@ public async Task TestCleanup_WhenTestReturnsTaskFromException_DisplayProperExce var exception = result.TestFailureException as TestFailedException; Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); - Verify(exception.InnerException!.GetType() == typeof(Exception)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(Exception)); + Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + } string errorMessage = string.Format( CultureInfo.InvariantCulture, @@ -830,13 +853,16 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsReturnsExpectedResult Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(expectedErrorMessage == exception.Message); - Verify(exception.InnerException!.GetType() == typeof(ArgumentException)); - Verify(exception.InnerException.InnerException!.GetType() == typeof(InvalidOperationException)); + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(ArgumentException)); + } -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + if (exception.StackTraceInformation is { } stackTraceInfo) + { + Verify(stackTraceInfo.ErrorStackTrace.StartsWith( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); + } } public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertInconclusiveReturnsExpectedResult() @@ -859,11 +885,15 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertInconclusiveRet Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Inconclusive); Verify(expectedErrorMessage == exception.Message); - Verify(exception.InnerException!.GetType() == typeof(AssertInconclusiveException)); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + if (exception.InnerException?.GetType() is { } innerType) + { + innerType.Should().Be(); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain(" at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertFailedReturnsExpectedResult() @@ -886,11 +916,16 @@ public async Task TestMethodInfoInvokeWhenTestCleanupThrowsAssertFailedReturnsEx Verify(exception is not null); Verify(exception.Outcome == UTF.UnitTestOutcome.Failed); Verify(expectedErrorMessage == exception.Message); - Verify(exception.InnerException!.GetType() == typeof(AssertFailedException)); -#if DEBUG - Verify(exception.StackTraceInformation!.ErrorStackTrace.StartsWith( - " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__", StringComparison.Ordinal)); -#endif + if (exception.InnerException?.GetType() is { } innerType) + { + Verify(innerType == typeof(AssertFailedException)); + } + + if (exception.StackTraceInformation is { } stackTraceInfo) + { + stackTraceInfo.ErrorStackTrace.Should().Contain( + " at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.<>c.b__"); + } } public async Task TestMethodInfoInvokeShouldAppendErrorMessagesIfBothTestMethodAndTestCleanupThrows() @@ -931,10 +966,11 @@ public async Task TestMethodInfoInvokeShouldAppendStackTraceInformationIfBothTes Verify(result.Outcome == UTF.UnitTestOutcome.Failed); Verify(exception is not null); -#if DEBUG - Verify(((TestFailedException)exception.InnerExceptions[0]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestMethod()")); - Verify(((TestFailedException)exception.InnerExceptions[1]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestCleanupMethod()")); -#endif + if (exception.InnerExceptions.Count >= 2) + { + Verify(((TestFailedException)exception.InnerExceptions[0]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestMethod()")); + Verify(((TestFailedException)exception.InnerExceptions[1]).StackTraceInformation!.ErrorStackTrace.Contains("Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution.TestMethodInfoTests.DummyTestClass.DummyTestCleanupMethod()")); + } } public async Task TestMethodInfoInvokeShouldSetOutcomeAsInconclusiveIfTestCleanupIsInconclusive() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs index bf28034646..afb05ff21f 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Extensions/ExceptionExtensionsTests.cs @@ -81,9 +81,12 @@ public void TryGetStackTraceInformationReturnsStackTraceForAnException() StackTraceInformation? stackTraceInformation = exception.TryGetStackTraceInformation(); - Verify(stackTraceInformation!.ErrorStackTrace.StartsWith(" at A()", StringComparison.Ordinal)); - Verify(stackTraceInformation.ErrorFilePath is null); - Verify(stackTraceInformation.ErrorLineNumber == 0); + if (stackTraceInformation is not null) + { + Verify(stackTraceInformation.ErrorStackTrace.StartsWith(" at A()", StringComparison.Ordinal)); + Verify(stackTraceInformation.ErrorFilePath is null); + Verify(stackTraceInformation.ErrorLineNumber == 0); + } } public void TryGetStackTraceInformationShouldThrowIfStackTraceThrows() diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj index 46cd16bf11..1b3a19001c 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj @@ -16,14 +16,6 @@ $(DefineConstants);WIN_UI - - - full - - - pdbonly - - @@ -40,6 +32,7 @@ + diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs index 3ca5bd35f4..b7b5e223a3 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs @@ -301,7 +301,6 @@ public void DeployShouldDeployContentsOfADirectoryIfSpecified() Times.Once); } -#if NET462 public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() { TestCase testCase = GetTestCaseAndTestRunDirectories(DefaultDeploymentItemPath, DefaultDeploymentItemOutputDirectory, out TestRunDirectories testRunDirectories); @@ -310,12 +309,12 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() _mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config")))) .Returns(true); _mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny())).Returns(true); - _mockAssemblyUtility.Setup( - au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) +#if NET462 + _mockAssemblyUtility.Setup(au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) .Returns(Array.Empty()); - _mockAssemblyUtility.Setup( - au => au.GetSatelliteAssemblies(It.IsAny())) + _mockAssemblyUtility.Setup(au => au.GetSatelliteAssemblies(It.IsAny())) .Returns([]); +#endif string? warning; _mockFileUtility.Setup(fu => fu.CopyFileOverwrite(It.IsAny(), It.IsAny(), out warning)) .Returns( @@ -325,6 +324,12 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() return y; }); + string sourceFile = Assembly.GetExecutingAssembly().GetName().Name + ".exe"; + string pdbFile = Assembly.GetExecutingAssembly().GetName().Name + ".pdb"; + _mockFileUtility.Setup( + fu => fu.DoesFileExist(Path.Combine(DefaultDeploymentItemPath, pdbFile))) + .Returns(true); + // Act. Verify( _deploymentUtility.Deploy( @@ -334,9 +339,7 @@ public void DeployShouldDeployPdbWithSourceIfPdbFileIsPresentInSourceDirectory() _mockTestExecutionRecorder.Object, testRunDirectories)); - // Assert. - string sourceFile = Assembly.GetExecutingAssembly().GetName().Name + ".exe"; - string pdbFile = Assembly.GetExecutingAssembly().GetName().Name + ".pdb"; + // Assert _mockFileUtility.Verify( fu => fu.CopyFileOverwrite( @@ -366,12 +369,12 @@ public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssembl _mockFileUtility.Setup(fu => fu.DoesDirectoryExist(It.Is(s => !s.EndsWith(".dll") && !s.EndsWith(".exe") && !s.EndsWith(".config")))) .Returns(true); _mockFileUtility.Setup(fu => fu.DoesFileExist(It.IsAny())).Returns(true); - _mockAssemblyUtility.Setup( - au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) +#if NET462 + _mockAssemblyUtility.Setup(au => au.GetFullPathToDependentAssemblies(It.IsAny(), It.IsAny(), out _warnings)) .Returns([dependencyFile]); - _mockAssemblyUtility.Setup( - au => au.GetSatelliteAssemblies(It.IsAny())) + _mockAssemblyUtility.Setup(au => au.GetSatelliteAssemblies(It.IsAny())) .Returns([]); +#endif string? warning; _mockFileUtility.Setup(fu => fu.CopyFileOverwrite(It.IsAny(), It.IsAny(), out warning)) .Returns( @@ -405,9 +408,8 @@ public void DeployShouldNotDeployPdbFileOfAssemblyIfPdbFileIsNotPresentInAssembl It.Is(s => s.Contains(pdbFile)), Path.Combine(testRunDirectories.OutDirectory, Path.GetFileName(pdbFile)), out warning), - Times.Never); + Times.Once); } -#endif #endregion diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config new file mode 100644 index 0000000000..4f2f99bce0 --- /dev/null +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/app.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/test/Utilities/Automation.CLI/CLITestBase.e2e.cs b/test/Utilities/Automation.CLI/CLITestBase.e2e.cs index 76d01fb92b..24930061f8 100644 --- a/test/Utilities/Automation.CLI/CLITestBase.e2e.cs +++ b/test/Utilities/Automation.CLI/CLITestBase.e2e.cs @@ -243,7 +243,6 @@ public void ValidateFailedTestsContain(bool validateStackTraceInfo, params strin test.Equals(f.DisplayName, StringComparison.Ordinal)); Assert.IsNotNull(testFound, "Test '{0}' does not appear in failed tests list.", test); -#if DEBUG if (!validateStackTraceInfo) { continue; @@ -259,7 +258,6 @@ public void ValidateFailedTestsContain(bool validateStackTraceInfo, params strin Assert.IsNotNull(testFound.ErrorStackTrace); Assert.Contains(testMethodName, testFound.ErrorStackTrace, $"No stack trace for failed test: {test}"); } -#endif } }