Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 20, 2025

This PR adds support for the Browser platform (WebAssembly) to TUnit's operating system constraints framework and resolves compatibility issues that arise when targeting browser platforms.

Changes Made

1. OS Enum Enhancement

Added Browser = 8 to the OS flags enum in TUnit.Core/Enums/OS.cs:

/// <summary>
/// Represents the Browser platform (WebAssembly).
/// </summary>
/// <remarks>
/// Tests with this flag will be executed on Browser platforms when used with <see cref="RunOnAttribute"/>.
/// </remarks>
Browser = 8

2. Platform Support Declaration

Added <SupportedPlatform Include="browser" /> to Library.props as requested in the issue.

3. Runtime Detection

Implemented Browser platform detection in both RunOnAttribute and ExcludeOnAttribute using System.OperatingSystem.IsBrowser() for .NET 5+ frameworks:

// Check for Browser platform (WebAssembly)
if (OperatingSystem.HasFlag(OS.Browser) && System.OperatingSystem.IsBrowser())
{
    return Task.FromResult(false);
}

4. Bug Fix

Fixed a critical logic error in ExcludeOnAttribute that was causing all tests to be skipped due to an erroneous || true condition at the end of the boolean expression.

5. WebAssembly Compatibility Fixes

Resolved build errors and warnings that surfaced after adding browser platform support by implementing platform-specific guards for APIs not supported on WebAssembly:

  • Console.CancelKeyPress: Guarded with !OperatingSystem.IsBrowser() checks using conditional compilation for .NET Standard 2.0 compatibility
  • Thread.Start(): Added fallback to direct execution on browser platforms where threading is not supported
  • Console.ForegroundColor: Added platform checks with default color fallback for browser environments

6. Testing and Documentation

  • Added comprehensive test cases for Browser platform scenarios in RunOnSkipTests.cs
  • Updated all code examples and documentation to include Browser platform support
  • Validated enum flag mathematics (Browser = 8 maintains proper power-of-2 progression)

Usage Examples

// Run test only on Browser platform
[Test, RunOn(OS.Browser)]
public async Task BrowserOnlyTest() { ... }

// Run test on all platforms including Browser
[Test, RunOn(OS.Windows | OS.Linux | OS.MacOs | OS.Browser)]
public async Task AllPlatformsTest() { ... }

// Exclude test from Browser platform
[Test, ExcludeOn(OS.Browser)]
public async Task NonBrowserTest() { ... }

The implementation maintains backward compatibility and follows TUnit's existing architectural patterns for platform detection while properly handling WebAssembly runtime limitations.

Fixes #2964.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Support 'Browser' platform Add Browser platform support to OS enum and RunOn/ExcludeOn attributes Aug 20, 2025
@Copilot Copilot AI requested a review from thomhurst August 20, 2025 14:30
Copilot finished work on behalf of thomhurst August 20, 2025 14:30
@thomhurst thomhurst marked this pull request as ready for review August 20, 2025 14:51
@thomhurst
Copy link
Owner

@copilot Fix any build errors and warnings from the new platform

Copy link
Contributor Author

Copilot AI commented Aug 20, 2025

@copilot Fix any build errors and warnings from the new platform

Fixed all build errors and warnings from the new browser platform support in commit c190643. The issues were caused by Console and Threading APIs that aren't supported on WebAssembly/browser platforms:

  1. Console.CancelKeyPress - Added platform guards with conditional compilation
  2. Thread.Start() - Added fallback to direct execution on browser platforms
  3. Console.ForegroundColor - Added platform checks with default fallback

All changes maintain backward compatibility with .NET Standard 2.0 while properly supporting the browser platform.

@Copilot Copilot AI changed the title Add Browser platform support to OS enum and RunOn/ExcludeOn attributes Add Browser platform support to OS enum and RunOn/ExcludeOn attributes with WebAssembly compatibility fixes Aug 20, 2025
Copilot finished work on behalf of thomhurst August 20, 2025 15:35
Copy link
Contributor

claude bot commented Aug 20, 2025

Claude finished @Copilot's task —— View job


PR Code Review

Review Checklist

  • Analyze code quality and implementation patterns
  • Check for potential bugs and logic issues
  • Evaluate performance considerations
  • Review security implications
  • Assess test coverage adequacy
  • Verify compliance with TUnit development guidelines

Code Quality & Implementation Patterns ✅

Excellent architectural consistency: The implementation follows TUnit's existing patterns perfectly:

  • Proper enum flag progression: Browser = 8 maintains power-of-2 sequence
  • Consistent attribute inheritance from SkipAttribute
  • Platform detection using appropriate .NET APIs (System.OperatingSystem.IsBrowser())
  • Proper conditional compilation for framework compatibility

Documentation & Examples: Comprehensive XML documentation with practical examples showing all usage patterns (single platform, combined platforms, exclusion scenarios).

WebAssembly Compatibility Fixes: Smart platform-specific guards implemented correctly:

  • EngineCancellationToken.cs:31-38: Console.CancelKeyPress properly guarded
  • DedicatedThreadExecutor.cs:11-17: Thread.Start() fallback to direct execution
  • TUnitFrameworkLogger.cs:79-88: Console.ForegroundColor with sensible default

The conditional compilation approach using #if NET5_0_OR_GREATER with OperatingSystem.IsBrowser() is the correct modern approach.

Potential Bugs & Logic Issues ✅

Critical Bug Fix Identified: The most important change in this PR is the fix in ExcludeOnAttribute.cs:54-62. The previous version had a logic error that would cause all tests to be skipped due to an erroneous boolean expression. The new implementation correctly evaluates platform exclusion logic.

Platform Detection Logic: Both RunOnAttribute and ExcludeOnAttribute implement the platform detection correctly:

  • RunOnAttribute: Returns false (don't skip) when platform matches, true (skip) otherwise
  • ExcludeOnAttribute: Returns true (skip) when platform matches excluded OS, false (don't skip) otherwise

Framework Compatibility: Proper handling of .NET Standard 2.0 vs .NET 5+ differences using conditional compilation.

Performance Considerations ✅

Minimal Performance Impact: The changes are primarily compile-time (conditional compilation) with minimal runtime overhead:

  • Platform checks use efficient OperatingSystem.IsBrowser() calls
  • Browser platform fallbacks avoid expensive threading operations
  • No additional allocations in hot paths

Smart Fallback Strategy: DedicatedThreadExecutor.cs:11-17 elegantly falls back to direct execution on browser platforms, avoiding the overhead of thread creation when threading isn't supported.

Efficient Color Handling: TUnitFrameworkLogger.cs:79-88 provides a sensible default color for browser platforms without additional runtime checks in non-browser environments.

Security Considerations ✅

No Security Concerns: The changes are purely platform detection and compatibility improvements without introducing any security risks:

  • No external input processing or validation
  • No network operations or file system access
  • Platform detection uses built-in .NET APIs
  • No elevation of privileges or sensitive operations

The browser platform support actually improves security by providing proper sandboxed execution environment detection.

Test Coverage Assessment ✅

Comprehensive Test Scenarios: RunOnSkipTests.cs includes all essential browser platform test cases:

  • RunOnBrowserOnlyTest() (lines 29-40): Browser-only execution with proper conditional compilation
  • ExcludeOnBrowserTest() (lines 43-54): Browser exclusion testing
  • Proper fallback handling for .NET Standard 2.0 frameworks

Test Logic Soundness: Tests correctly verify platform behavior using System.OperatingSystem.IsBrowser() with appropriate framework guards.

Coverage Completeness: All major usage patterns are covered, though additional edge case testing could be valuable (combining Browser with other OS flags in complex scenarios).

TUnit Development Guidelines Compliance ✅

Perfect Adherence to Standards: This PR exemplifies TUnit's development principles:

  • Behavioral parity: Browser platform detection works consistently across source-generated and reflection modes
  • Modern C#: Uses contemporary patterns like collection initializers and proper conditional compilation
  • Performance-focused: Minimal runtime overhead with smart compile-time optimizations
  • AOT-friendly: All browser platform detection uses supported .NET APIs
  • Thread safety: Browser platform fallbacks maintain concurrent test safety

Architectural Consistency: Changes integrate seamlessly with existing TUnit infrastructure without breaking existing patterns or introducing technical debt.

Overall Assessment ⭐

Excellent Implementation: This PR delivers high-quality browser platform support while fixing a critical logic bug in ExcludeOnAttribute. The WebAssembly compatibility fixes demonstrate thorough understanding of platform limitations and provide elegant fallback solutions.

Key Strengths:

  • Maintains full backward compatibility
  • Follows TUnit's architectural patterns exactly
  • Comprehensive documentation and testing
  • Smart platform-specific optimizations
  • Critical bug fix that improves reliability

Recommendation: APPROVE - This PR is ready for merge and represents a significant improvement to TUnit's platform support capabilities.


Copilot AI and others added 4 commits August 21, 2025 15:35
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
…APIs

Co-authored-by: thomhurst <30480171+thomhurst@users.noreply.github.com>
This was referenced Aug 22, 2025
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.

Support 'Browser' platform System.PlatformNotSupportedException when running test project in browser-wasm runtime
2 participants