-
Notifications
You must be signed in to change notification settings - Fork 470
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
CA-1418 Validate platform string #4838
CA-1418 Validate platform string #4838
Conversation
...nalyzers/UnitTests/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformStringTest.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. I just have a question about the versions that might have been covered during the API review of this already: Should we respect the number of version segments each OSPlatform supports? Several only support 3 segments; some don't support versions at all.
I don't think that is covered during API review, i am not sure if that information for all OS are available somewhere, if we could get that info we can add that check |
If the platform was found in the |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformString.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformString.cs
Outdated
Show resolved
Hide resolved
...nalyzers/UnitTests/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformStringTest.cs
Outdated
Show resolved
Hide resolved
{ | ||
public class UseValidPlatformStringTest | ||
{ | ||
private const string s_msBuildPlatforms = "build_property._SupportedPlatformList=CustomPlatform"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, some editorconfig options doesn't do anything without something like [*.cs]
.
@mavasani Can you confirm whether this is correct for this specific option?
If so, then there is a test gap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, some editorconfig options doesn't do anything without something like [*.cs].
hm, not sure, i was using it like this previously and it still works here too
...nalyzers/UnitTests/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformStringTest.cs
Show resolved
Hide resolved
Codecov Report
@@ Coverage Diff @@
## release/6.0.1xx-preview2 #4838 +/- ##
============================================================
- Coverage 95.61% 95.61% -0.01%
============================================================
Files 1167 1169 +2
Lines 266899 267438 +539
Branches 16134 16174 +40
============================================================
+ Hits 255206 255716 +510
- Misses 9613 9619 +6
- Partials 2080 2103 +23 |
@jeffhandley added validation for the number of version segments each OSPlatform supports, as version is optional we need only a max number of parameters. You might want to review messaging, sample messages are below cc @terrajobst : // versions are not supported case:
[SupportedOSPlatform("Linux4.1")|}] // The platform 'Linux' is not versioned, please remove version part
public void SupportedLinux_41() { }
// a platform support Max 4 version segments
[SupportedOSPlatform("Android4.8.1.2.3")|}] // The '4.8.1.2.3' is not a valid version. Please provide an input having 2 to 4 numbers separated with a dot.
public void SupportedOSPlatformAndroid5Parts() { }
// a platform support Max 3 version segments
[UnsupportedOSPlatform("Ios14.1.2.3")] // The '14.1.2.3' is not a valid version. Please provide an input having 2 to 3 numbers separated with a dot.
public void UnsupportedOSPlatformIos4PartsInvalid() { } |
I suggest the following messages. Each also shows using lower-case for the platform names in the messages to recommend that practice. // versions are not supported case:
[SupportedOSPlatform("Linux4.1")|}] // Version '4.1' is not valid for the platform 'linux'. Do not use versions for this platform.
public void SupportedLinux_41() { }
// a platform support Max 4 version segments
[SupportedOSPlatform("Android4.8.1.2.3")|}] // Version '4.8.1.2.3' is not valid for the platform 'android'. Use a version with 2-4 parts for this platform.
public void SupportedOSPlatformAndroid5Parts() { }
// a platform support Max 3 version segments
[UnsupportedOSPlatform("Ios14.1.2.3")] // Version '14.1.2.3' is not valid for the platform 'ios'. Use a version with 2-3 parts for this platform.
public void UnsupportedOSPlatformIos4PartsInvalid() { }
// a platform supporting Max 2 version segments
[UnsupportedOSPlatform("foo1.2.3"] // Version '1.2.3' is not valid for the platform 'foo'. Use a version with 2 parts for this platform. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a couple suggestions and I also commented with suggested messages, but otherwise looks good to me. Once my feedback is addressed, I approve without needing to re-review.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformString.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformString.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
Thanks, I like the messages except the lower casing part, isn't it better to report back the platform names as it is provided by the user for expressing that there is nothing wrong with the platform name itself? |
Fair enough; thanks for considering it. |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformString.cs
Show resolved
Hide resolved
|
||
if (constructorArguments.Length == 1) | ||
{ | ||
if (constructorArguments[0].Value is string value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will throw if the argument kind is array: http://sourceroslyn.io/#Microsoft.CodeAnalysis/Symbols/TypedConstant.cs,79
I would suggest adding a .Kind != TypedConstantKind.Array
before accessing .Value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is called only if the attribute were SupportedOSPlatform
or UnsupportedOSPlatform
attribute, which only accepts a string argument, if anyone calls them with an array type there will be a compiler error, so i think that is unnecessary, do you think that check is still needed?
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/InteropServices/UseValidPlatformString.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
* CA-1418 Validate platform string (#4838)
Validates platform string
Provided in constructor arguments
[SupportedOSPlatform("platform")]
,[UnsupportedOSPlatform("platform")]
attributes andOperatingSystem.IsOSPlatform("platform")
,OperatingSystem.IsOSPlatformVersionAtLeast("platform", 1)
methodsFixes dotnet/runtime#45851
Example: