##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2017/06/19
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, unload, switch, help, display, test, whatis
#   Modulefiles:	spread, spreadrc
#   Sub-Command:
#
#   Comment:	%C{
#			Tests how modulefile and modulerc interpreter let spread
#           variable and procedure definitions from one interpretation
#           to another.
#		}C%
#
##############################################################################

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

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

set mod "spread"
set mod1 "$mod/1.0"
set mod2 "$mod/2.0"
set modfile1 "$modpath/$mod1"
set modfilere1 "$modpathre/$mod1"
set modfile2 "$modpath/$mod2"
set modfilere2 "$modpathre/$mod2"

# mod1 and mod2 are designed to output any incorrect behavior detected
# so when nothing is displayed it means test went as expected
set spreadres ""

set mod1disph "$modlin\n$modfilere1:\n"
set mod2disph "$modlin\n$modfilere2:\n"
set mod1dispres "$mod"

set mod1helph "$modlin\nModule Specific Help for $modfilere1:\n"
set mod2helph "$modlin\nModule Specific Help for $modfilere2:\n"
set mod1helpres "$mod"
set mod2helpres "$warn_msgs: Unable to find ModulesHelp in $modfilere2."

set mod1testh "$modlin\nModule Specific Test for $modfilere1:\n"
set mod2testh "$modlin\nModule Specific Test for $modfilere2:\n"
set mod1testres "$mod\nTest result: PASS"
set mod2testres "$warn_msgs: Unable to find ModulesTest in $modfilere2."

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

lappend ans [list setpath LOADEDMODULES "$mod1:$mod2"]
lappend ans [list setpath _LMFILES_ "$modfile1:$modfile2"]


#
#  The tests
#

testouterr_cmd_re "sh" "load $mod1 $mod2" $ans "$spreadres"
testouterr_cmd_re "sh" "display $mod1 $mod2" "OK" "$mod1disph\n$mod1dispres\n$mod2disph\n$spreadres$modlin"
testouterr_cmd_re "sh" "help $mod1 $mod2" "OK" "$mod1helph\n$mod1helpres\n$mod2helph\n$spreadres$mod2helpres\n$modlin"
testouterr_cmd_re "sh" "test $mod1 $mod2" "OK" "$mod1testh\n$mod1testres\n$mod2testh\n$spreadres$mod2testres\n$modlin"
testouterr_cmd_re "sh" "whatis $mod" "OK" "$spreadres"
testouterr_cmd_re "sh" "avail -t $mod/" "OK" "$modpathre:\n$mod1\n$mod2\n$mod/3.0\n$mod/4.0\n$mod/5.0\n$mod/6.0\n$mod/7.0\n$mod/8.0"

# tests looping on same modulefile
testouterr_cmd_re "sh" "display $mod1 $mod1" "OK" "$mod1disph\n$mod1dispres\n$mod1disph\n$spreadres$mod1dispres\n$modlin"
testouterr_cmd_re "sh" "help $mod1 $mod1" "OK" "$mod1helph\n$mod1helpres\n$mod1helph\n$spreadres$mod1helpres\n$modlin"
testouterr_cmd_re "sh" "test $mod1 $mod1" "OK" "$mod1testh\n$mod1testres\n$mod1testh\n$spreadres$mod1testres\n$modlin"

# Set up the environment for test that require mod1 to be loaded
setenv_loaded_module $mod1 $modfile1

# test switching
set ans [list]
lappend ans [list set _LMFILES__modshare "$modfile2:1"]
lappend ans [list set LOADEDMODULES_modshare "$mod2:1"]
lappend ans [list set _LMFILES_ $modfile2]
lappend ans [list set LOADEDMODULES $mod2]
testouterr_cmd_re "sh" "switch $mod1 $mod2" $ans "$spreadres"

# Set up the environment for test that require mod1 and mod2 to be loaded
setenv_loaded_module [list $mod1 $mod2] [list $modfile1 $modfile2]

testouterr_cmd "sh" "list -t" "OK" "$cur_loaded\n$mod1\n$mod2"
# test unloading
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]

testouterr_cmd_re "sh" "unload $mod1 $mod2" $ans "$spreadres"

# Clean up the environment
unsetenv_loaded_module


#
# Test spreading with modulerc and modulefile
#

set mod "spreadrc"
set mod1 "$mod/dir1/1.0"
set mod2 "$mod/dir2/1.0"
set modfile1 "$modpath/$mod1"
set modfilere1 "$modpathre/$mod1"
set modfile2 "$modpath/$mod2"
set modfilere2 "$modpathre/$mod2"

# mod1 and mod2 are designed to output any incorrect behavior detected
# so when nothing is displayed it means test went as expected
set spreadres ""

set mod1disph "$modlin\n$modfilere1:\n"
set mod2disph "$modlin\n$modfilere2:\n"
set mod1dispres "spreadmod"

set mod1helph "$modlin\nModule Specific Help for $modfilere1:\n"
set mod2helph "$modlin\nModule Specific Help for $modfilere2:\n"
set mod1helpres "spreadmod"
set mod2helpres "$warn_msgs: Unable to find ModulesHelp in $modfilere2."

set mod1testh "$modlin\nModule Specific Test for $modfilere1:\n"
set mod2testh "$modlin\nModule Specific Test for $modfilere2:\n"
set mod1testres "spreadmod\nTest result: PASS"
set mod2testres "$warn_msgs: Unable to find ModulesTest in $modfilere2."

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

set ans [list]
lappend ans [list setpath LOADEDMODULES "$mod1:$mod2"]
lappend ans [list setpath _LMFILES_ "$modfile1:$modfile2"]


#
#  The tests
#

testouterr_cmd_re "sh" "load $mod1 $mod2" $ans "$spreadres"
testouterr_cmd_re "sh" "display $mod1 $mod2" "OK" "$mod1disph\n$mod1dispres\n$mod2disph\n$spreadres$modlin"
testouterr_cmd_re "sh" "help $mod1 $mod2" "OK" "$mod1helph\n$mod1helpres\n$mod2helph\n$spreadres$mod2helpres\n$modlin"
testouterr_cmd_re "sh" "test $mod1 $mod2" "OK" "$mod1testh\n$mod1testres\n$mod2testh\n$spreadres$mod2testres\n$modlin"
testouterr_cmd_re "sh" "whatis $mod" "OK" "$spreadres"
testouterr_cmd_re "sh" "avail -t $mod/" "OK" "$modpathre:\n$mod1\n$mod2\n$mod/dir3/1.0\n$mod/dir4/1.0\n$mod/dir5/1.0\n$mod/dir6/1.0\n$mod/dir7/1.0\n$mod/dir8/1.0"

# tests looping on same modulefile
testouterr_cmd_re "sh" "display $mod1 $mod1" "OK" "$mod1disph\n$mod1dispres\n$mod1disph\n$spreadres$mod1dispres\n$modlin"
testouterr_cmd_re "sh" "help $mod1 $mod1" "OK" "$mod1helph\n$mod1helpres\n$mod1helph\n$spreadres$mod1helpres\n$modlin"
testouterr_cmd_re "sh" "test $mod1 $mod1" "OK" "$mod1testh\n$mod1testres\n$mod1testh\n$spreadres$mod1testres\n$modlin"

# Set up the environment for test that require mod1 to be loaded
setenv_loaded_module $mod1 $modfile1

# test switching
set ans [list]
lappend ans [list set _LMFILES__modshare "$modfile2:1"]
lappend ans [list set LOADEDMODULES_modshare "$mod2:1"]
lappend ans [list set _LMFILES_ $modfile2]
lappend ans [list set LOADEDMODULES $mod2]
testouterr_cmd_re "sh" "switch $mod1 $mod2" $ans "$spreadres"

# Set up the environment for test that require mod1 and mod2 to be loaded
setenv_loaded_module [list $mod1 $mod2] [list $modfile1 $modfile2]

testouterr_cmd "sh" "list -t" "OK" "$cur_loaded\n$mod1\n$mod2"
# test unloading
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd_re "sh" "unload $mod1 $mod2" $ans "$spreadres"


#
#  Cleanup
#

reset_test_env
