From 86b7996598d9c58d59e433029b75898700dfbcb4 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 19 Oct 2025 11:50:28 -0600 Subject: [PATCH] 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> --- Terminal.Gui/App/ApplicationImpl.cs | 17 ++++++++++++ .../ConsoleDrivers/ConsoleDriverTests.cs | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/Terminal.Gui/App/ApplicationImpl.cs b/Terminal.Gui/App/ApplicationImpl.cs index 5c1c42766..4f76c331b 100644 --- a/Terminal.Gui/App/ApplicationImpl.cs +++ b/Terminal.Gui/App/ApplicationImpl.cs @@ -103,6 +103,23 @@ public class ApplicationImpl : IApplication private void CreateDriver (string? driverName) { + // When running unit tests, always use FakeDriver unless explicitly specified + if (ConsoleDriver.RunningUnitTests && + string.IsNullOrEmpty (driverName) && + _componentFactory is null) + { + Logging.Logger.LogDebug ("Unit test safeguard: forcing FakeDriver (RunningUnitTests=true, driverName=null, componentFactory=null)"); + _coordinator = CreateSubcomponents (() => new FakeComponentFactory ()); + _coordinator.StartAsync ().Wait (); + + if (Application.Driver == null) + { + throw new ("Application.Driver was null even after booting MainLoopCoordinator"); + } + + return; + } + PlatformID p = Environment.OSVersion.Platform; // Check component factory type first - this takes precedence over driverName diff --git a/Tests/UnitTests/ConsoleDrivers/ConsoleDriverTests.cs b/Tests/UnitTests/ConsoleDrivers/ConsoleDriverTests.cs index 3cf896d3d..d7ec1cc28 100644 --- a/Tests/UnitTests/ConsoleDrivers/ConsoleDriverTests.cs +++ b/Tests/UnitTests/ConsoleDrivers/ConsoleDriverTests.cs @@ -230,4 +230,30 @@ public class ConsoleDriverTests // // [Fact] // public void FakeDriver_IsValidInput_Correct_Surrogate_Sequence () + + /// + /// Tests that when ConsoleDriver.RunningUnitTests is true, Application.Init() without + /// parameters uses FakeDriver instead of platform-specific drivers. + /// This prevents intermittent failures on macOS where kernel32.dll might be referenced. + /// + [Fact] + public void Application_Init_Without_Params_Uses_FakeDriver_When_RunningUnitTests () + { + // Arrange + ConsoleDriver.RunningUnitTests = true; + Application.ResetState (true); + + // Act + Application.Init (); + + // Assert + Assert.NotNull (Application.Driver); + // In the modern v2 architecture, the driver will be a ConsoleDriverFacade wrapping FakeDriver + // The key is that it's not attempting to use Windows/Unix platform-specific drivers + Assert.True (Application.Initialized); + + // Cleanup + Application.Shutdown (); + Application.ResetState (true); + } }