From 8140c1b6277bf96789a0ccf0b712608f17435e61 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 17 Dec 2024 11:21:08 +1100 Subject: [PATCH 1/6] span TrimStackTrace --- .../Execution/ExceptionHelper.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs index 1bf1e69189..bf06167f1f 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs @@ -98,29 +98,30 @@ internal static class ExceptionHelper /// /// The trimmed stack trace removing traces of the framework and adapter from the stack. /// - internal static string TrimStackTrace(string stackTrace) + internal static string TrimStackTrace(ReadOnlySpan stackTrace) { if (stackTrace.Length == 0) { - return stackTrace; + return string.Empty; } StringBuilder result = new(stackTrace.Length); - string[] stackFrames = Regex.Split(stackTrace, Environment.NewLine); + MemoryExtensions.SpanSplitEnumerator stackFrames = stackTrace.Split(Environment.NewLine.AsSpan()); - foreach (string stackFrame in stackFrames) + foreach (Range frameRange in stackFrames) { - if (StringEx.IsNullOrEmpty(stackFrame)) + ReadOnlySpan frame = stackTrace[frameRange]; + if (frame.Length == 0) { continue; } // Add the frame to the result if it does not refer to // the assertion class in the test framework - bool hasReference = HasReferenceToUTF(stackFrame); + bool hasReference = HasReferenceToUTF(frame); if (!hasReference) { - result.Append(stackFrame); + result.Append(frameRange); result.Append(Environment.NewLine); } } @@ -212,11 +213,11 @@ internal static string GetFormattedExceptionMessage(this Exception ex) /// /// True if the framework or the adapter methods are in the stack frame. /// - internal static bool HasReferenceToUTF(string stackFrame) + internal static bool HasReferenceToUTF(ReadOnlySpan stackFrame) { foreach (string type in TypesToBeExcluded) { - if (stackFrame.IndexOf(type, StringComparison.Ordinal) > -1) + if (stackFrame.IndexOf(type.AsSpan(), StringComparison.Ordinal) > -1) { return true; } From 34db9d942450b691002e66faa85dfaa7a1c46bf1 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 17 Dec 2024 11:48:01 +1100 Subject: [PATCH 2/6] . --- Directory.Packages.props | 1 + samples/FxExtensibility/FxExtensibility.csproj | 2 +- .../Execution/ExceptionHelper.cs | 16 +++++++++------- .../MSTest.TestAdapter/MSTest.TestAdapter.csproj | 3 ++- .../MSTestAdapter.PlatformServices.csproj | 2 +- .../Microsoft.Testing.Platform.MSBuild.csproj | 4 ++-- .../TestFramework.Extensions.csproj | 2 +- .../TestFramework/TestFramework.csproj | 2 +- .../TestProject/TestProjectForDiscovery.csproj | 2 +- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index cf6e363599..11d3ff66f7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -48,6 +48,7 @@ + diff --git a/samples/FxExtensibility/FxExtensibility.csproj b/samples/FxExtensibility/FxExtensibility.csproj index 13face57bd..152e1d9d23 100644 --- a/samples/FxExtensibility/FxExtensibility.csproj +++ b/samples/FxExtensibility/FxExtensibility.csproj @@ -8,7 +8,7 @@ MSTest.Extensibility.Samples MSTest.Extensibility.Samples - TRACE + $(DefineConstants);TRACE prompt 4 diff --git a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs index bf06167f1f..946b66e27a 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs @@ -8,6 +8,8 @@ using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Polyfills; + namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; /// @@ -106,23 +108,23 @@ internal static string TrimStackTrace(ReadOnlySpan stackTrace) } StringBuilder result = new(stackTrace.Length); - MemoryExtensions.SpanSplitEnumerator stackFrames = stackTrace.Split(Environment.NewLine.AsSpan()); - foreach (Range frameRange in stackFrames) + SpanLineEnumerator stackFrames = stackTrace.EnumerateLines(); + + foreach (ReadOnlySpan stackFrame in stackFrames) { - ReadOnlySpan frame = stackTrace[frameRange]; - if (frame.Length == 0) + if (stackFrame.Length == 0) { continue; } // Add the frame to the result if it does not refer to // the assertion class in the test framework - bool hasReference = HasReferenceToUTF(frame); + bool hasReference = HasReferenceToUTF(stackFrame); if (!hasReference) { - result.Append(frameRange); - result.Append(Environment.NewLine); + result.Append(stackFrame); + result.AppendLine(); } } diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj index 0174d645d9..0c7d9d5284 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj @@ -41,7 +41,7 @@ Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter - TRACE + $(DefineConstants);TRACE true @@ -57,6 +57,7 @@ + diff --git a/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj b/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj index 4be35846e8..70922bf2bb 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj +++ b/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj @@ -13,7 +13,7 @@ Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices - TRACE + $(DefineConstants);TRACE diff --git a/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj b/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj index 3af417a7eb..9f216dda8a 100644 --- a/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj +++ b/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj @@ -1,7 +1,7 @@ $(MicrosoftTestingTargetFrameworks);netstandard2.0 - PLATFORM_MSBUILD + $(DefineConstants);PLATFORM_MSBUILD $(NoWarn);NU5100 @@ -94,4 +94,4 @@ This package provides MSBuild integration of the platform, its extensions and co - \ No newline at end of file + diff --git a/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj b/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj index a66f58bbf3..948a62f26b 100644 --- a/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj +++ b/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj @@ -34,7 +34,7 @@ Microsoft.VisualStudio.TestTools.UnitTesting Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions - TRACE + $(DefineConstants);TRACE diff --git a/src/TestFramework/TestFramework/TestFramework.csproj b/src/TestFramework/TestFramework/TestFramework.csproj index 8f67b0069d..67da309e7f 100644 --- a/src/TestFramework/TestFramework/TestFramework.csproj +++ b/src/TestFramework/TestFramework/TestFramework.csproj @@ -8,7 +8,7 @@ Microsoft.VisualStudio.TestTools.UnitTesting Microsoft.VisualStudio.TestPlatform.TestFramework - TRACE + $(DefineConstants);TRACE diff --git a/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj b/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj index c243ff45e2..47fb7b1742 100644 --- a/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj +++ b/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj @@ -5,7 +5,7 @@ - TRACE + $(DefineConstants);TRACE prompt 4 False From 5ad654ea8f94d1d30f7a3fdb27af43124d731c7d Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 17 Dec 2024 11:50:24 +1100 Subject: [PATCH 3/6] Update ExceptionHelper.cs --- src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs index 946b66e27a..36a6658e10 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs @@ -109,9 +109,7 @@ internal static string TrimStackTrace(ReadOnlySpan stackTrace) StringBuilder result = new(stackTrace.Length); - SpanLineEnumerator stackFrames = stackTrace.EnumerateLines(); - - foreach (ReadOnlySpan stackFrame in stackFrames) + foreach (ReadOnlySpan stackFrame in stackTrace.EnumerateLines()) { if (stackFrame.Length == 0) { @@ -201,7 +199,7 @@ internal static string GetFormattedExceptionMessage(this Exception ex) return CreateStackTraceInformation(ex.InnerException, checkInnerExceptions, stackTraceString); } - string stackTrace = TrimStackTrace(stackTraceString); + string stackTrace = TrimStackTrace(stackTraceString.AsSpan()); return !StringEx.IsNullOrEmpty(stackTrace) ? new StackTraceInformation(stackTrace, null, 0, 0) : null; } From 171b220dd630e3a6df8887606756785079b0cf4c Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 17 Dec 2024 12:04:13 +1100 Subject: [PATCH 4/6] . --- src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs | 3 --- src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs index 36a6658e10..1263232978 100644 --- a/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs +++ b/src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs @@ -3,13 +3,10 @@ using System.Globalization; using System.Text; -using System.Text.RegularExpressions; using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Polyfills; - namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution; /// diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj index 0c7d9d5284..75a3186290 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj @@ -150,6 +150,7 @@ + From c1a92d56e156d8e4a62e4cf9d9e62d2237319ee8 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 17 Dec 2024 13:01:16 +1100 Subject: [PATCH 5/6] . --- Directory.Packages.props | 2 +- src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj | 2 -- src/TestFramework/TestFramework/TestFramework.csproj | 2 ++ .../MSTestAdapter.PlatformServices.UnitTests.csproj | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 11d3ff66f7..11a6766bbe 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -47,7 +47,7 @@ - + diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj index 75a3186290..68cccc09f9 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj @@ -57,7 +57,6 @@ - @@ -150,7 +149,6 @@ - diff --git a/src/TestFramework/TestFramework/TestFramework.csproj b/src/TestFramework/TestFramework/TestFramework.csproj index 67da309e7f..ef22bd8630 100644 --- a/src/TestFramework/TestFramework/TestFramework.csproj +++ b/src/TestFramework/TestFramework/TestFramework.csproj @@ -21,6 +21,8 @@ + + diff --git a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj index b6f427823c..fc3cf9fa06 100644 --- a/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj +++ b/test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/MSTestAdapter.PlatformServices.UnitTests.csproj @@ -39,7 +39,6 @@ - From 3035989cff793ec89c1577df5663285328897db8 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 17 Dec 2024 16:48:44 +1100 Subject: [PATCH 6/6] Update Directory.Packages.props --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 11a6766bbe..11d3ff66f7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -47,7 +47,7 @@ - +