kward / shunit2

shUnit2 is a xUnit based unit test framework for Bourne based shell scripts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prefixing commands with command makes it easy to create stubs

alexharv074 opened this issue · comments

I have been using this project and "stubbing" out system commands and shell built-ins by defining functions in my tests.

It would be nice if I could also stub out built-ins/commands that are also used by shUnit2 internally, e.g."

[() {
  case "$*" in
  "-x /usr/local/rvm/bin/rvm ]")
    false
    ;;
  esac
}

Unfortunately, this currently breaks shUnit2 itself.

As mentioned here, however, this problem could be avoided if all calls to [ and other commands that the user might want to stub, like mkdir, rm etc, were prefixed by command.

E.g. instead of (on line 17)

[ -n "${SHUNIT_VERSION:-}" ] && exit 0

we would have

\[ -n "${SHUNIT_VERSION:-}" ] && exit 0

I tested this - and applied to a few other commands that a user might want to stub, like rm, mkdir, cat etc.

Pull request committed. Tks!

This person found a different way to stub commands https://github.com/zofrex/bourne-shell-unit-testing This is useful, because this feature will probably have to be reverted.

This solution is causing quite a few problems under macOS Catalina and the /bin/dash shell. I'm looking for some alternatives that meet your needs.

Fixed in 86f430c.