atty icon indicating copy to clipboard operation
atty copied to clipboard

Allow faking a terminal for testing

Open casey opened this issue 8 years ago • 6 comments

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

casey avatar Nov 11 '16 22:11 casey

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.

casey avatar Nov 12 '16 01:11 casey

Nice. I'll leave this open in case you need it. Also neat project!

softprops avatar Nov 12 '16 20:11 softprops

@casey how did you do this in just?

This might also be useful for #15.

hayd avatar Oct 19 '18 15:10 hayd

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.

casey avatar Oct 19 '18 21:10 casey

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.

hayd avatar Oct 19 '18 22:10 hayd

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

hayd avatar Oct 23 '18 20:10 hayd