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

OSX needs to treated as the same platform as MacOS, everywhere #5012

Merged
merged 4 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -55,6 +55,8 @@ public sealed partial class PlatformCompatibilityAnalyzer : DiagnosticAnalyzer
private const string IsPrefix = "Is";
private const string OptionalSuffix = "VersionAtLeast";
private const string Net = "net";
private const string macOS = nameof(macOS);
private const string MacSlashOSX = "macOS/OSX";

internal static DiagnosticDescriptor OnlySupportedCsReachable = DiagnosticDescriptorHelper.Create(RuleId,
s_localizableTitle,
Expand Down Expand Up @@ -648,7 +650,7 @@ static bool GetSupportedPlatforms(SmallDictionary<string, Versions> attributes,
platformNames.Add(GetFormattedString(MicrosoftNetCoreAnalyzersResources.PlatformCompatibilityAllVersions, pName));
continue;
}
platformNames.Add($"'{pName}'");
platformNames.Add(EncloseWithQuotes(pName));
}
else
{
Expand All @@ -664,7 +666,7 @@ static bool GetSupportedPlatforms(SmallDictionary<string, Versions> attributes,
platformNames.Add(GetFormattedString(MicrosoftNetCoreAnalyzersResources.PlatformCompatibilityAllVersions, pName));
continue;
}
platformNames.Add($"'{pName}'");
platformNames.Add(EncloseWithQuotes(pName));
}
else
{
Expand Down Expand Up @@ -755,7 +757,7 @@ static bool GetPlatformNames(SmallDictionary<string, Versions> attributes, Small
platformNames.Add(GetFormattedString(MicrosoftNetCoreAnalyzersResources.PlatformCompatibilityAllVersions, pName));
continue;
}
platformNames.Add($"'{pName}'");
platformNames.Add(EncloseWithQuotes(pName));
}
else
{
Expand All @@ -768,7 +770,7 @@ static bool GetPlatformNames(SmallDictionary<string, Versions> attributes, Small
unsupportedRule = false;
if (IsEmptyVersion(supportedVersion))
{
platformNames.Add($"'{pName}'");
platformNames.Add(EncloseWithQuotes(pName));
}
else
{
Expand Down Expand Up @@ -816,7 +818,7 @@ static List<string> GetCallsitePlatforms(SmallDictionary<string, Versions> attri
platformNames.Add(GetFormattedString(MicrosoftNetCoreAnalyzersResources.PlatformCompatibilityAllVersions, pName));
continue;
}
platformNames.Add($"'{pName}'");
platformNames.Add(EncloseWithQuotes(pName));
}
else
{
Expand Down Expand Up @@ -846,7 +848,7 @@ static List<string> GetCallsitePlatforms(SmallDictionary<string, Versions> attri
platformNames.Add(GetFormattedString(MicrosoftNetCoreAnalyzersResources.PlatformCompatibilityAllVersions, pName));
continue;
}
platformNames.Add($"'{pName}'");
platformNames.Add(EncloseWithQuotes(pName));
}
else
{
Expand All @@ -870,7 +872,13 @@ static List<string> GetCallsitePlatforms(SmallDictionary<string, Versions> attri
return platformNames;
}

static string GetFormattedString(string resource, params object[] args) => string.Format(CultureInfo.InvariantCulture, resource, args);
static string GetFormattedString(string resource, string platformName, object? arg1 = null, object? arg2 = null) =>
string.Format(CultureInfo.InvariantCulture, resource, AddOsxIfMacOS(platformName), arg1, arg2);

static string AddOsxIfMacOS(string platformName) =>
platformName.Equals(macOS, StringComparison.OrdinalIgnoreCase) ? MacSlashOSX : platformName;

static string EncloseWithQuotes(string pName) => $"'{AddOsxIfMacOS(pName)}'";

static string JoinNames(List<string> platformNames) => string.Join(MicrosoftNetCoreAnalyzersResources.CommaSeparator, platformNames);

Expand Down Expand Up @@ -1592,6 +1600,11 @@ attribute.ConstructorArguments[0] is { } argument &&
TryParsePlatformNameAndVersion(argument.Value.ToString(), out string platformName, out Version? version))
{
attributes ??= new SmallDictionary<string, Versions>(StringComparer.OrdinalIgnoreCase);
if (platformName.Equals("OSX", StringComparison.OrdinalIgnoreCase))
{
platformName = macOS;
}

if (!attributes.TryGetValue(platformName, out var _))
{
attributes[platformName] = new Versions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,147 @@ public static void SupportedOnWindows10AndBrowser() { }
await VerifyAnalyzerAsyncCs(source);
}

[Fact]
public async Task SupportedOnOsx_GuardedWithIsMacOS()
{
var source = @"
using System.Runtime.Versioning;
using System;

class Test
{
public void Api_Usage()
{
if (OperatingSystem.IsWindows() ||
OperatingSystem.IsLinux() ||
OperatingSystem.IsMacOS())
{
Api();
Api2();
}

[|Api()|]; // This call site is reachable on all platforms. 'Test.Api()' is only supported on: 'macOS/OSX', 'Linux', 'windows'.
[|Api2()|]; // This call site is reachable on all platforms. 'Test.Api2()' is only supported on: 'macOS/OSX', 'Linux', 'windows'.
}

[SupportedOSPlatform(""macos"")]
[SupportedOSPlatform(""windows"")]
[SupportedOSPlatform(""Linux"")]
void Api() { }

[SupportedOSPlatform(""windows"")]
[SupportedOSPlatform(""Osx"")]
[SupportedOSPlatform(""Linux"")]
void Api2() { }
}";

await VerifyAnalyzerAsyncCs(source, s_msBuildPlatforms);
}

[Fact]
public async Task UnsupportedOnOsx_GuardedWithIsMacOS()
{
var source = @"
using System.Runtime.Versioning;
using System;

class Test
{
public void Api_Usage()
{
if (!OperatingSystem.IsWindows() &&
!OperatingSystem.IsLinux() &&
!OperatingSystem.IsMacOS())
{
Api();
}

[|Api()|]; // This call site is reachable on all platforms. 'Test.Api()' is unsupported on: 'macOS/OSX', 'windows'.
}

[UnsupportedOSPlatform(""windows"")]
[UnsupportedOSPlatform(""Linux"")]
[UnsupportedOSPlatform(""Osx"")]
void Api() { }
}";

await VerifyAnalyzerAsyncCs(source, s_msBuildPlatforms);
}

[Fact]
public async Task SupportedOnOsxVersioned_GuardedWithIsMacOSVersioned()
{
var source = @"
using System.Runtime.Versioning;
using System;

class Test
{
public void Api_Usage()
{
if (OperatingSystem.IsWindowsVersionAtLeast(10) ||
OperatingSystem.IsLinux() ||
OperatingSystem.IsMacOSVersionAtLeast(11))
{
Api();
}

[|Api()|]; // This call site is reachable on all platforms. 'Test.Api2()' is only supported on: 'macOS/OSX' 10.1 and later, 'windows' 10.0 and later, 'Linux'.
}

[SupportedOSPlatform(""windows10.0"")]
[SupportedOSPlatform(""Linux"")]
[SupportedOSPlatform(""Osx10.1"")]
void Api() { }
}";

await VerifyAnalyzerAsyncCs(source, s_msBuildPlatforms);
}

[Fact]
public async Task SupportedUnsupportedOnOsx_GuardedWithIsMacOS_MessageParameterTest()
{
var source = @"
using System.Runtime.Versioning;
using System;

class Test
{
public void Api_Usage()
{
if (OperatingSystem.IsMacOS())
{
MacOsApi();
OsxApi();
{|#0:UnsupportedOsxApi()|};
}
else
{
{|#1:MacOsApi()|}; // This call site is reachable on all platforms. 'Test.Api()' is only supported on: 'macOS/OSX', 'Linux', 'windows'.
{|#2:OsxApi()|}; // This call site is reachable on all platforms. 'Test.Api2()' is only supported on: 'macOS/OSX', 'Linux', 'windows'.
UnsupportedOsxApi();
}
}

[SupportedOSPlatform(""macos"")]
void MacOsApi() { }

[SupportedOSPlatform(""Osx"")]
void OsxApi() { }

[UnsupportedOSPlatform(""Osx"")]
void UnsupportedOsxApi() { }
}";

await VerifyAnalyzerAsyncCs(source, s_msBuildPlatforms,
VerifyCS.Diagnostic(PlatformCompatibilityAnalyzer.UnsupportedCsReachable)
.WithLocation(0).WithArguments("Test.UnsupportedOsxApi()", "'macOS/OSX'", "'macOS/OSX'"),
VerifyCS.Diagnostic(PlatformCompatibilityAnalyzer.OnlySupportedCsAllPlatforms)
.WithLocation(1).WithArguments("Test.MacOsApi()", "'macOS/OSX'"),
VerifyCS.Diagnostic(PlatformCompatibilityAnalyzer.OnlySupportedCsAllPlatforms)
.WithLocation(2).WithArguments("Test.OsxApi()", "'macOS/OSX'"));
}

[Fact]
public async Task GuardsAroundSupported()
{
Expand Down
Loading