Move test runner into generic task runner script

This commit is contained in:
Alex Palaistras 2016-03-05 23:51:28 +00:00
parent 031fe1d0be
commit c104e833b4
2 changed files with 95 additions and 80 deletions

View File

@ -1,80 +0,0 @@
#!/bin/bash
#
# Test runner for Fawkss.
# --------------
# USER VARIABLES
# --------------
# Current working directory.
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Fawkss main script location.
FAWKSS_SCRIPT="${ROOT}/fawkss"
# ANSI Color definitions.
COLOR_WHITE="\033[1m"
COLOR_RED="\033[31m"
COLOR_GREEN="\033[32m"
COLOR_RESET="\033[0m"
# Initialize summary count array.
SUMMARY=(0 0 0)
# Helpers for extracting sections of test files.
read -d '' SECTION_TEST <<-'_EOF_'
/--- TEST ---/ { test = 1; next }
/--- EXPECTED ---/ { exit }
test { print }
_EOF_
read -d '' SECTION_EXPECTED <<-'_EOF_'
/--- EXPECTED ---/ { expected = 1; next }
/--- END ---/ { exit }
expected { print }
_EOF_
# --------------
# TEST EXECUTION
# --------------
echo -e ">> ${COLOR_WHITE}Executing tests...${COLOR_RESET}"
for file in ${@:-${ROOT}/tests/*.scss}
do
# Check if file exists and is readable.
if [ ! -f "${file}" ]
then
echo -e ">> ${COLOR_RED}Failed to find test file '${file}'${COLOR_RESET}"
continue
fi
# Increase count of total tests run.
SUMMARY[0]=$((${SUMMARY[0]} + 1))
# Get diff between expected and actual output.
result=$(colordiff -ud <(awk "${SECTION_EXPECTED}" "${file}") <(exec ${FAWKSS_SCRIPT} <(awk "${SECTION_TEST}" "${file}")) | tail -n +3)
# Echo per-test results.
echo -ne ">> ${COLOR_WHITE}Testing file '$(basename ${file})'...${COLOR_RESET} "
if [ "x${result}" != "x" ]
then
# Increase total of failed tests run.
SUMMARY[1]=$((${SUMMARY[1]} + 1))
echo -e "${COLOR_RED}FAIL${COLOR_RESET}"
echo "${result}"
else
# Increase total of successful tests run.
SUMMARY[2]=$((${SUMMARY[2]} + 1))
echo -e "${COLOR_GREEN}OK${COLOR_RESET}"
fi
done
# Echo total results.
echo -ne ">> ${COLOR_WHITE}Summary:${COLOR_RESET} "
echo -ne "Total ${COLOR_WHITE}${SUMMARY[0]}${COLOR_RESET}, "
echo -ne "Failed ${COLOR_RED}${SUMMARY[1]}${COLOR_RESET}, "
echo -e "Successful ${COLOR_GREEN}${SUMMARY[2]}${COLOR_RESET}"

95
task-runner Executable file
View File

@ -0,0 +1,95 @@
#!/bin/bash
# Task runner for Fawks. Used for producing documentation, running test cases,
# and other similar tasks.
# ----------------
# Global variables
# ----------------
# Current working directory.
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Fawkss main script location.
FAWKSS="${ROOT}/fawkss"
# ANSI Color definitions.
WHITE="\033[1m"
UNDERLINE="\033[4m"
RED="\033[31m"
GREEN="\033[32m"
BLUE="\033[36m"
RESET="\033[0m"
# ---------------
# Task defintions
# ---------------
## doc - Build documentation from source file in Markdown format.
function task_doc() {
read -d '' extract_markdown <<-'_EOF_'
/^(#|# .*)$/ {if (f==1) {f=0; printf "```\\n\\n"} print substr($0, 3)}
/^[^#]/ {if (f==0) {f=1; printf "\\n```awk\\n"} print}
!NF {print}
END {if (f==1) {printf "```\\n"}}
_EOF_
awk "${extract_markdown}" "${FAWKSS}"
}
## test - Execute test suite, accepts list of specific files to run.
function task_test() {
local differ=$(which colordiff || which diff)
local actual="/--- TEST ---/ {f=1;next} /--- EXPECTED ---/ {exit} f"
local expected="/--- EXPECTED ---/ {f=1;next} /--- END ---/ {exit} f"
local summary=(0 0)
echo -e ">> ${WHITE}Executing tests...${RESET}"
for file in ${@:-${ROOT}/tests/*.scss}
do
# Check if file exists and is readable.
if [ ! -f "${file}" ]
then
echo -e ">> ${RED}Failed to find test file '${file}'${RESET}"
continue
fi
# Get diff between expected and actual output.
result=$(${differ} -ud <(awk "${expected}" "${file}") <(exec ${FAWKSS} <(awk "${actual}" "${file}")) | tail -n +3)
# Echo per-test results.
echo -ne ">> ${WHITE}Testing file '$(basename ${file})'...${RESET} "
if [ "x${result}" != "x" ]
then
printf "${RED}FAIL${RESET}\n${result}\n"
summary[0]=$((${summary[0]} + 1))
else
printf "${GREEN}OK${RESET}\n"
summary[1]=$((${summary[1]} + 1))
fi
done
printf ">> ${WHITE}summary:${RESET} "
printf "Total ${WHITE}$((${summary[0]} + ${summary[1]}))${RESET}, "
printf "Failed ${RED}${summary[0]}${RESET}, "
printf "Successful ${GREEN}${summary[1]}${RESET}\n"
}
function task_help() {
printf "Fawkss — The simple CSS preprocessor.\n\n"
printf "${UNDERLINE}Available Tasks${RESET}\n\n"
tasklist="/^##/ {printf \"${BLUE}%10s${RESET} %s\n\", substr(\$1, 4), \$NF}"
awk -F ' - ' "${tasklist}" "${BASH_SOURCE[0]}"
printf "\n"
}
# --------------
# Task execution
# --------------
action=${1:-help}
test "$(type -t "task_${action}")" == "function" && task_${action} ${@:2} || task_help