From 23749521a650a3fc81ff5e09d2eb1082ad1c18de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 26 Aug 2022 11:42:24 +0200 Subject: [PATCH 1/2] Add StringBuilder polyfill --- .../Extensions/StringBuilderPolyfill.cs | 12 ++++++++++++ .../ProcDumpArgsBuilder.cs | 19 ++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) create mode 100644 src/Microsoft.TestPlatform.CoreUtilities/Extensions/StringBuilderPolyfill.cs diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Extensions/StringBuilderPolyfill.cs b/src/Microsoft.TestPlatform.CoreUtilities/Extensions/StringBuilderPolyfill.cs new file mode 100644 index 0000000000..7da7dee54e --- /dev/null +++ b/src/Microsoft.TestPlatform.CoreUtilities/Extensions/StringBuilderPolyfill.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System.Text; +internal static class StringBuilderPolyfill +{ +#if !NET7_0_OR_GREATER + [Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Added to match new API.")] + public static StringBuilder Append(this StringBuilder builder, IFormatProvider? provider, string? value) + => builder.Append(value); +#endif +} diff --git a/src/Microsoft.TestPlatform.Extensions.BlameDataCollector/ProcDumpArgsBuilder.cs b/src/Microsoft.TestPlatform.Extensions.BlameDataCollector/ProcDumpArgsBuilder.cs index d7e17ca50a..d68b9b569b 100644 --- a/src/Microsoft.TestPlatform.Extensions.BlameDataCollector/ProcDumpArgsBuilder.cs +++ b/src/Microsoft.TestPlatform.Extensions.BlameDataCollector/ProcDumpArgsBuilder.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text; using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers; @@ -50,18 +51,10 @@ public string BuildTriggerBasedProcDumpArgs(int processId, string filename, IEnu foreach (var exceptionFilter in procDumpExceptionsList) { - procDumpArgument.Append( -#if NET7_0_OR_GREATER - System.Globalization.CultureInfo.InvariantCulture, -#endif - $"-f {exceptionFilter} "); + procDumpArgument.Append(CultureInfo.InvariantCulture, $"-f {exceptionFilter} "); } - procDumpArgument.Append( -#if NET7_0_OR_GREATER - System.Globalization.CultureInfo.InvariantCulture, -#endif - $"{processId} {filename}.dmp"); + procDumpArgument.Append(CultureInfo.InvariantCulture, $"{processId} {filename}.dmp"); var argument = procdumpArgumentsFromEnv.IsNullOrWhiteSpace() ? procDumpArgument.ToString() @@ -87,11 +80,7 @@ public string BuildHangBasedProcDumpArgs(int processId, string filename, bool is procDumpArgument.Append(" -ma"); } - procDumpArgument.Append( -#if NET7_0_OR_GREATER - System.Globalization.CultureInfo.InvariantCulture, -#endif - $" {processId} {filename}.dmp"); + procDumpArgument.Append(CultureInfo.InvariantCulture, $" {processId} {filename}.dmp"); var argument = procDumpArgument.ToString(); return argument; From 7b3701018c1426e1a0ae6cdf10f563831cb223b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Fri, 26 Aug 2022 11:42:39 +0200 Subject: [PATCH 2/2] Add Guid polyfill --- .../DataCollectionAttachmentManager.cs | 6 +----- .../Serialization/TestCaseConverter.cs | 7 +------ .../Extensions/GuidPolyfill.cs | 16 ++++++++++++++++ .../TestCase.cs | 8 +------- 4 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 src/Microsoft.TestPlatform.CoreUtilities/Extensions/GuidPolyfill.cs diff --git a/src/Microsoft.TestPlatform.Common/DataCollection/DataCollectionAttachmentManager.cs b/src/Microsoft.TestPlatform.Common/DataCollection/DataCollectionAttachmentManager.cs index 242600db5a..0ec01aa802 100644 --- a/src/Microsoft.TestPlatform.Common/DataCollection/DataCollectionAttachmentManager.cs +++ b/src/Microsoft.TestPlatform.Common/DataCollection/DataCollectionAttachmentManager.cs @@ -290,11 +290,7 @@ private void AddNewFileTransfer(FileTransferInformation fileTransferInfo, AsyncC ex.ToString(), uri, friendlyName, - Guid.Parse(testCaseId -#if NET7_0_OR_GREATER - , CultureInfo.InvariantCulture -#endif - ) + GuidPolyfill.Parse(testCaseId, CultureInfo.InvariantCulture) ); throw; diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/Serialization/TestCaseConverter.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/Serialization/TestCaseConverter.cs index 8b9064fccc..4841312bd1 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/Serialization/TestCaseConverter.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/Serialization/TestCaseConverter.cs @@ -73,12 +73,7 @@ public override bool CanConvert(Type objectType) switch (testProperty.Id) { case "TestCase.Id": - testCase.Id = -#if NET7_0_OR_GREATER - Guid.Parse(propertyData!, CultureInfo.InvariantCulture); -#else - Guid.Parse(propertyData!); -#endif + testCase.Id = GuidPolyfill.Parse(propertyData!, CultureInfo.InvariantCulture); break; case "TestCase.ExecutorUri": testCase.ExecutorUri = new Uri(propertyData!); break; diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Extensions/GuidPolyfill.cs b/src/Microsoft.TestPlatform.CoreUtilities/Extensions/GuidPolyfill.cs new file mode 100644 index 0000000000..fa2fc3690e --- /dev/null +++ b/src/Microsoft.TestPlatform.CoreUtilities/Extensions/GuidPolyfill.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System; +/// +/// A polyfill helper for Guid. +/// +internal static class GuidPolyfill +{ + public static Guid Parse(string s, IFormatProvider? provider) + => Guid.Parse(s +#if NET7_0_OR_GREATER + , System.Globalization.CultureInfo.InvariantCulture +#endif + ); +} diff --git a/src/Microsoft.TestPlatform.ObjectModel/TestCase.cs b/src/Microsoft.TestPlatform.ObjectModel/TestCase.cs index fef61efea6..d949ede703 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/TestCase.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/TestCase.cs @@ -3,9 +3,7 @@ using System; using System.Collections.Generic; -#if NET7_0_OR_GREATER using System.Globalization; -#endif using System.IO; using System.Linq; using System.Runtime.Serialization; @@ -270,11 +268,7 @@ protected override void ProtectedSetPropertyValue(TestProperty property, object? } else if (value is string guidString) { -#if NET7_0_OR_GREATER - Id = Guid.Parse(guidString, CultureInfo.InvariantCulture); -#else - Id = Guid.Parse(guidString); -#endif + Id = GuidPolyfill.Parse(guidString, CultureInfo.InvariantCulture); } else {