From ce67785f06efad87d57f67c4d4744cba2f913a04 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 24 Aug 2023 19:11:53 +0300 Subject: [PATCH] More resilient approach in `GetTypeFullyQualifiedName()` --- .../Utils/Extensions/TestCaseExtensions.cs | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/GitHubActionsTestLogger/Utils/Extensions/TestCaseExtensions.cs b/GitHubActionsTestLogger/Utils/Extensions/TestCaseExtensions.cs index effabd2..79a1bf9 100644 --- a/GitHubActionsTestLogger/Utils/Extensions/TestCaseExtensions.cs +++ b/GitHubActionsTestLogger/Utils/Extensions/TestCaseExtensions.cs @@ -7,18 +7,34 @@ namespace GitHubActionsTestLogger.Utils.Extensions; internal static class TestCaseExtensions { public static string GetTypeFullyQualifiedName(this TestCase testCase) => - testCase.FullyQualifiedName.SubstringUntilLast(".", StringComparison.OrdinalIgnoreCase); + testCase.FullyQualifiedName + // Strip the test cases (if this is a parameterized test method) + .SubstringUntil("(", StringComparison.OrdinalIgnoreCase) + // Strip everything after the last dot, to leave the full type name + .SubstringUntilLast(".", StringComparison.OrdinalIgnoreCase); public static string GetTypeMinimallyQualifiedName(this TestCase testCase) { - var ns = Path.GetFileNameWithoutExtension(testCase.Source); var fullyQualifiedName = testCase.GetTypeFullyQualifiedName(); - return fullyQualifiedName.StartsWith(ns + '.', StringComparison.Ordinal) - ? fullyQualifiedName[(ns.Length + 1)..] - : fullyQualifiedName; + // We assume that the test assembly name matches the namespace. + // This is not always true, but it's the best we can do. + var nameSpace = Path.GetFileNameWithoutExtension(testCase.Source); + + // Strip the namespace from the type name, if it's there + if (fullyQualifiedName.StartsWith(nameSpace + '.', StringComparison.Ordinal)) + return fullyQualifiedName[(nameSpace.Length + 1)..]; + + return fullyQualifiedName; } - public static string GetMinimallyQualifiedName(this TestCase testCase) => - testCase.FullyQualifiedName.SubstringAfterLast(".", StringComparison.OrdinalIgnoreCase); + public static string GetMinimallyQualifiedName(this TestCase testCase) + { + var fullyQualifiedName = testCase.GetTypeFullyQualifiedName(); + + // Strip the full type name from the test method name, if it's there + return testCase.FullyQualifiedName.StartsWith(fullyQualifiedName, StringComparison.Ordinal) + ? testCase.FullyQualifiedName[(fullyQualifiedName.Length + 1)..] + : testCase.FullyQualifiedName; + } }