-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Fix profile var #957
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
Fix profile var #957
Conversation
Add tests for PROFILE overriding detected files
Linking to #833 |
# But $PROFILE should override | ||
_PROFILE="$(PROFILE=test_profile nvm_detect_profile)" | ||
_PROFILE=$(nvm_detect_profile) | ||
[ "_$_PROFILE" = "_$PROFILE" ] || ( echo "_\$_PROFILE: _$_PROFILE" && |
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.
this reads pretty confusing to me - could we rename _PROFILE
to DETECTED_PROFILE
or something?
# | ||
|
||
# .bashrc should be detected for bash | ||
NVM_DETECT_PROFILE="$(declare SHELL="/bin/bash"; unset PROFILE; nvm_detect_profile)" |
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.
just curious, why declare
instead of SHELL=/bin/bash PROFILE= nvm_detect_profile
?
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.
I was trying to figure out how to set variables inside a command substitution and for some reason that was the way I found. I'm not totally sure of the implications, using declare
seems ok. Retrying it now, I think I could have just added semicolons and didn't have to use declare
as the following shows.
$ TEST="123"; echo $(TEST="ABC" echo $TEST); echo $TEST
123
123
$ TEST="123"; echo $(TEST="ABC"; echo $TEST); echo $TEST
ABC
123
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.
It'd be ideal to simplify where possible. generally, you can set env vars for the duration of a command like TEST="ABC" echo $TEST
without having to use separate statements. does that work here?
I tried to keep the same logic and rewrote the tests. The |
The "if" approach is fine with me, altho the |
I removed the unnecessary usage of declare. Everything else is ok? |
fi | ||
|
||
# $PROFILE should override .bashrc profile detection | ||
NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; PROFILE="test_profile"; nvm_detect_profile)" |
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.
do these not work without the semicolons? I'd expect just SHELL="/bin/bash" PROFILE="test_profile" nvm_detect_profile
to work.
Some of the tests will not return an error even if the semicolon is left out due to side-effects. Though, to obtain the intended logic the semicolons are needed. # $PROFILE is not a valid file
# In this case no profile is set and nvm_detect_profile returns an empty value,
# still resulting in the test passing
rm "test_profile"
NVM_DETECT_PROFILE="$(PROFILE="test_profile" nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" = "test_profile" ]; then
die "nvm_detect_profile picked \$PROFILE when it was an invalid file"
fi Play around with the following code in your shell. The command substitution runs in a subshell containing its parent's variables, though, its parent doesn't have access to the child processes variables. As utilized in the tests, command substitutions are replaced by their stdout. $ TEST="123"; echo $(TEST="ABC" echo $TEST); echo $TEST
123
123
$ TEST="123"; echo $(TEST="ABC"; echo $TEST); echo $TEST
ABC
123 Also, you can try to make the tests fail to confirm they are not passing due to a side-effect. That is what I did. I'm still trying to learn more about shell and bash programming, there may still be some minor issues, though, I think less than before. I found this article covers a number of potential gotchas. |
Thanks! |
I just rebased david’s patch and applied your comments.
Let me know if there are any other changes that are needed to accept this.
Thank you!