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

Added support for spaces and other special characters in method names into the ManagedNameUtilities #2738

Merged
merged 14 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
14 changes: 7 additions & 7 deletions TestPlatform.sln
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330}"
ProjectSection(SolutionItems) = preProject
scripts\build.ps1 = scripts\build.ps1
scripts\build.sh = scripts\build.sh
scripts\PortableToFullPdb.ps1 = scripts\PortableToFullPdb.ps1
scripts\stylecop.json = scripts\stylecop.json
scripts\stylecop.ruleset = scripts\stylecop.ruleset
scripts\stylecop.test.ruleset = scripts\stylecop.test.ruleset
scripts\test.ps1 = scripts\test.ps1
scripts\verify-sign.ps1 = scripts\verify-sign.ps1
scripts\test.sh = scripts\test.sh
scripts\verify-nupkgs.ps1 = scripts\verify-nupkgs.ps1
scripts\PortableToFullPdb.ps1 = scripts\PortableToFullPdb.ps1
scripts\verify-sign.ps1 = scripts\verify-sign.ps1
scripts\vsts-prebuild.ps1 = scripts\vsts-prebuild.ps1
scripts\write-release-notes.ps1 = scripts\write-release-notes.ps1
scripts\build.sh = scripts\build.sh
scripts\test.sh = scripts\test.sh
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E344E0A2-7715-4C7F-BAF7-D64EA94CB19B}"
Expand Down Expand Up @@ -818,6 +818,8 @@ Global
{9EFCEFB5-253E-4DE2-8A70-821D7B8189DF} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{3A8080FB-9C93-45B9-8EB5-828DDC31FDF0} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{BFF7714C-E5A3-4EEB-B04B-5FA47F29AD03} = {020E15EA-731F-4667-95AF-226671E0C3AE}
{0D4DF78D-7E5F-4516-B19F-E6AA71A1DBF4} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330}
{DED1590A-ED25-413B-8590-006A4DD5B2FD} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330}
{E344E0A2-7715-4C7F-BAF7-D64EA94CB19B} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330}
{DD9382B5-5EC4-4B3D-BEB7-95423731AE29} = {46250E12-4CF1-4051-B4A7-80C8C06E0068}
{156F8811-28BB-4EC7-87D9-434F10FB7DBE} = {46250E12-4CF1-4051-B4A7-80C8C06E0068}
Expand All @@ -829,7 +831,6 @@ Global
{CAE652AF-6801-425E-AAF3-AB20DE7DF88E} = {7D4082EA-7AC9-4DFB-98E8-C5E08BDC0EC3}
{FF80D706-8309-4E02-BAC0-D28B4CBCF600} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{7D4082EA-7AC9-4DFB-98E8-C5E08BDC0EC3} = {ED0C35EB-7F31-4841-A24F-8EB708FFA959}
{DED1590A-ED25-413B-8590-006A4DD5B2FD} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330}
{E141A226-CC0A-4F26-BD17-4AE427D81C3B} = {D27E1CB4-C641-4C6C-A140-EF5F6215AE29}
{376C19DE-31E2-4FF6-88FC-0D0D6233C999} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{3E698655-0701-482E-9AA7-F956F6337FC7} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999}
Expand All @@ -842,15 +843,14 @@ Global
{65A25D6E-C9CC-4F45-8925-04087AC82634} = {B705537C-B82C-4A30-AFA5-6244D9A7DAEB}
{D9A30E32-D466-4EC5-B4F2-62E17562279B} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{21DB138B-85B7-479E-91FE-01E0F972EC56} = {D9A30E32-D466-4EC5-B4F2-62E17562279B}
{0D4DF78D-7E5F-4516-B19F-E6AA71A1DBF4} = {EE49F5DC-5835-4AE3-B3BA-8BDE0AD56330}
{8C068694-23A2-47A2-A0DD-DB82D0AF0142} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999}
{24C7683D-2607-4901-B8EB-83A57E49E93D} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999}
{69F5FF81-5615-4F06-B83C-FCF979BB84CA} = {ED0C35EB-7F31-4841-A24F-8EB708FFA959}
{E7D4921C-F12D-4E1C-85AC-8B7F91C59B0E} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{236A71E3-01DA-4679-9DFF-16A8E079ACFF} = {5E7F18A8-F843-4C8A-AB02-4C7D9205C6CF}
{41248B96-6E15-4E5E-A78F-859897676814} = {020E15EA-731F-4667-95AF-226671E0C3AE}
{074F5BD6-DC05-460B-B78F-044D125FD787} = {D9A30E32-D466-4EC5-B4F2-62E17562279B}
{DCD0C39E-C78C-4A44-B0BD-7325254A2E97} = {B27FAFDF-2DBA-4AB0-BA85-FD5F21D359D6}
{DCD0C39E-C78C-4A44-B0BD-7325254A2E97} = {376C19DE-31E2-4FF6-88FC-0D0D6233C999}
{2DE99835-A3A3-4922-82AD-6D10D284816D} = {7D4082EA-7AC9-4DFB-98E8-C5E08BDC0EC3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
Expand Down
6 changes: 6 additions & 0 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ $env:MSBUILD_VERSION = "15.0"
Write-Verbose "Setup build configuration."
$TPB_Solution = "TestPlatform.sln"
$TPB_TestAssets_Solution = Join-Path $env:TP_ROOT_DIR "test\TestAssets\TestAssets.sln"
$TPB_TestAssets_CILAssets = Join-Path $env:TP_ROOT_DIR "test\TestAssets\CILProject\CILProject.proj"
Haplois marked this conversation as resolved.
Show resolved Hide resolved
$TPB_TargetFramework45 = "net45"
$TPB_TargetFramework451 = "net451"
$TPB_TargetFramework472 = "net472"
Expand Down Expand Up @@ -277,6 +278,11 @@ function Invoke-Build
& $dotnetExe build $TPB_Solution --configuration $TPB_Configuration -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:TestPlatform.binlog
Write-Log ".. .. Build: Complete."

Write-Log ".. .. Build: Source: $TPB_TestAssets_CILAssets"
Write-Verbose "$dotnetExe build $TPB_TestAssets_CILAssets --configuration $TPB_Configuration -v:minimal -p:Version=$TPB_Version -p:CIBuild=$TPB_CIBuild"
& $dotnetExe build $TPB_TestAssets_CILAssets --configuration $TPB_Configuration -v:minimal -p:CIBuild=$TPB_CIBuild -p:LocalizedBuild=$TPB_LocalizedBuild -bl:"$($env:TP_ROOT_DIR)\CILAssets.binlog"
Write-Log ".. .. Build: Complete."

Set-ScriptFailedOnError

Write-Log "Invoke-Build: Complete. {$(Get-ElapsedTime($timer))}"
Expand Down
1 change: 1 addition & 0 deletions scripts/build/TestPlatform.Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<FileSystemGlobbingVersion>1.1.1</FileSystemGlobbingVersion>

<NuGetFrameworksVersion>5.0.0</NuGetFrameworksVersion>
<ILAsmPackageVersion>5.0.0</ILAsmPackageVersion>
<JsonNetVersion>9.0.1</JsonNetVersion>
<MoqVersion>4.7.63</MoqVersion>
<TestPlatformExternalsVersion>16.9.0-preview-4267359</TestPlatformExternalsVersion>
Expand Down
1 change: 1 addition & 0 deletions scripts/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ function Invoke-Test
}
else
{
Write-Log ".. . $testContainerName test container found. ($testOutputPath)"
$testContainers += ,"$testContainerPath"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.TestPlatform.AdapterUtilities.Helpers
using Microsoft.TestPlatform.AdapterUtilities.Resources;

using System;
using System.Text;

internal static partial class ReflectionHelpers
{
Expand All @@ -17,5 +18,64 @@ private static void AssertSupport<T>(T obj, string methodName, string className)
throw new NotImplementedException(string.Format(Resources.MethodNotImplementedOnPlatform, className, methodName));
}
}

internal static string ParseEscapedString(string escapedString)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
var stringBuilder = new StringBuilder();
var end = 0;
for (int i = 0; i < escapedString.Length; i++)
{
if (escapedString[i] == '\'')
{
stringBuilder.Append(escapedString, end, i - end);
end = i = ParseEscapedStringSegment(escapedString, i + 1, stringBuilder);
cvpoienaru marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (stringBuilder.Length == 0)
{
return escapedString;
}

if (end != 0 && end < escapedString.Length)
{
stringBuilder.Append(escapedString, end, escapedString.Length - end);
}

return stringBuilder.ToString();
}

private static int ParseEscapedStringSegment(string escapedStringSegment, int pos, StringBuilder stringBuilder)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
for (int i = pos; i < escapedStringSegment.Length; i++)
{
var c = escapedStringSegment[i];
if (c == '\\')
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
if (escapedStringSegment[i + 1] == 'u')
{
var code = escapedStringSegment.Substring(i + 2, 4);
Haplois marked this conversation as resolved.
Show resolved Hide resolved
c = (char)Convert.ToInt32(code, 16);
Haplois marked this conversation as resolved.
Show resolved Hide resolved
stringBuilder.Append(c);
i += 5;
Haplois marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
stringBuilder.Append(escapedStringSegment[i + 1]);
i += 1;
cvpoienaru marked this conversation as resolved.
Show resolved Hide resolved
}
}
else if (c == '\'')
{
return i + 1;
}
else
{
stringBuilder.Append(escapedStringSegment[i]);
}
}

return escapedStringSegment.Length;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ public static void GetManagedName(MethodBase method, out string managedTypeName,
method = ((MethodInfo)method).GetGenericMethodDefinition();
}

var methodBuilder = new StringBuilder();
var typeBuilder = new StringBuilder();
var methodBuilder = new StringBuilder();

// Namespace and Type Name (with arity designation)
AppendTypeString(typeBuilder, semanticType, closedType: false);

// Method Name with method arity
methodBuilder.Append(method.Name);
var arity = method.GetGenericArguments().Length;
AppendMethodString(methodBuilder, method.Name, arity);
if (arity > 0)
{
methodBuilder.Append('`');
Expand Down Expand Up @@ -143,12 +143,14 @@ public static MethodBase GetMethod(Assembly assembly, string managedTypeName, st
{
Type type;

var parsedManagedTypeName = ReflectionHelpers.ParseEscapedString(managedTypeName);

#if !NETSTANDARD1_0 && !NETSTANDARD1_3 && !WINDOWS_UWP
type = assembly.GetType(managedTypeName, throwOnError: false, ignoreCase: false);
type = assembly.GetType(parsedManagedTypeName, throwOnError: false, ignoreCase: false);
#else
try
{
type = assembly.GetType(managedTypeName);
type = assembly.GetType(parsedManagedTypeName);
}
catch
{
Expand All @@ -158,14 +160,14 @@ public static MethodBase GetMethod(Assembly assembly, string managedTypeName, st

if (type == null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resources.ErrorTypeNotFound, managedTypeName);
string message = string.Format(CultureInfo.CurrentCulture, Resources.ErrorTypeNotFound, parsedManagedTypeName);
throw new InvalidManagedNameException(message);
}

MethodInfo method = null;
ManagedNameParser.ParseManagedMethodName(managedMethodName, out var methodName, out var methodArity, out var parameterTypes);
#if NET20 || NET35

#if NET20 || NET35
if (!IsNullOrWhiteSpace(methodName))
#else
if (!string.IsNullOrWhiteSpace(methodName))
Expand Down Expand Up @@ -254,7 +256,7 @@ private static void AppendTypeString(StringBuilder b, Type type, bool closedType
}
else
{
b.Append(type.Namespace);
AppendNamespace(b, type.Namespace);
b.Append('.');

AppendNestedTypeName(b, type);
Expand All @@ -266,14 +268,169 @@ private static void AppendTypeString(StringBuilder b, Type type, bool closedType
}
}

private static void AppendNestedTypeName(StringBuilder b, Type type)
private static void AppendNamespace(StringBuilder b, string ns)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
int start = 0;
bool shouldEscape = false;

for (int i = 0; i <= ns.Length; i++)
{
if (i == ns.Length || ns[i] == '.')
{
if (start != 0)
{
b.Append('.');
}

var part = ns.Substring(start, i - start);
if (shouldEscape)
{
NormalizeString(b, part);
shouldEscape = false;
}
else
{
b.Append(part);
}

start = i + 1;
continue;
}

shouldEscape = shouldEscape || NeedsEscaping(ns[i], i - start);
}
}

private static void AppendMethodString(StringBuilder methodBuilder, string name, int methodArity)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
var arityStart = name.LastIndexOf('`');
var arity = 0;
if (arityStart > 0)
{
arityStart++;
var arityString = name.Substring(arityStart, name.Length - arityStart);
if (int.TryParse(arityString, out arity))
{
if (arity == methodArity)
{
name = name.Substring(0, arityStart - 1);
}
}
}

if (IsNormalized(name))
{
methodBuilder.Append(name);
}
else
{
NormalizeString(methodBuilder, name);
cvpoienaru marked this conversation as resolved.
Show resolved Hide resolved
}

if (arity > 0 && methodArity == arity)
{
methodBuilder.Append($"`{arity}");
}
}

private static void NormalizeString(StringBuilder b, string name)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
b.Append("'");
for (int i = 0; i < name.Length; i++)
{
char c = name[i];
if (NeedsEscaping(c, i))
{
if (c == '\\' || c == '\'')
{
// var encoded = Convert.ToString(((uint)c), 16);
cvpoienaru marked this conversation as resolved.
Show resolved Hide resolved
// b.Append("\\u");
// b.Append('0', 4 - encoded.Length);
// b.Append(encoded);

b.Append("\\");
b.Append(c);
continue;
}
}

b.Append(c);
}
b.Append("'");
}

private static bool IsNormalized(string s)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
for (int i = 0; i < s.Length; i++)
{
if (NeedsEscaping(s[i], i) && s[i] != '.')
{
return false;
}
}

return true;
}

private static bool NeedsEscaping(char c, int pos)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
if (pos == 0 && char.IsDigit(c))
{
return true;
}

if (c == '_'
|| char.IsLetterOrDigit(c) // Lu, Ll, Lt, Lm, Lo, or Nl
)
{
return false;
}

var category = CharUnicodeInfo.GetUnicodeCategory(c);
if (category == UnicodeCategory.NonSpacingMark // Mn
|| category == UnicodeCategory.SpacingCombiningMark // Mc
|| category == UnicodeCategory.ConnectorPunctuation // Pc
|| category == UnicodeCategory.Format) // Cf
{
return false;
}

return true;
}

private static int AppendNestedTypeName(StringBuilder b, Type type)
{
var outerArity = 0;
if (type.IsNested)
{
AppendNestedTypeName(b, type.DeclaringType);
outerArity = AppendNestedTypeName(b, type.DeclaringType);
b.Append('+');
}
b.Append(type.Name);

var typeName = type.Name;
var stars = 0;
if (type.IsPointer)
{
for (int i = typeName.Length - 1; i > 0; i--)
Haplois marked this conversation as resolved.
Show resolved Hide resolved
{
if (typeName[i] != '*')
{
stars = typeName.Length - i - 1;
typeName = typeName.Substring(0, i + 1);
break;
}
}
}

var info = type.GetTypeInfo();
var arity = info.IsGenericType == false ? 0
Haplois marked this conversation as resolved.
Show resolved Hide resolved
Haplois marked this conversation as resolved.
Show resolved Hide resolved
: info.GenericTypeParameters.Length > 0
? info.GenericTypeParameters.Length
: info.GenericTypeArguments.Length;

AppendMethodString(b, typeName, arity - outerArity);
b.Append('*', stars);
return arity;
}

private static void AppendGenericTypeParameters(StringBuilder b, Type type)
Expand Down
Loading