##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	1995/12/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Jens Hamisch, Jens.Hamisch@Strawberry.COM
#
#   Description:	Testuite testsequence
#   Command:		load
#   Modulefiles:	setenv/1.0, module/2.0
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the 'module load' subcommand
#		}C%
#
##############################################################################

#
#  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 "setenv/1.0"
set modulefile "$modpath/$module"

set module_1 "module/2.0"
set modulefile_1 "$modpath/$module_1"

set module_2 "trace/all_on"
set modulefile_2 "$modpath/$module_2"
set module_3 "prereq/module"
set modulefile_3 "$modpath/$module_3"
set module_4 "module/meta"
set modulefile_4 "$modpath/$module_4"

set module_5 "module/err"
set modulefile_5 "$modpath/$module_5"
set module_6 "module/bad"
set modulefile_6 "$modpath/$module_6"
set module_7 "module/unk"
set modulefile_7 "$modpath/$module_7"
set module_8 "module/lerr"
set modulefile_8 "$modpath/$module_8"
set module_9 "module/lbad"
set modulefile_9 "$modpath/$module_9"
set module_10 "module/lunk"
set modulefile_10 "$modpath/$module_10"
set module_11 "module/relpath"
set modulefile_11 "$modpath/$module_11"

set module_12 "module/empty"
set modulefile_12 "$modpath/$module_12"

set submod_5 "append/0.2"
set submodfile_5 "$modpath/$submod_5"
set submod_6 "bad2/body"
set submodfile_6 "$modpath/$submod_6"
set submod_7 "NonExistant"
set submodfile_7 "$modpath/$submod_7"
set submod_11 $submod_5
set submodfile_11 $submodfile_5

set moderr_5 [msg_load $submod_5 [msg_moderr "add-path $msg_patheqsep" "append-path\tPATH\t\t:" $submodfile_5 24]]\n\n[msg_load $module_5 [err_reqlo $submod_5]]
set moderr_5s [msg_load $submod_5 [msg_moderr "add-path $msg_patheqsep" "append-path\tPATH\t\t:" $submodfile_5 24]]\n\n[msg_load $module_5 [err_reqmis $submod_5]]
set moderr_6 [msg_load $submod_6 [msg_moderr "$msg_invcmdname \"bad-command\"" {bad-command} $submodfile_6 29]]\n\n[msg_load $module_6 [err_reqlo $submod_6]]
set moderr_6s [msg_load $submod_6 [msg_moderr "$msg_invcmdname \"bad-command\"" {bad-command} $submodfile_6 29]]\n\n[msg_load $module_6 [err_reqmis $submod_6]]
set moderr_7 [msg_load $module_7 "$err_path'$submod_7'" [err_reqlo $submod_7]]
set moderr_7s [msg_load $module_7 "$err_path'$submod_7'" [err_reqmis $submod_7]]
set moderr_11 [msg_load $submodfile_11 [msg_moderr "add-path $msg_patheqsep" "append-path\tPATH\t\t:" $submodfile_11 24]]\n\n[msg_load $module_11 [err_reqlo ../$submod_11]]

set msgtopun_5 \n\n[msg_top_unload $module_5 {} {} {} [err_urequn $submod_5]]
set msgtopun_6 \n\n[msg_top_unload $module_6 {} {} {} [err_urequn $submod_6]]
set msgtopun_7 \n\n[msg_top_unload $module_7 {} {} {} [err_urequn $submod_7]]
set msgtopun_8 \n\n[msg_top_unload $module_8 {} [list $module_5] {} [err_urequn $submod_5]]
set msgtopun_9 \n\n[msg_top_unload $module_9 {} [list $module_6] {} [err_urequn $submod_6]]
set msgtopun_10 \n\n[msg_top_unload $module_10 {} [list $module_7] {} [err_urequn $submod_7]]
set msgtopun_11 \n\n[msg_top_unload $module_11 {} {} {} [err_urequn $submod_11]]

set moderrun_5 [msg_unload $submod_5 [msg_moderr "unload-path $msg_patheqsep" "append-path\tPATH\t\t:" $submodfile_5 24]]$msgtopun_5
set moderrun_6 [msg_unload $submod_6  [msg_moderr "$msg_invcmdname \"bad-command\"" {bad-command} $submodfile_6 29]]$msgtopun_6
set moderrun_7 [msg_unload $submod_7 "$err_file'$submodfile_7'"]$msgtopun_7
set moderrun_8 [msg_unload $submod_5 [msg_moderr "unload-path $msg_patheqsep" "append-path\tPATH\t\t:" $submodfile_5 24]]$msgtopun_8
set moderrun_9 [msg_unload $submod_6  [msg_moderr "$msg_invcmdname \"bad-command\"" {bad-command} $submodfile_6 29]]$msgtopun_9
set moderrun_10 [msg_unload $submod_7 "$err_file'$submodfile_7'"]$msgtopun_10
set moderrun_11 [msg_unload $submod_11 [msg_moderr "unload-path $msg_patheqsep" "append-path\tPATH\t\t:" $submodfile_11 24]]$msgtopun_11

#
#  The tests
#

lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module:$module_1"]
lappend ans [list setpath _LMFILES_ "$modulefile:$modulefile_1"]
lappend ans [list setpath MODULES_LMPREREQ $module_1&$module]
lappend ans [list setpath MODULES_LMNOTUASKED $module]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $module&as|setenv/default&as|setenv/latest]
}

test_cmd_re "csh" "load $module_1" $ans

#
# Test load of meta-module
#

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module:$module_2:$module_3:$module_4"]
lappend ans [list setpath _LMFILES_ "$modulefile:$modulefile_2:$modulefile_3:$modulefile_4"]
lappend ans [list setpath MODULES_LMPREREQ "$module_3&trace:$module_4&setenv&trace&$module_3"]
lappend ans [list setpath MODULES_LMNOTUASKED "$module:$module_2:$module_3"]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $module&as|setenv/default&as|setenv/latest:$module_2&as|trace/default&as|trace/latest]
}

test_cmd_re "csh" "load $module_4" [if_implicitdefault $ans ERR]

#
# Test unload of meta-module
#

# Set up the environment for test that require the module to be loaded
setenv_loaded_module [list $module $module_2 $module_3 $module_4] [list $modulefile $modulefile_2 $modulefile_3 $modulefile_4] [list $module $module_2 $module_3]
setenv_path_var MODULES_LMPREREQ $module_3&trace $module_4&setenv&trace&$module_3

set ans [list]
# unload interpretations are not cascaded if auto_handling enabled so answer order change
if {$install_autohandling eq {n}} {
lappend ans [list unset testsuite]
}
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMPREREQ]
if {$install_autohandling eq {y}} {
lappend ans [list unset testsuite]
}
lappend ans [list unsetpath MODULES_LMNOTUASKED]

test_cmd_re "csh" "unload $module_4" $ans

# Clean up the just changed environment
unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ

#
# Test load of module with issue, nothing loads
#

testouterr_cmd "csh" "load $module_5" "ERR" "$moderr_5"
testouterr_cmd "csh" "load $module_6" "ERR" "$moderr_6"
testouterr_cmd "csh" "load $module_7" "ERR" "$moderr_7"
testouterr_cmd "csh" "load $module_8" "ERR" "$moderr_5s\n\n[msg_load $module_8 [err_reqlo $submod_5] [err_reqlo $module_5]]"
testouterr_cmd "csh" "load $module_9" "ERR" "$moderr_6s\n\n[msg_load $module_9 [err_reqlo $submod_6] [err_reqlo $module_6]]"
testouterr_cmd "csh" "load $module_10" "ERR" "$moderr_7s\n\n[msg_load $module_10 [err_reqlo $submod_7] [err_reqlo $module_7]]"
testouterr_cmd "csh" "load $module_11" "ERR" "$moderr_11"
testouterr_cmd "csh" "load $module_12" "ERR" [msg_load $module_12 $err_emptymodname [err_reqlo {}]]

#
# Test unload of module with issue
#

setenv_loaded_module [list $submod_5 $module_5] [list $submodfile_5 $modulefile_5] [list $submod_5]
setenv_path_var MODULES_LMPREREQ $module_5&$submod_5
set ans [list]
# unload interpretations are not cascaded if auto_handling enabled so answer order change
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_5]
lappend ans [list setpath LOADEDMODULES $submod_5]
} else {
lappend ans [list setpath LOADEDMODULES $submod_5]
lappend ans [list setpath _LMFILES_ $submodfile_5]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_5" $ans "$moderrun_5"

setenv_loaded_module [list $submod_6 $module_6] [list $submodfile_6 $modulefile_6] [list $submod_6]
setenv_path_var MODULES_LMPREREQ $module_6&$submod_6
set ans [list]
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_6]
lappend ans [list setpath LOADEDMODULES $submod_6]
} else {
lappend ans [list setpath LOADEDMODULES $submod_6]
lappend ans [list setpath _LMFILES_ $submodfile_6]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_6" $ans "$moderrun_6"

setenv_loaded_module [list $submod_7 $module_7] [list $submodfile_7 $modulefile_7] [list $submod_7]
setenv_path_var MODULES_LMPREREQ $module_7&$submod_7
set ans [list]
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_7]
lappend ans [list setpath LOADEDMODULES $submod_7]
} else {
lappend ans [list setpath LOADEDMODULES $submod_7]
lappend ans [list setpath _LMFILES_ $submodfile_7]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_7" $ans "$moderrun_7"

setenv_loaded_module [list $submod_5 $module_5 $module_8] [list $submodfile_5 $modulefile_5 $modulefile_8] [list $submod_5 $module_5]
setenv_path_var MODULES_LMPREREQ $module_5&$submod_5 $module_8&$module_5
set ans [list]
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_5]
lappend ans [list setpath LOADEDMODULES $submod_5]
} else {
lappend ans [list setpath LOADEDMODULES $submod_5]
lappend ans [list setpath _LMFILES_ $submodfile_5]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list setpath MODULES_LMNOTUASKED $submod_5]
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_8" $ans "$moderrun_8"

setenv_loaded_module [list $submod_6 $module_6 $module_9] [list $submodfile_6 $modulefile_6 $modulefile_9] [list $submod_6 $module_6]
setenv_path_var MODULES_LMPREREQ $module_6&$submod_6 $module_9&$module_6
set ans [list]
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_6]
lappend ans [list setpath LOADEDMODULES $submod_6]
} else {
lappend ans [list setpath LOADEDMODULES $submod_6]
lappend ans [list setpath _LMFILES_ $submodfile_6]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list setpath MODULES_LMNOTUASKED $submod_6]
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_9" $ans "$moderrun_9"

setenv_loaded_module [list $submod_7 $module_7 $module_10] [list $submodfile_7 $modulefile_7 $modulefile_10] [list $submod_7 $module_7]
setenv_path_var MODULES_LMPREREQ $module_7&$submod_7 $module_10&$module_7
set ans [list]
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_7]
lappend ans [list setpath LOADEDMODULES $submod_7]
} else {
lappend ans [list setpath LOADEDMODULES $submod_7]
lappend ans [list setpath _LMFILES_ $submodfile_7]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list setpath MODULES_LMNOTUASKED $submod_7]
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_10" $ans "$moderrun_10"

setenv_loaded_module [list $submod_11 $module_11] [list $submodfile_11 $modulefile_11] [list $submod_11]
setenv_path_var MODULES_LMPREREQ $module_11&$submod_11
set ans [list]
if {$install_autohandling eq {y}} {
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ $submodfile_11]
lappend ans [list setpath LOADEDMODULES $submod_11]
} else {
lappend ans [list setpath LOADEDMODULES $submod_11]
lappend ans [list setpath _LMFILES_ $submodfile_11]
lappend ans [list unsetpath MODULES_LMPREREQ]
}
lappend ans [list ERR]
testouterr_cmd "csh" "unload $module_11" $ans "$moderrun_11"


# Clean up the just changed environment
unsetenv_path_var MODULES_LMPREREQ
unsetenv_loaded_module

#
#  Cleanup
#

reset_test_env
