commit: bcfadc38f52e4c83ce0c7f93f84983725dcd31d0
parent 4fcf1a40c1d1129af9f447ad02c8397413865864
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun,  1 Sep 2024 16:49:43 +0200
test-cmd/tap.sh: Add t_args function
Diffstat:
| M | test-cmd/tap.sh | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | 
1 file changed, 60 insertions(+), 0 deletions(-)
diff --git a/test-cmd/tap.sh b/test-cmd/tap.sh
@@ -65,6 +65,66 @@ t ()
 	fi
 }
 
+# t_args [--exit=n] [--input=str] <test_name> <expected_output> <arguments ...>
+t_args() {
+	exp_ret=0
+	for i; do
+		case "$i" in
+		--exit=*)
+			exp_ret="${i#*=}"
+			shift
+		;;
+		--input=*)
+			input="${i#*=}"
+			shift
+		;;
+		--)
+			shift
+			break
+		;;
+		# Note: * is still a wildcard, even with a range before
+		-[a-zA-Z0-9]|--[a-zA-Z0-9]*=*)
+			printf 'Unknown option: %s\n' "$i"
+			exit 2
+		;;
+		*)
+			break
+		;;
+		esac
+	done
+	name="$1"; shift
+	exp_out="$1"; shift
+
+	if [ "${input+set}" = "set" ]; then
+		# Append a final slash so sh(1) doesn't trims final newlines
+		out="$(printf "${input?}" | "${target?}" "$@" 2>&1;r=$?;echo -n /;exit $r)"
+		ret="$?"
+	else
+		# Append a final slash so sh(1) doesn't trims final newlines
+		out="$("${target?}" "$@" 2>&1;r=$?;echo -n /;exit $r)"
+		ret="$?"
+	fi
+
+	out="${out%/}"
+	count=$((count+1))
+
+	if [ "$ret" != "$exp_ret" ]; then
+		printf 'not ok %d - %s\n' "$count" "$name"
+		printf '# Expected exit code %d, got %d\n' "$exp_ret" "$ret"
+		printf "$out" | sed -e 's;^;# ;'
+		err=1
+	elif [ "$out" != "$exp_out" ]; then
+		printf 'not ok %d - %s\n' "$count" "$name"
+		printf '# == Expected ==\n'
+		echo "$exp_out" | sed -e 's;^;# ;'
+		printf '# == Got ==\n'
+		echo "$out" | sed -e 's;^;# ;'
+		err=1
+	else
+		printf 'ok %d - %s\n' "$count" "$name"
+	fi
+}
+
 # $1 -> name
 # $2 -> arguments
 # $3 -> file to compare output against