Skip to content

Fixes #4804 - Disable all real driver input/output in unit tests, both locally and GH actions#4811

Merged
tig merged 16 commits intov2_developfrom
fix/4804-terminal-detection
Mar 9, 2026
Merged

Fixes #4804 - Disable all real driver input/output in unit tests, both locally and GH actions#4811
tig merged 16 commits intov2_developfrom
fix/4804-terminal-detection

Conversation

@tig
Copy link
Copy Markdown
Collaborator

@tig tig commented Mar 8, 2026

Summary

  • Centralizes terminal detection in Driver.IsAttachedToTerminal so drivers correctly detect when no terminal is attached
  • Adds DisableRealDriverIO environment variable support for CI test jobs to force degraded mode
  • Gracefully handles no-terminal scenarios in output drivers (AnsiOutput, kitty keyboard, cursor management)
  • Refactors IsAttachedToTerminal checks and cleans up driver code with modern C# patterns

Fixes #4804

Test plan

  • Existing unit tests pass
  • New tests verify kitty keyboard and dispose behavior when no terminal is available
  • CI jobs set DisableRealDriverIO to prevent hangs in test runners

🤖 Generated with Claude Code

tig added 6 commits March 8, 2026 16:45
Refactor terminal attachment checks into Driver.IsAttachedToTerminal, which now handles platform-specific logic and honors the DisableRealDriverIO environment variable for test environments. Updated all drivers to use this unified method, and simplified AnsiTerminalHelper to delegate to it. Moved relevant P/Invoke code to Driver. Added comprehensive unit tests for all drivers and the environment variable gating. Introduced test.runsettings and Directory.Build.props changes to ensure DisableRealDriverIO is set during test runs, improving testability and consistency of degraded mode detection.
- Reformat method calls and declarations for readability
- Use C# pattern matching and switch expressions
- Refactor OutputBase properties to auto-properties
- Convert trivial methods to expression-bodied members
- Fix logic in OutputBase.Write, UnixInput init, and WindowsOutput error handling
- Remove AnsiTerminalHelper.IsAttachedToTerminal and related test
- Use explicit types for clarity
- Reformat and clarify P/Invoke signatures
- Update .DotSettings and documentation comments
- Improve maintainability and correctness throughout drivers
Add IsAttachedToTerminal to OutputBase and update all output drivers (AnsiOutput, NetOutput, UnixOutput, WindowsOutput) to check this property before performing terminal I/O. When not attached to a real terminal, output methods now return early, perform no-ops, or return default values. Constructors log lifecycle messages if no terminal is attached. Tests updated to verify no escape sequences or state changes occur in these scenarios. This prevents errors and unwanted output in redirected or non-interactive environments.
Move IsAttachedToTerminal checks after base logic in Output classes to ensure base behavior is always executed. Streamline and centralize terminal checks, reduce code duplication, and improve consistency across AnsiOutput, NetOutput, UnixOutput, and WindowsOutput. Adjust platform-specific logic and early returns for better maintainability and correct output buffer handling.
Centralize IsAttachedToTerminal logic in InputImpl<T> and update all input classes (AnsiInput, NetInput, UnixInput, WindowsInput) to use it for degraded mode detection and logging. Simplify AnsiOutput by removing redundant terminal checks and updating Kitty keyboard methods. Refactor MainLoopCoordinatorTests for modern C# syntax, restoring and improving Kitty protocol and error handling tests. Improves clarity, maintainability, and test reliability.
@tig tig marked this pull request as draft March 8, 2026 22:49
@tig tig changed the title Fix unit tests not running in degraded mode (#4804) Fixes #4804 - Disable all real driver input/output in unit tests, both locally and GH actions Mar 8, 2026
tig and others added 4 commits March 8, 2026 16:51
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Moved and duplicated tests for DisableRealDriverIO env var and Driver.IsAttachedToTerminal from DriverTests to IntegrationTests. Also refactored test output usage to use the output parameter directly. This ensures environment variable handling is verified in both test contexts.
@tig tig marked this pull request as ready for review March 8, 2026 23:00
@tig tig requested a review from Copilot March 8, 2026 23:00
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enforces “degraded mode” behavior during test runs by centralizing terminal-attachment detection in Driver.IsAttachedToTerminal and using an environment-variable switch (DisableRealDriverIO=1) to disable real driver I/O in CI and unit tests, preventing hangs in test harnesses without a real terminal.

Changes:

  • Added Driver.IsAttachedToTerminal(out in, out out) with DisableRealDriverIO gating to force degraded mode during tests/CI.
  • Updated multiple input/output drivers to no-op (or avoid terminal operations) when no real terminal is attached.
  • Added runsettings/MSBuild wiring + GitHub Actions env to propagate DisableRealDriverIO=1, plus tests to assert expected degraded-mode behavior.

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
Tests/test.runsettings Sets DisableRealDriverIO=1 for test runs via runsettings.
Tests/Directory.Build.props Plumbs RunSettingsFilePath for test projects under Tests/.
Tests/UnitTestsParallelizable/Drivers/Windows/WindowsInputOutputTests.cs Adds assertion that Driver.IsAttachedToTerminal returns false in the test harness.
Tests/UnitTestsParallelizable/Drivers/Unix/UnixInputOutputTests.cs Adds assertion that Driver.IsAttachedToTerminal returns false in the test harness.
Tests/UnitTestsParallelizable/Drivers/Dotnet/NetInputOutputTests.cs Adds assertion that Driver.IsAttachedToTerminal returns false in the test harness.
Tests/UnitTestsParallelizable/Drivers/Ansi/AnsiTerminalHelperTests.cs New tests validating env-var propagation and IsAttachedToTerminal gating.
Tests/UnitTestsParallelizable/Drivers/Ansi/AnsiInputOutputTests.cs Adds assertion that Driver.IsAttachedToTerminal returns false in the test harness.
Tests/UnitTestsParallelizable/Application/MainLoopCoordinatorTests.cs Modernizes syntax and moves/extends the “input crash surfaces” test into the parallelizable suite.
Tests/UnitTests/Application/MainLoopCoordinatorTests.cs Removes the non-parallel test file (logic moved to parallelizable tests).
Tests/IntegrationTests/FluentTests/TestContextTests.cs Adds env-var + IsAttachedToTerminal gating tests to integration suite.
Terminal.sln.DotSettings Adds csbi to the ReSharper user dictionary.
Terminal.Gui/Drivers/Driver.cs Adds centralized IsAttachedToTerminal implementation and env-var gating.
Terminal.Gui/Drivers/Output/OutputBase.cs Caches terminal-attachment state and refactors output writing logic; adds doc comments.
Terminal.Gui/Drivers/Input/InputImpl.cs Caches terminal-attachment state for input implementations.
Terminal.Gui/Drivers/AnsiDriver/AnsiTerminalHelper.cs Removes per-helper terminal detection (now centralized in Driver).
Terminal.Gui/Drivers/AnsiDriver/AnsiOutput.cs Uses shared terminal-attachment detection and refactors write buffering.
Terminal.Gui/Drivers/AnsiDriver/AnsiInput.cs Uses shared terminal-attachment detection and refactors lifecycle logging.
Terminal.Gui/Drivers/UnixDriver/UnixTerminalHelper.cs Switches to shared Driver.IsAttachedToTerminal check.
Terminal.Gui/Drivers/UnixDriver/UnixOutput.cs Adds terminal-attachment guards to output operations and size reporting.
Terminal.Gui/Drivers/UnixDriver/UnixInput.cs Adds terminal-attachment guard + minor refactors for degraded mode behavior.
Terminal.Gui/Drivers/UnixDriver/UnixIOHelper.cs Formatting/interop signature tweaks + minor pattern matching cleanup.
Terminal.Gui/Drivers/WindowsDriver/WindowsOutput.cs Adds terminal-attachment guards throughout; refactors and improves no-terminal behavior.
Terminal.Gui/Drivers/WindowsDriver/WindowsInput.cs Adds terminal-attachment guard and lifecycle trace on init.
Terminal.Gui/Drivers/DotNetDriver/NetOutput.cs Adds terminal-attachment guards and lifecycle trace; minor modern C# refactors.
Terminal.Gui/Drivers/DotNetDriver/NetInput.cs Adds terminal-attachment guard and lifecycle trace; minor doc/formatting changes.
.github/workflows/unit-tests.yml Sets DisableRealDriverIO=1 for unit-test jobs.
.github/workflows/stress-tests.yml Sets DisableRealDriverIO=1 for stress-test jobs.
.github/workflows/integration-tests.yml Sets DisableRealDriverIO=1 for integration-test jobs.

Comment thread Terminal.Gui/Drivers/WindowsDriver/WindowsInput.cs
Comment thread Terminal.Gui/Drivers/DotNetDriver/NetOutput.cs
Comment thread Terminal.Gui/Drivers/Output/OutputBase.cs Outdated
Comment thread Terminal.Gui/Drivers/DotNetDriver/NetInput.cs Outdated
Comment thread Tests/IntegrationTests/FluentTests/TestContextTests.cs Outdated
Comment thread Tests/UnitTestsParallelizable/Application/MainLoopCoordinatorTests.cs Outdated
Comment thread Terminal.Gui/Drivers/DotNetDriver/NetOutput.cs
Comment thread Terminal.Gui/Drivers/Output/OutputBase.cs Outdated
Comment thread Terminal.Gui/Drivers/Input/InputImpl.cs Outdated
tig and others added 5 commits March 8, 2026 17:15
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@tig tig requested a review from BDisp March 8, 2026 23:23
…l Logger

- Use ITestOutputHelper primary constructor for test output
- Replace manual Logging.Logger swap with TestLogging.BindTo scope
- Remove mock logger verification (exception propagation is the key assertion)
- Safe for parallel test execution

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@BDisp BDisp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@tig tig merged commit 870a5bf into v2_develop Mar 9, 2026
11 checks passed
@tig tig deleted the fix/4804-terminal-detection branch March 9, 2026 02:38
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.

Unit Tests are not running in degraded mode

3 participants