123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #!/bin/bash
- # tfkit - Shellfu's movable test framework
- # See LICENSE file for copyright and license details.
-
- . "$TF_DIR/include/common.sh"
-
-
- __tf_collect_if_needed() {
- #
- # Collect artifact if exit status suggests it
- #
- # Use test exit status $1 to help decide if artifacts are
- # needed, and collect them if so.
- #
- # If TF_COLLECT is set to "always", collect regardless of
- # the status. If set to "never", do not collect anything.
- # The default setting, "auto" collects unless status is 0
- # or 1 (pass or bailout); in that case do nothing.
- #
- local tes=$1 # test exit status
- local will # should we collect artifacts?
- case "$TF_COLLECT:$tes" in
- always:*) will=true ;;
- never:*) will=false ;;
- auto:0) will=false ;;
- auto:2) will=false ;;
- auto:*) will=true ;;
- *) tf_exit_bailout "bad value of TF_COLLECT: $TF_COLLECT" ;;
- esac
- $will || return 0
- mkdir -p "$artifact_dir/$stamp"
- cp -r "$tmpdir"/* "$artifact_dir/$stamp"
- }
-
- __tf_header() {
- #
- # create header to add to output before test
- #
- local hdrline # each line from generator
- local hdrgen="$TF_SUITE/TF_HEADER" # header generator script
- tf_think "#"
- tf_think "# ---"
- tf_think "# tfkit:"
- if test -x "$hdrgen";
- then
- tf_think "# sut:"
- $hdrgen \
- | while IFS= read -r hdrline;
- do
- test -n "$hdrline" || break
- tf_think "# $hdrline"
- done
- else
- tf_think "# hint: >"
- tf_think "# Add $hdrgen executable for own header."
- tf_think "# It should output YAML (\"key: value\" pairs) about"
- tf_think "# your SUT ('version' at least')."
- fi
- tf_think "# run:"
- tf_think "# start_time: $(date -Iseconds)"
- test -n "$TF_FILTER_TEST" \
- && tf_think "# TF_FILTER_TEST: $TF_FILTER_TEST"
- test -n "$TF_FILTER_SUBTEST" \
- && tf_think "# TF_FILTER_SUBTEST: $TF_FILTER_SUBTEST"
- tf_think "#"
- tf_think ""
- }
-
- tf_enum_tests() {
- #
- # List what looks like test; relative to $TF_SUITE
- #
- tf_debug "TF_SUITE='$TF_SUITE'"
- test -d "$TF_SUITE" || return 0
- find -L \
- "$TF_SUITE" \
- -mindepth 2 \
- -maxdepth 2 \
- -type f \
- -perm /111 \
- -name TF_RUN \
- | cut -d/ -f2
- }
-
- tf_run_tests() {
- #
- # Discover and run tests
- #
- local es=0 # overall exit status
- local tmpdir="" # test temporary dir
- local tname="" # test name
- local tes=0 # test result
- local stamp="" # test stamp to use as artifact name
- local tf_dir tf_suite # to keep absolute paths for TF_RUN
- local artifact_dir # where to keep artifacts
- artifact_dir="$(readlink -f "$TF_ARTIFACTS")"
- __tf_header
- tf_debug "TF_VERSION='$TF_VERSION'"
- tf_dir="$(readlink -f "$TF_DIR")"
- tf_suite="$(readlink -f "$TF_SUITE")"
- es=0
- for tname in $(tf_enum_tests | grep -e "$TF_FILTER_TEST");
- do
- tf_think "... $tname"
- tmpdir=$(mktemp -d)
- stamp=$(date "+artifacts-$tname-%Y%m%d-%H%M%S")
- cp -r "$TF_SUITE/$tname/"* "$tmpdir"
- pushd "$tmpdir" >/dev/null
- TF_DIR="$tf_dir" TF_SUITE=$tf_suite TF_TEST="$tname" \
- ./TF_RUN
- tes=$?
- __tf_collect_if_needed $tes
- test $tes -gt $es && es=$tes
- popd >/dev/null
- rm -rf "$tmpdir"
- if test $tes -eq 0;
- then
- tf_think "''' $tname ($tes)"
- else
- tf_warn "??? $tname ($tes)"
- fi
- done
- return $es
- }
|