| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | #!/bin/bash
# tfkit - Shellfu's movable test framework
# See LICENSE file for copyright and license details.
. "$TF_DIR/include/common.sh"
#
# Default path to header generator
#
__TF_HDRCMD="$TF_SUITE/TF_HEADER"
__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_default_header() {
    #
    # Create default header
    #
    echo "hint = Add $__TF_HDRCMD executable for own header."
    echo "hint = It should output 'key = value' pairs about"
    echo "hint = your SUT ('version' at least')."
}
__tf_header() {
    #
    # create header to add to output before test
    #
    local field
    local hdrcmd="$__TF_HDRCMD"
    test -x "$hdrcmd" || hdrcmd="__tf_default_header"
    tf_think "#"
    tf_think "# [tfkit.sut]"
    tf_think "#"
    $hdrcmd \
      | while read field;
        do
            test -n "$field" || break
            tf_think "#     $field"
        done
    tf_think "#"
    tf_think "# [tfkit.run]"
    tf_think "#"
    tf_think "#     start_time = $(date -Iseconds)"
    test "$TF_FILTER_TEST" = '.*' \
     || tf_think "#     TF_FILTER_TEST = $TF_FILTER_TEST"
    test "$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="$(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_TNAME="$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
}
 |