xref: /aosp_15_r20/external/toybox/scripts/test.sh (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3source scripts/runtest.sh
4source scripts/portability.sh
5
6# Kill child processes when we exit
7trap 'kill $(jobs -p) 2>/dev/null; exit 1' INT
8
9# Create working directory
10TOPDIR="$PWD"
11export FILES="$PWD"/tests/files PREFIX=generated/testdir
12rm -rf "$PREFIX"
13mkdir -p "$PREFIX"/testdir
14
15# Populate working directory
16if [ -z "$TEST_HOST" ]
17then
18  if [ $# -ne 0 ]
19  then
20    scripts/single.sh "$@" || exit 1
21  else
22    scripts/install.sh --symlink --force || exit 1
23  fi
24fi
25
26# Add prefix to $PATH
27export -n PREFIX
28cd "$PREFIX"
29PATH="$PWD:$PATH" TESTDIR="$PWD"
30export LC_COLLATE=C
31
32# Collection OPTIONFLAGS for optional()
33[ -f "$TOPDIR/generated/config.h" ] &&
34  export OPTIONFLAGS=:$($SED -nr 's/^#define CFG_(.*) 1$/\1/p' "$TOPDIR/generated/config.h" | tr '\n' :)
35
36# Run a test file in $TESTDIR/testdir with $CMDNAME and $C set, parse $FAILCOUNT
37do_test()
38{
39  # reset testdir
40  cd "$TESTDIR" && rm -rf testdir continue && mkdir testdir && cd testdir ||
41    exit 1
42
43  # set CMDNAME to base name of test file, and C to full path to command
44  CMDNAME="${1##*/}" CMDNAME="${CMDNAME%.test}"
45  if [ -z "$TEST_HOST" ]
46  then
47    C="$TESTDIR/$CMDNAME"
48    [ ! -e "$C" ] && echo "$SHOWSKIP: $CMDNAME disabled" && return
49    C="$(dirname $(realpath "$C"))/$CMDNAME"
50  else
51    C="$(which $CMDNAME 2>/dev/null)"
52    [ -z "$C" ] && printf '%s\n' "$SHOWSKIP: no $CMDNAME" && return
53  fi
54
55  # Run command.test in a subshell
56  (. "$1"; cd "$TESTDIR"; echo "$FAILCOUNT" > continue)
57  cd "$TESTDIR"
58  [ -e continue ] && FAILCOUNT=$(($(cat continue)+$FAILCOUNT)) || exit 1
59}
60
61# Run each test listed on command line or else all tests with executable bit set
62if [ $# -ne 0 ]
63then
64  for i in "$@"
65  do
66    do_test "$TOPDIR"/tests/$i.test
67  done
68else
69  for i in "$TOPDIR"/tests/*.test
70  do
71    [ -z "$TEST_ALL" ] && [ ! -x "$i" ] && continue
72    do_test "$i"
73  done
74fi
75
76[ $FAILCOUNT -eq 0 ]
77