jmelahman / bats-shellmock

A shell script mocking utility/framework for the BASH shell
Apache License 2.0
0 stars 1 forks source link

exit status is 99 for mock reading from pipe #3

Open magwas opened 1 year ago

magwas commented 1 year ago

Sorry to bother you, but you have the most up-to-date repo of shellmock with issues open for submission. It is still the least worst mocking lib for bats. so I treat you here as acting maintainer. With the understanding that you have all rights not to act like one. I am willing to work on the issue, but have no idea where to start.

I have found that if the stub reads from a pipe, the exit status of it is 99. It only observable if that stub call is the last one in the sut, but doing TDD it is often the case, and it probably hints to some failure in the code running after the stub.

I would like that to be fixed.

I have minimal code to recreate and experiment with the problem. I left diagnostics in the test and commented out experiments in the sut.

The test:

. shellmock

@test "does not like pipes" {
    shellmock_clean
    shellmock_expect grep -t regex -m "." -s 0

    run batbug

    shellmock_verify
    echo "status:$status"
    echo "capture 0:${capture[0]}"
    echo "capture 1:${capture[1]}"
    echo "capture 2:${capture[2]}"
    expected="grep-stub root"
    [[ ${capture[0]} =~ $expected ]]
    [ "$status" -eq 0 ]
}

batbug, the sut:

#!/bin/bash

grep root /etc/passwd
cat /etc/passwd |grep daemon
#echo "status:$?"
#grep root /etc/passwd
magwas commented 1 year ago

Okay, I have found the problem. The default for -T is exact, and for -M is "". So if there is any stdin, there will not be matching mocks, unless some have set -T and/or -M.

It is a sane behaviour, though not apparent from the README.

Also, calls to mock without match return with exit status 99. This is also a sane behaviour, just utterly undocumented.