Skip to content

Commit

Permalink
OSX is alias for MacOS (#4952)
Browse files Browse the repository at this point in the history
* OSX is alias for MacOS
  • Loading branch information
buyaa-n authored Mar 18, 2021
1 parent 8eb850e commit 988eaf5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed class UseValidPlatformString : DiagnosticAnalyzer
private static readonly ImmutableArray<string> methodNames = ImmutableArray.Create("IsOSPlatform", "IsOSPlatformVersionAtLeast");
private const string IsPrefix = "Is";
private const string VersionSuffix = "VersionAtLeast";
private const string macOS = nameof(macOS);

private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.UseValidPlatformStringTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources));
private static readonly LocalizableString s_localizableUnknownPlatform = new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.UseValidPlatformStringUnknownPlatform), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources));
Expand Down Expand Up @@ -76,6 +77,11 @@ public override void Initialize(AnalysisContext context)
AddPlatformsFromMsBuildOptions(knownPlatforms, context.Options.GetMSBuildItemMetadataValues(
MSBuildItemOptionNames.SupportedPlatform, context.Compilation, context.CancellationToken));
if (knownPlatforms.TryGetValue(macOS, out var versions))
{
knownPlatforms.Add("OSX", versions);
}
context.RegisterOperationAction(context => AnalyzeOperation(context.Operation, context, knownPlatforms), OperationKind.Invocation);
context.RegisterSymbolAction(context => AnalyzeSymbol(context.ReportDiagnostic, context.Symbol,
supportedAttriubte, unsupportedAttribute, knownPlatforms, context.CancellationToken), s_symbols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,67 @@ await VerifyAnalyzerAsyncCs(source,
VerifyCS.Diagnostic(UseValidPlatformString.NoVersion).WithLocation(4).WithArguments("4.1", "Linux"));
}

[Fact]
public async Task PlatformOSXIsAliasForMacOS()
{
var csSource = @"
using System.Runtime.Versioning;
public class Test
{
[{|#0:SupportedOSPlatform(""MacOS1.2.3.4"")|}] // Version '1.2.3.4' is not valid for platform 'MacOS'. Use a version with 2-3 parts for this platform.
public void SupportedOSPlatformMac4PartsInvalid() { }
[SupportedOSPlatform(""MacOS1.2"")]
public void SupportedMacOs2PartsValid() { }
[{|#1:SupportedOSPlatform(""OSX1.2.3.4"")|}] // Version '1.2.3.4' is not valid for platform 'OSX'. Use a version with 2-3 parts for this platform.
public void SupportedOSPlatformOSX4PartsInvalid() { }
[SupportedOSPlatform(""Osx1.2"")]
public void SupportedOsx2PartsValid() { }
}";
await VerifyAnalyzerAsyncCs(csSource,
VerifyCS.Diagnostic(UseValidPlatformString.InvalidVersion).WithLocation(0).WithArguments("1.2.3.4", "MacOS", "-3"),
VerifyCS.Diagnostic(UseValidPlatformString.InvalidVersion).WithLocation(1).WithArguments("1.2.3.4", "OSX", "-3"));
}

[Fact]
public async Task APIsWithMultiplePlatformSupportUnsupport()
{
var csSource = @"
using System.Runtime.Versioning;
public class Test
{
[{|#0:SupportedOSPlatform(""MacOS1.2.3.4"")|}] // Version '1.2.3.4' is not valid for platform 'MacOS'. Use a version with 2-3 parts for this platform.
[SupportedOSPlatform(""Osx2.3"")]
[SupportedOSPlatform(""Linux"")]
[[|SupportedOSPlatform(""Browser4.3"")|]] // Browser should not have a version
public void SupportedOSPlatformMac4PartsInvalid() { }
[SupportedOSPlatform(""MacOS1.2"")]
[SupportedOSPlatform(""Osx2.3"")]
public void SupportedMacOsOsxPartsValid() { }
[UnsupportedOSPlatform(""osx"")]
[{|#1:SupportedOSPlatform(""OSX1.2.3.4"")|}] // Version '1.2.3.4' is not valid for platform 'OSX'. Use a version with 2-3 parts for this platform.
[UnsupportedOSPlatform(""browser"")]
public void UnsupportedBrowserOSXSupportHasWarning() { }
[UnsupportedOSPlatform(""browser"")]
[[|UnsupportedOSPlatform(""Linux1.0"")|]] // Linux should not have a version
public void UnsupportedOSPlatformOSX4PartsInvalid() { }
[SupportedOSPlatform(""Osx1.2"")]
[[|SupportedOSPlatform(""MacOS1.2.3.4"")|]] // Version '1.2.3.4' is not valid for platform 'MacOS'. Use a version with 2-3 parts for this platform.
public void SupportedOsx2PartValid() { }
}";
await VerifyAnalyzerAsyncCs(csSource,
VerifyCS.Diagnostic(UseValidPlatformString.InvalidVersion).WithLocation(0).WithArguments("1.2.3.4", "MacOS", "-3"),
VerifyCS.Diagnostic(UseValidPlatformString.InvalidVersion).WithLocation(1).WithArguments("1.2.3.4", "OSX", "-3"));
}

private static async Task VerifyAnalyzerAsyncCs(string sourceCode, params DiagnosticResult[] expectedDiagnostics)
{
var test = PopulateTestCs(sourceCode);
Expand Down

0 comments on commit 988eaf5

Please sign in to comment.