atty
atty copied to clipboard
Allow faking a terminal for testing
Thanks for this! I'm using it a command runner[0] to figure out when to produce colored error messages and it works great.
It would be great if I could somehow fool atty into thinking that a stream is a terminal for testing, so that I can make sure that error messages are being formatted correctly when a terminal is attached.
[0] https://github.com/casey/just
Actually, I'm not entirely certain this is so useful. I was able to relatively cleanly add testing to my own project, so I don't have a strong use case for it. Other projects that use atty might be structured differently and thus benefit from the ability to mock atty, but not mine.
Nice. I'll leave this open in case you need it. Also neat project!
@casey how did you do this in just?
This might also be useful for #15.
Hmm, it's been a while since I wrote the tests. I think that I had an option to force color, regardless of if we were printing to a terminal, so if I wanted to test formatting I could set that option to be true.
We have something like:
fn permission_prompt(message: String) -> DenoResult<()> {
if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) { return Err(permission_denied()) };
eprint!("{} Grant? yN", message);
... // more stuff
What maybe you'd want to do is refactor to use dependency injection instead? https://stackoverflow.com/a/28370712/1240268 but then you not able to get an atty::Stream.
Okay, here's one way to fake TTYs in stdin, stdout and stderr: https://stackoverflow.com/a/52954716/1240268 https://gist.github.com/hayd/4f46a68fc697ba8888a7b517a414583e