-
Notifications
You must be signed in to change notification settings - Fork 388
Add ConditionalAssemblyAttribute #16820
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
Open
Copilot
wants to merge
5
commits into
main
Choose a base branch
from
copilot/add-conditionalassemblyattribute-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e31aacf
Add ConditionalAssemblyAttribute and tests for xunit v3
Copilot ae71aca
Move ConditionalAssemblyAttribute to V3 src and add end-to-end test p…
Copilot 224172d
Also use additional arguments for Helix
AndriySvyryd c126c87
Register new V3 test project for Helix in tests/UnitTests.proj
Copilot adb912d
Merge branch 'main' into copilot/add-conditionalassemblyattribute-tests
AndriySvyryd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Microsoft.DotNet.XUnitV3Extensions/src/ConditionalAssemblyAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Microsoft.DotNet.XUnitExtensions; | ||
| using Xunit.Sdk; | ||
|
|
||
| namespace Xunit | ||
| { | ||
| /// <summary> | ||
| /// An assembly-level attribute that conditionally marks all tests in the assembly to be skipped | ||
| /// based on the evaluation of one or more static boolean members. When any of the referenced | ||
| /// condition members evaluates to <c>false</c>, the attribute contributes a <c>category=failing</c> | ||
| /// trait so that the test runner can exclude the affected tests. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
| public sealed class ConditionalAssemblyAttribute : Attribute, ITraitAttribute | ||
| { | ||
| [DynamicallyAccessedMembers(StaticReflectionConstants.ConditionalMemberKinds)] | ||
| public Type CalleeType { get; private set; } | ||
| public string[] ConditionMemberNames { get; private set; } | ||
|
|
||
| public ConditionalAssemblyAttribute( | ||
| [DynamicallyAccessedMembers(StaticReflectionConstants.ConditionalMemberKinds)] | ||
| Type calleeType, | ||
| params string[] conditionMemberNames) | ||
| { | ||
| CalleeType = calleeType; | ||
| ConditionMemberNames = conditionMemberNames; | ||
| } | ||
|
|
||
| public IReadOnlyCollection<KeyValuePair<string, string>> GetTraits() | ||
| { | ||
| // If evaluated to false, skip all tests in the assembly. | ||
| if (!EvaluateParameterHelper()) | ||
| { | ||
| return [new KeyValuePair<string, string>(XunitConstants.Category, XunitConstants.Failing)]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| internal bool EvaluateParameterHelper() | ||
| { | ||
| Type calleeType = null; | ||
| string[] conditionMemberNames = null; | ||
|
|
||
| if (ConditionalTestDiscoverer.CheckInputToSkipExecution([CalleeType, ConditionMemberNames], ref calleeType, ref conditionMemberNames)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| return DiscovererHelpers.Evaluate(calleeType, conditionMemberNames); | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/Microsoft.DotNet.XUnitV3Extensions/tests/AlwaysFalseConditionalAssemblyTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Xunit; | ||
|
|
||
| // The assembly-level ConditionalAssembly attribute below references a condition member | ||
| // that always returns false. As a result, every test in this assembly is tagged with the | ||
| // "category=failing" trait, and the test runner is configured (via the project's | ||
| // TestRunnerAdditionalArguments) to skip tests with that trait. If the attribute were | ||
| // ever broken and stopped contributing the trait, the deliberately failing test below | ||
| // would run and fail the build, catching the regression. | ||
| [assembly: ConditionalAssembly(typeof(Microsoft.DotNet.XUnitV3Extensions.AlwaysFalseConditionalAssemblyTests.Conditions), | ||
| nameof(Microsoft.DotNet.XUnitV3Extensions.AlwaysFalseConditionalAssemblyTests.Conditions.AlwaysFalse))] | ||
|
|
||
| namespace Microsoft.DotNet.XUnitV3Extensions.AlwaysFalseConditionalAssemblyTests | ||
| { | ||
| public static class Conditions | ||
| { | ||
| public static bool AlwaysFalse => false; | ||
| } | ||
|
|
||
| public class FailingTests | ||
| { | ||
| [Fact] | ||
| public void AlwaysFails() | ||
| { | ||
| Assert.Fail("This test is expected to be skipped via [assembly: ConditionalAssembly]."); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...ions/tests/Microsoft.DotNet.XUnitV3Extensions.AlwaysFalseConditionalAssembly.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(BundledNETCoreAppTargetFramework)</TargetFramework> | ||
| <TestRunnerName>XUnitV3</TestRunnerName> | ||
| <OutputType>Exe</OutputType> | ||
| <!-- | ||
| The assembly-level [ConditionalAssembly] in this project always evaluates to false, | ||
| which tags every test in the assembly with the 'category=failing' trait. Filter out | ||
| tests with that trait so the deliberately failing test is skipped, which validates | ||
| that ConditionalAssemblyAttribute is applied to all tests in the assembly. After the | ||
| filter, no tests run, so ignore the MTP "zero tests" exit code (8). | ||
|
|
||
| Keep the argument string in one property and surface it through both | ||
| TestRunnerAdditionalArguments and Arguments so it applies to direct execution and | ||
| to Helix XUnitV3 work item creation paths that only propagate Arguments. | ||
| --> | ||
| <ConditionalAssemblyFilterArguments>--filter-not-trait "category=failing" --ignore-exit-code 8</ConditionalAssemblyFilterArguments> | ||
| <TestRunnerAdditionalArguments>$(ConditionalAssemblyFilterArguments)</TestRunnerAdditionalArguments> | ||
| <Arguments>$(ConditionalAssemblyFilterArguments)</Arguments> | ||
| <!-- | ||
| This project lives alongside Microsoft.DotNet.XUnitV3Extensions.Tests.csproj in the | ||
| same folder. Restrict default compile globbing to just this project's source so the | ||
| assembly-level attribute and failing test don't bleed into the sibling project. | ||
| --> | ||
| <EnableDefaultCompileItems>false</EnableDefaultCompileItems> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="AlwaysFalseConditionalAssemblyTests.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\src\Microsoft.DotNet.XUnitV3Extensions.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.