commit: 47a7619e85eaa5de068dcc35b8b8aa411bd87a90
parent a8d2823178976da1f842f4c51424c29586589ef5
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Tue, 17 Sep 2024 08:14:05 +0200
test-cmd/yes: Change from cram to tap.sh
Diffstat:
3 files changed, 74 insertions(+), 41 deletions(-)
diff --git a/test-cmd/tap.sh b/test-cmd/tap.sh
@@ -156,6 +156,36 @@ t_file()
fi
}
+# t_cmd <test_name> <expected_output> <command> [arguments]
+t_cmd() {
+ exp_ret=0
+ name="$1"; shift
+ exp_out="$1"; shift
+
+ # Append a final slash so sh(1) doesn't trims final newlines
+ out="$("$@" 2>&1;r=$?;echo -n /;exit $r)"
+ ret="$?"
+ 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
+}
+
t_end ()
{
if [ $count -ne $plans ]
diff --git a/test-cmd/yes.sh b/test-cmd/yes.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-License-Identifier: MPL-2.0
+
+WD="$(dirname "$0")"
+target="${WD}/../cmd/yes"
+plans=5
+. "${WD}/tap.sh"
+
+yes_n3() { "$target" | head -n 3 ; }
+t_cmd n3 'y
+y
+y
+' yes_n3
+
+yes_foo4() { "$target" foo | head -n 4 ; }
+t_cmd foo4 'foo
+foo
+foo
+foo
+' yes_foo4
+
+yes_empty4() { "$target" "" | head -n 4 ; }
+t_cmd empty4 '
+
+
+
+' yes_empty4
+
+yes_foo6() { "$target" foo | head -n 6 ; }
+t_cmd foo6 'foo
+foo
+foo
+foo
+foo
+foo
+' yes_foo6
+
+yes_nl() { "$target" "$(printf 'foo\nbar')" | head -n 4 ; }
+t_cmd newline 'foo
+bar
+foo
+bar
+' yes_nl
diff --git a/test-cmd/yes.t b/test-cmd/yes.t
@@ -1,41 +0,0 @@
-#!/usr/bin/env cram
-# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: MPL-2.0
-
- $ export PATH="$TESTDIR/../cmd:$PATH"
-
- $ test "$(command -v yes)" = "$TESTDIR/../cmd/yes"
-
- $ yes | head -n 3
- y
- y
- y
- $ yes foo | head -n 4
- foo
- foo
- foo
- foo
- $ yes "" | head -n 5
-
-
-
-
-
-
- $ yes foo '' | head -n 6
- foo
- foo
- foo
- foo
- foo
- foo
-
- $ yes "$(printf 'foo\nbar')" | head -n 4
- foo
- bar
- foo
- bar
-
- $ yes 2>&1 >/dev/full
- yes: Write Error: No space left on device
- [1]