##############################################################################
#   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, unload, switch, purge, reload
#   Modulefiles:	prereq/full, trace/all_on, trace/all_off
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the 'prereq' command using a full qualified
#			module name. This tests the reactions of modulecmd
#			if the prerequired module is load or not and if
#			another module of the same category is load.
#		}C%
#
##############################################################################

## ensure auto symbolic versions are not set for these tests
setenv_var MODULES_ADVANCED_VERSION_SPEC 0


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

set module_tron "trace/all_on"
set modulefile_tron "$modpath/$module_tron"

set module_troff "trace/all_off"
set modulefile_troff "$modpath/$module_troff"

#
#  Error messages
#

set err_pre [msg_load $module [err_prereq "trace/all_on"]]
set err_trace "$warn_msgs: 'module-trace' command not implemented"

set msg_load_tron [msg_load $module_tron $err_trace]
set msg_unload_tron [msg_unload $module_tron $err_trace]
set msg_top_load_module [msg_top_load $module {} $module_tron {}]
set msg_top_unload_module [msg_top_unload $module {} $module_tron {}]
set msg_top_switch_module [msg_top_switch $module $module {} {} {} $module_tron {}]

#
#  The tests
#

# prereq is loaded automatically when autohandling is enabled
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module_tron:$module"]
lappend ans [list setpath _LMFILES_ "$modulefile_tron:$modulefile"]
lappend ans [list set testsuite yes]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
lappend ans [list setpath MODULES_LMNOTUASKED $module_tron]
testouterr_cmd_re "ALL" "load --auto $module" $ans $msg_load_tron\n\n$msg_top_load_module

# switch sub-command should obtain same result here
testouterr_cmd_re "sh" "switch --auto $module" $ans $msg_load_tron\n\n$msg_top_switch_module

set ans [list]
lappend ans [list setpath LOADEDMODULES "$module_troff:$module_tron:$module"]
lappend ans [list setpath _LMFILES_ "$modulefile_troff:$modulefile_tron:$modulefile"]
lappend ans [list set testsuite yes]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
lappend ans [list setpath MODULES_LMNOTUASKED $module_tron]
testouterr_cmd_re "ALL" "load --auto $module_troff $module" $ans $msg_load_tron\n\n$msg_top_load_module

testouterr_cmd "ALL" "load --no-auto $module" "ERR" $err_pre

# switch sub-command should obtain same result here
testouterr_cmd "sh" "switch --no-auto $module" "ERR" $err_pre\n\n[msg_switch $module $module [err_swon $module]]

set ans [list]
lappend ans [list setpath _LMFILES_ $modulefile_troff]
lappend ans [list setpath LOADEDMODULES $module_troff]
lappend ans [list ERR]
testouterr_cmd_re "ALL" "load --no-auto $module_troff $module" $ans $err_pre

set ans [list]
lappend ans [list setpath LOADEDMODULES "$module_tron:$module"]
lappend ans [list setpath _LMFILES_ "$modulefile_tron:$modulefile"]
lappend ans [list set testsuite yes]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
testouterr_cmd_re "ALL" "load $module_tron $module" $ans $msg_load_tron


# test situations where auto_handling is disabled and some constraints are unsatisfied
# force module load without prereq loaded
set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module"]
lappend ans [list setpath _LMFILES_ "$modulefile"]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
set tserr [msg_load $module [err_reqmisf $module_tron]]
testouterr_cmd "sh" "load --no-auto --force $module" $ans $tserr

# switch sub-command should obtain same result here
testouterr_cmd "sh" "switch --no-auto --force $module" $ans $tserr

# force unload of a prereq
setenv_loaded_module [list $module_tron $module] [list $modulefile_tron $modulefile]
setenv_path_var MODULES_LMPREREQ "$module&$module_tron"
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module"]
lappend ans [list setpath _LMFILES_ "$modulefile"]
set tserr [msg_unload $module_tron [err_deplof $module] $err_trace]
testouterr_cmd "sh" "unload --no-auto --force $module_tron" $ans $tserr

set ans [list]
lappend ans [list setpath LOADEDMODULES "$module:$module_troff"]
lappend ans [list setpath _LMFILES_ "$modulefile:$modulefile_troff"]
testouterr_cmd_re "sh" "switch --no-auto --force $module_tron $module_troff" $ans $tserr

# switch raise error on unload phase
set tserr [msg_unload $module_tron [err_prerequn $module]]
testouterr_cmd_re "sh" "switch --no-auto $module_tron $module_troff" ERR $tserr\n\n[msg_switch $module_tron $module_troff [err_swoff $module_tron]]

set ans [list]
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list setpath _LMFILES_ "$modulefile_troff"]
lappend ans [list setpath LOADEDMODULES "$module_troff"]
lappend ans [list unset testsuite]
lappend ans [list ERR]
# dependent module cannot be reloaded now its prereq has gone missing
set tserr [msg_unload $module_tron $err_trace]\n\n[msg_load $module [err_reqmis $module_tron]]\n\n[msg_top_switch $module_tron $module_troff {} {} {} {} [list [list $module] {}] [err_depref $module]]
testouterr_cmd_re "sh" "switch --auto $module_tron $module_troff" $ans $tserr

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module_troff:$module"]
lappend ans [list setpath _LMFILES_ "$modulefile_troff:$modulefile"]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
# dependent module is forced to be reloaded with its prereq missing
set tserr [msg_unload $module_tron $err_trace]\n\n[msg_load $module [err_reqmisf $module_tron]]\n\n[msg_top_switch $module_tron $module_troff {} {} {} {} $module]
testouterr_cmd_re "sh" "switch --auto --force $module_tron $module_troff" $ans $tserr

# from this situation where prereq is not loaded
setenv_loaded_module [list $module] [list $modulefile]

# load prereq after will not trigger reload of dependent module when auto_handling is disabled
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module:$module_tron"]
lappend ans [list setpath _LMFILES_ "$modulefile:$modulefile_tron"]
testouterr_cmd_re "sh" "load --no-auto $module_tron" $ans $msg_load_tron

# switch sub-command should obtain same result here
testouterr_cmd_re "sh" "switch --no-auto $module_tron" $ans $msg_load_tron
testouterr_cmd_re "sh" "switch --no-auto --force $module_tron" $ans $msg_load_tron

# in auto mode, requiring module is reloaded
set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module_tron:$module"]
lappend ans [list setpath _LMFILES_ "$modulefile_tron:$modulefile"]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
set tserr [msg_load $module_tron $err_trace]\n\n[msg_top_switch $module_tron $module_tron {} {} {} {} $module]
testouterr_cmd_re "sh" "switch --auto $module_tron" $ans $tserr
testouterr_cmd_re "sh" "switch --auto --force $module_tron" $ans $tserr

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMPREREQ]
testouterr_cmd "sh" "purge" $ans ""

testouterr_cmd_re "sh" "reload" "ERR" $err_unsatreload

# force unload of a prereq loaded after dependent module (loaded in force mode)
# no impact on dependent module as prereq is loaded after
setenv_loaded_module [list $module $module_tron] [list $modulefile $modulefile_tron]
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module"]
lappend ans [list setpath _LMFILES_ "$modulefile"]
set tserr [msg_unload $module_tron $err_trace]
testouterr_cmd "sh" "unload --no-auto --force $module_tron" $ans $tserr

# set situation where a module has been loaded without its prerequirement
setenv_loaded_module [list $module_troff $module] [list $modulefile_troff $modulefile]
# load missing prereq to clear violation
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module:$module_tron"]
lappend ans [list setpath _LMFILES_ "$modulefile:$modulefile_tron"]
set tserr [msg_load $module_tron $err_trace]
testouterr_cmd_re "sh" "switch --no-auto $module_troff $module_tron" $ans $tserr
testouterr_cmd_re "sh" "switch --no-auto --force $module_troff $module_tron" $ans $tserr

set ans [list]
lappend ans [list setpath LOADEDMODULES "$module_tron:$module"]
lappend ans [list setpath _LMFILES_ "$modulefile_tron:$modulefile"]
lappend ans [list set testsuite yes]
lappend ans [list setpath MODULES_LMPREREQ "$module&$module_tron"]
set tserr [msg_load $module_tron $err_trace]\n\n[msg_top_switch $module_troff $module_tron {} {} {} {} $module]
testouterr_cmd_re "sh" "switch --auto $module_troff $module_tron" $ans $tserr
testouterr_cmd_re "sh" "switch --auto --force $module_troff $module_tron" $ans $tserr


# test empty or bad element found in MODULES_LMPREREQ control variable
# test empty element found in MODULES_LMNOTUASKED control variable
foreach badelt [list {} $module] {
setenv_path_var MODULES_LMPREREQ $badelt
unsetenv_loaded_module
setenv_path_var MODULES_LMNOTUASKED {}
set ans [list]
lappend ans [list setpath LOADEDMODULES $module_tron:$module]
lappend ans [list setpath _LMFILES_ $modulefile_tron:$modulefile]
lappend ans [list set testsuite yes]
lappend ans [list setpath MODULES_LMPREREQ $badelt:$module&$module_tron]
lappend ans [list setpath MODULES_LMNOTUASKED :$module_tron]
testouterr_cmd_re sh "load --auto $module" $ans $msg_load_tron\n\n$msg_top_load_module

setenv_loaded_module [list $module_tron $module] [list $modulefile_tron $modulefile] [list {} $module_tron]
setenv_path_var MODULES_LMPREREQ $badelt $module&$module_tron
set ans [list]
lappend ans [list unset testsuite]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list setpath MODULES_LMPREREQ $badelt]
lappend ans [list setpath MODULES_LMNOTUASKED {}]
testouterr_cmd_re sh "unload --auto $module" $ans $msg_unload_tron\n\n$msg_top_unload_module
}


#
#  Cleanup
#

reset_test_env
