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

Allow faking a terminal for testing #8

Open
casey opened this issue Nov 11, 2016 · 6 comments
Open

Allow faking a terminal for testing #8

casey opened this issue Nov 11, 2016 · 6 comments

Comments

@casey
Copy link

casey commented Nov 11, 2016

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
Copy link
Author

casey commented Nov 12, 2016

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.

@softprops
Copy link
Owner

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

@hayd
Copy link

hayd commented Oct 19, 2018

@casey how did you do this in just?

This might also be useful for #15.

@casey
Copy link
Author

casey commented Oct 19, 2018

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.

@hayd
Copy link

hayd commented Oct 19, 2018

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
Copy link

hayd commented Oct 23, 2018

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

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

No branches or pull requests

3 participants