#!/bin/bash export good_exit=64 exitcode=${good_exit} [ -z "${keep_going}" ] && export keep_going="" [[ "$*" == *"-k"* ]] && export keep_going="-k" runtime_testing=4 [[ "$(whoami)" == "jenkins" ]] && INTERACTIVE=0 || INTERACTIVE=1 if [ "$1" == "-i" ]; then INTERACTIVE=1 shift 1 fi export next_cls=0 export test_completed=test_completed if [ "$1" == "-k" ]; then keep_going="-k" runtime_testing=5 shift fi [ -z "${TESTING_TEMP}" ] && [ -d "$(pwd)/test_results" ] && export TESTING_TEMP=$(pwd)/test_results/$(whoami) [ -z "${TESTING_TEMP}" ] && [ -d "${LOGICMOO_WS}/test_results" ] && export TESTING_TEMP=${LOGICMOO_WS}/test_results/$(whoami) [ -z "${TESTING_TEMP}" ] && export TESTING_TEMP=$(mktemp -d -t logicmoo_testing-$(date +%Y-%m-%d-%H-%M-%S)-XXXXXXXXXX) export TESTING_TEMP mkdir -p ${TESTING_TEMP}/ parent-find() { local file="$1" local dir="$(realpath $2)" # echo parent-find "$file" "$(dirname "$dir")" test -e "$dir/$file" && echo "$dir" && return 0 [ '/' = "$dir" ] && return 1 parent-find "$file" "$(dirname "$dir")" } export PACK_DIR=$(basename junit/$(parent-find "pack.pl" . )) echo "" export JUNIT_PACKAGE="${PACK_DIR}.$(basename `realpath .. | sed -e 's|/[^.]/|/|g' `).$(basename `realpath .`)" JUNIT_PACKAGE=$(echo $JUNIT_PACKAGE | sed -e "s/logicmoo_//g" -e "s|\.t\.|\.test\.|g" -e "s|tests|test|g" ) JUNIT_PACKAGE="logicmoo.${JUNIT_PACKAGE}" echo "" export JUNIT_PACKAGE_STEM=$(echo "${JUNIT_PACKAGE}" | sed -e "s/\./-/g" -e "s/--/-/g" | rev | cut -c 1-110 | rev) export GLOB="$*" [ -z "$GLOB" ] && GLOB="*_01.*" GLOBSTEM=$(echo "${JUNIT_PACKAGE_STEM}-${GLOB}" | sed -e "s/[*]/vSTARv/g" -e "s/[?]/vQUESTv/g" -e "s/[.]/vDOTv/g" -e "s/[^_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]/-/g" -e "s/--/-/g" | rev | cut -c 1-110 | rev) echo "" [ -z "${TEST_STEM}" ] && export TEST_STEM=Report-${GLOBSTEM}-Units echo "" [ -z "${TEST_STEM_PATH}" ] && export TEST_STEM_PATH="${TESTING_TEMP}/${TEST_STEM}" echo "" echo "" OUTER_TEE="" [ -t 1 ] && OUTER_TEE="1" #// For test_prolog (no args) declare -a listOfNames=( # // sanity tests "*_01*.p*" "*_02*.p*" # // full tests "*_03*.p*" "*_04*.p*" "*_05*.p*" "*_06*.p*" "*_07*.p*" "*_08*.p*" "*_09*.p*" "*_10*.p*" "*_11*.p*" "*_12*.p*" # // feature tests "*_f01*.p*" "*_f02*.p*" "*_f03*.p*" "_f04*.p*" "*_f05*.p*" "*_f06*.p*" "*_f07*.p*" "*_f08*.p*" "*_f09*.p*" "*_f10*.p*" "*_f11*.p*" "*_f12*.p*" ) # kill old dead jobs (kill -9 %1 %2 %6 %5 %4 %3 %2 %1) &>/dev/null (kill -9 %1 %2 %6 %5 %4 %3 %2 %1) &>/dev/null #[ -z "${OUTER_TEE}" ] && echo "" (kill -9 %1 %2 %6 %5 %4 %3 %2 %1) &>/dev/null if [ $# -ne 0 ] then listOfNames=( "$@" ) if [ $# -eq 1 ] then [ -z "${OUTER_TEE}" ] && echo "" test_completed=test_completed else echo -e "" # [ -z "${OUTER_TEE}" ] && echo "" fi else echo -e "" fi export ROLLUP_STEM=${TEST_STEM}-Rollup echo "" export ROLLUP_TMP="${TESTING_TEMP}/${ROLLUP_STEM}.tmp" #touch ${ROLLUP_TMP}.stamp JUNIT_TESTS_GTOTAL_TESTS=0 JUNIT_TESTS_GTOTAL_FAILURES=0 JUNIT_TESTS_GTOTAL_SKIPPED=0 JUNIT_TESTS_GTOTAL_PASSED=0 JUNIT_TESTS_GTOTAL_ERRORS=0 echo "" echo "" > ${ROLLUP_TMP} export TEE_FILE=/dev/null export TEE_FILE_NOANSI=/dev/null function JECHO { (echo -e "${*}\n") >> ${ROLLUP_TMP} } function INFO { doesBe="${*}\n" willBe=$(echo "${*}" | sed -e 's/-/=/g' ) echo -e "${doesBe}" echo -e "${doesBe}" >> ${TEE_FILE} echo -e "${doesBe}" >> ${TEE_FILE_NOANSI} JECHO "" } function INFO2 { doesBe="${*}\n" willBe=$(echo "${*}" | sed -e 's/-/=/g' ) echo -e "${doesBe}" echo -e "${doesBe}" >> ${TEE_FILE_NOANSI} JECHO "" } SWIPL="swipl -x ${LOGICMOO_WS}/bin/lmoo-clif" [ -z "$SWIPL" ] && ( export SWIPL=swipl if [ -z `which swipl` ]; then # default locations on OS X SWIPL=/Applications/SWI-Prolog.app/Contents/MacOS/swipl; if ! type ${SWIPL} > /dev/null 2>&1; then SWIPL=~/bin/swipl; fi fi if type lmoo-pfc > /dev/null 2>&1; then SWIPL=lmoo-pfc fi if type lmoo-clif > /dev/null 2>&1; then SWIPL=lmoo-clif fi if ! type ${SWIPL} > /dev/null 2>&1; then echo LOGICMOO requires SWI-Prolog. Please download from http://www.swi-prolog.org/ return 1 2>/dev/null ; exit 1 fi ) function expand { for arg in "$@"; do [[ -f $arg ]] && echo $arg; done } me="${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}" INFO "Running Matching Tests: (cd $PWD ; $me ${keep_going} ${listOfNames[*]})" FILES="" for ele1 in "${listOfNames[@]}" do FILE=$(expand $ele1) [ ! -z "$FILE" ] && FILES="$FILES $FILE" done FILES=$(echo $FILES | xargs) if [ -z "$FILES" ]; then echo "FILES=$FILES" return 0 2>/dev/null ; exit 0 fi if [ -n "${MAX_TEST_SUITE_NUM}" ]; then if [ $MAX_TEST_SUITE_NUM -le 0 ]; then echo "Warn exceeded MAX_TEST_SUITE_NUM" return 0 2>/dev/null ; exit 0 fi fi export MAYBE_GH="gh" export MAYBE_GH="echo SKIPPING gh" $MAYBE_GH api repos/logicmoo/logicmoo_workspace/labels -f name="${JUNIT_PACKAGE}" -f color="888000" 2>&1 > /dev/null export GH_MASTER_ISSUE_LIST=`gh issue list -l ${JUNIT_PACKAGE} -L 1000 | sed -e "s/UTC /\n/g" | grep "unit_test"` #DIRS_SORTED=`find $TEST_DIRS -maxdepth 0 -type d -printf "%T+ %p\n" | sort -r -u | cut -d " " -f 2` if [ -z "$SORTED_FILES" ]; then SORTED_FILES=`find $FILES -name "*" -printf "%T+ %p\n" | sort -r -u | cut -d " " -f 2` fi if [ -z "$SORTED_FILES" ]; then SORTED_FILES=$(set -f; IFS=$'\n'; ls -t $(echo $FILES)) fi if [ -z "$SORTED_FILES" ]; then SORTED_FILES=$FILES fi CRLF=' ' SORTED_FILES=$(echo $SORTED_FILES | sed -e "s|\n| |g" -e "s| | |g") echo "SORTED_FILES=$SORTED_FILES" ( for ele2 in "${SORTED_FILES[@]}" do [ -n "${MAX_TEST_SUITE_TIME}" ] && [ $MAX_TEST_SUITE_NUM -le 0 ] && echo "Warn: reached MAX_TEST_SUITE_NUM .. skipping .. $ele2" && continue for ele in $ele2 do [ -n "${MAX_TEST_SUITE_TIME}" ] && [ $MAX_TEST_SUITE_NUM -le 0 ] && echo "Warn: reached MAX_TEST_SUITE_NUM .. skipping .. $ele" && continue retry=1 while [ $retry == 1 ] do retry=0 export RELPATH=$(echo "$PWD/${ele}" | sed -e "s|${LOGICMOO_WS}@2/||g" -e "s|$LOGICMOO_WS/||g") export FILENAME="${ele}" export JUNIT_SHORTCLASS=`echo "${FILENAME^^}" | cut -d'.' -f1 ` export JUNIT_CLASSNAME="${JUNIT_PACKAGE}.${JUNIT_SHORTCLASS}" export JUNIT_SUITE="${JUNIT_CLASSNAME}" export FileTestCase="${JUNIT_CLASSNAME} JUnit" INFO "FileTestCase=${FileTestCase}" [[ "$ele" == *".ansi" ]] && continue [[ "$ele" == *".txt" ]] && continue [[ "$ele" == *".tmp" ]] && continue [[ "$ele" == *".html" ]] && continue [[ "$ele" == *".bak" ]] && continue [[ "$ele" == *".hide" ]] && continue [[ "$ele" == *".xml" ]] && continue [[ "$ele" == *".sh" ]] && continue if [[ "$ele" == *".sh" && -x "./$ele" ]]; then CMD="./${ele}" else : 'if ! type $SWIPL > /dev/null 2>&1; then EXT=`echo "$ele" | cut -d'.' -f2` if type lmoo-${EXT} > /dev/null 2>&1; then SWIPL=lmoo-${EXT} fi fi ' #// Runs the test -f .swiplrc #//CMD="swipl -g 'set_prolog_flag(runtime_testing,${runtime_testing})' -g \"thread_create(['${ele}'],Id),thread_join(Id),$test_completed\" " #//CMD="$SWIPL -g 'set_prolog_flag(runtime_testing,${runtime_testing})' -g \"(['${ele}'])\" -g \"$test_completed\" " CMD="$SWIPL -t \"['${ele}']\"" fi #stty echo [ -n "${MAX_TEST_SUITE_TIME}" ] && [ $MAX_TEST_SUITE_NUM -le 0 ] && echo "Warn: reached MAX_TEST_SUITE_NUM .. skipping .. $CMD" && continue [ -n "${MAX_TEST_SUITE_NUM}" ] && export MAX_TEST_SUITE_NUM=$((MAX_TEST_SUITE_NUM - 1)) export Test_9999="Test_9999" lmoo-make [ -z "${CMD_TIMEOUT}" ] && CMD_TIMEOUT="10s" #[ ${INTERACTIVE} -eq 0 ] && [ -z "${CMD_WRAPPER}" ] && CMD_WRAPPER="timeout --foreground --preserve-status -s SIGKILL -k ${CMD_TIMEOUT} ${CMD_TIMEOUT}" #[ -z "${CMD_WRAPPER}" ] && CMD_WRAPPER="trap 'echo caught interrupt and exiting;exit' INT ; timeout --foreground --preserve-status -s SIGKILL -k ${CMD_TIMEOUT} ${CMD_TIMEOUT}" [ -z "${CMD_WRAPPER}" ] && CMD_WRAPPER=" " CMD="${CMD_WRAPPER} ${CMD}" export GIT_REV=$(git rev-parse HEAD) export JUNIT_CMD="${CMD}" JUNIT_TESTS_GTOTAL_TESTS=$((JUNIT_TESTS_GTOTAL_TESTS+1)) INFO "CMD=$CMD" export TEE_FILE=${TESTING_TEMP}/CMD_LAST.ansi export TEE_FILE_NOANSI=${TEE_FILE}.too cat /dev/null > ${TEE_FILE} cat /dev/null > ${TEE_FILE_NOANSI} INFO "${date} (cd $PWD ; $CMD)" #export FTC_LINK=$(echo "${JUNIT_CLASSNAME}" | sed -e "s/[@. ]/_/g") export LAST_BUILD_REF="https://jenkins.logicmoo.org/job/logicmoo_workspace/lastBuild/testReport/${JUNIT_PACKAGE}/${JUNIT_SHORTCLASS}" export BUILD_REF=$(echo $LAST_BUILD_REF | sed -e "s/lastBuild/${BUILD_ID}/g") export ISSUE_SEARCH="https://github.com/logicmoo/logicmoo_workspace/issues?q=is%3Aissue+label%3A${JUNIT_SHORTCLASS}" LINKS="" LINKS+="% EDIT: https://github.com/logicmoo/logicmoo_workspace/edit/master/$RELPATH $CRLF" LINKS+="% JENKINS: ${LAST_BUILD_REF}/ $CRLF" LINKS+="% ISSUE_SEARCH: ${ISSUE_SEARCH} $CRLF" ISSUE_IS_NEW=0 export GH_ISSUE_INFO=$(echo "${GH_MASTER_ISSUE_LIST}" | grep "${FileTestCase}" | grep ${Test_9999} | head -1) export GH_ISSUE_ID=$(echo "${GH_ISSUE_INFO}" | cut -f1) [ -z "${GH_ISSUE_ID}" ] && ( $MAYBE_GH api repos/logicmoo/logicmoo_workspace/labels -f name="${JUNIT_SHORTCLASS}" -f color="888000" $MAYBE_GH issue create -t "${FileTestCase}" -l "${JUNIT_PACKAGE},${JUNIT_SHORTCLASS},${Test_9999},unit_test,Skipped" -b "${LINKS}" export GH_MASTER_ISSUE_LIST=`gh issue list -l ${JUNIT_PACKAGE} -L 1000 | sed -e "s/UTC /\n/g" | grep "unit_test"` export GH_ISSUE_INFO=$(echo "${GH_MASTER_ISSUE_LIST}" | grep "${FileTestCase}" | head -1) export GH_ISSUE_ID=$(echo "${GH_ISSUE_INFO}" | cut -f1) ISSUE_IS_NEW=1 ) LINKS+="% ISSUE: https://github.com/logicmoo/logicmoo_workspace/issues/$GH_ISSUE_ID " && LINKS+='\n' Add_LABELS="" Rem_LABELS="" #INFO "GH_ISSUE_INFO=${GH_ISSUE_INFO}" #INFO "GH_ISSUE_ID=#${GH_ISSUE_ID} '${GH_ISSUE_OC}'" #export GH_ISSUE_OC=$(echo "${GH_ISSUE_INFO}" | cut -f2) #LINKS+="PREVIOUS: ${BUILD_REF}/${FTC_LINK}/ \n" #LINKS+="GITLAB: https://gitlab.logicmoo.org/gitlab/logicmoo/logicmoo_workspace/-/blob/${GIT_REV}/$RELPATH \n" #LINKS+="GITHUB: https://github.com/logicmoo/logicmoo_workspace/commit/${GIT_REV} \n" INFO2 "${LINKS}" if ! grep -q "JENKINS" "${ele}"; then [ -w "${ele}" ] && echo -e "\n${LINKS}" >> "${ele}" fi if ! grep -q "ISSUE.*$GH_ISSUE_ID" "${ele}"; then [ -w "${ele}" ] && { sed -i "/ISSUE: https/d" -i "${ele}" echo "% ISSUE: https://github.com/logicmoo/logicmoo_workspace/issues/$GH_ISSUE_ID" >> "${ele}" } fi startTime=$(date +%s%3N); echo '```' >> ${TEE_FILE} echo '```' >> ${TEE_FILE_NOANSI} # CMD is allowed to clobber TEE_FILE if [ ${INTERACTIVE} -eq 1 ]; then eval $CMD else ( eval $CMD ) 2>&1 | sed -e "s/> ${TEE_FILE_NOANSI} echo '```' >> ${TEE_FILE} INFO "totalTime=${totalTime}" : ' 1=SystemError 2=SystemAborted 4=UserAborted 8=Failure 16=Warnings 32=Errors 64=Passed 128=Overtime ' TEST_RESULT="ERROR" COMMENT="" [ "$test_completed" == 'test_completed' ] && [ $exitcode -ne $good_exit ] && TEST_RESULT="FAILED" [ $exitcode -eq 0 ] && [ "$test_completed" == 'true' ] && TEST_RESULT="SUCCESS" [ $exitcode -eq $good_exit ] && TEST_RESULT="SUCCESS" || Rem_LABELS+=" Skipped" [ $(($exitcode & 32)) -ne 0 ] && Add_LABELS="Errors" || Rem_LABELS+=" Skipped" [ $(($exitcode & 2)) -ne 0 ] && COMMENT+=" SystemAborted" && Add_LABELS="Errors" [ $(($exitcode & 1)) -ne 0 ] && COMMENT+=" SystemError" && Add_LABELS="Errors" [ -z "$Add_LABELS" ] && Rem_LABELS+=" Errors" [ $(($exitcode & 16)) -ne 0 ] && Add_LABELS+=" Warnings" || Rem_LABELS+=" Warnings" [ $(($exitcode & 128)) -ne 0 ] && Add_LABELS+=" Overtime" || Rem_LABELS+=" Overtime" [ $(($exitcode & $good_exit)) -ne 0 ] && TEST_RESULT="SUCCESS" && [ "${next_cls}" == 1 ] && cls && next_cls=0 [ $(($exitcode & 8)) -ne 0 ] && TEST_RESULT="FAILED" || Rem_LABELS+=" Skipped" [ $(($exitcode & 4)) -ne 0 ] && (COMMENT+=" UserAborted" && TEST_RESULT="SKIPPED" && Add_LABELS+=" Skipped") || Rem_LABELS+=" Skipped" #BASH_EVAL=$(grep "% Bash Eval: " ${TEE_FILE_NOANSI} | sed -e "s|% Bash Eval: ||g") #[ ! -z "$BASH_EVAL" ] && $BASH_EVAL Add_LABELS=$(echo $Add_LABELS | xargs | sed -e "s/ /,/g") Rem_LABELS=$(echo $Rem_LABELS | xargs | sed -e "s/ /,/g") # INFO "${LINKS}" INFO "${TEST_RESULT}: $0 ${keep_going} ${ele} (returned ${exitcode}) Add_LABELS='$Add_LABELS' Rem_LABELS='$Rem_LABELS'" #echo "=========================================" #cat ${TEE_FILE_NOANSI} #echo "=========================================" [ ${ISSUE_IS_NEW} -eq 1 ] && ( [ ! -z "$GH_ISSUE_ID "] && ( $MAYBE_GH issue edit $GH_ISSUE_ID --body-file "${TEE_FILE_NOANSI}" ) ) [ ${INTERACTIVE} -eq 0 ] && ( source $LOGICMOO_WS/bin/lmoo-issue ${JUNIT_PACKAGE} ${JUNIT_SHORTCLASS} ${Test_9999} ${TEST_RESULT} "${FileTestCase}" ${TEE_FILE_NOANSI} "$Add_LABELS" "$Rem_LABELS" ) if [ "${TEST_RESULT}" = "SUCCESS" ]; then JECHO "" JECHO "\n" JUNIT_TESTS_GTOTAL_PASSED=$((JUNIT_TESTS_GTOTAL_PASSED+1)) elif [ "${TEST_RESULT}" = "SKIPPED" ]; then JECHO "" JECHO "\n" JUNIT_TESTS_GTOTAL_SKIPPED=$((JUNIT_TESTS_GTOTAL_SKIPPED+1)) elif [ "${TEST_RESULT}" = "ERROR" ]; then JECHO "" JECHO " \n" JUNIT_TESTS_GTOTAL_ERRORS=$((JUNIT_TESTS_GTOTAL_ERRORS+1)) [ -w "${ele}" ] && touch "$ele" ; /bin/true else JECHO "" JECHO " \n" JUNIT_TESTS_GTOTAL_FAILURES=$((JUNIT_TESTS_GTOTAL_FAILURES+1)) [ -w "${ele}" ] && touch "$ele" ; /bin/true fi [ $exitcode -eq 6 ] && retry=1 && continue # // 2 -> 1 if [ $exitcode -eq 2 ]; then [ "${keep_going}" == "-k" ] && INFO "...keep going..." && continue INFO "...NOT keep going..." exit 1 fi # // Not Abort [ $exitcode -ne 1 ] && [ "${keep_going}" == "-k" ] && continue echo "