Skip to content

Commit 86b7996

Browse files
Copilottig
andauthored
Fix intermittent macOS unit test failures by forcing FakeDriver when ConsoleDriver.RunningUnitTests is true (#4291)
* Initial plan * Add safeguard to force FakeDriver when ConsoleDriver.RunningUnitTests is true Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add test to verify FakeDriver is used when RunningUnitTests is true Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add debug logging to make unit test safeguard visible in CI logs Co-authored-by: tig <585482+tig@users.noreply.github.com> * Modify unit-tests.yml to run tests 10 times on macOS to verify fix stability Co-authored-by: tig <585482+tig@users.noreply.github.com> * Revert "Modify unit-tests.yml to run tests 10 times on macOS to verify fix stability" Co-authored-by: tig <585482+tig@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com>
1 parent 8aec052 commit 86b7996

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Terminal.Gui/App/ApplicationImpl.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,23 @@ public void Init (IConsoleDriver? driver = null, string? driverName = null)
103103

104104
private void CreateDriver (string? driverName)
105105
{
106+
// When running unit tests, always use FakeDriver unless explicitly specified
107+
if (ConsoleDriver.RunningUnitTests &&
108+
string.IsNullOrEmpty (driverName) &&
109+
_componentFactory is null)
110+
{
111+
Logging.Logger.LogDebug ("Unit test safeguard: forcing FakeDriver (RunningUnitTests=true, driverName=null, componentFactory=null)");
112+
_coordinator = CreateSubcomponents (() => new FakeComponentFactory ());
113+
_coordinator.StartAsync ().Wait ();
114+
115+
if (Application.Driver == null)
116+
{
117+
throw new ("Application.Driver was null even after booting MainLoopCoordinator");
118+
}
119+
120+
return;
121+
}
122+
106123
PlatformID p = Environment.OSVersion.Platform;
107124

108125
// Check component factory type first - this takes precedence over driverName

Tests/UnitTests/ConsoleDrivers/ConsoleDriverTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,30 @@ public void TerminalResized_Simulation (Type driverType)
230230
//
231231
// [Fact]
232232
// public void FakeDriver_IsValidInput_Correct_Surrogate_Sequence ()
233+
234+
/// <summary>
235+
/// Tests that when ConsoleDriver.RunningUnitTests is true, Application.Init() without
236+
/// parameters uses FakeDriver instead of platform-specific drivers.
237+
/// This prevents intermittent failures on macOS where kernel32.dll might be referenced.
238+
/// </summary>
239+
[Fact]
240+
public void Application_Init_Without_Params_Uses_FakeDriver_When_RunningUnitTests ()
241+
{
242+
// Arrange
243+
ConsoleDriver.RunningUnitTests = true;
244+
Application.ResetState (true);
245+
246+
// Act
247+
Application.Init ();
248+
249+
// Assert
250+
Assert.NotNull (Application.Driver);
251+
// In the modern v2 architecture, the driver will be a ConsoleDriverFacade wrapping FakeDriver
252+
// The key is that it's not attempting to use Windows/Unix platform-specific drivers
253+
Assert.True (Application.Initialized);
254+
255+
// Cleanup
256+
Application.Shutdown ();
257+
Application.ResetState (true);
258+
}
233259
}

0 commit comments

Comments
 (0)