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

set -u is not supported #8

Closed
srcbucket opened this issue Nov 8, 2020 · 0 comments · Fixed by #9
Closed

set -u is not supported #8

srcbucket opened this issue Nov 8, 2020 · 0 comments · Fixed by #9

Comments

@srcbucket
Copy link

bats-mock fails if optional arguments to getter or setter functions are omitted and the expansion of unset variables / parameters is not allowed (that is, when set -u is used).

Some people are using set -euo pipefail in scripts to enforce a clean coding style. Sourcing such a script for testing may break tests using bats-mock.

How to reproduce:

#!/usr/bin/env bats

load bats-mock/src/bats-mock

setup() {
        mock="$(mock_create)"
        "${mock}"
}

@test "mock_get_call_args() with omitted optional argument succeeds when expansion of unset arguments is allowed" {
        set +u

        run mock_get_call_args "${mock}"

        [[ "${status}" -eq 0 ]]
}

@test "mock_get_call_args() with omitted optional argument fails with unbound variable error when expansion of unset arguments is not allowed" {
        set -u

        run mock_get_call_args "${mock}"

        [[ "${status}" -ne 0 ]]
        [[ "${output}" =~ '/bats-mock.bash: line '[0-9]*': $2: unbound variable' ]]
}

Workaround:

Instead of

        [[ "$(mock_get_call_args "${mock}")" -ne 0 ]]

use

        [[ "$(set +u; mock_get_call_args "${mock}")" -ne 0 ]]

Possible Solution:
Use ${2-} instead of $2 (provided $2 is optional).

Example (from mock_get_call_args):

  n="$(mock_default_n ${mock} ${2-})" || exit "$?"
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

Successfully merging a pull request may close this issue.

1 participant