<commit_before>#!/bin/bash

# Automated tests to test the stb-tester framework itself.

#/ Usage: run-tests.sh [options] [testnames...]
#/
#/         -l      Leave the scratch dir created in /tmp.
#/         -v      Verbose (don't suppress console output from tests).
#/
#/         If any test names are specified, only those test cases will be run.

cd "$(dirname "$0")"
testdir="$PWD"
for tests in ./test-*.sh; do
    source $tests
done

while getopts "lv" option; do
    case $option in
        l) leave_scratch_dir=true;;
        v) verbose=true;;
        *) grep '^#/' < "$0" | cut -c4- >&2; exit 1;; # Print usage message
    esac
done
shift $(($OPTIND-1))

srcdir="$testdir/.."
export PATH="$srcdir:$PATH"
export STBT_CONFIG_FILE="$testdir/stbt.conf"
export GST_PLUGIN_PATH="$srcdir/gst:$GST_PLUGIN_PATH"
export PYTHONPATH="$srcdir:$PYTHONPATH"
export PYTHONUNBUFFERED=x
export PYLINTRC="$testdir/pylint.conf"
rm -f ~/.gstreamer-0.10/registry.*

run() {
    scratchdir=$(mktemp -d -t stb-tester.XXX)
    [ -n "$scratchdir" ] || { echo "$0: mktemp failed" >&2; exit 1; }
    printf "$1... "
    ( cd "$scratchdir" && $1 ) > "$scratchdir/log" 2>&1
    local status=$?
    [ $status -eq 0 ] && echo "OK" || echo "FAIL"
    if [[ "$verbose" = "true" || $status -ne 0 ]]; then
        echo "Showing '$scratchdir/log':"
        cat "$scratchdir/log"
    fi
    if [[ "$leave_scratch_dir" != "true" && $status -eq 0 ]]; then
        rm -rf "$scratchdir"
    fi
    [ $status -eq 0 ]
}

# Portable timeout command. Usage: timeout <secs> <command> [<args>...]
timeout() { "$testdir"/timeout.pl "$@"; }
timedout=142

fail() { echo "error: $*"; exit 1; }

assert() {
    local not ret
    [[ "$1" == '!' ]] && { not='!'; shift; } || not=
    "$@"
    ret=$?
    case "$not,$ret" in
        ,0) ;;
        ,*) fail "Command failed: $*";;
        !,0) fail "Expected command to fail: $*";;
        !,*) ;;
    esac
}

killtree() {
    local parent=$1 child
    for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do
        killtree $child
    done
    kill $parent
}


# Run the tests ############################################################
ret=0
for t in ${*:-$(declare -F | awk '/ test_/ {print $3}')}; do
    run $t || ret=1
done
exit $ret


# bash-completion script: Add the below to ~/.bash_completion
_stbt_run_tests() {
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local testdir="$(dirname \
        $(echo $COMP_LINE | grep -o '\b[^ ]*run-tests\.sh\b'))"
    local testfiles="$(\ls $testdir/test-*.sh)"
    local testcases="$(awk -F'[ ()]' '/^test_[a-z_]*()/ {print $1}' $testfiles)"
    COMPREPLY=( $(compgen -W "$testcases" -- "$cur") )
}
complete -F _stbt_run_tests run-tests.sh<commit_msg>tests/run-tests.sh accepts testsuite as argument<commit_after>#!/bin/bash

# Automated tests to test the stb-tester framework itself.

#/ Usage: run-tests.sh [options] [testsuite or testcase names...]
#/
#/         -l      Leave the scratch dir created in /tmp.
#/         -v      Verbose (don't suppress console output from tests).
#/
#/         If any test names are specified, only those test cases will be run.

cd "$(dirname "$0")"
testdir="$PWD"

while getopts "lv" option; do
    case $option in
        l) leave_scratch_dir=true;;
        v) verbose=true;;
        *) grep '^#/' < "$0" | cut -c4- >&2; exit 1;; # Print usage message
    esac
done
shift $(($OPTIND-1))

testsuites=()
testcases=()
while [[ $# -gt 0 ]]; do
    [[ -f $(basename $1) ]] && testsuites+=($(basename $1)) || testcases+=($1)
    shift
done
for testsuite in ${testsuites[*]:-./test-*.sh}; do
    source $testsuite
done
: ${testcases:=$(declare -F | awk '/ test_/ {print $3}')}

srcdir="$testdir/.."
export PATH="$srcdir:$PATH"
export STBT_CONFIG_FILE="$testdir/stbt.conf"
export GST_PLUGIN_PATH="$srcdir/gst:$GST_PLUGIN_PATH"
export PYTHONPATH="$srcdir:$PYTHONPATH"
export PYTHONUNBUFFERED=x
export PYLINTRC="$testdir/pylint.conf"
rm -f ~/.gstreamer-0.10/registry.*

run() {
    scratchdir=$(mktemp -d -t stb-tester.XXX)
    [ -n "$scratchdir" ] || { echo "$0: mktemp failed" >&2; exit 1; }
    printf "$1... "
    ( cd "$scratchdir" && $1 ) > "$scratchdir/log" 2>&1
    local status=$?
    [ $status -eq 0 ] && echo "OK" || echo "FAIL"
    if [[ "$verbose" = "true" || $status -ne 0 ]]; then
        echo "Showing '$scratchdir/log':"
        cat "$scratchdir/log"
    fi
    if [[ "$leave_scratch_dir" != "true" && $status -eq 0 ]]; then
        rm -rf "$scratchdir"
    fi
    [ $status -eq 0 ]
}

# Portable timeout command. Usage: timeout <secs> <command> [<args>...]
timeout() { "$testdir"/timeout.pl "$@"; }
timedout=142

fail() { echo "error: $*"; exit 1; }

assert() {
    local not ret
    [[ "$1" == '!' ]] && { not='!'; shift; } || not=
    "$@"
    ret=$?
    case "$not,$ret" in
        ,0) ;;
        ,*) fail "Command failed: $*";;
        !,0) fail "Expected command to fail: $*";;
        !,*) ;;
    esac
}

killtree() {
    local parent=$1 child
    for child in $(ps -o ppid= -o pid= | awk "\$1==$parent {print \$2}"); do
        killtree $child
    done
    kill $parent
}


# Run the tests ############################################################
ret=0
for t in ${testcases[*]}; do
    run $t || ret=1
done
exit $ret


# bash-completion script: Add the below to ~/.bash_completion
_stbt_run_tests() {
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local testdir="$(dirname \
        $(echo $COMP_LINE | grep -o '\b[^ ]*run-tests\.sh\b'))"
    local testfiles="$(\ls $testdir/test-*.sh)"
    local testcases="$(awk -F'[ ()]' '/^test_[a-z_]*()/ {print $1}' $testfiles)"
    COMPREPLY=( $(
        compgen -W "$testcases $(basename -a $testfiles)" -- "$cur") )
}
complete -F _stbt_run_tests run-tests.sh
{'fraction_non_alphanumeric': 0.21651824690064606, 'fraction_numerical': 0.012572027239392353, 'mean_word_length': 2.8755074424898512, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7347740', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2086, 'n_words': 566}