Skip to content
17 changes: 16 additions & 1 deletion src/tests/Common/XUnitWrapperGenerator/ITestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,18 @@ public ConditionalTest(ITestInfo innerTest, string condition)

_innerTest = innerTest;
_condition = condition;
}
}

public ConditionalTest(ITestInfo innerTest, Xunit.TestPlatforms platform)
: this(innerTest, GetPlatformConditionFromTestPlatform(platform))
{
}

public ConditionalTest(ITestInfo innerTest, string condition, Xunit.TestPlatforms platform)
: this(innerTest, $"{(condition.Length == 0 ? "true" : condition)} && ({GetPlatformConditionFromTestPlatform(platform)})")
{
}

public string TestNameExpression { get; }
public string DisplayNameForFiltering { get; }
public string Method { get; }
Expand Down Expand Up @@ -207,6 +212,16 @@ private static string GetPlatformConditionFromTestPlatform(Xunit.TestPlatforms p
{
List<string> platformCheckConditions = new();

if (platform == Xunit.TestPlatforms.Any)
{
return "true";
}

if (platform == 0)
{
return "false";
}

if (platform.HasFlag(Xunit.TestPlatforms.Windows))
{
platformCheckConditions.Add("global::System.OperatingSystem.IsWindows()");
Expand Down
65 changes: 32 additions & 33 deletions src/tests/Common/XUnitWrapperGenerator/XUnitWrapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,9 @@ private static IEnumerable<ITestInfo> GetTestMethodInfosForMethod(IMethodSymbol
continue;
}

Xunit.TestPlatforms skippedTestPlatforms = 0;
Xunit.RuntimeConfiguration skippedConfigurations = 0;
Xunit.RuntimeTestModes skippedTestModes = 0;
Xunit.TestPlatforms skippedTestPlatforms = Xunit.TestPlatforms.Any;
Xunit.RuntimeConfiguration skippedConfigurations = Xunit.RuntimeConfiguration.Any;
Xunit.RuntimeTestModes skippedTestModes = Xunit.RuntimeTestModes.Any;

for (int i = 1; i < filterAttribute.ConstructorArguments.Length; i++)
{
Expand All @@ -875,12 +875,13 @@ void ReadSkippedInformationFromSkipOnCoreClrAttributeArgument(TypedConstant argu
}
}

if (skippedTestModes == Xunit.RuntimeTestModes.Any)
if (skippedTestModes == Xunit.RuntimeTestModes.Any
&& skippedConfigurations == Xunit.RuntimeConfiguration.Any
&& skippedTestPlatforms == Xunit.TestPlatforms.Any)
{
testInfos = FilterForSkippedRuntime(testInfos, (int)Xunit.TestRuntimes.CoreCLR, options);
}
testInfos = DecorateWithSkipOnPlatform(testInfos, (int)skippedTestPlatforms, options);
testInfos = DecorateWithSkipOnCoreClrConfiguration(testInfos, skippedTestModes, skippedConfigurations);
testInfos = DecorateWithSkipOnCoreClrConfiguration(testInfos, skippedTestModes, skippedConfigurations, skippedTestPlatforms, options);

break;
}
Expand All @@ -889,15 +890,10 @@ void ReadSkippedInformationFromSkipOnCoreClrAttributeArgument(TypedConstant argu
return testInfos;
}

private static ImmutableArray<ITestInfo> DecorateWithSkipOnCoreClrConfiguration(ImmutableArray<ITestInfo> testInfos, Xunit.RuntimeTestModes skippedTestModes, Xunit.RuntimeConfiguration skippedConfigurations)
private static ImmutableArray<ITestInfo> DecorateWithSkipOnCoreClrConfiguration(ImmutableArray<ITestInfo> testInfos, Xunit.RuntimeTestModes skippedTestModes, Xunit.RuntimeConfiguration skippedConfigurations, Xunit.TestPlatforms skippedTestPlatforms, AnalyzerConfigOptionsProvider options)
{
const string ConditionClass = "TestLibrary.CoreClrConfigurationDetection";
List<string> conditions = new();
if (skippedConfigurations.HasFlag(Xunit.RuntimeConfiguration.Debug | Xunit.RuntimeConfiguration.Checked | Xunit.RuntimeConfiguration.Release))
{
// If all configurations are skipped, just skip the test as a whole
return ImmutableArray<ITestInfo>.Empty;
}

if (skippedConfigurations.HasFlag(Xunit.RuntimeConfiguration.Debug))
{
Expand Down Expand Up @@ -961,7 +957,10 @@ private static ImmutableArray<ITestInfo> DecorateWithSkipOnCoreClrConfiguration(
conditions.Add($"!{ConditionClass}.IsGCStressC");
}

return ImmutableArray.CreateRange<ITestInfo>(testInfos.Select(t => new ConditionalTest(t, string.Join(" && ", conditions))));
options.GlobalOptions.TryGetValue("build_property.TargetOS", out string? targetOS);
Xunit.TestPlatforms targetPlatform = GetPlatformForTargetOS(targetOS);

return ImmutableArray.CreateRange<ITestInfo>(testInfos.Select(t => new ConditionalTest(t, string.Join(" && ", conditions), targetPlatform & ~skippedTestPlatforms)));
}

private static ImmutableArray<ITestInfo> FilterForSkippedTargetFrameworkMonikers(ImmutableArray<ITestInfo> testInfos, int v)
Expand Down Expand Up @@ -1079,28 +1078,28 @@ private static ImmutableArray<ITestInfo> DecorateWithSkipOnPlatform(ImmutableArr
// The target platform is not mentioned in the attribute, just run it as-is.
return testInfos;
}
}

static Xunit.TestPlatforms GetPlatformForTargetOS(string? targetOS)
private static Xunit.TestPlatforms GetPlatformForTargetOS(string? targetOS)
{
return targetOS?.ToLowerInvariant() switch
{
return targetOS?.ToLowerInvariant() switch
{
"windows" => Xunit.TestPlatforms.Windows,
"linux" => Xunit.TestPlatforms.Linux,
"osx" => Xunit.TestPlatforms.OSX,
"illumos" => Xunit.TestPlatforms.illumos,
"solaris" => Xunit.TestPlatforms.Solaris,
"android" => Xunit.TestPlatforms.Android,
"ios" => Xunit.TestPlatforms.iOS,
"tvos" => Xunit.TestPlatforms.tvOS,
"maccatalyst" => Xunit.TestPlatforms.MacCatalyst,
"browser" => Xunit.TestPlatforms.Browser,
"wasi" => Xunit.TestPlatforms.Wasi,
"freebsd" => Xunit.TestPlatforms.FreeBSD,
"netbsd" => Xunit.TestPlatforms.NetBSD,
null or "" or "anyos" => Xunit.TestPlatforms.Any,
_ => 0
};
}
"windows" => Xunit.TestPlatforms.Windows,
"linux" => Xunit.TestPlatforms.Linux,
"osx" => Xunit.TestPlatforms.OSX,
"illumos" => Xunit.TestPlatforms.illumos,
"solaris" => Xunit.TestPlatforms.Solaris,
"android" => Xunit.TestPlatforms.Android,
"ios" => Xunit.TestPlatforms.iOS,
"tvos" => Xunit.TestPlatforms.tvOS,
"maccatalyst" => Xunit.TestPlatforms.MacCatalyst,
"browser" => Xunit.TestPlatforms.Browser,
"wasi" => Xunit.TestPlatforms.Wasi,
"freebsd" => Xunit.TestPlatforms.FreeBSD,
"netbsd" => Xunit.TestPlatforms.NetBSD,
null or "" or "anyos" => Xunit.TestPlatforms.Any,
_ => 0
};
}

private static ImmutableArray<ITestInfo> DecorateWithUserDefinedCondition(
Expand Down
Loading