kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.52k stars 175 forks source link

Impossible testing with pexpect when external printer used #703

Open godzie44 opened 1 year ago

godzie44 commented 1 year ago

I'm using rustyline and python pexpect for integration testing. This worked fine, until i'm try to use an external_printer. With external_printer pextect.send do nothing.

Steps to reproduce

This can be reproduced on external_print example:

>>> import pexpect
>>> proc = pexpect.spawn('./target/debug/examples/external_print')
>>> proc.sendline('123')
>>> print(proc.readline())
b'\x1b[?2004h\r\x1b[K> \r\x1b[2C\r\x1b[KExternal message #0\r\n'
>>> print(proc.readline())
b'\r\x1b[K> \r\x1b[2C\r\x1b[KExternal message #1\r\n'
>>> print(proc.readline())

and so on, expected output "Line: 123" never reads.

Workaround

In my app i use cfg conditional checks for determine test environment or not, then in test env i replace external_printer.print to println!, but would be nice to have a better workaround...

gwenn commented 1 year ago

690 reveals that rustyline doesn't behave correctly with expect / pexpect.

So maybe we should introduce a feature X (for testing) that deactivates input buffering ? And maybe when this feature X is activated, external_printer are disabled ? (I am not sure that we can actually activate a feature only for testing)

godzie44 commented 1 year ago

Yep i think better if feature for testing will be on rustyline side. But there is still a problem, that output observable by user and output observable by integration tests may be different.