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

Fix Issue #367: Value of $_ overwritten in bash #381

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

HaleTom
Copy link

@HaleTom HaleTom commented Dec 3, 2016

Fix Issue #367 based on this answer

@postmodern
Copy link
Owner

I don't think how we hook in chruby_auto works within the unit-test functions. I had to explicitly invoke chruby_auto to simulate it running after other commands.

@@ -121,6 +121,12 @@ function test_chruby_auto_invalid_ruby_version()
"$expected_auto_version" "$RUBY_AUTO_VERSION"
}

function test_chruby_auto_preserves_dollar_underscore()
{
: expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to be

: expected 1 2 3
assertEquals "value of $_ does not change" "$_" "expected 1 2 3"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be just 3 as it's the last argument only...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The : is just a dummy command - one that returns true and has the side-effect of setting $_.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be rewritten as explicit code that sets $_ or returns an a return value?

@HaleTom
Copy link
Author

HaleTom commented Dec 7, 2016

@postmodern how should I deal with that in this PR?

@HaleTom
Copy link
Author

HaleTom commented Dec 20, 2016

@postmodern Merry Festivus. Where to from here?

@@ -22,12 +23,13 @@ function chruby_auto() {
chruby_reset
unset RUBY_AUTO_VERSION
fi
: "$old_" # restore $_ (last argument of last command executed)
}

if [[ -n "$ZSH_VERSION" ]]; then
if [[ ! "$preexec_functions" == *chruby_auto* ]]; then
preexec_functions+=("chruby_auto")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe zsh also supports the $_ variable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but it does not suffer from this issue.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be time to create a special chruby_auto_bash function to wrap the Bash-specific logic.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, the save/restore $_ also works in zsh, even though it is not required as zsh saves/restores automagically.

@postmodern
Copy link
Owner

postmodern commented Dec 23, 2016

@HaleTom so trap '...' DEBUG does not capture commands invoked from inside of functions, only at the top level. This means chruby_auto is automatically invoked in your unit-test. We need to device a test that actually exercises this somehow.

#!/bin/sh

trap 'echo "Command: $BASH_COMMAND"' DEBUG

func() {
	echo 'inside of a function'
}

echo 'outside of a function'
func

@HaleTom
Copy link
Author

HaleTom commented Feb 7, 2017

set -o functrace is what you're after.

Adding this, the output is:

Command: echo 'outside of a function'
outside of a function
Command: func
Command: func
Command: echo 'inside of a function'
inside of a function

I'm not sure why Command: func is duplicated... let me know if it's an issue. This may help.

I'm having a brain-fart: how do I run the tests again? (I couldn't see anything in CONTRIBUTING about it).

Let me know if I can help out with anything from here.

@HaleTom
Copy link
Author

HaleTom commented May 19, 2017

@postmodern Here is the code you want:

#!/bin/bash
set +o functrace
trap 'trap_func' DEBUG

trap_func() {
  [[ $BASH_COMMAND != "${FUNCNAME:0}" ]] && echo "Command: $BASH_COMMAND"
}

func() {
  echo 'inside of a function'
}

echo 'outside of a function'
func

Please let me know what the next step is.

@HaleTom
Copy link
Author

HaleTom commented Oct 27, 2017

@postmodern, @steakknife,
Any blockers to merge this?

@HaleTom
Copy link
Author

HaleTom commented Jul 20, 2018

Just checking in on this as it's been one human gestation since the last comment.

What is the next step here? @postmodern @steakknife

@HaleTom
Copy link
Author

HaleTom commented Aug 25, 2021

I see that Travis on Apple didn't seem to run correctly - it has null output.

Is this expected? Any other blockers to merging?

@postmodern
Copy link
Owner

@HaleTom Apple (still) ships with Bash 3, so unfortunately all code has to be Bash 3 compatible. I have since switched to GitHub Actions, however I still need to fix how the test suite downloads/installs the macOS test ruby, which must be compatible with GitHub Action version of macOS.

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

Successfully merging this pull request may close these issues.

3 participants