Skip to content

Commit 20d52b6

Browse files
enlightened88ljharb
andcommitted
[tests] remove double-substitution in assert_ok and assert_not_ok
Co-authored-by: Андрій Шовкошитний <198119344+enlightened88@users.noreply.github.com> Co-authored-by: Jordan Harband <ljharb@gmail.com>
1 parent d200a21 commit 20d52b6

4 files changed

Lines changed: 59 additions & 3 deletions

File tree

test/common.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ assert_ok() {
2828
local FUNCTION=$1
2929
shift
3030

31-
$($FUNCTION $@) || die '"'"$FUNCTION $@"'" should have succeeded, but failed'
31+
"$FUNCTION" "$@" || die '"'"$FUNCTION $@"'" should have succeeded, but failed'
3232
}
3333

3434
assert_not_ok() {
3535
local FUNCTION=$1
3636
shift
3737

38-
! $($FUNCTION $@) || die '"'"$FUNCTION $@"'" should have failed, but succeeded'
38+
! "$FUNCTION" "$@" || die '"'"$FUNCTION $@"'" should have failed, but succeeded'
3939
}
4040

4141
strip_colors() {
@@ -46,7 +46,7 @@ strip_colors() {
4646

4747
make_echo() {
4848
echo "#!/bin/sh" > "$1"
49-
echo "echo \"${2}\"" > "$1"
49+
echo "echo \"${2}\"" >> "$1"
5050
chmod a+x "$1"
5151
}
5252

test/fast/Unit tests/assert_not_ok

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
die () { echo "$@" ; exit 1; }
4+
5+
\. ../../common.sh
6+
7+
_returns_zero() { return 0; }
8+
_returns_nonzero() { return 1; }
9+
10+
# assert_not_ok should pass for a non-zero-exit function
11+
assert_not_ok _returns_nonzero \
12+
|| die 'assert_not_ok failed on a function that returns 1'
13+
14+
# assert_not_ok should fail for a zero-exit function
15+
if (assert_not_ok _returns_zero 2>/dev/null); then
16+
die 'assert_not_ok incorrectly passed for a function that returns 0'
17+
fi

test/fast/Unit tests/assert_ok

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
die () { echo "$@" ; exit 1; }
4+
5+
\. ../../common.sh
6+
7+
_returns_zero() { return 0; }
8+
_returns_nonzero() { return 1; }
9+
10+
# assert_ok should pass for a zero-exit function
11+
assert_ok _returns_zero \
12+
|| die 'assert_ok failed on a function that returns 0'
13+
14+
# assert_ok should fail for a non-zero-exit function
15+
if (assert_ok _returns_nonzero 2>/dev/null); then
16+
die 'assert_ok incorrectly passed for a function that returns 1'
17+
fi

test/fast/Unit tests/make_echo

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
die () { echo "$@" ; exit 1; }
4+
5+
\. ../../common.sh
6+
7+
TMPFILE="$(mktemp)"
8+
trap 'rm -f "${TMPFILE}"' EXIT
9+
10+
make_echo "${TMPFILE}" "hello_nvm" || die 'make_echo returned non-zero'
11+
12+
# shebang must still be on line 1
13+
[ "$(head -n 1 "${TMPFILE}")" = '#!/bin/sh' ] \
14+
|| die 'make_echo overwrote the shebang'
15+
16+
# script body must be present
17+
grep -q 'hello_nvm' "${TMPFILE}" \
18+
|| die 'make_echo did not write the echo body'
19+
20+
# file must be executable
21+
[ -x "${TMPFILE}" ] \
22+
|| die 'make_echo did not chmod the file'

0 commit comments

Comments
 (0)