Skip to content

Commit bca3547

Browse files
Copilotthomhurst
andcommitted
Fix browser platform compatibility issues with Console and Threading APIs
Co-authored-by: thomhurst <[email protected]>
1 parent de215b1 commit bca3547

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

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/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)

0 commit comments

Comments
 (0)