Skip to content

Commit 85b8164

Browse files
[dotnet] Fix current version of IgnoreTargetAttribute in test suite (#15051)
1 parent 398a082 commit 85b8164

File tree

1 file changed

+34
-49
lines changed

1 file changed

+34
-49
lines changed

dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs

+34-49
Original file line numberDiff line numberDiff line change
@@ -22,89 +22,74 @@
2222
using NUnit.Framework.Internal;
2323
using OpenQA.Selenium.Environment;
2424
using System;
25-
using System.Collections.Generic;
2625

26+
#nullable enable
2727

2828
namespace OpenQA.Selenium
2929
{
3030
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
3131
public class IgnoreTargetAttribute : NUnitAttribute, IApplyToTest
3232
{
33-
private readonly String target;
34-
private readonly string ignoreReason = string.Empty;
35-
3633
public IgnoreTargetAttribute(string target)
3734
{
38-
this.target = target.ToLower();
35+
this.Value = target.ToLower();
3936
}
4037

4138
public IgnoreTargetAttribute(string target, string reason)
4239
: this(target)
4340
{
44-
this.ignoreReason = reason;
41+
this.Reason = reason;
4542
}
4643

47-
public string Value
48-
{
49-
get { return target; }
50-
}
44+
public string Value { get; }
5145

52-
public string Reason
53-
{
54-
get { return ignoreReason; }
55-
}
46+
public string Reason { get; } = string.Empty;
5647

5748
public void ApplyToTest(Test test)
5849
{
59-
if (test.RunState != RunState.NotRunnable)
50+
if (test.RunState is RunState.NotRunnable)
6051
{
61-
List<Attribute> ignoreAttributes = new List<Attribute>();
62-
if (test.IsSuite)
63-
{
64-
Attribute[] ignoreClassAttributes =
65-
test.TypeInfo.GetCustomAttributes<IgnoreTargetAttribute>(true);
66-
if (ignoreClassAttributes.Length > 0)
67-
{
68-
ignoreAttributes.AddRange(ignoreClassAttributes);
69-
}
70-
}
71-
else
52+
return;
53+
}
54+
IgnoreTargetAttribute[] ignoreAttributes;
55+
if (test.IsSuite)
56+
{
57+
ignoreAttributes = test.TypeInfo!.GetCustomAttributes<IgnoreTargetAttribute>(true);
58+
}
59+
else
60+
{
61+
ignoreAttributes = test.Method!.GetCustomAttributes<IgnoreTargetAttribute>(true);
62+
}
63+
64+
foreach (IgnoreTargetAttribute platformToIgnoreAttr in ignoreAttributes)
65+
{
66+
if (IgnoreTestForPlatform(platformToIgnoreAttr.Value))
7267
{
73-
IgnoreTargetAttribute[] ignoreMethodAttributes =
74-
test.Method.GetCustomAttributes<IgnoreTargetAttribute>(true);
75-
if (ignoreMethodAttributes.Length > 0)
68+
string ignoreReason = $"Ignoring target {EnvironmentManager.Instance.Browser}";
69+
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
7670
{
77-
ignoreAttributes.AddRange(ignoreMethodAttributes);
71+
ignoreReason = ignoreReason + ": " + platformToIgnoreAttr.Reason;
7872
}
79-
}
8073

81-
foreach (Attribute attr in ignoreAttributes)
82-
{
83-
IgnoreTargetAttribute platformToIgnoreAttr = attr as IgnoreTargetAttribute;
84-
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
85-
{
86-
string ignoreReason =
87-
"Ignoring target " + EnvironmentManager.Instance.Browser.ToString() + ".";
88-
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
89-
{
90-
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
91-
}
74+
test.RunState = RunState.Ignored;
75+
test.Properties.Set(PropertyNames.SkipReason, ignoreReason);
9276

93-
test.RunState = RunState.Ignored;
94-
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
95-
}
9677
}
9778
}
9879
}
9980

100-
private bool IgnoreTestForPlatform(string platformToIgnore)
81+
private static bool IgnoreTestForPlatform(string platformToIgnore)
10182
{
102-
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
83+
return CurrentPlatform().Equals(platformToIgnore, StringComparison.OrdinalIgnoreCase);
10384
}
10485

105-
private string CurrentPlatform()
86+
private static string CurrentPlatform()
10687
{
107-
return "net6";
88+
#if NET8_0
89+
return "net8";
90+
#else
91+
#error Update IgnoreTargetAttribute.CurrentPlatform to the current TFM
92+
#endif
10893
}
10994
}
11095
}

0 commit comments

Comments
 (0)