Skip to content

[dotnet] Fix current version of IgnoreTargetAttribute in test suite #15051

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

Merged
Merged
Changes from all 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
83 changes: 34 additions & 49 deletions dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attribute> ignoreAttributes = new List<Attribute>();
if (test.IsSuite)
{
Attribute[] ignoreClassAttributes =
test.TypeInfo.GetCustomAttributes<IgnoreTargetAttribute>(true);
if (ignoreClassAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreClassAttributes);
}
}
else
return;
}
IgnoreTargetAttribute[] ignoreAttributes;
if (test.IsSuite)
{
ignoreAttributes = test.TypeInfo!.GetCustomAttributes<IgnoreTargetAttribute>(true);
}
else
{
ignoreAttributes = test.Method!.GetCustomAttributes<IgnoreTargetAttribute>(true);
}

foreach (IgnoreTargetAttribute platformToIgnoreAttr in ignoreAttributes)
{
if (IgnoreTestForPlatform(platformToIgnoreAttr.Value))
{
IgnoreTargetAttribute[] ignoreMethodAttributes =
test.Method.GetCustomAttributes<IgnoreTargetAttribute>(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
}
}
}
Loading