1+ using System . Runtime . InteropServices ;
2+ using TUnit . Core . Enums ;
3+
4+ namespace TUnit . Core ;
5+
6+ /// <summary>
7+ /// Attribute that excludes a test from running on specific operating systems.
8+ /// </summary>
9+ /// <param name="OperatingSystem">
10+ /// Defines the operating systems on which the test should not run.
11+ /// </param>
12+ /// <remarks>
13+ /// <para>
14+ /// The <see cref="ExcludeOnAttribute"/> is used to specify that a test should not run on certain operating systems.
15+ /// Tests with this attribute will be skipped on operating systems that match the specified criteria.
16+ /// </para>
17+ /// <para>
18+ /// You can specify multiple operating systems by combining the <see cref="OS"/> enum values with the bitwise OR operator.
19+ /// </para>
20+ /// </remarks>
21+ /// <example>
22+ /// <code>
23+ /// // Skip on Windows
24+ /// [Test, ExcludeOn(OS.Windows)]
25+ /// public void NonWindowsOnlyTest()
26+ /// {
27+ /// // This test will run on Linux and macOS, but not on Windows
28+ /// }
29+ ///
30+ /// // Skip on both Windows and Linux
31+ /// [Test, ExcludeOn(OS.Windows | OS.Linux)]
32+ /// public void MacOsOnlyTest()
33+ /// {
34+ /// // This test will only run on macOS
35+ /// }
36+ ///
37+ /// // Skip on all supported platforms (essentially always skips the test)
38+ /// [Test, ExcludeOn(OS.Windows | OS.Linux | OS.MacOs)]
39+ /// public void NeverRunTest()
40+ /// {
41+ /// // This test will not run on any supported platform
42+ /// }
43+ /// </code>
44+ /// </example>
45+ /// <seealso cref="SkipAttribute"/>
46+ /// <seealso cref="RunOnAttribute"/>
47+ /// <seealso cref="OS"/>
48+ public sealed class ExcludeOnAttribute ( OS OperatingSystem ) : SkipAttribute ( GetReason ( OperatingSystem ) )
49+ {
50+ /// <inheritdoc />
51+ public override Task < bool > ShouldSkip ( BeforeTestContext context )
52+ {
53+ // Check if the current platform matches any of the excluded operating systems
54+ bool shouldSkip =
55+ ( OperatingSystem . HasFlag ( OS . Windows ) && RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
56+ #if NET
57+ // Only validate Linux and macOS on .NET 5+ where these OS flags are available
58+ || ( OperatingSystem . HasFlag ( OS . Linux ) && RuntimeInformation . IsOSPlatform ( OSPlatform . Linux ) )
59+ || ( OperatingSystem . HasFlag ( OS . MacOs ) && RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) )
60+ #endif
61+ ;
62+
63+ // Return true if the test should be skipped (if we're on an excluded OS)
64+ return Task . FromResult ( shouldSkip ) ;
65+ }
66+
67+ private static string GetReason ( OS operatingSystems )
68+ {
69+ var excludedOperatingSystems =
70+ #if NET
71+ Enum . GetValues < OS > ( )
72+ #else
73+ Enum . GetValues ( typeof ( OS ) ) . Cast < OS > ( )
74+ #endif
75+ . Where ( os => operatingSystems . HasFlag ( os ) )
76+ . ToArray ( ) ;
77+
78+ return $ "The test is skipped because it is configured to not run on the current operating system: `{ string . Join ( "`, `" , excludedOperatingSystems ) } `";
79+ }
80+ }
0 commit comments