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

read fails to stub, halts test runner #23

Open
SeanDez opened this issue May 24, 2020 · 2 comments
Open

read fails to stub, halts test runner #23

SeanDez opened this issue May 24, 2020 · 2 comments

Comments

@SeanDez
Copy link

SeanDez commented May 24, 2020

When I attempt to run a test with a stub, the test runner gets stuck without throwing an error. It must be exited with CTRL + C

$ find -type f | entr ./node_modules/bats/bin/bats -r ./helpers/tests
 ✓ a/b matcher to ensure the asserts are working
 ✓ createConfigFile
 ✓ toLowercase
   gatherConfigOptions                    4/4

I need to stub read to allow automated testing to work. But this code is causing the halt:

# tests.bats
load "batsMock/stub" # saved via git submodule into this folder

@test "gatherConfigOptions" {
   # setup
   stub read \
      "echo 'returnValue1'" \
      "echo 'returnValue2'"

   # test execution & assert
   local options=$(gatherConfigOptions)
   assert_equal $options "wrongValue"

   # teardown
   unstub read
}

Also tried

  • Commenting out the unstub statement. No effect

I suspect that read is running unstubbed and causing the infinite delay. I could be wrong though.

Anyone able to tell what the issue is and how to overcome it?

Here is the function I am attempting to stub the internals of:

# ./helpers/helpers.sh

function gatherConfigOptions {
   declare -A configData

   read -p "prompt text here" "configData[languageCode]"
   read -p "prompt text here" "configData[numberOfRepeats]"

   echo "${configData[*]}"
}
@jasonkarns
Copy link
Owner

jasonkarns commented May 29, 2020

Disclaimer: I haven't looked into this deeply.

read is a shell builtin, so the builtin will always take priority over a disk command. Recall that bats-mock works by making an executable shim on disk, which is placed first in PATH, thereby "shadowing" other disk commands.

Because shell builtins take precedence over disk commands, the read builtin will never be shadowed by the bats-mock shims. 😞

However! the enable builtin allows you to disable shell builtins. 🎉

Disabling allows you to execute a disk command which has the same name as a shell builtin without using a full pathname.

See the enable man page with help enable. I can't vouch for whether it will work within a bats test, though, because the bats run command executes things in a subshell, IIRC. And I'm not certain if enable affects subshells or not. But it's worth a try, and I'd love to hear if it works for you (or not).

@anubisthejackle
Copy link

Just a note to say that I attempted this in my own tests and it didn't work. If someone else tries and figures out a way to make it work, please update here.

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