#!/usr/bin/sh

# ====================================================================
# Tool to run unit tests on all `*.spec.qml` files.
# ====================================================================

TEST_RUNNER='qmltestrunner-qt5'

RESOURCES_FILE='resources.qrc'
TEST_FILE_EXTENSION='spec.qml'

MODULES_PATH='./ui/modules'
SCRIPTS_PATH='./ui/scripts'

RED='\e[1;31m'
GREEN='\e[1;32m'
BLUE='\e[1;34m'
NC='\e[0m'

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $SCRIPT_DIR/..

# ====================================================================

so_far_so_good=0

# Check all `*.spec.qml` files.
while read line
do
    source_file=$(
        printf "$line" |
        sed -n 's/^\s*<\s*file\s*>\s*\(.*\.\(qml\|js\)\)\s*<\s*\/\s*file\s*>\s*$/\1/p'
    )
    if [[ ! -z  $source_file ]]; then
        spec_file="${source_file%.*}.${TEST_FILE_EXTENSION}"

        if [ -f $spec_file ]; then
            printf "${BLUE}Running unit qml tests of '${source_file}'...${NC}\n"
            $TEST_RUNNER -import $MODULES_PATH -import $SCRIPTS_PATH -input "$spec_file"

            if [[ $? == 0 ]]; then
                printf "${GREEN}All unit tests have succeeded for '${spec_file}'.\n"
            else
                printf "${RED}Unit tests have failed for '${spec_file}'.\n"
                so_far_so_good=1
            fi
            printf "${NC}\n"
        fi
    fi
done < $RESOURCES_FILE

if [[ $so_far_so_good == 0 ]]; then
    printf "${GREEN}Done. All tests have succeeded.\n"
else
    printf "${RED}Fail. One or many tests have failed.\n"
    so_far_so_good=1
fi
printf "${NC}\n"

exit $so_far_so_good
