fail_stderr_grep
This commit is contained in:
+38
-15
@@ -2,15 +2,19 @@
|
|||||||
|
|
||||||
# CONSTANTS
|
# 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
|
# functions
|
||||||
|
|
||||||
function assert {
|
function assert {
|
||||||
((test_count++))
|
((test_count++))
|
||||||
cmd="./test/stub/scenario.bash $1"
|
cmd="./test/stub/scenario.bash $1"
|
||||||
if $cmd >/dev/null 2>/dev/null 3>"$TMP_DIRECTORY"/_miaou; then
|
if $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then
|
||||||
if [[ -s "$TMP_DIRECTORY"/_miaou ]]; then
|
if [[ -s "$TMP_MIAOU" ]]; then
|
||||||
echo -n .
|
echo -n .
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -31,8 +35,8 @@ function assert_grep {
|
|||||||
local script="$1" content="$2" extra_args=()
|
local script="$1" content="$2" extra_args=()
|
||||||
[[ $# -gt 3 ]] && shift 3 && extra_args=("$@")
|
[[ $# -gt 3 ]] && shift 3 && extra_args=("$@")
|
||||||
cmd="./test/stub/scenario.bash $script"
|
cmd="./test/stub/scenario.bash $script"
|
||||||
if $cmd "${extra_args[@]}" >"$TMP_DIRECTORY"/_stdout 2>/dev/null 3>/dev/null; then
|
if $cmd "${extra_args[@]}" >"$TMP_OUT" 2>/dev/null 3>/dev/null; then
|
||||||
if grep -q "$content" "$TMP_DIRECTORY"/_stdout; then
|
if grep -q "$content" "$TMP_OUT"; then
|
||||||
echo -n .
|
echo -n .
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
@@ -52,15 +56,15 @@ function fail_type {
|
|||||||
|
|
||||||
cmd="./test/stub/scenario.bash $1"
|
cmd="./test/stub/scenario.bash $1"
|
||||||
# mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)"
|
# 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
|
$cmd >/dev/null 2>"$TMP_ERR" 3>"$TMP_MIAOU"
|
||||||
mapfile -t stderr <"$TMP_DIRECTORY"/_stderr
|
mapfile -t stderr <"$TMP_ERR"
|
||||||
|
|
||||||
last_error="${stderr[-1]}"
|
last_error="${stderr[-1]}"
|
||||||
regex="^(.*): line ([0-9]+): miaou_error: $2 ([0-9]+)"
|
regex="^(.*): line ([0-9]+): miaou_error: $2 ([0-9]+)"
|
||||||
[[ "${last_error}" =~ $regex ]] && success=true
|
[[ "${last_error}" =~ $regex ]] && success=true
|
||||||
|
|
||||||
if [[ $success == true && -n "${hint_message}" ]]; then
|
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
|
fi
|
||||||
|
|
||||||
if [[ $success == true ]]; then
|
if [[ $success == true ]]; then
|
||||||
@@ -72,14 +76,28 @@ function fail_type {
|
|||||||
return 1
|
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 {
|
function fail_stderr_count {
|
||||||
((test_count++))
|
((test_count++))
|
||||||
local regex count="$2"
|
local regex count="$2"
|
||||||
|
|
||||||
cmd="./test/stub/scenario.bash $1"
|
cmd="./test/stub/scenario.bash $1"
|
||||||
# mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)"
|
# 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
|
if ! $cmd >/dev/null 2>"$TMP_ERR" 3>/dev/null; then
|
||||||
mapfile -t stderr <"$TMP_DIRECTORY"/_stderr
|
mapfile -t stderr <"$TMP_ERR"
|
||||||
if [[ "${#stderr[@]}" == "$count" ]]; then
|
if [[ "${#stderr[@]}" == "$count" ]]; then
|
||||||
echo -n .
|
echo -n .
|
||||||
return
|
return
|
||||||
@@ -87,7 +105,7 @@ function fail_stderr_count {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo -n F
|
echo -n F
|
||||||
failures+=("$cmd 2>"$TMP_DIRECTORY"/_stderr")
|
failures+=("$cmd 2>$TMP_ERR")
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,8 +115,8 @@ function fail_miaou_count {
|
|||||||
|
|
||||||
cmd="./test/stub/scenario.bash $1"
|
cmd="./test/stub/scenario.bash $1"
|
||||||
# mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)"
|
# 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
|
if ! $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then
|
||||||
mapfile -t miaou <"$TMP_DIRECTORY"/_miaou
|
mapfile -t miaou <"$TMP_MIAOU"
|
||||||
if [[ "${#miaou[@]}" == "$count" ]]; then
|
if [[ "${#miaou[@]}" == "$count" ]]; then
|
||||||
echo -n .
|
echo -n .
|
||||||
return
|
return
|
||||||
@@ -106,7 +124,7 @@ function fail_miaou_count {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
echo -n F
|
echo -n F
|
||||||
failures+=("$cmd 3>$TMP_DIRECTORY/_miaou")
|
failures+=("$cmd 3>$TMP_MIAOU")
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
# main
|
# main
|
||||||
@@ -123,6 +141,9 @@ time {
|
|||||||
assert exit_0
|
assert exit_0
|
||||||
assert return_conditional
|
assert return_conditional
|
||||||
assert_grep success_with_args 'count args: 2' -- 'A B' C
|
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 subshell_term SUBSHELL_ERROR
|
||||||
fail_type unbound_variable UNBOUND_VARIABLE
|
fail_type unbound_variable UNBOUND_VARIABLE
|
||||||
@@ -141,7 +162,9 @@ time {
|
|||||||
fail_stderr_count child_error 4
|
fail_stderr_count child_error 4
|
||||||
fail_miaou_count command_error_multiline 7
|
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[@]}
|
failed=${#failures[@]}
|
||||||
|
|||||||
@@ -12,6 +12,37 @@ function do_subshell_false {
|
|||||||
result=$(false)
|
result=$(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function do_heredoc {
|
||||||
|
# miaou-bash <<EOF
|
||||||
|
source /dev/stdin <<EOF
|
||||||
|
echo start of heredoc
|
||||||
|
true
|
||||||
|
echo end of heredoc
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_pipe1 {
|
||||||
|
# miaou-bash < <(...)
|
||||||
|
source /dev/stdin < <(echo 'echo PIPE1')
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_pipe2 {
|
||||||
|
# cmd | miaou-bash
|
||||||
|
echo 'echo PIPE2' | source /dev/stdin
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_two_options {
|
||||||
|
. "$MIAOU_BASH_DIR"/bin/miaou-bash --help --version
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_script_not_found {
|
||||||
|
. "$MIAOU_BASH_DIR"/bin/miaou-bash doesnt_exist
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_no_script {
|
||||||
|
. "$MIAOU_BASH_DIR"/bin/miaou-bash
|
||||||
|
}
|
||||||
|
|
||||||
function do_command_error_multiline {
|
function do_command_error_multiline {
|
||||||
source /dev/stdin <<EOF
|
source /dev/stdin <<EOF
|
||||||
echo ok
|
echo ok
|
||||||
|
|||||||
Reference in New Issue
Block a user