Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Span trim stack trace #4368

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<PackageVersion Include="Microsoft.TestPlatform.TranslationLayer" Version="$(MicrosoftNETTestSdkVersion)" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.0.0" />
<PackageVersion Include="Polyfill" Version="7.5.0" />
<PackageVersion Include="System.Memory" Version="4.6.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />
<!-- CVE-2019-0820 -->
<PackageVersion Include="System.Diagnostics.TextWriterTraceListener" Version="4.3.0" />
Expand Down
2 changes: 1 addition & 1 deletion samples/FxExtensibility/FxExtensibility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PropertyGroup>
<RootNamespace>MSTest.Extensibility.Samples</RootNamespace>
<AssemblyName>MSTest.Extensibility.Samples</AssemblyName>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Expand Down
18 changes: 8 additions & 10 deletions src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -100,19 +99,18 @@ internal static class ExceptionHelper
/// <returns>
/// The trimmed stack trace removing traces of the framework and adapter from the stack.
/// </returns>
internal static string TrimStackTrace(string stackTrace)
internal static string TrimStackTrace(ReadOnlySpan<char> stackTrace)
{
if (stackTrace.Length == 0)
{
return stackTrace;
return string.Empty;
}

StringBuilder result = new(stackTrace.Length);
string[] stackFrames = Regex.Split(stackTrace, Environment.NewLine);

foreach (string stackFrame in stackFrames)
foreach (ReadOnlySpan<char> stackFrame in stackTrace.EnumerateLines())
{
if (StringEx.IsNullOrEmpty(stackFrame))
if (stackFrame.Length == 0)
{
continue;
}
Expand All @@ -123,7 +121,7 @@ internal static string TrimStackTrace(string stackTrace)
if (!hasReference)
{
result.Append(stackFrame);
result.Append(Environment.NewLine);
result.AppendLine();
}
}

Expand Down Expand Up @@ -200,7 +198,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;
}
Expand All @@ -214,11 +212,11 @@ internal static string GetFormattedExceptionMessage(this Exception ex)
/// <returns>
/// True if the framework or the adapter methods are in the stack frame.
/// </returns>
internal static bool HasReferenceToUTF(string stackFrame)
internal static bool HasReferenceToUTF(ReadOnlySpan<char> stackFrame)
{
foreach (string type in TypesToBeExcluded)
{
if (stackFrame.IndexOf(type, StringComparison.Ordinal) > -1)
if (stackFrame.IndexOf(type.AsSpan(), StringComparison.Ordinal) > -1)
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter</RootNamespace>
<AssemblyName>Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter</AssemblyName>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
<!-- Force NuGet package dependencies to be copied to the output directory so we can embed AdapterUtilities in our NuGet. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices</RootNamespace>
<AssemblyName>Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices</AssemblyName>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
</PropertyGroup>

<!-- Properties specific to UWP -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(MicrosoftTestingTargetFrameworks);netstandard2.0</TargetFrameworks>
<DefineConstants>PLATFORM_MSBUILD</DefineConstants>
<DefineConstants>$(DefineConstants);PLATFORM_MSBUILD</DefineConstants>
<NoWarn>$(NoWarn);NU5100</NoWarn>
</PropertyGroup>

Expand Down Expand Up @@ -94,4 +94,4 @@ This package provides MSBuild integration of the platform, its extensions and co
<Using Include="Polyfills" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.VisualStudio.TestTools.UnitTesting</RootNamespace>
<AssemblyName>Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions</AssemblyName>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
</PropertyGroup>

<!-- WinUI specific properties -->
Expand Down
4 changes: 3 additions & 1 deletion src/TestFramework/TestFramework/TestFramework.csproj
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
Expand All @@ -8,7 +8,7 @@
<PropertyGroup>
<RootNamespace>Microsoft.VisualStudio.TestTools.UnitTesting</RootNamespace>
<AssemblyName>Microsoft.VisualStudio.TestPlatform.TestFramework</AssemblyName>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,6 +21,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ValueTuple" Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == '$(NetFrameworkMinimum)' OR '$(TargetFramework)' == '$(UwpMinimum)' " />
<PackageReference Include="System.Memory" Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == '$(NetFrameworkMinimum)' OR '$(TargetFramework)' == '$(UwpMinimum)' " />
<PackageReference Include="Polyfill" PrivateAssets="all" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>$(DefineConstants);TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<IsCodedUITest>False</IsCodedUITest>
Expand Down
Loading