"Fossies" - the Fresh Open Source Software Archive

Member "detox-1.4.5/tests/test-functions" (15 Aug 2021, 3808 Bytes) of package /linux/privat/detox-1.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test-functions": 1.4.4_vs_1.4.5.

    1 #!/bin/sh
    2 #
    3 # unit test functions
    4 #
    5 
    6 ##
    7 # Realpath polyfill for systems where it doesn't exist
    8 #
    9 # @param $1 Path to examine
   10 #
   11 
   12 if [ ! -x "$(command -v realpath)" ] ; then
   13     function realpath()
   14     {
   15         readlink -f $1
   16     }
   17 fi
   18 
   19 ##
   20 # Executes a single test against a single function using a single table.
   21 #
   22 # @param $1 Detox Executable
   23 # @param $2 Input Filename
   24 # @param $3 Output Filename
   25 # @param $4 Method (utf_8, safe, etc)
   26 # @param $5 Table (path to the table to use)
   27 #
   28 # @return int 0 for success, 1 for failure
   29 #
   30 function test_single_table ()
   31 {
   32     if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" -o -z "$5" ] ; then
   33         echo missing parameters
   34         return 1
   35     fi
   36 
   37     local DETOX=$1
   38     local INPUT=$2
   39     local OUTPUT=$3
   40     local METHOD=$4
   41     local TABLE=$5
   42 
   43     if [ ! -x "$DETOX" ] ; then
   44         echo $DETOX is not executable
   45         return 1
   46     fi
   47 
   48     if [ ! -f "$TABLE" ] ; then
   49         echo $TABLE is not a file
   50         return 1
   51     fi
   52 
   53     local BASE=/tmp/detoxtest/
   54     if [ ! -d $BASE ] ; then
   55         mkdir $BASE
   56     fi
   57     local WORK=$(realpath $(mktemp -d $BASE/test-XXXXXX))
   58 
   59     local TMPTABLE=$WORK/test.tbl
   60     cp $TABLE $TMPTABLE
   61 
   62     local SEQUENCE=unittest
   63     local RC=$WORK/detoxrc
   64 
   65     cat <<- DONE > $RC
   66     sequence $SEQUENCE {
   67         $METHOD {
   68             filename "$TMPTABLE";
   69         };
   70     };
   71     DONE
   72 
   73     cd $WORK
   74 
   75     local ACTION="touch"
   76     local OPERATOR="-f"
   77     local CMDINPUT="$INPUT"
   78     if [ "${INPUT: -1}" = "/" ] ; then
   79         ACTION="mkdir"
   80         OPERATOR="-d"
   81         CMDINPUT="${INPUT:0: -1}"
   82     fi
   83 
   84     $ACTION "$CMDINPUT"
   85     $DETOX -s $SEQUENCE -f $RC "$CMDINPUT"
   86     if [ ! $OPERATOR "$OUTPUT" ] ; then
   87         echo renaming "$INPUT" to "$OUTPUT" failed
   88         return 1
   89     fi
   90 
   91     return 0
   92 }
   93 
   94 ##
   95 # Executes a single test against one or more functions in a sequence, using the
   96 # stock tables when applicable, from source control.
   97 #
   98 # @param $1 Detox Executable
   99 # @param $2 Input Filename
  100 # @param $3 Output Filename
  101 # @param $4 Path to Stock Tables
  102 # @param $5 First method (utf_8, safe, etc)
  103 # @param ... Additional methods
  104 #
  105 # @return int 0 for success, 1 for failure
  106 #
  107 function test_sequence ()
  108 {
  109     if [ -z "$1" -o -z "$2" -o -z "$3" -o -z "$4" -o -z "$5" ] ; then
  110         echo missing parameters
  111         return 1
  112     fi
  113 
  114     local DETOX=$1
  115     local INPUT=$2
  116     local OUTPUT=$3
  117     local TABLEPATH=$4
  118     shift 4
  119 
  120     if [ ! -x "$DETOX" ] ; then
  121         echo $DETOX is not executable
  122         return 1
  123     fi
  124 
  125     if [ ! -d "$TABLEPATH" ] ; then
  126         echo $TABLEPATH is not a dir
  127         return 1
  128     fi
  129 
  130     local BASE=/tmp/detoxtest/
  131     if [ ! -d $BASE ] ; then
  132         mkdir $BASE
  133     fi
  134     local WORK=$(realpath $(mktemp -d $BASE/test-XXXXXX))
  135 
  136     local SEQUENCE=unittest
  137     local RC=$WORK/detoxrc
  138 
  139     cat <<- DONE > $RC
  140     sequence $SEQUENCE {
  141     DONE
  142 
  143     while [ -n "$1" ] ; do
  144 
  145         case $1 in
  146             safe | iso8859_1 | utf_8 )
  147                 local TABLE=$1.tbl
  148                 if [ $1 = "utf_8" ] ; then
  149                     TABLE="unicode.tbl"
  150                 fi
  151                 cp $TABLEPATH/$TABLE.sample $WORK/$TABLE
  152 
  153                 cat <<- DONE >> $RC
  154                     $1 {
  155                         filename "$WORK/$TABLE";
  156                     };
  157                 DONE
  158                 ;;
  159 
  160             lower | uncgi | wipeup )
  161                 cat <<- DONE >> $RC
  162                     $1;
  163                 DONE
  164                 ;;
  165 
  166             wipeup-remove-trailing )
  167                 cat <<- DONE >> $RC
  168                     wipeup {
  169                         remove_trailing;
  170                     };
  171                 DONE
  172                 ;;
  173 
  174             max-length-32 )
  175                 cat <<- DONE >> $RC
  176                     max_length {
  177                         length 32;
  178                     };
  179                 DONE
  180                 ;;
  181 
  182             safe-basic )
  183                 cat <<- DONE >> $RC
  184                     safe;
  185                 DONE
  186                 ;;
  187 
  188             utf_8-basic )
  189                 cat <<- DONE >> $RC
  190                     utf_8;
  191                 DONE
  192                 ;;
  193 
  194             iso8859_1-basic )
  195                 cat <<- DONE >> $RC
  196                     iso8859_1;
  197                 DONE
  198                 ;;
  199 
  200             *)
  201                 echo "Unknown method $1";
  202                 return 1;
  203                 ;;
  204         esac
  205 
  206         shift
  207     done
  208 
  209     cat <<- DONE >> $RC
  210     };
  211     DONE
  212 
  213     cd $WORK
  214 
  215     local ACTION="touch"
  216     local OPERATOR="-f"
  217     local CMDINPUT="$INPUT"
  218     if [ "${INPUT: -1}" = "/" ] ; then
  219         ACTION="mkdir"
  220         OPERATOR="-d"
  221         CMDINPUT="${INPUT:0: -1}"
  222     fi
  223 
  224     $ACTION "$CMDINPUT"
  225     $DETOX -s $SEQUENCE -f $RC "$CMDINPUT"
  226     if [ ! $OPERATOR "$OUTPUT" ] ; then
  227         echo renaming "$INPUT" to "$OUTPUT" failed
  228         return 1
  229     fi
  230 
  231     return 0
  232 }