interactiveui: make getchar() testable - #1412
Open
anushamukka-dev wants to merge 1 commit into
Open
Conversation
Summary: `interactiveui.getchar()` had a `# TODO: figure out tests` on its non-tty early return, and no test coverage at all - there is no existing test file for `interactiveui` anywhere in `eden/scm`. This extracts the raw terminal read into a module-level `_readraw(fd)` helper and gives `getchar()` two optional injectable seams: - `stdin` - defaults to `sys.stdin` - `readraw` - defaults to `_readraw` Real terminal behavior is unchanged: production callers pass neither argument, `_readraw` contains the identical `tcgetattr` / `setraw` / `os.read` / `tcsetattr` sequence, and the `os.isatty(fd)` guard and ctrl-c / ctrl-d handling are byte-for-byte the same. Initializing `ch = None` before the `try` also resolves the three `pyre-fixme[61]` suppressions. Those flagged a real latent bug: if `tcgetattr` raised `termios.error`, the `except` branch read `ch` before it was ever assigned and would have raised `NameError` instead of falling back to `""`. Adds `tests/test-interactiveui.py` covering the non-tty early return (backed by a real pipe fd), a single normal keypress, ctrl-c / ctrl-d, and the escape sequence branch for arrow keys - including a run of several arrow keys plus a normal key delivered in one read, checked through `_splitkeypresses`. The tty cases use a real pty fd so the `os.isatty` guard is exercised for real rather than mocked, with only the blocking read faked. The suite follows the existing `silenttestrunner` convention used by neighboring tests such as `test-extutil.py` and needs no BUCK change - `unittestify.py` globs `test-*.py`. Differential Revision: D115572436
|
@anushamukka-dev has exported this pull request. If you are a Meta employee, you can view the originating Diff in D115572436. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
interactiveui.getchar()had a# TODO: figure out testson its non-ttyearly return, and no test coverage at all - there is no existing test file
for
interactiveuianywhere ineden/scm.This extracts the raw terminal read into a module-level
_readraw(fd)helperand gives
getchar()two optional injectable seams:stdin- defaults tosys.stdinreadraw- defaults to_readrawReal terminal behavior is unchanged: production callers pass neither argument,
_readrawcontains the identicaltcgetattr/setraw/os.read/tcsetattrsequence, and theos.isatty(fd)guard and ctrl-c / ctrl-dhandling are byte-for-byte the same.
Initializing
ch = Nonebefore thetryalso resolves the threepyre-fixme[61]suppressions. Those flagged a real latent bug: iftcgetattrraisedtermios.error, theexceptbranch readchbefore itwas ever assigned and would have raised
NameErrorinstead of falling backto
"".Adds
tests/test-interactiveui.pycovering the non-tty early return (backedby a real pipe fd), a single normal keypress, ctrl-c / ctrl-d, and the escape
sequence branch for arrow keys - including a run of several arrow keys plus a
normal key delivered in one read, checked through
_splitkeypresses. The ttycases use a real pty fd so the
os.isattyguard is exercised for real ratherthan mocked, with only the blocking read faked. The suite follows the existing
silenttestrunnerconvention used by neighboring tests such astest-extutil.pyand needs no BUCK change -unittestify.pyglobstest-*.py.Differential Revision: D115572436