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

$_ does not work correctly in functions called from test #798

Open
tooptoop4 opened this issue Sep 29, 2023 · 1 comment
Open

$_ does not work correctly in functions called from test #798

tooptoop4 opened this issue Sep 29, 2023 · 1 comment
Labels
Component: Bash Code Everything regarding the bash code Component: Docs Priority: Low Spelling mistakes Status: Confirmed The reproducer worked as described Type: Bug

Comments

@tooptoop4
Copy link

my code file (mycode.sh) has:

function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
if [ "${0##*/}" = mycode.sh ]; then
echo do_stuff
fi

my test file has:

. mycode.sh

@test "urldecode test" {
    urldecode 'day%3D29' | grep -qFx 'day=29'
}

To Reproduce
/usr/local/bin/bats /fol/tes.sh fails it seems to get the output of filename instead of day

Expected behavior
test should pass

Environment (please complete the following information):

  • Bats version: Bats 1.10.0
  • operating system (including version): Debian GNU/Linux 11 (bullseye)
  • bash --version: GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)
  • Install method: git
@tooptoop4 tooptoop4 added Priority: NeedsTriage Issue has not been vetted yet Type: Bug labels Sep 29, 2023
@martin-schulze-vireso martin-schulze-vireso added Component: Docs Priority: Low Spelling mistakes Component: Bash Code Everything regarding the bash code Status: Confirmed The reproducer worked as described and removed Priority: NeedsTriage Issue has not been vetted yet labels Oct 13, 2023
@martin-schulze-vireso
Copy link
Member

martin-schulze-vireso commented Oct 13, 2023

This is most likely a bad interaction with the debug trap. I am afraid we can't do much about that since the debug trap is needed. However, you can likely work around that by not calling your function directly in the test but under run.

I think this would warrant a section in the gotchas documentation.

Once you are using run, I would suggest you also use assert_output from the bats-assert library to get better diagnostics. This would be the resulting code:

setup() {
  # sourcing in setup gives better diagnostics than in free code
  . mycode.sh
  bats_load_library bats-support # needed as helper for bats-assert
  bats_load_library bats-assert
}

@test "urldecode test" {
    run urldecode 'day%3D29' 
    assert-output 'day=29'
}

@martin-schulze-vireso martin-schulze-vireso changed the title output not captured correctly? $_ does not work correctly in functions called from test Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Bash Code Everything regarding the bash code Component: Docs Priority: Low Spelling mistakes Status: Confirmed The reproducer worked as described Type: Bug
Projects
None yet
Development

No branches or pull requests

2 participants