##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:       modules.50-cmds/%M%
#   Revision:       %I%
#   First Edition:  2017/01/22
#   Last Mod.:      %U%, %G%
#
#   Authors:        Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:    Testuite testsequence
#   Command:        load, display, help, avail, aliases, whatis
#   Command:        apropos, list, switch, unload, test
#   Modulefiles:    inforc
#   Sub-Command:
#
#   Comment:    %C{
#           Test 'module-info' commands when called from a .modulerc file
#       }C%
#
##############################################################################

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

#
#  Variables. This test forces a module load command. It will result in the
#    environment variables "_LMFILES_", "LOADEDMODULES" and "testsuite" to
#    be set up
#

set module "inforc"
set module2 "inforc/2.0"
set modulefile2 "$modpath/$module2"
set modulefilere2 "$modpathre/$module2"

#
#  For sh shell only (no need to check every shells)
#

lappend ans [list set _LMFILES__modshare "$modulefile2:1"]
lappend ans [list set LOADEDMODULES_modshare "$module2:1"]
lappend ans [list set MODULES_LMALTNAME_modshare "$module2&inforc/bar&inforc/default&inforc:1"]
lappend ans [list set _LMFILES_ $modulefile2]
lappend ans [list set LOADEDMODULES $module2]
lappend ans [list set MODULES_LMALTNAME "$module2&inforc/bar&inforc/default&inforc"]

set out_rc "type: Tcl
name: $module/.modulerc
specified: $module/.modulerc
shell: sh
shelltype: sh
alias $module/foo: $module/1.0
version $module/bar: $module/2.0
symbols $module/2.0: bar:default"
set out_rc_load "mode: load\ncommand: load\n$out_rc"
set out_rc_display "mode: display\ncommand: display\n$out_rc"
set out_rc_help "mode: help\ncommand: help\n$out_rc"
set out_rc_test "mode: test\ncommand: test\n$out_rc"
set out_rc_switch "mode: load\ncommand: switch\n$out_rc"

set header_list $cur_loaded
set header "$modlin $modpathre $modlin"

#
#  The tests
#

# test loading
testouterr_cmd "sh" "load $module" $ans "$out_rc_load"

# test displaying
testouterr_cmd_re "sh" "display $module" "OK" "$out_rc_display\n$modlin\n$modulefilere2:\n\n$modlin"

# test help
testouterr_cmd_re "sh" "help $module" "OK" "$out_rc_help\n$modlin\nModule Specific Help for $modulefilere2:\n\n$modlin"

# test test
testouterr_cmd_re "sh" "test $module" "OK" "$out_rc_test\n$modlin\nModule Specific Test for $modulefilere2:\n\nTest result: PASS\n$modlin"

# test avail
# no output as defined in modulerc if mode is not set
testouterr_cmd_re "sh" "avail $module" "OK" "$header\n$module/1.0\\s+$module/2.0\\(avail:bar:default\\)\\s+$module/foo\\(@\\)\\s+\n\nKey:\n\\(@\\)=module-alias  \\(symbolic-version\\)  "

# test aliases
testouterr_cmd_re "sh" "aliases" "OK" ".*$module/aliases -> $module2\n.*"

# test whatis
# no output as defined in modulerc if mode = whatis
testouterr_cmd "sh" "whatis $module" "OK" ""

# test apropos
# no output as defined in modulerc if mode = whatis
# error found in interpreted module, so an error code is rendered
testouterr_cmd "sh" "apropos $module" "ERR" ""

# Set up the environment for test that require the module to be loaded
setenv_loaded_module $module2 $modulefile2
setenv_path_var MODULES_LMALTNAME "$module2&inforc/bar&inforc/default&inforc"

# test listing
testouterr_cmd_re "sh" "list" "OK" "$header_list\n 1\\) $module2\\(bar:default\\)\\s+\n\nKey:\n\\(symbolic-version\\)  "

# test switching
# modulerc is interpreted on the second phase of switching (load)
testouterr_cmd "sh" "switch $module $module2" $ans "$out_rc_switch"

# test unloading
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMALTNAME]
# modulerc is not interpreted when unloading a loaded module
testouterr_cmd "sh" "unload $module" $ans ""


#
#  Cleanup
#

reset_test_env
