Files
makronix/mobile/userland/dirname/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

91 lines
2.9 KiB
Bash

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# Test of the dirname port: the one case of NetBSD's own test,
# src/tests/usr.bin/dirname/t_dirname.sh (basic), 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 DIRNAME DATADIR
# DIRNAME 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/dirname (this test needs no file)
# Prints one line per case and exits non-zero if one failed.
[ $# -eq 2 ] || { echo "usage: $0 DIRNAME DATADIR" >&2; exit 2; }
dn=$1
fails=0
t=${TMPDIR:-/tmp}/dirname-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: "dirname deletes the filename portion,
# beginning with the last slash character to the end of string" (lines 61-63).
x() {
name=$1 exp=$2
shift 2
printf '%s\n' "$exp" > "$t/exp"
if $dn "$@" > "$t/out" 2> "$t/err" && cmp -s "$t/exp" "$t/out"; then
ok "$name"
else
no "$name"
fi
}
# t_dirname.sh basic-2: `dirname //`. NetBSD answers "/" (its libc,
# src/lib/libc/gen/dirname.c, strips all trailing slashes and then the
# doubled one), glibc 2.41 answers "//" (it keeps a leading "//" as a root of
# its own). POSIX makes exactly this case implementation-defined: "If path
# is //, it is implementation-defined whether steps 3 to 6 are skipped or
# processed" (XCU, dirname). The phone's libc is glibc, so the port may
# answer either; this case takes both and names glibc's answer in a NOTE
# line. Every other case below is NetBSD's answer exactly -- including
# "//usr//bin//" -> "//usr", where the two libcs agree.
y() { # NAME: dirname // -- "/" (NetBSD) or "//" (glibc)
printf '/\n' > "$t/exp"
printf '//\n' > "$t/exp2"
if ! $dn // > "$t/out" 2> "$t/err"; then
no "$1"
elif cmp -s "$t/exp" "$t/out"; then
ok "$1"
elif cmp -s "$t/exp2" "$t/out"; then
echo "NOTE $1: // -> // (glibc keeps the // root, POSIX" \
"implementation-defined; NetBSD prints /)"
ok "$1"
else
no "$1"
fi
}
# t_dirname.sh basic
x basic-1 / /
y basic-2
x basic-3 /usr /usr/bin/
x basic-4 //usr //usr//bin//
x basic-5 . usr
x basic-6 . ""
x basic-7 / /usr
x basic-8 /usr /usr/bin
x basic-9 usr usr/bin
# basename.1 EXAMPLES (lines 74-80): "FOO=`dirname /usr/bin/trail`" sets FOO
# to /usr/bin
x example /usr/bin /usr/bin/trail
# 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 $dn > "$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 "dirname: $fails failed"
[ "$fails" -eq 0 ]