-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add skipping tests based on the [SupportedOSPlatform] attribute
Adding support for `SupportedOSPlatform` is a great because this attributes suppresses the CA1416 code analysis warning. This feature was requested in xunit/xunit#2820 but was not implemented in xUnit.net
- Loading branch information
Showing
5 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Andrew Arnott. All rights reserved. | ||
// Licensed under the Microsoft Public License (Ms-PL). See LICENSE.txt file in the project root for full license information. | ||
|
||
using System.Runtime.InteropServices; | ||
using Xunit.Abstractions; | ||
|
||
namespace Xunit.Sdk; | ||
|
||
/// <summary> | ||
/// Extensions methods on <see cref="ITestMethod"/>. | ||
/// </summary> | ||
internal static class TestMethodExtensions | ||
{ | ||
/// <summary> | ||
/// Assesses whether the test method can run on the current platform by looking at the <c>[SupportedOSPlatform]</c> attributes on the test method and on the test class. | ||
/// </summary> | ||
/// <param name="testMethod">The <see cref="ITestMethod"/>.</param> | ||
/// <returns>A description of the supported platforms if the test can not run on the current platform or <see langword="null"/> if the test can run on the current platform.</returns> | ||
public static string? GetPlatformSkipReason(this ITestMethod testMethod) | ||
{ | ||
#if NET462 | ||
return null; | ||
#else | ||
var platforms = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
AddPlatforms(platforms, testMethod.Method.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute")); | ||
AddPlatforms(platforms, testMethod.Method.Type.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute")); | ||
|
||
if (platforms.Count == 0 || platforms.Any(platform => RuntimeInformation.IsOSPlatform(OSPlatform.Create(platform)))) | ||
{ | ||
return null; | ||
} | ||
|
||
string platformsDescription = platforms.Count == 1 ? platforms.First() : string.Join(", ", platforms.Reverse().Skip(1).Reverse()) + " and " + platforms.Last(); | ||
return $"Only supported on {platformsDescription}"; | ||
#endif | ||
} | ||
|
||
#if !NET462 | ||
private static void AddPlatforms(HashSet<string> platforms, IEnumerable<IAttributeInfo> supportedPlatformAttributes) | ||
{ | ||
foreach (IAttributeInfo supportedPlatformAttribute in supportedPlatformAttributes) | ||
{ | ||
platforms.Add(supportedPlatformAttribute.GetNamedArgument<string>("PlatformName")); | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters