From a1c0074ce198aa8333599f38a1343646792e9806 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Fri, 30 Jul 2021 18:54:53 -0700 Subject: [PATCH] Updated SC2293 (markdown) --- SC2293.md | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/SC2293.md b/SC2293.md index d1837e4..a678448 100644 --- a/SC2293.md +++ b/SC2293.md @@ -1,49 +1,29 @@ -## bats: ! \ will never fail the test +## When eval'ing @Q-quoted words, use * rather than @ as the index. ### Problematic code: ```sh -#!/usr/bin/env bats - -@test "test" { - # ... code - ! test_file_exists - # ... more code -} +eval "$MYCOMMAND ${@@Q}" ``` ### Correct code: ```sh -#!/usr/bin/env bats - -@test "test" { - # ... code - run ! test_file_exists - # ... more code -} +eval "$MYCOMMAND ${*@Q}" ``` ### Rationale: -Bats uses `set -e` and `trap ERR` to catch test failures as early as possible. -Although the return code of a `!` negated command is inverted, they will never trigger `errexit`, due to a bash design decision (see [Related Resources](#related-resources)). -This means that tests which use `!` can never fail. +ShellCheck noticed that you are calling `eval` and including an escaped array. However, the array is passed as multiple arguments and relies on being implicitly joined together to form a single shell string, which `eval` can then evaluate. -Starting with bats 1.5.0 you can use `!` inside `run`. -If you are still using an older bats version, you can rewrite `! ` to ` && exit 1`. +Instead, prefer building your shell string with explicit string concatenation by using `*` instead of `@` for the index, such as `${*@Q}` or `${array[*]@Q}`. + +This suggestion is equivalent to [[SC2124]], but for `eval` arguments rather than string variables. ### Exceptions: -The return code of the last command in the test will be the exit code of the test function. -This means that you can use `! ` on the last line of the test and it will still fail appropriately. -However, you are encouraged to still use `run !` in this case for consistency. - -Shellcheck won't emit this error when using `! ` as last command in a test to avoid confusing users whose tests work as intended. +None. ### Related resources: -* [SC2251: This ! is not on a condition and skips errexit](SC2251.md) -* [Stackoverflow: Why do I need parenthesis In bash `set -e` and negated return code](https://stackoverflow.com/a/39582012/760746) -* [bash manpage](https://linux.die.net/man/1/bash) (look at `trap [-lp] [[arg] sigspec ...]`): - > The ERR trap is not executed [...] if the command's return value is being inverted via ! \ No newline at end of file +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file