##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.00-init/%M%
#   Revision:		%I%
#   First Edition:	2018/06/25
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Sub-Command:
#
#   Comment:	%C{
#			Check auto_handling-related switches and configs.
#		}C%
#
##############################################################################

set module "prereq/full"
set modulefile "$modpath/$module"
set module_tron "trace/all_on"
set modulefile_tron "$modpath/$module_tron"

set err_pre [msg_load $module [err_prereq $module_tron]]
set err_trace [msg_load $module_tron "$warn_msgs: 'module-trace' command not implemented"]
append err_trace \n\n[msg_top_load $module {} $module_tron {}]

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]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $module_tron&as|trace/default&as|trace/latest]
}

#
#  The tests
#

# test auto_handling set at configure time
if {$install_autohandling eq "y"} {
    testouterr_cmd_re "sh" "load $module" $ans $err_trace
} else {
    testouterr_cmd "sh" "load $module" "ERR" $err_pre
}

# overriden configuration with command-line option
testouterr_cmd_re "sh" "load --auto $module" $ans $err_trace
testouterr_cmd "sh" "load --no-auto $module" "ERR" $err_pre


# disable auto_handling with environment variable
setenv_var MODULES_AUTO_HANDLING 0
testouterr_cmd "sh" "load $module" "ERR" $err_pre

# overriden configuration with command-line option
testouterr_cmd_re "sh" "load --auto $module" $ans $err_trace
testouterr_cmd "sh" "load --no-auto $module" "ERR" $err_pre


# enable auto_handling with environment variable
setenv_var MODULES_AUTO_HANDLING 1
testouterr_cmd_re "sh" "load $module" $ans $err_trace

# overriden configuration with command-line option
testouterr_cmd_re "sh" "load --auto $module" $ans $err_trace
testouterr_cmd "sh" "load --no-auto $module" "ERR" $err_pre


# bad environment variable value should not interfer
setenv_var MODULES_AUTO_HANDLING foo
if {$install_autohandling eq "y"} {
    testouterr_cmd_re "sh" "load $module" $ans $err_trace
} else {
    testouterr_cmd "sh" "load $module" "ERR" $err_pre
}

# overriden configuration with command-line option
testouterr_cmd_re "sh" "load --auto $module" $ans $err_trace
testouterr_cmd "sh" "load --no-auto $module" "ERR" $err_pre


#
#  Cleanup
#

reset_test_env
