Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active December 9, 2024 08:27
Show Gist options
  • Save x-yuri/e5b0baabc76e5e4a00d3e1fb40ebe256 to your computer and use it in GitHub Desktop.
Save x-yuri/e5b0baabc76e5e4a00d3e1fb40ebe256 to your computer and use it in GitHub Desktop.
bats

bats

a.bats:

strict() { set -euo pipefail; shopt -s inherit_errexit; "$@"; }

setup() {
    [ "$BATS_LIB_PATH" = /usr/lib/bats ] && BATS_LIB_PATH=$HOME/.bats/lib:$BATS_LIB_PATH
    bats_load_library bats-support
    bats_load_library bats-assert
    strict
}

f() { :; }

@test whatever {
    run strict f

    assert_output ''
}
$ docker run --rm -itv "$PWD":/app -w /app alpine:3.21
/ # apk add git bash ncurses
/ # git clone https://github.com/bats-core/bats-core ~/.bats
/ # git clone https://github.com/bats-core/bats-support ~/.bats/lib/bats-support
/ # git clone https://github.com/bats-core/bats-assert ~/.bats/lib/bats-assert
/ # ~/.bats/bin/bats a.bats

ncurses is needed for the pretty formatter.

b.bats:

strict() { set -euo pipefail; shopt -s inherit_errexit; "$@"; }

setup() {
    [ "$BATS_LIB_PATH" = /usr/lib/bats ] && BATS_LIB_PATH=$HOME/.bats/lib:$BATS_LIB_PATH
    bats_load_library bats-support
    bats_load_library bats-assert
    bats_load_library bats-mock/stub.bash
    strict
}

@test whatever {
    stub whatever \
        'a b c : echo output'
    run whatever a b c
    assert_output output
}

teardown() {
    unstub whatever
}
/ # git clone https://github.com/jasonkarns/bats-mock ~/.bats/lib/bats-mock
  • It only matches the arguments that are in the plan. E.g. if a plan argument is a : :, then the first argument passed to the stub must be a, the rest (if any) are ignored.
  • The stub must be called at least the specified number of times in the specified order (for the test to pass). E.g. with plan arguments a : :, a b : :, the stub must be called with a, then with a, b.
  • If there's no : word in a plan argument, the plan argument is considered to have only a command (matches with any arguments). But : makes it match any arguments and invoke :, : makes it match any arguments and invoke an empty command.
  • If the invocation doesn't match the current plan argument or if it runs out of the plan arguments, the stub exits with 127.
  • All stubs must be unstubbed in teardown(). Because if a test fails, unstub wouldn't get invoked and the next test will run with stubs from the previous one.
  • unstub exits with an error if the stub doesn't exist. If unstub is the last argument in teardown() this makes the test fail.

c.bats:

strict() { set -euo pipefail; shopt -s inherit_errexit; "$@"; }

setup() {
    [ "$BATS_LIB_PATH" = /usr/lib/bats ] && BATS_LIB_PATH=$HOME/.bats/lib:$BATS_LIB_PATH
    bats_load_library bats-support
    bats_load_library bats-assert
    bats_load_library bats-mock
    strict
}

@test whatever {
    mock="`mock_create`"
    mock_set_output "$mock" output

    run "$mock" a b c

    [ "`mock_get_call_num "$mock"`" = 1 ]
    [ "`mock_get_call_args "$mock"`" = 'a b c' ]
    assert_output output
}
/ # git clone https://github.com/grayhemp/bats-mock ~/.bats/lib/bats-mock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment