##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2017/08/23
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, unload, switch, display, help, test, avail,
#                   whatis, path, paths
#   Modulefiles:	loc_dv1, loc_dv2
#   Sub-Command:
#
#   Comment:	%C{
#           Test the correct resolution of default version for a module
#           depending of the command-context when another version of this
#           module is currently loaded
#		}C%
#
##############################################################################


# ensure avail tests are made using in depth mode
setenv_var MODULES_AVAIL_INDEPTH 1

# ensure default tag abbreviation is defined
setenv_var MODULES_TAG_ABBREV {auto-loaded=aL:loaded=L:hidden=H:hidden-loaded=H:forbidden=F:nearly-forbidden=nF}

# module implicit default version
set modname_1 "loc_dv1"
set module_1_loa "loc_dv1/1.0"
set module_1_def "loc_dv1/2.0"
if {[is_conf_enabled advversspec implicitdefault]} {
    set modalt_1_def as|loc_dv1/default&as|loc_dv1/latest
}
set modulefile_1_loa "$modpath/$module_1_loa"
set modulefile_1_def "$modpath/$module_1_def"
set modulefilere_1_def "$modpathre/$module_1_def"
set modavail_1 "$module_1_loa <L>\n$module_1_def"
set modwhatis_1 "\\s+$module_1_loa: $module_1_loa\n\\s+$module_1_def: $module_1_def"
set ans_paths_1 [list]
lappend ans_paths_1 [list text [list $modulefile_1_loa $modulefile_1_def]]
set nodefault_1 "$err_nodefault'loc_dv1'"

# module explicitely set default version
set modname_2 "loc_dv2"
set module_2_loa "loc_dv2/2.0"
set module_2_def "loc_dv2/1.0"
set modalt_2_def loc_dv2/default&loc_dv2
set modulefile_2_loa "$modpath/$module_2_loa"
set modulefile_2_def "$modpath/$module_2_def"
set modulefilere_2_def "$modpathre/$module_2_def"
set modavail_2 "$module_2_def\\(default\\)\n$module_2_loa <L>"
set modwhatis_2 "\\s+$module_2_def: $module_2_def\n\\s+$module_2_loa: $module_2_loa"
set ans_paths_2 [list]
lappend ans_paths_2 [list text [list $modulefile_2_def $modulefile_2_loa]]


set help_pre "$modlin\nModule Specific Help for "
set help_mid ":\n\nWARNING: Unable to find ModulesHelp in "
set help_post ".\n$modlin"
set test_pre "$modlin\nModule Specific Test for "
set test_mid ":\n\nWARNING: Unable to find ModulesTest in "
set test_post ".\n$modlin"
set disp_pre "$modlin\n"
set disp_wi "module-whatis\\s+"
set disp_post "\n$modlin"
set header "$modlin $modpathre $modlin"


#
#  The tests
#

for {set i 1} {$i <= 2} {incr i} {
    set modname [set "modname_${i}"]
    set module_loa [set "module_${i}_loa"]
    set modulefile_loa [set "modulefile_${i}_loa"]
    set module_def [set "module_${i}_def"]
    if {[info exists "modalt_${i}_def"]} {
        set modalt_def $module_def&[set "modalt_${i}_def"]
    } else {
        set modalt_def ""
    }
    set modulefile_def [set "modulefile_${i}_def"]
    set modulefilere_def [set "modulefilere_${i}_def"]
    set modavail [set "modavail_${i}"]
    set modwhatis [set "modwhatis_${i}"]
    set ans_paths [set "ans_paths_${i}"]
    if {[info exists "nodefault_${i}"]} {
        set nodefault [set "nodefault_${i}"]
    } else {
        set nodefault ""
    }

    # setup environment with module to load
    setenv_loaded_module $module_loa $modulefile_loa
    unsetenv_path_var MODULES_LMALTNAME

    set ans [list]
    lappend ans [list setpath LOADEDMODULES "$module_loa:$module_def"]
    lappend ans [list setpath _LMFILES_ "$modulefile_loa:$modulefile_def"]
    if {$modalt_def ne ""} {
        lappend ans [list setpath MODULES_LMALTNAME $modalt_def]
    }
    if {$nodefault eq {}} {
        testouterr_cmd_re "sh" "load $modname" $ans ""
    } else {
        testouterr_cmd_re "sh" "load $modname" [if_implicitdefault $ans ERR] [if_implicitdefault {} $nodefault]
    }

    set ansun [list]
    lappend ansun [list unsetpath LOADEDMODULES]
    lappend ansun [list unsetpath _LMFILES_]
    testouterr_cmd_re "sh" "unload $modname" $ansun ""

    set ans [list]
    lappend ans [list setpath LOADEDMODULES "$module_def"]
    lappend ans [list setpath _LMFILES_ "$modulefile_def"]
    if {$modalt_def ne ""} {
        lappend ans [list setpath MODULES_LMALTNAME $modalt_def]
    }
    if {$nodefault eq {}} {
        testouterr_cmd_re "sh" "switch $modname $modname" $ans ""
    } else {
        lappend ansun [list ERR]
        testouterr_cmd_re "sh" "switch $modname $modname" [if_implicitdefault $ans $ansun] [if_implicitdefault {} [msg_switch $module_loa $modname $nodefault]]
    }

    set displayerr "$disp_pre$modulefilere_def:\n\n$disp_wi$module_def$disp_post"
    set helperr "$help_pre$modulefilere_def$help_mid$modulefilere_def$help_post"
    set testerr "$test_pre$modulefilere_def$test_mid$modulefilere_def$test_post"

    if {$nodefault eq {}} {
        testouterr_cmd_re "sh" "display $modname" "OK" $displayerr
        testouterr_cmd_re "sh" "help $modname" "OK" $helperr
        testouterr_cmd_re "sh" "test $modname" "OK" $testerr
    } else {
        testouterr_cmd_re "sh" "display $modname" [if_implicitdefault OK ERR] [if_implicitdefault $displayerr $nodefault]
        testouterr_cmd_re "sh" "help $modname" [if_implicitdefault OK ERR] [if_implicitdefault $helperr $nodefault]
        testouterr_cmd_re "sh" "test $modname" [if_implicitdefault OK ERR] [if_implicitdefault $testerr $nodefault]
    }

    testouterr_cmd_re "sh" "avail -t $modname" "OK" "$modpathre:\n$modavail"
    testouterr_cmd_re "sh" "whatis $modname" "OK" "$modlin $modpathre $modlin\n$modwhatis"

    set ans [list]
    lappend ans [list text $modulefile_def]
    if {$nodefault eq {}} {
        testouterr_cmd "sh" "path $modname" $ans ""
    } else {
        testouterr_cmd "sh" "path $modname" [if_implicitdefault $ans [list [list text {}]]] [if_implicitdefault {} $nodefault]
    }
    testouterr_cmd "sh" "paths $modname" $ans_paths ""
}


#
#  Cleanup
#

reset_test_env
