Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): mock PromptSession to prevent console error #851

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

Pouyanpi
Copy link
Collaborator

@Pouyanpi Pouyanpi commented Nov 12, 2024

Description

The tests in tests/test_cli.py and tests/test_cli_migration.py were failing with the following error:

prompt_toolkit.output.win32.NoConsoleScreenBufferError: Found xterm-256color, while expecting a Windows console. Maybe try to run this program using "winpty" or run it in cmd.exe instead. Or otherwise, in case of Cygwin, use the Python executable that is compiled for Cygwin.

This error occurs because the prompt_toolkit library expects a Windows console but find xterm-256color instead. This happens when running tests in an environment that doesn't have a proper console buffer. This issue occurs in Windows GitHub runners when the runner is windows-latest.

Root Cause

The PromptSession class from the prompt_toolkit library attempts to create a console application, which fails in environments without a proper console buffer, leading to the NoConsoleScreenBufferError.

Fix

To resolve this issue, the PromptSession class is mocked during the tests to avoid initialization. This prevents the NoConsoleScreenBufferError from occurring.

Testing

  • Verified that the tests in tests/test_cli.py and tests/test_cli_migration.py pass successfully in both local and CI.
  • Ensured that the mocking does not interfere with the actual functionality being tested.

Related Issue(s)

prompt-toolkit/python-prompt-toolkit#406

Checklist

  • I've read the CONTRIBUTING guidelines.
  • I've updated the documentation if applicable.
  • I've added tests if applicable.
  • @mentions of the person or team responsible for reviewing proposed changes.

The tests in `tests/test_cli.py` and `tests/test_cli_migration.py` were
failing with `NoConsoleScreenBufferError` due to `prompt_toolkit`
expecting a Windows console but finding `xterm-256color` instead. This
issue occurs in Windows GitHub runners when the runner is `windows-latest`.

To resolve this, `PromptSession` is mocked globally before any tests
are collected, preventing the error. This fix ensures the tests run
successfully in all environments.

Changes:
- Added `conftest.py` to mock `PromptSession` globally.
@Pouyanpi Pouyanpi self-assigned this Nov 12, 2024
Copy link
Collaborator

@schuellc-nvidia schuellc-nvidia left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this! Looks good!

@schuellc-nvidia schuellc-nvidia merged commit 3f2d4d7 into develop Nov 15, 2024
4 checks passed
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.

2 participants