-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
nvm.sh: added nvm_stdout_is_terminal() #2007
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
Conversation
|
You've got good tests! I didn't know I had to add |
ljharb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'd need some unit tests for nvm_is_terminal; however when I try this locally it doesn't seem to detect that it's in a pipe.
|
I'll try to add unit tests tomorrow. I ran a few simple tests with if nvm_is_terminal; then
echo term
else
echo no term
fiWhen I call the function with / without a pipe, the results look OK: > nvm_ls
term
N/A
> nvm_ls | cat
no term
N/A(I ran Could you post more details about what's not working on your machine? |
|
I put the function in my shell, and piped something to it. (specifically, a function that echoed yes or no based on that -t test) |
|
Testing the function in the terminal can be a bit delicate... First, I'm not sure what you mean by "piped something to it", but it might be the wrong thing. The Shell syntax can also be tricky. For example, this command $ nvm_is_terminal ; echo $?
0has the expected result If I want to check the result of $ nvm_is_terminal ; echo $? | cat
0Oops... Still $ (nvm_is_terminal ; echo $?) | cat
1Now both commands are attached to the pipe, and I get the expected result I'm pretty sure P.S.: The |
nvm_is_terminal() { if [ -t 1 ]; then echo y; else echo n; fi } ;
echo hi | nvm_is_terminal // i expect n, i get y
nvm_is_terminal // i get y as expected |
echo hi | nvm_is_terminal // i expect n, i get yThat's the Try this instead: $ nvm_is_terminal() { if [ -t 1 ]; then echo y; else echo n; fi }
$ nvm_is_terminal | cat
n
|
|
Thanks, that clarifies it. Perhaps a better name then would be |
|
It only checks stdout. (It would be easy to also check stderr, but I don't think that would make much sense. I think when people redirect stderr, they expect normal output not to be affected, so I guess the part of the code that decides how output will be formatted should not check stderr. For example, a common idiom to discard error messages is If stdout is redirected to a file or /dev/null, that would not be a terminal. The specification for the I like short names, so I chose |
|
|
|
OK, I renamed the function to nvm_stdout_is_terminal (also in the unload section) and renamed this PR. I'll look at the unit tests next. |
|
The |
|
Added another little fix. (A failure in one of the tests didn't properly signal a failure, it just printed an error message.) |
|
I found a way for the test with the pipe to transfer the return value of I temporarily checked in a deliberately broken version of the test to see if it fails on travis-ci. I simply changed this line (! nvm_stdout_is_terminal || echo "boo!") | read && die 'nvm_stdout_is_terminal should be false when stdout goes to a pipe'to this (without the negation at the start): (nvm_stdout_is_terminal || echo "boo!") | read && die 'nvm_stdout_is_terminal should be false when stdout goes to a pipe'The results are here: It signals an error in bash and zsh (good), but success in dash and sh (bad). I don't have dash, so I can't test it. I do have sh, and when I run the deliberately broken test locally in sh, it signals an error (as expected). So I don't know what's going on with dash and sh on travis-ci because I can't reproduce it. But I guess bash and zsh are much more relevant anyway, so I'd say the tests are good enough... |
|
I'll take a look; it'd be ideal to make the test fail in dash as well, when expected. Can we modify the test so it's both testing |
|
Oh, you're right, on my Mac I just installed dash (via Homebrew) and ran the test by executing The Anyway, I wouldn't bother with the tests on travis-ci anymore. I mean, the function |
See #2005 (comment)