From 3263e0d78bb0cd5e7d279347ae9aae18bddc6532 Mon Sep 17 00:00:00 2001 From: Michael Render Date: Thu, 9 Jan 2025 00:15:28 -0500 Subject: [PATCH] [dotnet] Fix current version of `IgnoreTargetAttribute` in test --- .../IgnoreTargetAttribute.cs | 83 ++++++++----------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs b/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs index a3b71df30d12d..152e71cc946bf 100644 --- a/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs +++ b/dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs @@ -22,89 +22,74 @@ using NUnit.Framework.Internal; using OpenQA.Selenium.Environment; using System; -using System.Collections.Generic; +#nullable enable namespace OpenQA.Selenium { [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest { - private readonly String target; - private readonly string ignoreReason = string.Empty; - public IgnoreTargetAttribute(string target) { - this.target = target.ToLower(); + this.Value = target.ToLower(); } public IgnoreTargetAttribute(string target, string reason) : this(target) { - this.ignoreReason = reason; + this.Reason = reason; } - public string Value - { - get { return target; } - } + public string Value { get; } - public string Reason - { - get { return ignoreReason; } - } + public string Reason { get; } = string.Empty; public void ApplyToTest(Test test) { - if (test.RunState != RunState.NotRunnable) + if (test.RunState is RunState.NotRunnable) { - List ignoreAttributes = new List(); - if (test.IsSuite) - { - Attribute[] ignoreClassAttributes = - test.TypeInfo.GetCustomAttributes(true); - if (ignoreClassAttributes.Length > 0) - { - ignoreAttributes.AddRange(ignoreClassAttributes); - } - } - else + return; + } + IgnoreTargetAttribute[] ignoreAttributes; + if (test.IsSuite) + { + ignoreAttributes = test.TypeInfo!.GetCustomAttributes(true); + } + else + { + ignoreAttributes = test.Method!.GetCustomAttributes(true); + } + + foreach (IgnoreTargetAttribute platformToIgnoreAttr in ignoreAttributes) + { + if (IgnoreTestForPlatform(platformToIgnoreAttr.Value)) { - IgnoreTargetAttribute[] ignoreMethodAttributes = - test.Method.GetCustomAttributes(true); - if (ignoreMethodAttributes.Length > 0) + string ignoreReason = $"Ignoring target {EnvironmentManager.Instance.Browser}"; + if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason)) { - ignoreAttributes.AddRange(ignoreMethodAttributes); + ignoreReason = ignoreReason + ": " + platformToIgnoreAttr.Reason; } - } - foreach (Attribute attr in ignoreAttributes) - { - IgnoreTargetAttribute platformToIgnoreAttr = attr as IgnoreTargetAttribute; - if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value)) - { - string ignoreReason = - "Ignoring target " + EnvironmentManager.Instance.Browser.ToString() + "."; - if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason)) - { - ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason; - } + test.RunState = RunState.Ignored; + test.Properties.Set(PropertyNames.SkipReason, ignoreReason); - test.RunState = RunState.Ignored; - test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason); - } } } } - private bool IgnoreTestForPlatform(string platformToIgnore) + private static bool IgnoreTestForPlatform(string platformToIgnore) { - return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform()); + return CurrentPlatform().Equals(platformToIgnore, StringComparison.OrdinalIgnoreCase); } - private string CurrentPlatform() + private static string CurrentPlatform() { - return "net6"; +#if NET8_0 + return "net8"; +#else +#error Update IgnoreTargetAttribute.CurrentPlatform to the current TFM +#endif } } }