#!/bin/bash
# -*- coding: utf-8 -*-
# Regression tests on z0lib
#
# Based on template 2.1.1
[ $BASH_VERSINFO -lt 4 ] && echo "This script $0 requires bash 4.0+!" && exit 4
READLINK=$(which readlink 2>/dev/null)
export READLINK
THIS=$(basename "$0")
TDIR=$(readlink -f $(dirname $0))
ME=$(readlink -e $0)
if [[ -d $HOME/devel || -z $HOME_DEVEL || ! -d $HOME_DEVEL ]]; then
  [[ -d $HOME/odoo/devel ]] && HOME_DEVEL="$HOME/odoo/devel" || HOME_DEVEL="$HOME/devel"
fi
PYPATH=""
[[ $(basename $PWD) == "tests" && $(basename $PWD/../..) == "build" ]] && PYPATH="$(dirname $PWD)"
[[ $(basename $PWD) == "tests" && $(basename $PWD/../..) == "build" && -d $PWD/../scripts ]] && PYPATH="$PYPATH $(readlink -f $PWD/../scripts)"
x=$ME; while [[ $x != $HOME && $x != "/" && ! -d $x/lib && ! -d $x/bin && ! -d $x/pypi ]]; do x=$(dirname $x); done
[[ -d $x/pypi ]] && PYPATH="$PYPATH $x/pypi"
[[ -d $x/pypi/z0lib ]] && PYPATH="$PYPATH $x/pypi/z0lib"
[[ -d $x/pypi/z0lib/z0lib ]] && PYPATH="$PYPATH $x/pypi/z0lib/z0lib"
[[ -d $x/tools ]] && PYPATH="$PYPATH $x/tools"
[[ -d $x/tools/z0lib ]] && PYPATH="$PYPATH $x/tools/z0lib"
[[ -d $x/bin ]] && PYPATH="$PYPATH $x/bin"
[[ -d $x/lib ]] && PYPATH="$PYPATH $x/lib"
[[ -d $HOME_DEVEL/venv/bin ]] && PYPATH="$PYPATH $HOME_DEVEL/venv/bin"
[[ -d $HOME_DEVEL/../tools ]] && PYPATH="$PYPATH $(readlink -f $HOME_DEVEL/../tools)"
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "PYPATH=$PYPATH"
for d in $TDIR $PYPATH /etc; do
  if [[ -e $d/z0librc ]]; then
    . $d/z0librc
    Z0LIBDIR=$(readlink -e $d)
    break
  fi
done
[[ -z "$Z0LIBDIR" ]] && echo "Library file z0librc not found in <$PYPATH>!" && exit 72
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "Z0LIBDIR=$Z0LIBDIR"
TESTDIR=$(findpkg "" "$TDIR . .." "tests")
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "TESTDIR=$TESTDIR"
RUNDIR=$(readlink -e $TESTDIR/..)
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "RUNDIR=$RUNDIR"
Z0TLIBDIR=$(findpkg z0testrc "$PYPATH" "zerobug")
[[ -z "$Z0TLIBDIR" ]] && echo "Library file z0testrc not found!" && exit 72
. $Z0TLIBDIR
Z0TLIBDIR=$(dirname $Z0TLIBDIR)
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "Z0TLIBDIR=$Z0TLIBDIR"

# DIST_CONF=$(findpkg ".z0tools.conf" "$PYPATH")
# TCONF="$HOME/.z0tools.conf"
CFG_init "ALL"
link_cfg_def
link_cfg $DIST_CONF $TCONF
[[ $TRAVIS_DEBUG_MODE -ge 8 ]] && echo "DIST_CONF=$DIST_CONF" && echo "TCONF=$TCONF"
get_pypi_param ALL
RED="\e[1;31m"
CYAN="\e[1;36m"
GREEN="\e[1;32m"
CLR="\e[0m"

__version__=2.0.9


Z0BUG_setup() {
    # This test could fail because opt_dry_run is parsed by test itself
    # So opt_dry_run is saved and restored
    SAVED_OPT_DRY_RUN=$opt_dry_run
    FOUT=$TESTDIR/z0librc.out
    FTEST=$TESTDIR/z0librc.test
}

Z0BUG_teardown() {
    :
}

test_01() {
    local tested_opt_dry_run
    unset OPTLONG
    OPTOPTS=(h        n            V           v)
    OPTDEST=(opt_help opt_dry_run  opt_version opt_verbose)
    OPTACTI=(1        "1>"         "*>"        1)
    OPTDEFL=(1        0            ""          0)
    OPTMETA=("help"   "do nothing" "version"   "verbose")
    OPTHELP=("this help"\
     "do nothing (dry-run)"\
     "show version"\
     "verbose mode")
    OPTARGS=()

    parseoptargs "-V"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -V [-h]" "$opt_help" "0"
    test_result "cmd -V" "$opt_version" "-V"
    test_result "cmd -V [-n]" "$tested_opt_dry_run" "0"
    test_result "cmd -V [-v]" "$opt_verbose" "0"
    cat <<EOF >$FTEST
Usage: test_parseoptargs [-hnVv]
Unit test
 -h                   this help
 -n                   do nothing (dry-run)
 -V                   show version
 -v                   verbose mode


EOF
    print_help "Unit test" "">$FOUT
    test_result "print_help" "$FOUT" "$FTEST" "diff -qEbBZ"
}

test_02() {
    local tested_opt_dry_run
    OPTOPTS=(h        n            V           p        q           v)
    OPTDEST=(opt_help opt_dry_run  opt_version opt_path opt_verbose opt_verbose)
    OPTACTI=(1        "1>"         "*>"        "=>"     0           1)
    OPTDEFL=(1        0            ""          "~/"     "#"         "#")
    OPTMETA=("help"   "do nothing" "version"   "path"   "quiet"     "verbose")
    OPTHELP=("this help"\
     "do nothing (dry-run)"\
     "show version"\
     "path"
     "silent mode"\
     "verbose mode")
    OPTARGS=(tgt)

    opt_verbose=-1
    parseoptargs "mytarget"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd mytarget [-h]" "$opt_help" "0"
    test_result "cmd mytarget [-V]" "$opt_version" ""
    test_result "cmd mytarget [-n]" "$tested_opt_dry_run" "0"
    test_result "cmd mytarget [-p]" "$opt_path" "~/"
    if [ "$VERBOSE_MODE" == "1" -o "$VERBOSE_MODE" == "0" ]; then
      test_result "cmd mytarget [-qv]" "$opt_verbose" "$VERBOSE_MODE"
    elif [[ -t 0 || -p /dev/stdin ]]; then
      test_result "cmd mytarget [-qv]" "$opt_verbose" "0"
    else
      test_result "cmd mytarget [-qv]" "$opt_verbose" "1"
    fi

    opt_verbose=-1
    parseoptargs "mytarget" "-q"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd mytarget [-q]" "$opt_verbose" "0"

    opt_verbose=-1
    parseoptargs "mytarget" "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd mytarget [-v]" "$opt_verbose" "1"

    echo "Usage: test_parseoptargs [-hnV][-p path][-qv] tgt">$FTEST
    echo "Unit test">>$FTEST
    echo " -h             this help">>$FTEST
    echo " -n             do nothing (dry-run)">>$FTEST
    echo " -V             show version">>$FTEST
    echo " -p path        path">>$FTEST
    echo " -q             silent mode">>$FTEST
    echo " -v             verbose mode">>$FTEST
    print_help "Unit test"\
      "">$FOUT
    test_result "print_help" "$FOUT" "$FTEST" "diff -qEbB"

    parseoptargs "-p" "./mypath" "yourtarget"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -p ./mypath yourtarget [-h]" "$opt_help" "0"
    test_result "cmd -p ./mypath yourtarget [-V]" "$opt_version" ""
    test_result "cmd -p ./mypath yourtarget [-n]" "$tested_opt_dry_run" "0"
    test_result "cmd -p ./mypath yourtarget [-p]" "$opt_path" "./mypath"
    test_result "cmd -p ./mypath yourtarget [\$1]" "$tgt" "yourtarget"
}

test_03() {
    local tested_opt_dry_run
    OPTOPTS=(h        n            O       p        V)
    OPTDEST=(opt_help opt_dry_run  opt_own opt_path opt_version)
    OPTACTI=(1        "1>"         "="     "=>"     "*>")
    OPTDEFL=(1        0            "$USER" "~/"     "")
    OPTMETA=("help"   "do nothing" "own"   "path"   "version")
    OPTHELP=("this help"\
     "do nothing (dry-run)"\
     "owner username"\
     "path"\
     "show version")
    OPTARGS=(tgt)

    parseoptargs "mytarget"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd mytarget [-h]" "$opt_help" "0"
    test_result "cmd mytarget [-V]" "$opt_version" ""
    test_result "cmd mytarget [-n]" "$tested_opt_dry_run" "0"
    test_result "cmd mytarget [-p]" "$opt_path" "~/"
    test_result "cmd mytarget [-O]" "$opt_own" "$USER"
    test_result "cmd mytarget [\$1]" "$tgt" "mytarget"
    echo "Usage: test_parseoptargs [-hn][-O own][-p path][-V] tgt">$FTEST
    echo "Unit test">>$FTEST
    echo " -h             this help">>$FTEST
    echo " -n             do nothing (dry-run)">>$FTEST
    echo " -O own owner username">>$FTEST
    echo " -p path        path">>$FTEST
    echo " -V             show version">>$FTEST
    print_help "Unit test"\
      "">$FOUT
    test_result "print_help" "$FOUT" "$FTEST" "diff -qEbB"

    parseoptargs "mytarget" "-x"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -x mytarget [-h]" "$opt_help" "1"
    if [ -f $FOUT ]; then rm -f $FOUT; fi
    if [ -f $FTEST ]; then rm -f $FTEST; fi
}

test_04() {
    local tested_opt_dry_run
    OPTOPTS=(h        a     b     c     d     e     f     g     i     j     k     l     V)
    OPTLONG=(help     o-aa  o-bb  o-cc  o-dd  o-ee  o-ff  o-gg  o-ii  o-jj  o-kk  o-ll  version)
    OPTDEST=(opt_help opt_a opt_b opt_c opt_d opt_e opt_f opt_g opt_i opt_j opt_k opt_l opt_version)
    OPTACTI=(1        1     1     1     1     1     1     1     1     1     1     1     "*>")
    OPTDEFL=(0        0     0     0     0     0     0     0     0     0     0     0     "")
    OPTMETA=("help"   "o_a" "o_b" "o_c" "0_d" "o_e" "o_f" "o_g" "o_i" "o_j" "o_k" "o_l" "version")
    OPTHELP=("this help"
     "opt_a"
     "opt_b"
     "opt_c"
     "opt_d"
     "opt_e"
     "opt_f"
     "opt_g"
     "opt_i"
     "opt_j"
     "opt_k"
     "opt_l"
     "show version")
    OPTARGS=()

    parseoptargs "-a" "-b" "-c" "-d" "-e" "-f" "-g" "-i" "-j" "-k" "-l"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    for i in {a..l}; do
      if [ "$i" == "h" ]; then
        test_result "cmd -a -b -c ... [-h]" "$opt_help" "0"
      else
        o="opt_$i"
        test_result "cmd -a -b -c ... [-$i]" "${!o}" "1"
      fi
    done

    parseoptargs "--o-aa" "--o-bb" "--o-cc" "--o-dd" "--o-ee" "--o-ff" "--o-gg" "--o-ii" "--o-jj" "--o-kk" "--o-ll"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    for i in {a..l}; do
      if [ "$i" == "h" ]; then
        test_result "cmd --o-aa --o-bb --o-cc ... [--help]" "$opt_help" "0"
      else
        o="opt_$i"
        test_result "cmd --o-aa --o-bb --o-cc ... [--o-$i$i]" "${!o}" "1"
      fi
    done

}

test_05() {
    local tested_opt_dry_run
    unset OPTLONG
    OPTOPTS=(h        n            v           V)
    OPTDEST=(opt_help opt_dry_run  opt_verbose opt_version)
    OPTACTI=(1        1            "+"         "*>")
    OPTDEFL=(0        0            0           "")
    OPTMETA=("help"   "do nothing" "verbose"   "version")
    OPTHELP=("this help"\
     "do nothing (dry-run)"\
     "verbose mode"\
     "show version")
    OPTARGS=()

    parseoptargs "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -v [-h]" "$opt_help" "0"
    test_result "cmd -v" "$opt_verbose" "1"

    parseoptargs "-vv"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -vv" "$opt_verbose" "2"

    parseoptargs "-v" "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -v -v" "$opt_verbose" "2"

    parseoptargs "-v" "-n" "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -v -n -v" "$opt_verbose" "2"
}

test_06() {
    local tested_opt_dry_run
    OPTOPTS=(h        n            v           V)
    OPTDEST=(opt_help opt_dry_run  opt_verbose opt_version)
    OPTACTI=(1        1            "+"         "*>")
    OPTDEFL=(0        0            -1          "")
    OPTMETA=("help"   "do nothing" "verbose"   "version")
    OPTHELP=("this help"\
     "do nothing (dry-run)"\
     "verbose mode"\
     "show version")
    OPTARGS=()

    parseoptargs "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -v" "$opt_verbose" "1"

    parseoptargs "-vv"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -vv" "$opt_verbose" "2"

    parseoptargs "-v" "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -v -v" "$opt_verbose" "2"

    parseoptargs "-v" "-n" "-v"
    tested_opt_dry_run=$opt_dry_run
    opt_dry_run=$SAVED_OPT_DRY_RUN
    test_result "cmd -v -n -v" "$opt_verbose" "2"
}

Z0BUG_init
parseoptest -l$TESTDIR/test_z0lib.log "$@"
sts=$?
[[ $sts -ne 127 ]] && exit $sts
for p in z0librc odoorc travisrc zarrc z0testrc; do
  if [[ -f $RUNDIR/$p ]]; then
    [[ $p == "z0librc" ]] && Z0LIBDIR="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "odoorc" ]] && ODOOLIBDIR="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "travisrc" ]] && TRAVISLIBDIR="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "zarrc" ]] && ZARLIB="$RUNDIR" && source $RUNDIR/$p
    [[ $p == "z0testrc" ]] && Z0TLIBDIR="$RUNDIR" && source $RUNDIR/$p
  fi
done



UT1_LIST=
UT_LIST=
[[ "$(type -t Z0BUG_setup)" == "function" ]] && Z0BUG_setup
Z0BUG_main_file "$UT1_LIST" "$UT_LIST"
sts=$?
[[ "$(type -t Z0BUG_teardown)" == "function" ]] && Z0BUG_teardown
exit $sts
