Skip to content

Commit

Permalink
[dotnet] add test attributes to guard for platforms and targets
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Nov 29, 2022
1 parent 8fb8fd7 commit b3c3903
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
116 changes: 116 additions & 0 deletions dotnet/test/common/CustomTestAttributes/IgnorePlatformAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using NUnit.Framework.Internal;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using OpenQA.Selenium.Environment;

#if !NET45 && !NET46 && !NET47
using System.Runtime.InteropServices;
using OSPlatform = System.Runtime.InteropServices.OSPlatform;
#endif


namespace OpenQA.Selenium
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class IgnorePlatformAttribute : NUnitAttribute, IApplyToTest
{
private readonly String platform;
private readonly string ignoreReason = string.Empty;

public IgnorePlatformAttribute(string platform)
{
this.platform = platform.ToLower();
}

public IgnorePlatformAttribute(string platform, string reason)
: this(platform)
{
this.ignoreReason = reason;
}

public string Value
{
get { return platform; }
}

public string Reason
{
get { return ignoreReason; }
}

public void ApplyToTest(Test test)
{
if (test.RunState != RunState.NotRunnable)
{
List<Attribute> ignoreAttributes = new List<Attribute>();
if (test.IsSuite)
{
Attribute[] ignoreClassAttributes =
test.TypeInfo.GetCustomAttributes<IgnorePlatformAttribute>(true);
if (ignoreClassAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreClassAttributes);
}
}
else
{
IgnorePlatformAttribute[] ignoreMethodAttributes =
test.Method.GetCustomAttributes<IgnorePlatformAttribute>(true);
if (ignoreMethodAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreMethodAttributes);
}
}

foreach (Attribute attr in ignoreAttributes)
{
IgnorePlatformAttribute platformToIgnoreAttr = attr as IgnorePlatformAttribute;
if (platformToIgnoreAttr != null && IgnoreTestForPlatform(platformToIgnoreAttr.Value))
{
string ignoreReason =
"Ignoring platform " + EnvironmentManager.Instance.Browser.ToString() + ".";
if (!string.IsNullOrEmpty(platformToIgnoreAttr.Reason))
{
ignoreReason = ignoreReason + " " + platformToIgnoreAttr.Reason;
}

test.RunState = RunState.Ignored;
test.Properties.Set(PropertyNames.SkipReason, platformToIgnoreAttr.Reason);
}
}
}
}

private bool IgnoreTestForPlatform(string platformToIgnore)
{
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
}

private string CurrentPlatform()
{
#if NET45 || NET46 || NET47
return null;
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "windows";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return "linux";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "mac";
}
else
{
throw new WebDriverException("Selenium Manager did not find supported operating system");
}
#endif
}
}
}
100 changes: 100 additions & 0 deletions dotnet/test/common/CustomTestAttributes/IgnoreTargetAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using System;
using NUnit.Framework.Internal;
using System.Collections.Generic;
using OpenQA.Selenium.Environment;


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();
}

public IgnoreTargetAttribute(string target, string reason)
: this(target)
{
this.ignoreReason = reason;
}

public string Value
{
get { return target; }
}

public string Reason
{
get { return ignoreReason; }
}

public void ApplyToTest(Test test)
{
if (test.RunState != 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
{
IgnoreTargetAttribute[] ignoreMethodAttributes =
test.Method.GetCustomAttributes<IgnoreTargetAttribute>(true);
if (ignoreMethodAttributes.Length > 0)
{
ignoreAttributes.AddRange(ignoreMethodAttributes);
}
}

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, platformToIgnoreAttr.Reason);
}
}
}
}

private bool IgnoreTestForPlatform(string platformToIgnore)
{
return CurrentPlatform() != null && platformToIgnore.Equals(CurrentPlatform());
}

private string CurrentPlatform()
{
#if NET6_0
return "net6";
#endif
#if NETCOREAPP3_1
return "netcore3";
#endif
#if NET48
return "net48";
#endif
return null;
}
}
}

0 comments on commit b3c3903

Please sign in to comment.