Files
makronix/mobile/userland/basename/test.sh
T
lotherkandClaude Sonnet 5.5 73c9de51a6 licence: the CNCL line in every file Makronix wrote (tools, docs, www, mobile, top level)
consmirror.c: the BSD notice is replaced (Makronix's own file).  Files with
changes of other sessions pending are left for their commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 11:50:26 +00:00

78 lines
2.5 KiB
Bash

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# Test of the basename port: the two cases of NetBSD's own test,
# src/tests/usr.bin/basename/t_basename.sh (basic, suffix), with every
# atf_check -o inline: as one case in plain sh until atf runs on the phone
# (docs/NETBSD-USERLAND.md C-T1); then what basename.1 says beyond the test.
#
# Usage: test.sh BASENAME DATADIR
# BASENAME the command that runs the port (a path, or "qemu-aarch64 -L
# <sysroot> <path>"; tools/userland-test.sh passes it)
# DATADIR a copy of src/tests/usr.bin/basename (this test needs no file)
# Prints one line per case and exits non-zero if one failed.
[ $# -eq 2 ] || { echo "usage: $0 BASENAME DATADIR" >&2; exit 2; }
bn=$1
fails=0
t=${TMPDIR:-/tmp}/basename-test.$$
mkdir -p "$t" || exit 1
trap 'rm -rf "$t"' EXIT
ok() { echo "PASS $1"; }
no() { echo "FAIL $1"; fails=$((fails + 1)); }
# One atf_check -o inline:"EXPECTED\n": NAME EXPECTED ARGS...
# The expected text is basename.1: "deletes any prefix ending with the last
# slash character present in string, and a suffix, if given" (lines 50-56),
# "A non-existent suffix is ignored" (line 58).
x() {
name=$1 exp=$2
shift 2
printf '%s\n' "$exp" > "$t/exp"
if $bn "$@" > "$t/out" 2> "$t/err" && cmp -s "$t/exp" "$t/out"; then
ok "$name"
else
no "$name"
fi
}
# t_basename.sh basic: the filename portion, trailing slashes deleted, "."
# for an empty result (1.1-1994: a string of slashes keeps one slash)
x basic-1 bin /usr/bin
x basic-2 usr /usr
x basic-3 / /
x basic-4 / ///
x basic-5 usr /usr//
x basic-6 bin //usr//bin
x basic-7 usr usr
x basic-8 bin usr/bin
# t_basename.sh suffix: the suffix is removed, also when the name is a slash
# ("/" minus "/" is still "/") and only when it is the tail
x suffix-1 bi /usr/bin n
x suffix-2 bin /usr/bin bin
x suffix-3 / / /
x suffix-4 g /usr/bin/gcc cc
# basename.1 line 58: "A non-existent suffix is ignored" -- "z" is not the
# tail of "x.y", so the name stays whole
x suffix-ignored x.y /usr/bin/x.y z
# basename.1 lines 50-52: with no slash in the string there is no prefix to
# delete, so the string is the answer
x no-slash readme.txt readme.txt
# basename.1 EXIT STATUS (line 71): "exit 0 on success, and >0 if an error
# occurs" -- no operand at all is an error, and it says so
if $bn > "$t/out" 2> "$t/err"; then
no no-operand-exit
elif [ -s "$t/err" ]; then
ok no-operand-exit
else
no no-operand-exit
fi
echo "basename: $fails failed"
[ "$fails" -eq 0 ]