#!/bin/bash

COMMAND=$1
PROJECT_PATH=${PROJECT_PATH-$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )}
ROVER=${ROVER-/usr/local/openresty/luajit/bin/rover}

function run_busted_tests {
    cd "${PROJECT_PATH}" || exit
    ${ROVER} exec ./bin/busted
}

function run_nginx_unit_tests {
    cd "${PROJECT_PATH}" || exit

    "${ROVER}" exec prove

    # Text::Nginx runs nginx in the t/servroot dir. Clean it.
    rm -rf "${PROJECT_PATH}/t/servroot"
}

function run_all_tests {
    run_busted_tests
    run_nginx_unit_tests
}

case "${COMMAND}" in
    busted)
        run_busted_tests
        ;;
    nginx)
        run_nginx_unit_tests
        ;;
    *)
        run_all_tests
        ;;
esac
