File: XUnit\ConditionalFactAttribute.cs
Web Access
Project: ..\..\..\test\dotnet-format.UnitTests\dotnet-format.UnitTests.csproj (dotnet-format.UnitTests)
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the MIT license.  See License.txt in the project root for license information.
 
namespace Microsoft.CodeAnalysis.Tools.Tests.XUnit
{
    public class ConditionalFactAttribute : FactAttribute
    {
        /// <summary>
        /// This property exists to prevent users of ConditionalFact from accidentally putting documentation
        /// in the Skip property instead of Reason. Putting it into Skip would cause the test to be unconditionally
        /// skipped vs. conditionally skipped which is the entire point of this attribute.
        /// </summary>
        [Obsolete("ConditionalFact should use Reason or AlwaysSkip", error: true)]
        public new string Skip
        {
            get => base.Skip;
            set => base.Skip = value;
        }
 
        /// <summary>
        /// Used to unconditionally Skip a test. For the rare occasion when a conditional test needs to be
        /// unconditionally skipped (typically short term for a bug to be fixed).
        /// </summary>
        public string AlwaysSkip
        {
            get => base.Skip;
            set => base.Skip = value;
        }
 
        public string? Reason { get; set; }
 
        public ConditionalFactAttribute(params Type[] skipConditions)
        {
            foreach (var skipCondition in skipConditions)
            {
                var condition = (ExecutionCondition?)Activator.CreateInstance(skipCondition);
                if (condition?.ShouldSkip == true)
                {
                    base.Skip = Reason ?? condition.SkipReason;
                    break;
                }
            }
        }
    }
 
    public class ConditionalTheoryAttribute : TheoryAttribute
    {
        /// <summary>
        /// This property exists to prevent users of ConditionalFact from accidentally putting documentation
        /// in the Skip property instead of Reason. Putting it into Skip would cause the test to be unconditionally
        /// skipped vs. conditionally skipped which is the entire point of this attribute.
        /// </summary>
        [Obsolete("ConditionalTheory should use Reason or AlwaysSkip")]
        public new string Skip
        {
            get => base.Skip;
            set => base.Skip = value;
        }
 
        /// <summary>
        /// Used to unconditionally Skip a test. For the rare occasion when a conditional test needs to be
        /// unconditionally skipped (typically short term for a bug to be fixed).
        /// </summary>
        public string AlwaysSkip
        {
            get => base.Skip;
            set => base.Skip = value;
        }
 
        public string? Reason { get; set; }
 
        public ConditionalTheoryAttribute(params Type[] skipConditions)
        {
            foreach (var skipCondition in skipConditions)
            {
                var condition = (ExecutionCondition?)Activator.CreateInstance(skipCondition);
                if (condition?.ShouldSkip == true)
                {
                    base.Skip = Reason ?? condition.SkipReason;
                    break;
                }
            }
        }
    }
 
    public abstract class ExecutionCondition
    {
        public abstract bool ShouldSkip { get; }
        public abstract string SkipReason { get; }
    }
 
    public static class ExecutionConditionUtil
    {
        public static bool IsWindows => Path.DirectorySeparatorChar == '\\';
        public static bool IsMacOS => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
        public static bool IsLinux => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
    }
 
    public class WindowsOnly : ExecutionCondition
    {
        public override bool ShouldSkip => !ExecutionConditionUtil.IsWindows;
        public override string SkipReason => "Test not supported on Mac and Linux";
    }
}