-
Notifications
You must be signed in to change notification settings - Fork 310
Add ArchitectureConditionAttribute to gate tests by process architecture #9233
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
Amaury Levé (Evangelink)
merged 9 commits into
microsoft:main
from
Evangelink:dev/architecture-condition-attribute
Jun 19, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fac0172
Add ArchitectureConditionAttribute to gate tests by process architecture
Evangelink 420a44c
Address review: rename enum to TestArchitectures, fix net462 detectio…
Evangelink d26906e
Make ArchitectureCondition .NET-only, drop net462 custom detection
Evangelink 7ea3bb8
Address review feedback on ArchitectureCondition
3e0658d
Gate RiscV64 behind NET9_0_OR_GREATER and use named Architecture members
Evangelink 35400be
Address review feedback: reword RiscV64 comment and harden GetCurrent…
Evangelink 66df8f9
Merge remote-tracking branch 'origin/main' into fix/9233-merge-main
Copilot 4146e29
Restore TerminalResources translations wiped by OneLocBuild (#9248)
Copilot 5470b5b
Merge remote-tracking branch 'origin/main' into dev/architecture-cond…
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/TestFramework/TestFramework/Attributes/TestMethod/ArchitectureConditionAttribute.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,87 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| #if NET | ||
| /// <summary> | ||
| /// This attribute is used to ignore a test class or a test method based on the current process architecture. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This attribute isn't inherited. Applying it to a base class will not cause derived classes to be ignored. | ||
| /// It can be combined with <see cref="OSConditionAttribute"/> (or any other condition attribute) to gate a test | ||
| /// on both the operating system and the process architecture, because condition attributes that expose a different | ||
| /// <see cref="ConditionBaseAttribute.GroupName"/> are combined with a logical AND. | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false)] | ||
| public sealed class ArchitectureConditionAttribute : ConditionBaseAttribute | ||
| { | ||
| private readonly TestArchitectures _architectures; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ArchitectureConditionAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="mode">Decides whether the architectures will be included or excluded.</param> | ||
| /// <param name="architectures">The process architectures that this test includes/excludes.</param> | ||
| public ArchitectureConditionAttribute(ConditionMode mode, TestArchitectures architectures) | ||
| : base(mode) | ||
| { | ||
| _architectures = architectures; | ||
| IgnoreMessage = mode == ConditionMode.Include | ||
| ? $"Test is only supported on {architectures}" | ||
| : $"Test is not supported on {architectures}"; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ArchitectureConditionAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="architectures">The process architectures that this test supports.</param> | ||
| public ArchitectureConditionAttribute(TestArchitectures architectures) | ||
| : this(ConditionMode.Include, architectures) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether the test method or test class should be ignored. | ||
| /// </summary> | ||
| public override bool IsConditionMet | ||
| { | ||
| get | ||
| { | ||
| TestArchitectures current = MapArchitecture(RuntimeInformation.ProcessArchitecture); | ||
| return (_architectures & current) != 0; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the group name for this attribute. | ||
| /// </summary> | ||
| public override string GroupName => "ArchitectureCondition"; | ||
|
|
||
| /// <summary> | ||
| /// Maps a <see cref="Architecture"/> value to the matching <see cref="TestArchitectures"/> flag. | ||
| /// </summary> | ||
| /// <param name="architecture">The current process <see cref="Architecture"/>.</param> | ||
| /// <returns>The matching <see cref="TestArchitectures"/> flag.</returns> | ||
| private static TestArchitectures MapArchitecture(Architecture architecture) | ||
| => architecture switch | ||
| { | ||
| Architecture.X86 => TestArchitectures.X86, | ||
| Architecture.X64 => TestArchitectures.X64, | ||
| Architecture.Arm => TestArchitectures.Arm, | ||
| Architecture.Arm64 => TestArchitectures.Arm64, | ||
| Architecture.Wasm => TestArchitectures.Wasm, | ||
| Architecture.S390x => TestArchitectures.S390x, | ||
| Architecture.LoongArch64 => TestArchitectures.LoongArch64, | ||
| Architecture.Armv6 => TestArchitectures.Armv6, | ||
| Architecture.Ppc64le => TestArchitectures.Ppc64le, | ||
| #if NET9_0_OR_GREATER | ||
| // Architecture.RiscV64 was added in .NET 9 and isn't part of the net8.0 ref assembly, so both the | ||
| // enum member and the mapping are compiled only when targeting net9.0+. When this assembly runs on an | ||
| // older runtime the value can't be reported; any other unknown value falls through to the throw below. | ||
| Architecture.RiscV64 => TestArchitectures.RiscV64, | ||
| #endif | ||
| _ => throw new ArgumentOutOfRangeException(nameof(architecture), architecture, null), | ||
| }; | ||
|
Evangelink marked this conversation as resolved.
Comment on lines
+66
to
+85
|
||
| } | ||
| #endif | ||
65 changes: 65 additions & 0 deletions
65
src/TestFramework/TestFramework/Attributes/TestMethod/TestArchitectures.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,65 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| #if NET | ||
| /// <summary> | ||
| /// An enum that is used with <see cref="ArchitectureConditionAttribute"/> to control which process architectures a test method or test class supports or doesn't support. | ||
| /// </summary> | ||
| [Flags] | ||
|
Evangelink marked this conversation as resolved.
|
||
| public enum TestArchitectures | ||
| { | ||
|
Evangelink marked this conversation as resolved.
|
||
| /// <summary> | ||
| /// Represents the x86 architecture. | ||
| /// </summary> | ||
| X86 = 1 << 0, | ||
|
|
||
| /// <summary> | ||
| /// Represents the x64 architecture. | ||
| /// </summary> | ||
| X64 = 1 << 1, | ||
|
|
||
| /// <summary> | ||
| /// Represents the ARM architecture. | ||
| /// </summary> | ||
| Arm = 1 << 2, | ||
|
|
||
| /// <summary> | ||
| /// Represents the ARM64 architecture. | ||
| /// </summary> | ||
| Arm64 = 1 << 3, | ||
|
|
||
| /// <summary> | ||
| /// Represents the WebAssembly platform. | ||
| /// </summary> | ||
| Wasm = 1 << 4, | ||
|
|
||
| /// <summary> | ||
| /// Represents the S390x architecture. | ||
| /// </summary> | ||
| S390x = 1 << 5, | ||
|
|
||
| /// <summary> | ||
| /// Represents the LoongArch64 architecture. | ||
| /// </summary> | ||
| LoongArch64 = 1 << 6, | ||
|
|
||
| /// <summary> | ||
| /// Represents the 32-bit ARMv6 architecture. | ||
| /// </summary> | ||
| Armv6 = 1 << 7, | ||
|
|
||
| /// <summary> | ||
| /// Represents the 64-bit PowerPC little-endian architecture. | ||
| /// </summary> | ||
| Ppc64le = 1 << 8, | ||
|
|
||
| #if NET9_0_OR_GREATER | ||
| /// <summary> | ||
| /// Represents the 64-bit RISC-V architecture. | ||
| /// </summary> | ||
| RiscV64 = 1 << 9, | ||
| #endif | ||
| } | ||
| #endif | ||
15 changes: 15 additions & 0 deletions
15
src/TestFramework/TestFramework/PublicAPI/net/PublicAPI.Unshipped.txt
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
1 change: 1 addition & 0 deletions
1
src/TestFramework/TestFramework/PublicAPI/net9.0/PublicAPI.Shipped.txt
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 @@ | ||
| #nullable enable |
2 changes: 2 additions & 0 deletions
2
src/TestFramework/TestFramework/PublicAPI/net9.0/PublicAPI.Unshipped.txt
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,2 @@ | ||
| #nullable enable | ||
| Microsoft.VisualStudio.TestTools.UnitTesting.TestArchitectures.RiscV64 = 512 -> Microsoft.VisualStudio.TestTools.UnitTesting.TestArchitectures |
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
116 changes: 116 additions & 0 deletions
116
test/UnitTests/TestFramework.UnitTests/Attributes/ArchitectureConditionAttributeTests.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,116 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #if NET | ||
| using AwesomeAssertions; | ||
|
Evangelink marked this conversation as resolved.
|
||
|
|
||
| using TestFramework.ForTestingMSTest; | ||
|
|
||
| namespace UnitTestFramework.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Tests for class ArchitectureConditionAttribute. | ||
| /// </summary> | ||
| public class ArchitectureConditionAttributeTests : TestContainer | ||
| { | ||
| private const TestArchitectures AllArchitectures = | ||
| TestArchitectures.X86 | TestArchitectures.X64 | TestArchitectures.Arm | TestArchitectures.Arm64 | TestArchitectures.Wasm | ||
| | TestArchitectures.S390x | TestArchitectures.LoongArch64 | TestArchitectures.Armv6 | TestArchitectures.Ppc64le | ||
| #if NET9_0_OR_GREATER | ||
| | TestArchitectures.RiscV64 | ||
| #endif | ||
| ; | ||
|
|
||
| public void Constructor_SetsCorrectMode() | ||
| { | ||
| // Act | ||
| var includeAttribute = new ArchitectureConditionAttribute(ConditionMode.Include, TestArchitectures.X64); | ||
| var excludeAttribute = new ArchitectureConditionAttribute(ConditionMode.Exclude, TestArchitectures.X64); | ||
|
|
||
| // Assert | ||
| includeAttribute.Mode.Should().Be(ConditionMode.Include); | ||
| excludeAttribute.Mode.Should().Be(ConditionMode.Exclude); | ||
| } | ||
|
|
||
| public void Constructor_SingleArgument_DefaultsToIncludeMode() | ||
| { | ||
| // Act | ||
| var attribute = new ArchitectureConditionAttribute(TestArchitectures.X64); | ||
|
|
||
| // Assert | ||
| attribute.Mode.Should().Be(ConditionMode.Include); | ||
| } | ||
|
|
||
| public void GroupName_ReturnsCorrectValue() | ||
| { | ||
| // Arrange | ||
| var attribute = new ArchitectureConditionAttribute(TestArchitectures.X64); | ||
|
|
||
| // Act & Assert | ||
| attribute.GroupName.Should().Be("ArchitectureCondition"); | ||
| } | ||
|
|
||
| // A different GroupName is what makes MSTest combine the two conditions with a logical AND, | ||
| // so an architecture requirement can be composed with an OS requirement. | ||
| public void GroupName_DiffersFromOSCondition() | ||
| => new ArchitectureConditionAttribute(TestArchitectures.X64).GroupName | ||
| .Should().NotBe(new OSConditionAttribute(OperatingSystems.Windows).GroupName); | ||
|
|
||
| public void IgnoreMessage_IncludeMode_ReturnsCorrectMessage() | ||
| { | ||
| // Arrange | ||
| var attribute = new ArchitectureConditionAttribute(ConditionMode.Include, TestArchitectures.X64); | ||
|
|
||
| // Act & Assert | ||
| attribute.IgnoreMessage.Should().Be($"Test is only supported on {TestArchitectures.X64}"); | ||
| } | ||
|
|
||
| public void IgnoreMessage_ExcludeMode_ReturnsCorrectMessage() | ||
| { | ||
| // Arrange | ||
| var attribute = new ArchitectureConditionAttribute(ConditionMode.Exclude, TestArchitectures.X64); | ||
|
|
||
| // Act & Assert | ||
| attribute.IgnoreMessage.Should().Be($"Test is not supported on {TestArchitectures.X64}"); | ||
| } | ||
|
|
||
| public void IsConditionMet_WhenAllArchitecturesIncluded_ReturnsTrue() | ||
| { | ||
| // The current process architecture is necessarily one of the known TestArchitectures. | ||
| var attribute = new ArchitectureConditionAttribute(AllArchitectures); | ||
|
|
||
| attribute.IsConditionMet.Should().BeTrue(); | ||
| } | ||
|
|
||
| public void IsConditionMet_WhenNoArchitecturesIncluded_ReturnsFalse() | ||
| { | ||
| var attribute = new ArchitectureConditionAttribute(default); | ||
|
|
||
| attribute.IsConditionMet.Should().BeFalse(); | ||
| } | ||
|
|
||
| public void IsConditionMet_WhenCurrentArchitectureMatches_ReturnsTrue() | ||
| { | ||
| var attribute = new ArchitectureConditionAttribute(GetCurrentArchitecture()); | ||
|
|
||
| attribute.IsConditionMet.Should().BeTrue(); | ||
| } | ||
|
|
||
| public void IsConditionMet_WhenCurrentArchitectureExcludedFromSet_ReturnsFalse() | ||
| { | ||
| var attribute = new ArchitectureConditionAttribute(AllArchitectures & ~GetCurrentArchitecture()); | ||
|
|
||
| attribute.IsConditionMet.Should().BeFalse(); | ||
| } | ||
|
|
||
| // Derive the expected flag from the *name* of the current architecture rather than re-implementing the | ||
| // integer→flag mapping, so this test doesn't silently mirror (and therefore can't validate) the production logic. | ||
| // Enum.GetName returns null (rather than a numeric string) for a value missing from this TFM's Architecture enum, | ||
| // so an unknown architecture fails fast instead of Enum.TryParse accepting a numeric string as a bogus flag. | ||
| private static TestArchitectures GetCurrentArchitecture() | ||
| => Enum.GetName(typeof(Architecture), RuntimeInformation.ProcessArchitecture) is { } name | ||
| && Enum.TryParse(name, out TestArchitectures architecture) | ||
| ? architecture | ||
| : throw new InvalidOperationException($"Unknown process architecture: {RuntimeInformation.ProcessArchitecture}"); | ||
| } | ||
| #endif | ||
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.