diff options
author | EuAndreh <eu@euandre.org> | 2025-05-04 09:45:46 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-05-04 09:45:46 -0300 |
commit | 0735b6e8ceb5525c43156b3c9ec172979eb5e65c (patch) | |
tree | f60df966782a8639fb5a6876e0e7cd29aa80fae5 | |
parent | Add i18n for manpages (diff) | |
download | uuid-0735b6e8ceb5525c43156b3c9ec172979eb5e65c.tar.gz uuid-0735b6e8ceb5525c43156b3c9ec172979eb5e65c.tar.xz |
tests/{cli-opts,integration}.sh: Add test implementations
-rwxr-xr-x | tests/cli-opts.sh | 54 | ||||
-rwxr-xr-x | tests/integration.sh | 26 | ||||
-rw-r--r-- | tests/lib.sh | 122 |
3 files changed, 202 insertions, 0 deletions
diff --git a/tests/cli-opts.sh b/tests/cli-opts.sh index e69de29..39f01f4 100755 --- a/tests/cli-opts.sh +++ b/tests/cli-opts.sh @@ -0,0 +1,54 @@ +#!/bin/sh +set -eu + +. tests/lib.sh + + +test_generates_uuid_with_0_args() { + testing 'generates UUID with 0 arguments' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./uuid.bin 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 0 + assert_empty_stderr + rm "$OUT" "$ERR" + + test_ok +} + +test_checks_string_with_1_arg() { + testing 'checks string with 1 arg' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./uuid.bin 'cac94e13-41fa-40c4-bd46-5b7b3b46c09e' 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 0 + assert_empty_stdout + assert_empty_stderr + rm "$OUT" "$ERR" + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./uuid.bin 'bad-uuid-string' 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 3 + assert_empty_stdout + assert_stderr "uuid: str isn't of the correct length" + rm "$OUT" "$ERR" + + test_ok +} + + + +test_generates_uuid_with_0_args +test_checks_string_with_1_arg diff --git a/tests/integration.sh b/tests/integration.sh index e69de29..04d37ac 100755 --- a/tests/integration.sh +++ b/tests/integration.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -eu + +. tests/lib.sh + + +test_a_generated_uuid_is_always_valid() { + testing 'a generated UUID is always valid' + + N="$LINENO" + OUT="$(mkstemp)" + ERR="$(mkstemp)" + trap 'rm -f "$OUT" "$ERR"' EXIT + STATUS=0 + ./uuid.bin "$(./uuid.bin)" 1>"$OUT" 2>"$ERR" || STATUS=$? + assert_status 0 + assert_empty_stdout + assert_empty_stderr + rm "$OUT" "$ERR" + + test_ok +} + + + +test_a_generated_uuid_is_always_valid diff --git a/tests/lib.sh b/tests/lib.sh new file mode 100644 index 0000000..649c61d --- /dev/null +++ b/tests/lib.sh @@ -0,0 +1,122 @@ +#!/bin/sh + + +export TMPDIR="${TMPDIR:-$XDG_RUNTIME_DIR}" + +end="\033[0m" +red="\033[0;31m" +green="\033[0;32m" +yellow="\033[0;33m" + +N= +OUT= +ERR= +STATUS= + +ERROR() { + # shellcheck disable=2059 + printf "${red}ERROR${end}" +} + +print_debug_info() { + # shellcheck disable=2016 + printf 'LINENO: %s\n$OUT: %s\n$ERR: %s\n' \ + "$N" "$OUT" "$ERR" >&2 +} + +assert_status() { + if [ "$STATUS" != "$1" ]; then + printf '\n%s: Bad status.\n\nexpected: %s\ngot: %s\n' \ + "$(ERROR)" "$1" "$STATUS" >&2 + print_debug_info + exit 1 + fi +} + +assert_usage() { + if ! grep -Fq 'Usage' "$1"; then + echo 'Expected to find "Usage" text, it was missing:' >&2 + cat "$1" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stream() { + if [ -s "$2" ]; then + FMT='\n%s: Expected %s (%s) to be empty, but has content:\n%s\n' + # shellcheck disable=2059 + printf "$FMT" \ + "$(ERROR)" "$1" "$2" "$(cat "$2")" >&2 + print_debug_info + exit 1 + fi +} + +assert_empty_stdout() { + assert_empty_stream STDOUT "$OUT" +} + +assert_empty_stderr() { + assert_empty_stream STDERR "$ERR" +} + +assert_stream() { + if [ "$(cat "$2")" != "$3" ]; then + 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 +} + +assert_stdout() { + assert_stream STDOUT "$OUT" "$1" +} + +assert_stderr() { + assert_stream STDERR "$ERR" "$1" +} + +assert_grep_stream() { + if ! grep -qE "$3" "$2"; then + 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 +} + +assert_grep_stdout() { + assert_grep_stream STDOUT "$OUT" "$1" +} + +assert_grep_stderr() { + assert_grep_stream STDERR "$ERR" "$1" +} + +assert_fgrep_stream() { + if ! grep -Fq -- "$3" "$2"; then + 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 +} + +assert_fgrep_stdout() { + assert_fgrep_stream STDOUT "$OUT" "$1" +} + +assert_fgrep_stderr() { + assert_fgrep_stream STDERR "$ERR" "$1" +} + +testing() { + printf "${yellow}testing${end}: %s..." "$1" >&2 +} + +test_ok() { + # shellcheck disable=2059 + printf " ${green}OK${end}.\n" >&2 +} |