SHARE
TWEET

Untitled

a guest Sep 11th, 2019 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # SMALL TESTING FRAMEWORK
  3. # Author: Tomasz Leman
  4. # Creation date: 30/08/2019
  5.  
  6. # GLOBAL VARIABLES
  7. if [ -z ${LOG_LEVEL+x} ]; then
  8.        export LOG_LEVEL=4
  9. fi
  10.  
  11. if [ -z ${PASS_COUNT+x} ]; then
  12.        export PASS_COUNT=0
  13. fi
  14.  
  15. if [ -z ${FAIL_COUNT+x} ]; then
  16.        export FAIL_COUNT=0
  17. fi
  18.  
  19. declare -r NC='\033[0m'
  20. declare -r RED='\033[0;31m'
  21. declare -r BLUE='\033[0;34m'
  22. declare -r LIGHT_RED='\033[1;31m'
  23. declare -r LIGHT_GREEN='\033[1;32m'
  24. declare -r YELLOW='\033[1;33m'
  25. declare -r CYAN='\033[1;36m'
  26. declare -r WHITE='\033[1;37m'
  27.  
  28. # Disable unicode for a performance increase.
  29. #LC_ALL=C
  30. #LANG=C
  31.  
  32. # LOGGING FUNCTIONS
  33. function log () {
  34.     (>&2 echo -e $*)
  35. }
  36.  
  37. function pass_msg () {
  38.     echo -e "$LIGHT_GREEN[PASS] $*$NC"
  39.     let PASS_COUNT=PASS_COUNT+1
  40. }
  41.  
  42. function fail_msg () {
  43.     echo -e "$LIGHT_RED[FAIL] $*$NC"
  44.     let FAIL_COUNT=FAIL_COUNT+1
  45. }
  46.  
  47. function log_err () {
  48.     if [ $LOG_LEVEL -gt 0 ]; then
  49.         log "$RED[ERR] $*$NC"
  50.     fi
  51. }
  52.  
  53. function log_wrn () {
  54.     if [ $LOG_LEVEL -gt 1 ]; then
  55.         log "$YELLOW[WRN] $*$NC"
  56.     fi
  57. }
  58.  
  59. function log_inf () {
  60.     if [ $LOG_LEVEL -gt 2 ]; then
  61.         log "$CYAN[INF] $*$NC"
  62.     fi
  63. }
  64.  
  65. function log_dbg () {
  66.     if [ $LOG_LEVEL -gt 3 ]; then
  67.         log "$BLUE[DBG] $*$NC"
  68.     fi
  69. }
  70.  
  71. function msg () {
  72.     echo -e "$WHITE[${FUNCNAME[1]}] $*$NC"
  73. }
  74.  
  75. # FUNCTIONS FOR WORK WITH FILES AND DIRECTORIES
  76. function check_dir () {
  77.     log_dbg "checking directory '$1'"
  78.     if [ -d $1 ]; then
  79.         return 0
  80.     else
  81.         log_err "directory '$1' does not exist"
  82.     fi
  83.  
  84.     return 1
  85. }
  86.  
  87. function check_file () {
  88.     log_dbg "checking file '$1'"
  89.     if [ -e $1 ]; then
  90.         return 0
  91.     else
  92.         log_err "file '$1' does not exist"
  93.     fi
  94.  
  95.     return 1
  96. }
  97.  
  98. function check_exe_file () {
  99.     log_dbg "checking file '$1'"
  100.     if [ -x $1 ]; then
  101.         return 0
  102.     else
  103.         log_err "'$1' is not an executable file"
  104.     fi
  105.  
  106.     return 1
  107. }
  108.  
  109. # TESTING FUNCTIONS
  110. function execute () {
  111.     log_inf "executing command: $@"
  112.     eval $@
  113.     if [ $? -ne 0 ]; then
  114.         fail_msg "test failed!"
  115.         return 1
  116.     fi
  117.  
  118.     pass_msg "test was successful"
  119.     return 0
  120. }
  121.  
  122. function load_script () {
  123.     log_inf "loading test script $1"
  124.     check_file $1
  125.     if [ $? -eq 0 ]; then
  126.         source $1
  127.         for f in $(declare -F); do
  128.             if [[ $f == stf_test_* ]]; then
  129.                 execute $f
  130.             fi
  131.         done
  132.     else
  133.         fail_msg "unable to load script: $1"
  134.     fi
  135. }
  136.  
  137. function load_directory () {
  138.     log_inf "loading scripts from directory: $1"
  139.     check_dir $1
  140.     if [ $? -eq 0 ]; then
  141.         for file in $1/*.sh; do
  142.             check_file $file
  143.             if [ $? -eq 0 ]; then
  144.                 load_script $file
  145.             fi
  146.         done
  147.  
  148.         for dir in $1/*/; do
  149.             load_directory $dir
  150.         done
  151.     else
  152.         log_err "can't load $1"
  153.     fi
  154. }
  155.  
  156. function report () {
  157.     if [ $FAIL_COUNT -eq 0 ] && [ $PASS_COUNT -eq 0 ]; then
  158.         log_wrn "it seems that no test was executed"
  159.     fi
  160.  
  161.     msg "User: $USER, PID: $$, Date: `date +"%F %R"`"
  162.     msg "Execution status: PASS=$PASS_COUNT, FAIL=$FAIL_COUNT (run time: ${SECONDS}s)"
  163. }
  164.  
  165. function run_test () {
  166.     for test in $@; do
  167.         execute $test
  168.     done
  169.  
  170.     return $FAIL_COUNT
  171. }
  172.  
  173. function run_test_seq () {
  174.     for test in $@; do
  175.         execute $test
  176.         if [ $? -ne 0 ]; then
  177.             break
  178.         fi
  179.     done
  180.  
  181.     return $FAIL_COUNT
  182. }
  183.  
  184. function main () {
  185.     while [ $# -gt 0 ]; do
  186.         log_dbg "number of parameters $# : $@"
  187.         case $1 in
  188.             "-S") shift; load_script $1; shift;;
  189.             "-D") shift; load_directory $1; shift;;
  190.             "-X") shift; execute $@; break;;
  191.             *) log_err "bad parameter: $1"; break;;
  192.         esac
  193.     done
  194.  
  195.     exit $FAIL_COUNT
  196. }
  197.  
  198. trap report EXIT
  199. [[ "$0" != "$BASH_SOURCE" ]] && log_dbg "sourcing BashTestFramework" || main $@
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top