From e82a5a5fb28cb4afd9e81d00b5bf64664bd37ff2 Mon Sep 17 00:00:00 2001 From: pvincent Date: Fri, 14 Aug 2026 12:16:26 +0400 Subject: [PATCH] fail_stderr_grep --- test/miaou-bash.test.bash | 53 ++++++++++++++++++++++++++++----------- test/stub/scenario.bash | 31 +++++++++++++++++++++++ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/test/miaou-bash.test.bash b/test/miaou-bash.test.bash index ce40078..9fa7a31 100755 --- a/test/miaou-bash.test.bash +++ b/test/miaou-bash.test.bash @@ -2,15 +2,19 @@ # CONSTANTS -readonly TMP_DIRECTORY="/tmp/$(basename "$0")-$UID" +TMP_DIRECTORY="/tmp/$(basename "$0")-$UID" +TMP_OUT="$TMP_DIRECTORY/_stdout" +TMP_ERR="$TMP_DIRECTORY/_stderr" +TMP_MIAOU="$TMP_DIRECTORY/_miaou" +readonly TMP_DIRECTORY TMP_OUT TMP_ERR TMP_MIAOU # functions function assert { ((test_count++)) cmd="./test/stub/scenario.bash $1" - if $cmd >/dev/null 2>/dev/null 3>"$TMP_DIRECTORY"/_miaou; then - if [[ -s "$TMP_DIRECTORY"/_miaou ]]; then + if $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then + if [[ -s "$TMP_MIAOU" ]]; then echo -n . return fi @@ -31,8 +35,8 @@ function assert_grep { local script="$1" content="$2" extra_args=() [[ $# -gt 3 ]] && shift 3 && extra_args=("$@") cmd="./test/stub/scenario.bash $script" - if $cmd "${extra_args[@]}" >"$TMP_DIRECTORY"/_stdout 2>/dev/null 3>/dev/null; then - if grep -q "$content" "$TMP_DIRECTORY"/_stdout; then + if $cmd "${extra_args[@]}" >"$TMP_OUT" 2>/dev/null 3>/dev/null; then + if grep -q "$content" "$TMP_OUT"; then echo -n . return fi @@ -52,15 +56,15 @@ function fail_type { cmd="./test/stub/scenario.bash $1" # mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)" - $cmd >/dev/null 2>"$TMP_DIRECTORY"/_stderr 3>"$TMP_DIRECTORY"/_miaou - mapfile -t stderr <"$TMP_DIRECTORY"/_stderr + $cmd >/dev/null 2>"$TMP_ERR" 3>"$TMP_MIAOU" + mapfile -t stderr <"$TMP_ERR" last_error="${stderr[-1]}" regex="^(.*): line ([0-9]+): miaou_error: $2 ([0-9]+)" [[ "${last_error}" =~ $regex ]] && success=true if [[ $success == true && -n "${hint_message}" ]]; then - grep -q "${hint_message}" "$TMP_DIRECTORY"/_miaou || success=false + grep -q "${hint_message}" "$TMP_MIAOU" || success=false fi if [[ $success == true ]]; then @@ -72,14 +76,28 @@ function fail_type { return 1 } +function fail_stderr_grep { + ((test_count++)) + cmd="./test/stub/scenario.bash $1" + stderr_line="$2" + if ! $cmd 2>"$TMP_ERR" 3>/dev/null && grep -q "$stderr_line" "$TMP_ERR"; then + echo -n . + return + fi + + echo -n F + failures+=("$cmd 2>&1 3>/dev/null | grep \"$stderr_line\"") + return 1 +} + function fail_stderr_count { ((test_count++)) local regex count="$2" cmd="./test/stub/scenario.bash $1" # mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)" - if ! $cmd >/dev/null 2>"$TMP_DIRECTORY"/_stderr 3>/dev/null; then - mapfile -t stderr <"$TMP_DIRECTORY"/_stderr + if ! $cmd >/dev/null 2>"$TMP_ERR" 3>/dev/null; then + mapfile -t stderr <"$TMP_ERR" if [[ "${#stderr[@]}" == "$count" ]]; then echo -n . return @@ -87,7 +105,7 @@ function fail_stderr_count { fi echo -n F - failures+=("$cmd 2>"$TMP_DIRECTORY"/_stderr") + failures+=("$cmd 2>$TMP_ERR") return 1 } @@ -97,8 +115,8 @@ function fail_miaou_count { cmd="./test/stub/scenario.bash $1" # mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)" - if ! $cmd >/dev/null 2>/dev/null 3>"$TMP_DIRECTORY"/_miaou; then - mapfile -t miaou <"$TMP_DIRECTORY"/_miaou + if ! $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then + mapfile -t miaou <"$TMP_MIAOU" if [[ "${#miaou[@]}" == "$count" ]]; then echo -n . return @@ -106,7 +124,7 @@ function fail_miaou_count { fi echo -n F - failures+=("$cmd 3>$TMP_DIRECTORY/_miaou") + failures+=("$cmd 3>$TMP_MIAOU") return 1 } # main @@ -123,6 +141,9 @@ time { assert exit_0 assert return_conditional assert_grep success_with_args 'count args: 2' -- 'A B' C + assert heredoc + assert pipe1 + assert pipe2 fail_type subshell_term SUBSHELL_ERROR fail_type unbound_variable UNBOUND_VARIABLE @@ -141,7 +162,9 @@ time { fail_stderr_count child_error 4 fail_miaou_count command_error_multiline 7 - echo TODO: source EOF + pipe + fail_stderr_grep two_options 'ERROR: too many args 2, either single option or SCRIPT + args accepted!' + fail_stderr_grep script_not_found "ERROR: script 'doesnt_exist' not found!" + fail_stderr_grep no_script 'ERROR: script expected!' } failed=${#failures[@]} diff --git a/test/stub/scenario.bash b/test/stub/scenario.bash index 85fc09d..f27e075 100755 --- a/test/stub/scenario.bash +++ b/test/stub/scenario.bash @@ -12,6 +12,37 @@ function do_subshell_false { result=$(false) } +function do_heredoc { + # miaou-bash <