##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2018/04/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, unload, display, help, test
#   Modulefiles:	setenv/0.6
#   Sub-Command:
#
#   Comment:	%C{
#           Check the effect of 'setenv' command on $env() array variable
#           through the different modulefile evaluation mode
#		}C%
#
##############################################################################

set module "setenv/0.6"
set modulefile "$modpath/$module"
set modulefilere "$modpathre/$module"

#
#  test load mode
#

set ans [list]
lappend ans [list set TS1 yes]
lappend ans [list set TS2 yes]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]
testouterr_cmd "sh" "load $module" $ans ""

# test with value set to variable prior to load
setenv_var TS1 "no"
testouterr_cmd "sh" "load $module" $ans ""


#
#  test unload mode
#

# test with module loaded
setenv_var TS1 "yes"
setenv_var TS2 "yes"
setenv_loaded_module $module $modulefile

set ans [list]
lappend ans [list unset TS1]
lappend ans [list unset TS2]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd "sh" "unload $module" $ans ""

# test with module loaded but vanished env vars
unsetenv_var TS1
unsetenv_var TS2

testouterr_cmd "sh" "unload $module" $ans ""


# restore environment
unsetenv_loaded_module


#
#  test display mode
#

set tserr "$modlin
$modulefilere:

setenv\t\tTS1 yes
setenv\t\tTS2 yes
$modlin"

# test without module loaded
testouterr_cmd_re "sh" "display $module" "" $tserr

# test with module loaded
setenv_var TS1 "yes"
setenv_var TS2 "yes"
setenv_loaded_module $module $modulefile

testouterr_cmd_re "sh" "display $module" "" $tserr

# restore environment
unsetenv_var TS1
unsetenv_var TS2
unsetenv_loaded_module


#
#  test help mode
#

set tserr "$modlin
Module Specific Help for $modulefilere:

$warn_msgs: Unable to find ModulesHelp in $modulefilere.
$modlin"

# test without module loaded
testouterr_cmd_re "sh" "help $module" "" $tserr

# test with module loaded
setenv_var TS1 "yes"
setenv_var TS2 "yes"
setenv_loaded_module $module $modulefile

testouterr_cmd_re "sh" "help $module" "" $tserr

# restore environment
unsetenv_var TS1
unsetenv_var TS2
unsetenv_loaded_module


#
#  test test mode
#

set tserr "$modlin
Module Specific Test for $modulefilere:

$warn_msgs: Unable to find ModulesTest in $modulefilere.
$modlin"

# test without module loaded
testouterr_cmd_re "sh" "test $module" "" $tserr

# test with module previously loaded
setenv_var TS1 "yes"
setenv_var TS2 "yes"
setenv_loaded_module $module $modulefile

testouterr_cmd_re "sh" "test $module" "" $tserr

# restore environment
unsetenv_var TS1
unsetenv_var TS2
unsetenv_loaded_module


#
# test appending variable after defining it with setenv
#

set module $env(TESTSUITEDIR)/modulefiles.2/setenv/0.1
set ans [list]
lappend ans [list set TS1 ok]
lappend ans [list set TS2 okok]
lappend ans [list set TS3 okok]
lappend ans [list set TS4 s\}ec\"a\{\\]
lappend ans [list set TS5 s\}ec\"a\{\\ok]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $module]
testouterr_cmd sh "load $module" $ans {}


#
#  Cleanup
#

reset_test_env
