From 33579d6a22107834f8007a20559f05e55f5ec669 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 17 Feb 2021 23:55:34 -0300 Subject: Refactor tests/lib.sh --- tests/cli-opts.sh | 1 + tests/lib.sh | 57 ++++++++++++++++++++++++++++++++++++++++++------------- tests/ranking.sh | 3 +++ 3 files changed, 48 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh index 05a898e..d1fee95 100755 --- a/tests/cli-opts.sh +++ b/tests/cli-opts.sh @@ -1,6 +1,7 @@ #!/bin/sh set -u +. aux/utils.sh . tests/lib.sh assert_usage() { diff --git a/tests/lib.sh b/tests/lib.sh index 9f3c629..957566e 100755 --- a/tests/lib.sh +++ b/tests/lib.sh @@ -1,6 +1,5 @@ #!/bin/sh -export XDG_DATA_HOME="$PWD/tests/test-profiles" OUT= ERR= STATUS= @@ -13,28 +12,60 @@ assert_status() { fi } +assert_usage() { + if ! grep -Fq 'Usage' "$1"; then + echo 'Expected to find "Usage" text, it was missing:' >&2 + cat "$1" >&2 + exit 1 + fi +} + +assert_empty_stream() { + if [ -s "$2" ]; then + printf '\nERR: Expected %s (%s) to be empty, but has content:\n%s\n' \ + "$1" "$2" "$(cat "$2")" >&2 + exit 1 + fi +} + +assert_empty_stdout() { + assert_empty_stream STDOUT "$OUT" +} + assert_empty_stderr() { - if [ "$(cat "$ERR")" != '' ]; then - printf '\nERR: Expected STDERR (%s) to be empty, but has content:\n%s\n' \ - "$ERR" "$(cat "$ERR")" >&2 + assert_empty_stream STDERR "$ERR" +} + +assert_stream() { + if [ "$(cat "$2")" != "$3" ]; then + printf '\nERR: Bad %s (%s)\n\nexpected: %s\ngot: %s\n' \ + "$1" "$2" "$3" "$(cat "$2")" >&2 exit 1 fi } assert_stdout() { - if [ "$(cat "$OUT")" != "$1" ]; then - printf '\nERR: Bad STDOUT (%s)\n\nexpected: %s\ngot: %s\n' \ - "$OUT" "$1" "$(cat "$OUT")" >&2 + assert_stream STDOUT "$OUT" "$1" +} + +assert_stderr() { + assert_stream STDERR "$ERR" "$1" +} + +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 exit 1 fi } -uuid() { - # Taken from: - # https://serverfault.com/a/799198 - od -xN20 /dev/urandom | \ - head -1 | \ - awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' +assert_grep_stdout() { + assert_grep_stream STDOUT "$OUT" "$1" +} + +assert_grep_stderr() { + assert_grep_stream STDERR "$ERR" "$1" } testing() { diff --git a/tests/ranking.sh b/tests/ranking.sh index c3bee84..3b9ac6e 100755 --- a/tests/ranking.sh +++ b/tests/ranking.sh @@ -1,8 +1,11 @@ #!/bin/sh set -eu +. aux/utils.sh . tests/lib.sh +export XDG_DATA_HOME="$PWD/tests/test-profiles" + test_picking_first_makes_it_be_always_first() { testing 'picking first makes it be always first' OUT="$(mktemp)" -- cgit v1.2.3