Skip to content

Throw PlatformNotSupportedException in STATestMethodAttribute on non-Windows#7862

Merged
Evangelink merged 4 commits intomainfrom
dev/amauryleve/sta-non-windows-warning
Apr 27, 2026
Merged

Throw PlatformNotSupportedException in STATestMethodAttribute on non-Windows#7862
Evangelink merged 4 commits intomainfrom
dev/amauryleve/sta-non-windows-warning

Conversation

@Evangelink
Copy link
Copy Markdown
Member

@Evangelink Evangelink commented Apr 27, 2026

Summary

STATestMethodAttribute.ExecuteAsync had an ambiguous // TODO: Throw? comment on the non-Windows code path (line 79). The test would silently run without STA context on non-Windows, with no indication to the user.

Change

Replace the // TODO: Throw? with a PlatformNotSupportedException. STA (Single Threaded Apartment) is a Windows-only COM threading concept — silently running without it is misleading. Failing explicitly makes the contract clear.

The error message guides users to use [OSCondition(OperatingSystems.Windows)] to skip the test on non-Windows platforms.

Changes

  • STATestMethodAttribute.cs: Throw PlatformNotSupportedException on non-Windows instead of silently executing without STA.
  • FrameworkMessages.resx: Added STATestMethodNonWindowsNotSupported resource string.
  • xlf/ files: Auto-generated by build.

STA (Single Threaded Apartment) is a Windows-only COM threading concept.
On non-Windows platforms, STATestMethodAttribute now:
- Runs the test without STA context (unchanged behaviour)
- Appends a warning to the test output explaining STA was not applied
- Documents the design decision with a clear code comment

Removes the ambiguous '// TODO: Throw?' comment.
Copilot AI review requested due to automatic review settings April 27, 2026 10:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR clarifies and formalizes the behavior of STATestMethodAttribute on non-Windows platforms by documenting the design decision and emitting a localized warning in test output when STA cannot be applied.

Changes:

  • Updated STATestMethodAttribute.ExecuteAsync to keep running tests on non-Windows, and append a warning to TestResult.LogOutput.
  • Added a new localized resource string STATestMethodNonWindowsWarning.
  • Updated the generated localization .xlf files to include the new resource entry.
Show a summary per file
File Description
src/TestFramework/TestFramework/Attributes/TestMethod/STATestMethodAttribute.cs Adds documented non-Windows behavior and appends a warning message to each returned TestResult.
src/TestFramework/TestFramework/Resources/FrameworkMessages.resx Introduces the new STATestMethodNonWindowsWarning resource string.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.cs.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.de.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.es.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.fr.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.it.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ja.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ko.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pl.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.pt-BR.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.ru.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.tr.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hans.xlf Adds the new trans-unit for localization.
src/TestFramework/TestFramework/Resources/xlf/FrameworkMessages.zh-Hant.xlf Adds the new trans-unit for localization.

Copilot's findings

  • Files reviewed: 15/15 changed files
  • Comments generated: 2

Comment thread src/TestFramework/TestFramework/Attributes/TestMethod/STATestMethodAttribute.cs Outdated
@Evangelink Evangelink marked this pull request as draft April 27, 2026 11:04
@Evangelink
Copy link
Copy Markdown
Member Author

Sorry I asked to create PR in the wrong chat... will do the changes I wanted here

STATestMethodAttribute now throws PlatformNotSupportedException on
non-Windows platforms since STA is a Windows-only COM concept.
The error message guides users to use [OSCondition(OperatingSystems.Windows)]
to skip the test on non-Windows platforms.
@Evangelink Evangelink changed the title Document STATestMethodAttribute non-Windows behaviour and add warning Throw PlatformNotSupportedException in STATestMethodAttribute on non-Windows Apr 27, 2026
@Evangelink Evangelink marked this pull request as ready for review April 27, 2026 11:25
Copilot AI review requested due to automatic review settings April 27, 2026 11:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 15/15 changed files
  • Comments generated: 2

Add NETFRAMEWORK-compatible OS check using Environment.OSVersion.Platform
so that STATestMethodAttribute throws PlatformNotSupportedException
on Mono running on non-Windows, not just on .NET Core/5+.
@Evangelink Evangelink enabled auto-merge (squash) April 27, 2026 12:51
Comment thread src/TestFramework/TestFramework/Attributes/TestMethod/STATestMethodAttribute.cs Outdated
@Evangelink
Copy link
Copy Markdown
Member Author

@copilot please revert last commit

Copilot AI review requested due to automatic review settings April 27, 2026 14:12
@Evangelink Evangelink review requested due to automatic review settings April 27, 2026 14:12
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 27, 2026

@copilot please revert last commit

Done — reverted the last commit in 06a6587. The file is back to the simple #if !NETFRAMEWORK guard without the Mono/NETFRAMEWORK OS check.

@Evangelink
Copy link
Copy Markdown
Member Author

@Youssef1313 PR is ready

@Evangelink Evangelink merged commit 3bba7e3 into main Apr 27, 2026
10 checks passed
@Evangelink Evangelink deleted the dev/amauryleve/sta-non-windows-warning branch April 27, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants