Skip to content

Commit e11e726

Browse files
Copilotthomhurst
andauthored
Add Browser platform support to OS enum and RunOn/ExcludeOn attributes with WebAssembly compatibility fixes (#2965)
* Initial plan: Add Browser platform support Co-authored-by: thomhurst <[email protected]> * Add Browser platform support to OS enum and attributes Co-authored-by: thomhurst <[email protected]> * Complete Browser platform support implementation Co-authored-by: thomhurst <[email protected]> * Fix browser platform compatibility issues with Console and Threading APIs Co-authored-by: thomhurst <[email protected]> * Add missing Engine test and update public API snapshots for Browser platform support Co-authored-by: thomhurst <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: thomhurst <[email protected]>
1 parent 4e4a78f commit e11e726

14 files changed

+163
-261
lines changed

Library.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)strongname.snk</AssemblyOriginatorKeyFile>
1717
<PublicKey>0024000004800000940000000602000000240000525341310004000001000100698a70398fa0b2230c5a72e3bd9d56b48f809f6173e49a19fbb942d621be93ad48c5566b47b28faabc359b9ad3ff4e00bbdea88f5bdfa250f391fedd28182b2e37b55d429c0151a42a98ea7a5821818cd15a79fef9903e8607a88304cf3e0317bf86ec96e32e1381535a6582251e5a6eed40b5a3ed82bc444598b1269cce57a7</PublicKey>
1818
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<SupportedPlatform Include="browser" />
22+
</ItemGroup>
1923
</Project>

TUnit.Core/Attributes/TestMetadata/ExcludeOnAttribute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace TUnit.Core;
3535
/// }
3636
///
3737
/// // Skip on all supported platforms (essentially always skips the test)
38-
/// [Test, ExcludeOn(OS.Windows | OS.Linux | OS.MacOs)]
38+
/// [Test, ExcludeOn(OS.Windows | OS.Linux | OS.MacOs | OS.Browser)]
3939
/// public void NeverRunTest()
4040
/// {
4141
/// // This test will not run on any supported platform
@@ -57,8 +57,9 @@ public override Task<bool> ShouldSkip(TestRegisteredContext context)
5757
// Only validate Linux and macOS on .NET 5+ where these OS flags are available
5858
|| (OperatingSystem.HasFlag(OS.Linux) && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
5959
|| (OperatingSystem.HasFlag(OS.MacOs) && RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
60+
|| (OperatingSystem.HasFlag(OS.Browser) && System.OperatingSystem.IsBrowser())
6061
#endif
61-
|| true;
62+
;
6263

6364
// Return true if the test should be skipped (if we're on an excluded OS)
6465
return Task.FromResult(shouldSkip);

TUnit.Core/Attributes/TestMetadata/RunOnAttribute.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace TUnit.Core;
3535
/// }
3636
///
3737
/// // Run on all supported platforms
38-
/// [Test, RunOn(OS.Windows | OS.Linux | OS.MacOs)]
38+
/// [Test, RunOn(OS.Windows | OS.Linux | OS.MacOs | OS.Browser)]
3939
/// public void AllPlatformsTest()
4040
/// {
4141
/// // This test will run on all supported platforms
@@ -64,6 +64,12 @@ public override Task<bool> ShouldSkip(TestRegisteredContext context)
6464
{
6565
return Task.FromResult(false);
6666
}
67+
68+
// Check for Browser platform (WebAssembly)
69+
if (OperatingSystem.HasFlag(OS.Browser) && System.OperatingSystem.IsBrowser())
70+
{
71+
return Task.FromResult(false);
72+
}
6773
#endif
6874

6975
return Task.FromResult(true);

TUnit.Core/EngineCancellationToken.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ internal void Initialise(CancellationToken cancellationToken)
2727
CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
2828
Token = CancellationTokenSource.Token;
2929

30-
Console.CancelKeyPress += OnCancelKeyPress;
30+
// Console.CancelKeyPress is not supported on browser platforms
31+
#if NET5_0_OR_GREATER
32+
if (!OperatingSystem.IsBrowser())
33+
{
34+
#endif
35+
Console.CancelKeyPress += OnCancelKeyPress;
36+
#if NET5_0_OR_GREATER
37+
}
38+
#endif
3139
}
3240

3341
private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
@@ -68,7 +76,15 @@ private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
6876
/// </summary>
6977
public void Dispose()
7078
{
71-
Console.CancelKeyPress -= OnCancelKeyPress;
79+
// Console.CancelKeyPress is not supported on browser platforms
80+
#if NET5_0_OR_GREATER
81+
if (!OperatingSystem.IsBrowser())
82+
{
83+
#endif
84+
Console.CancelKeyPress -= OnCancelKeyPress;
85+
#if NET5_0_OR_GREATER
86+
}
87+
#endif
7288
_forcefulExitCts?.Cancel();
7389
_forcefulExitCts?.Dispose();
7490
CancellationTokenSource.Dispose();

TUnit.Core/Enums/OS.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/// [RunOn(OS.Windows | OS.Linux)]
2323
///
2424
/// // Specify a test should run on all supported platforms
25-
/// [RunOn(OS.Windows | OS.Linux | OS.MacOs)]
25+
/// [RunOn(OS.Windows | OS.Linux | OS.MacOs | OS.Browser)]
2626
/// </code>
2727
/// </example>
2828
/// <seealso cref="RunOnAttribute"/>
@@ -51,5 +51,13 @@ public enum OS
5151
/// <remarks>
5252
/// Tests with this flag will be executed on macOS platforms when used with <see cref="RunOnAttribute"/>.
5353
/// </remarks>
54-
MacOs = 4
54+
MacOs = 4,
55+
56+
/// <summary>
57+
/// Represents the Browser platform (WebAssembly).
58+
/// </summary>
59+
/// <remarks>
60+
/// Tests with this flag will be executed on Browser platforms when used with <see cref="RunOnAttribute"/>.
61+
/// </remarks>
62+
Browser = 8
5563
}

TUnit.Core/Executors/DedicatedThreadExecutor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ public class DedicatedThreadExecutor : GenericAbstractExecutor, ITestRegisteredE
77
{
88
protected sealed override async ValueTask ExecuteAsync(Func<ValueTask> action)
99
{
10+
// On browser platforms, threading is not supported, so fall back to direct execution
11+
#if NET5_0_OR_GREATER
12+
if (OperatingSystem.IsBrowser())
13+
{
14+
await action();
15+
return;
16+
}
17+
#endif
18+
1019
var tcs = new TaskCompletionSource<object?>();
1120

1221
var thread = new Thread(() =>

TUnit.Engine/Logging/TUnitFrameworkLogger.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ private static ConsoleColor GetConsoleColor(LogLevel logLevel)
7676
return ConsoleColor.DarkRed;
7777
}
7878

79+
// Console.ForegroundColor is not supported on browser platforms
80+
#if NET5_0_OR_GREATER
81+
if (!OperatingSystem.IsBrowser())
82+
{
83+
return Console.ForegroundColor;
84+
}
85+
return ConsoleColor.Gray; // Default color for browser platforms
86+
#else
7987
return Console.ForegroundColor;
88+
#endif
8089
}
8190

8291
public bool IsEnabled(LogLevel logLevel)

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.DotNet8_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,7 @@ namespace .Enums
16951695
Linux = 1,
16961696
Windows = 2,
16971697
MacOs = 4,
1698+
Browser = 8,
16981699
}
16991700
public enum Priority
17001701
{

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.DotNet9_0.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,7 @@ namespace .Enums
16951695
Linux = 1,
16961696
Windows = 2,
16971697
MacOs = 4,
1698+
Browser = 8,
16981699
}
16991700
public enum Priority
17001701
{

TUnit.PublicAPI/Tests.Core_Library_Has_No_API_Changes.Net4_7.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,7 @@ namespace .Enums
16071607
Linux = 1,
16081608
Windows = 2,
16091609
MacOs = 4,
1610+
Browser = 8,
16101611
}
16111612
public enum Priority
16121613
{

0 commit comments

Comments
 (0)