Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # SMALL TESTING FRAMEWORK
- # Author: Tomasz Leman
- # Creation date: 30/08/2019
- # GLOBAL VARIABLES
- if [ -z ${LOG_LEVEL+x} ]; then
- export LOG_LEVEL=4
- fi
- if [ -z ${PASS_COUNT+x} ]; then
- export PASS_COUNT=0
- fi
- if [ -z ${FAIL_COUNT+x} ]; then
- export FAIL_COUNT=0
- fi
- declare -r NC='\033[0m'
- declare -r RED='\033[0;31m'
- declare -r BLUE='\033[0;34m'
- declare -r LIGHT_RED='\033[1;31m'
- declare -r LIGHT_GREEN='\033[1;32m'
- declare -r YELLOW='\033[1;33m'
- declare -r CYAN='\033[1;36m'
- declare -r WHITE='\033[1;37m'
- # Disable unicode for a performance increase.
- #LC_ALL=C
- #LANG=C
- # LOGGING FUNCTIONS
- function log () {
- (>&2 echo -e $*)
- }
- function pass_msg () {
- echo -e "$LIGHT_GREEN[PASS] $*$NC"
- let PASS_COUNT=PASS_COUNT+1
- }
- function fail_msg () {
- echo -e "$LIGHT_RED[FAIL] $*$NC"
- let FAIL_COUNT=FAIL_COUNT+1
- }
- function log_err () {
- if [ $LOG_LEVEL -gt 0 ]; then
- log "$RED[ERR] $*$NC"
- fi
- }
- function log_wrn () {
- if [ $LOG_LEVEL -gt 1 ]; then
- log "$YELLOW[WRN] $*$NC"
- fi
- }
- function log_inf () {
- if [ $LOG_LEVEL -gt 2 ]; then
- log "$CYAN[INF] $*$NC"
- fi
- }
- function log_dbg () {
- if [ $LOG_LEVEL -gt 3 ]; then
- log "$BLUE[DBG] $*$NC"
- fi
- }
- function msg () {
- echo -e "$WHITE[${FUNCNAME[1]}] $*$NC"
- }
- # FUNCTIONS FOR WORK WITH FILES AND DIRECTORIES
- function check_dir () {
- log_dbg "checking directory '$1'"
- if [ -d $1 ]; then
- return 0
- else
- log_err "directory '$1' does not exist"
- fi
- return 1
- }
- function check_file () {
- log_dbg "checking file '$1'"
- if [ -e $1 ]; then
- return 0
- else
- log_err "file '$1' does not exist"
- fi
- return 1
- }
- function check_exe_file () {
- log_dbg "checking file '$1'"
- if [ -x $1 ]; then
- return 0
- else
- log_err "'$1' is not an executable file"
- fi
- return 1
- }
- # TESTING FUNCTIONS
- function execute () {
- log_inf "executing command: $@"
- eval $@
- if [ $? -ne 0 ]; then
- fail_msg "test failed!"
- return 1
- fi
- pass_msg "test was successful"
- return 0
- }
- function load_script () {
- log_inf "loading test script $1"
- check_file $1
- if [ $? -eq 0 ]; then
- source $1
- for f in $(declare -F); do
- if [[ $f == stf_test_* ]]; then
- execute $f
- fi
- done
- else
- fail_msg "unable to load script: $1"
- fi
- }
- function load_directory () {
- log_inf "loading scripts from directory: $1"
- check_dir $1
- if [ $? -eq 0 ]; then
- for file in $1/*.sh; do
- check_file $file
- if [ $? -eq 0 ]; then
- load_script $file
- fi
- done
- for dir in $1/*/; do
- load_directory $dir
- done
- else
- log_err "can't load $1"
- fi
- }
- function report () {
- if [ $FAIL_COUNT -eq 0 ] && [ $PASS_COUNT -eq 0 ]; then
- log_wrn "it seems that no test was executed"
- fi
- msg "User: $USER, PID: $$, Date: `date +"%F %R"`"
- msg "Execution status: PASS=$PASS_COUNT, FAIL=$FAIL_COUNT (run time: ${SECONDS}s)"
- }
- function run_test () {
- for test in $@; do
- execute $test
- done
- return $FAIL_COUNT
- }
- function run_test_seq () {
- for test in $@; do
- execute $test
- if [ $? -ne 0 ]; then
- break
- fi
- done
- return $FAIL_COUNT
- }
- function main () {
- while [ $# -gt 0 ]; do
- log_dbg "number of parameters $# : $@"
- case $1 in
- "-S") shift; load_script $1; shift;;
- "-D") shift; load_directory $1; shift;;
- "-X") shift; execute $@; break;;
- *) log_err "bad parameter: $1"; break;;
- esac
- done
- exit $FAIL_COUNT
- }
- trap report EXIT
- [[ "$0" != "$BASH_SOURCE" ]] && log_dbg "sourcing BashTestFramework" || main $@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement