From 4039717113876f6f1352130190f790177ecb983a Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 28 Jun 2021 07:14:55 -0300 Subject: tests/*.sh: Refactor error messages to be consistent, move helper all helper funtions to the top --- tests/ranking.sh | 81 +++++++++++++++++++++++++++--------------------------- tests/tests-lib.sh | 14 +++++----- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/tests/ranking.sh b/tests/ranking.sh index 45629d8..807f17b 100755 --- a/tests/ranking.sh +++ b/tests/ranking.sh @@ -6,25 +6,6 @@ set -eu export XDG_DATA_HOME="$PWD/tests/test-profiles" export LANG=C.UTF-8 -test_picking_first_makes_it_be_always_first() { - testing 'picking first makes it be always first' - OUT="$(mktemp)" - ERR="$(mktemp)" - PROFILE="always-picks-first-$(uuid)" - for _ in $(seq 10); do - printf 'always-picked\nnever-picked\n' | \ - ./src/remembering \ - -p "$PROFILE" \ - -c 'head -n1' \ - 1>"$OUT" 2>"$ERR" - STATUS=$? - assert_status 0 - assert_empty_stderr - assert_stdout 'always-picked' - done - test_ok -} - INPUT='a b c @@ -69,12 +50,49 @@ pick_x() { assert_first() { FIRST="$(head -n1 "$ERR")" if [ "$FIRST" != "$1" ]; then - printf '\nERR: Previous choice did not appear at the beginning of the list:\n\nexpected: %s\ngot: %s\n' \ - "$1" "$FIRST" >&2 + printf '\n%s: Previous choice did not appear at the beginning of the list:\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$FIRST" >&2 + exit 1 + fi +} + +BASE_PROFILE='0:a +0:b +0:c +0:d +0:e' +BASE_PROFILE_A_PICKED='1:a +0:b +0:c +0:d +0:e' +assert_profile() { + if [ "$(cat "$XDG_DATA_HOME/$1")" != "$2" ]; then + printf '\n%s: Bad profile merge (%s).\n\nExpected:\n%s\nGot\n%s\n' \ + "$(ERROR)" "$PROFILE" "$2" "$(cat "$XDG_DATA_HOME/$1")" >&2 exit 1 fi } +test_picking_first_makes_it_be_always_first() { + testing 'picking first makes it be always first' + OUT="$(mktemp)" + ERR="$(mktemp)" + PROFILE="always-picks-first-$(uuid)" + for _ in $(seq 10); do + printf 'always-picked\nnever-picked\n' | \ + ./src/remembering \ + -p "$PROFILE" \ + -c 'head -n1' \ + 1>"$OUT" 2>"$ERR" + STATUS=$? + assert_status 0 + assert_empty_stderr + assert_stdout 'always-picked' + done + test_ok +} + test_promoting_values() { testing 'promoting values' PROFILE="promoting-$(uuid)" @@ -151,7 +169,8 @@ m s' ACTUAL="$(head -n9 "$ERR")" if [ "$ACTUAL" != "$EXPECTED" ]; then - printf '\nERR: Bad order!\n\nexpected: %s\ngot: %s\n' \ + printf '\n%s: Bad order!\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" \ "$(echo "$EXPECTED" | tr '\n' ' ')" \ "$(echo "$ACTUAL" | tr '\n' ' ')" \ >&2 @@ -181,24 +200,6 @@ e' test_ok } -BASE_PROFILE='0:a -0:b -0:c -0:d -0:e' -BASE_PROFILE_A_PICKED='1:a -0:b -0:c -0:d -0:e' -assert_profile() { - if [ "$(cat "$XDG_DATA_HOME/$1")" != "$2" ]; then - printf '\nERR: Bad profile merge (%s).\n\nExpected:\n%s\nGot\n%s\n' \ - "$PROFILE" "$2" "$(cat "$XDG_DATA_HOME/$1")" >&2 - exit 1 - fi -} - test_stdin_is_larger_than_profile() { testing 'STDIN is larger than profile' PROFILE="stdin-is-larger-than-profile-$(uuid)" diff --git a/tests/tests-lib.sh b/tests/tests-lib.sh index a3ce7dc..76f22e3 100755 --- a/tests/tests-lib.sh +++ b/tests/tests-lib.sh @@ -16,7 +16,7 @@ ERROR() { } print_debug_info() { - printf 'LINENO: %s\nOUT: %s\nERR: %s\n' "$N" "$OUT" "$ERR" >&2 + printf 'LINENO: %s\nOUT: %s\n%s: %s\n' "$N" "$OUT" "$(ERROR)" "$ERR" >&2 } assert_status() { @@ -56,8 +56,8 @@ assert_empty_stderr() { assert_stream() { if [ "$(cat "$2")" != "$3" ]; then - printf '\nERR: Bad %s (%s)\n\nexpected: %s\ngot: %s\n' \ - "$1" "$2" "$3" "$(cat "$2")" >&2 + printf '\n%s: Bad %s (%s)\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 print_debug_info exit 1 fi @@ -73,8 +73,8 @@ assert_stderr() { assert_grep_stream() { if ! grep -qE "$3" "$2"; then - printf '\nERR: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ - "$1" "$2" "$3" "$(cat "$2")" >&2 + printf '\n%s: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 print_debug_info exit 1 fi @@ -90,8 +90,8 @@ assert_grep_stderr() { assert_fgrep_stream() { if ! grep -Fq -- "$3" "$2"; then - printf '\nERR: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ - "$1" "$2" "$3" "$(cat "$2")" >&2 + printf '\n%s: Bad %s (%s)\n\ngrepping: %s\nin:\n%s\n' \ + "$(ERROR)" "$1" "$2" "$3" "$(cat "$2")" >&2 print_debug_info exit 1 fi -- cgit v1.2.3