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

Add some polyfill to simplify code across compilations #3974

Merged
merged 2 commits into from
Aug 29, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
/// <summary>
/// A polyfill helper for Guid.
/// </summary>
internal static class GuidPolyfill
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoRossignoli Was suggesting to have only one class called Polyfill that would host all polyfills as sub-classes (e.g. Polyfill.Guid.Parse. It looks like a good idea but doesn't allow for extension methods (need to be defined at top class-level).

We can:

  1. Keep solution implemented in this PR
  2. Go with @MarcoRossignoli's suggestion for non-extension methods (not ideal as it would force us to have 2 ways)
  3. Same as 2. but we do that also for extension methods (will no longer be extension methods).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see theoretical way around it but it requires three classes and instances. Seems like PolyfillGuid or GuidPolyfill in separate class is much easier way to go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's ok as-is to me.

{
public static Guid Parse(string s, IFormatProvider? provider)
=> Guid.Parse(s
#if NET7_0_OR_GREATER
, System.Globalization.CultureInfo.InvariantCulture
#endif
);
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers;
Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand Down
8 changes: 1 addition & 7 deletions src/Microsoft.TestPlatform.ObjectModel/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Contributor

@MarcoRossignoli MarcoRossignoli Aug 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the used signature be the "older" one, I mean from user perspective looks like the culture is important...but it's important only for one version...so I wonder if we should have the one without culture and setup the correct default for the newer one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good suggestion. I don't see us using anything else than InvariantCulture so it would simplify calls. My idea here is to match newest API so that code looks like what you would expect if using recent .NET.

Maybe I will keep it as-is.

}
else
{
Expand Down