##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2020/12/20
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, unload
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Check sticky/super-sticky modules that should not be unloaded
#           unless reloaded
#		}C%
#
##############################################################################

set mp $modpath.3
set mpre $modpathre.3
setenv_path_var MODULEPATH $mp

# disable advanced version specifiers not to get auto symbols in the way
setenv_var MODULES_ADVANCED_VERSION_SPEC 0

# ensure avail tests have implicit default enabled
setenv_var MODULES_IMPLICIT_DEFAULT 1


# test mod that is declared both sticky and super-sticky
set anslo [list]
lappend anslo [list setpath LOADEDMODULES sticky/2.0]
lappend anslo [list setpath _LMFILES_ $mp/sticky/2.0]
lappend anslo [list setpath MODULES_LMTAG sticky/2.0&sticky&super-sticky]
testouterr_cmd_re sh {load sticky/2.0} $anslo {}

setenv_loaded_module [list sticky/2.0] [list $mp/sticky/2.0]
setenv_path_var MODULES_LMTAG sticky/2.0&sticky&super-sticky

testouterr_cmd_re sh {unload sticky/2.0} ERR [msg_unload sticky/2.0 $err_superstickyunload]
testouterr_cmd_re sh {unload -f sticky/2.0} ERR [msg_unload sticky/2.0 $err_superstickyunload]

# test when persistent info is cleared
unsetenv_path_var MODULES_LMTAG
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd_re sh {unload sticky/2.0} $ans {}

setenv_loaded_module [list sticky/3.0] [list $mp/sticky/3.0]
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd_re sh {unload sticky/3.0} $ans {}

# persistent info cleared but break in modulefile
setenv_var TESTSUITE_STICKY_BREAK 1
testouterr_cmd_re sh {unload sticky/3.0} ERR [msg_unload sticky/3.0 $err_evalabort]

# test cumulative stickyness declarations
setenv_loaded_module [list sticky/1.0] [list $mp/sticky/1.0]
setenv_path_var MODULES_LMTAG sticky/1.0&sticky
setenv_var TESTSUITE_STICKY_GENERIC 2

set tserr [msg_switch sticky/1.0 sticky/2.0 $err_stickyunload]
testouterr_cmd_re sh {switch sticky/2.0} ERR $tserr
set tserr [msg_switch sticky/1.0 sticky $err_stickyunload]
testouterr_cmd_re sh {switch sticky} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/1.0:sticky/2.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/1.0:$mp/sticky/2.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/1.0&sticky:sticky/2.0&sticky]
lappend ans [list ERR]
set tserr [msg_unload sticky/1.0 $err_stickyunload]\n\n[msg_load sticky/2.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-4} $ans $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/1.0:sticky/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/1.0:$mp/sticky/4.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/1.0&sticky:sticky/4.0&sticky]
lappend ans [list ERR]
set tserr [msg_unload sticky/1.0 $err_stickyunload]\n\n[msg_load sticky/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-5} $ans $tserr

setenv_var TESTSUITE_SUPER_STICKY 1
setenv_path_var MODULES_LMTAG sticky/1.0&super-sticky
set tserr [msg_switch sticky/1.0 sticky $err_superstickyunload]
testouterr_cmd_re sh {switch sticky} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/1.0:sticky/2.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/1.0:$mp/sticky/2.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/1.0&super-sticky:sticky/2.0&super-sticky]
lappend ans [list ERR]
set tserr [msg_unload sticky/1.0 $err_superstickyunload]\n\n[msg_load sticky/2.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-4} $ans $tserr
unsetenv_var TESTSUITE_SUPER_STICKY
setenv_path_var MODULES_LMTAG sticky/1.0&sticky&super-sticky

setenv_var TESTSUITE_STICKY_GENERIC 3
set tserr [msg_switch sticky/1.0 sticky/2.0 $err_stickyunload]
testouterr_cmd_re sh {switch sticky/2.0} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/1.0:sticky/2.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/1.0:$mp/sticky/2.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/1.0&sticky&super-sticky:sticky/2.0&super-sticky]
lappend ans [list ERR]
set tserr [msg_unload sticky/1.0 $err_stickyunload]\n\n[msg_load sticky/2.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-4} $ans $tserr

setenv_var TESTSUITE_STICKY_GENERIC 4
set tserr [msg_switch sticky/1.0 sticky $err_superstickyunload]
testouterr_cmd_re sh {switch sticky} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/1.0:sticky/2.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/1.0:$mp/sticky/2.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/1.0&sticky&super-sticky:sticky/2.0&sticky]
lappend ans [list ERR]
set tserr [msg_unload sticky/1.0 $err_superstickyunload]\n\n[msg_load sticky/2.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-4} $ans $tserr

setenv_var TESTSUITE_STICKY_GENERIC 5
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/2.0&sticky&super-sticky]
testouterr_cmd_re sh {switch sticky/2.0} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES sticky/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/sticky/4.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG sticky/4.0&sticky&super-sticky]
set tserr [msg_unload sticky/1.0]\n[msg_load sticky/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-5} $ans $tserr

# test cumulative stickyness declarations on deep modules
setenv_var TESTSUITE_STICKY_GENERIC 6
setenv_loaded_module [list stickysub/dir1/1.0] [list $mp/stickysub/dir1/1.0]
setenv_path_var MODULES_LMTAG stickysub/dir1/1.0&sticky

set tserr [msg_switch stickysub/dir1/1.0 stickysub $err_stickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir1} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&sticky]
testouterr_cmd_re sh {switch stickysub/dir1/1.0} $ans {}
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1/2.0 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir1/2.0} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0:stickysub/dir2/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0:$mp/stickysub/dir2/4.0:$mp/foo/2.0]
lappend ans [list ERR]
set tserr [msg_unload stickysub/dir1/1.0 $err_stickyunload]\n\n[msg_load stickysub/dir2/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr

setenv_var TESTSUITE_SUPER_STICKY 1
setenv_path_var MODULES_LMTAG stickysub/dir1/1.0&super-sticky
set tserr [msg_switch stickysub/dir1/1.0 stickysub $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1 $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub/dir1} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&super-sticky]
testouterr_cmd_re sh {switch stickysub/dir1/1.0} $ans {}
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1/2.0 $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub/dir1/2.0} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0:stickysub/dir2/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0:$mp/stickysub/dir2/4.0:$mp/foo/2.0]
lappend ans [list ERR]
set tserr [msg_unload stickysub/dir1/1.0 $err_superstickyunload]\n\n[msg_load stickysub/dir2/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr
unsetenv_var TESTSUITE_SUPER_STICKY
setenv_path_var MODULES_LMTAG stickysub/dir1/1.0&sticky&super-sticky

setenv_var TESTSUITE_STICKY_GENERIC 7
set tserr [msg_switch stickysub/dir1/1.0 stickysub $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&sticky&super-sticky]
testouterr_cmd_re sh {switch stickysub/dir1/1.0} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/2.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/2.0&super-sticky&sticky]
testouterr_cmd_re sh {switch stickysub/dir1/2.0} $ans {}
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir2 $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub/dir2} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0:stickysub/dir2/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0:$mp/stickysub/dir2/4.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&sticky&super-sticky:stickysub/dir2/4.0&sticky]
lappend ans [list ERR]
set tserr [msg_unload stickysub/dir1/1.0 $err_superstickyunload]\n\n[msg_load stickysub/dir2/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr

setenv_var TESTSUITE_STICKY_GENERIC 8
set tserr [msg_switch stickysub/dir1/1.0 stickysub $err_stickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1/2.0 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir1/2.0} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir2 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir2} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0:stickysub/dir2/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0:$mp/stickysub/dir2/4.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&sticky&super-sticky:stickysub/dir2/4.0&super-sticky]
lappend ans [list ERR]
set tserr [msg_unload stickysub/dir1/1.0 $err_stickyunload]\n\n[msg_load stickysub/dir2/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr

setenv_var TESTSUITE_STICKY_GENERIC 9
set tserr [msg_switch stickysub/dir1/1.0 stickysub $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1 $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub/dir1} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir2/3.0 $err_superstickyunload]
testouterr_cmd_re sh {switch stickysub/dir2/3.0} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0:stickysub/dir2/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0:$mp/stickysub/dir2/4.0:$mp/foo/2.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&sticky&super-sticky:stickysub/dir2/4.0&sticky]
lappend ans [list ERR]
set tserr [msg_unload stickysub/dir1/1.0 $err_superstickyunload]\n\n[msg_load stickysub/dir2/4.0]\n[msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr

setenv_var TESTSUITE_STICKY_GENERIC 10
setenv_path_var MODULES_LMTAG stickysub/dir1/1.0&sticky
setenv_path_var MODULES_LMALTNAME stickysub/dir1/1.0&stickysub/dir1/default&stickysub/dir1&stickysub/default&stickysub

#FIXME: even if stickysub/dir1/1.0 is declared default, switch sub-command does not know it
# it should reload stickysub/dir1/1.0 with no error in the 2 following cases
set tserr [msg_switch stickysub/dir1/1.0 stickysub $err_stickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir1} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0]
lappend ans [list setpath MODULES_LMALTNAME stickysub/dir1/1.0&stickysub/dir1/default&stickysub/dir1&stickysub/default&stickysub]
lappend ans [list setpath MODULES_LMTAG stickysub/dir1/1.0&sticky]
testouterr_cmd_re sh {switch stickysub/dir1/1.0} $ans {}
set tserr [msg_switch stickysub/dir1/1.0 stickysub/dir1/2.0 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir1/2.0} ERR $tserr
# stickysub/dir1/1.0 is correctly seen as default when restoring
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir1/1.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir1/1.0:$mp/foo/2.0]
set tserr [msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr

setenv_var TESTSUITE_STICKY_GENERIC 11
setenv_loaded_module [list stickysub/dir2/4.0] [list $mp/stickysub/dir2/4.0]
setenv_path_var MODULES_LMTAG stickysub/dir2/4.0&sticky
unsetenv_path_var MODULES_LMALTNAME

#FIXME: even if stickysub/dir2/4.0 is the implicit default, switch sub-command does not know it
# it should reload stickysub/dir2/4.0 with no error in the 2 following cases
set tserr [msg_switch stickysub/dir2/4.0 stickysub $err_stickyunload]
testouterr_cmd_re sh {switch stickysub} ERR $tserr
set tserr [msg_switch stickysub/dir2/4.0 stickysub/dir2 $err_stickyunload]
testouterr_cmd_re sh {switch stickysub/dir2} ERR $tserr
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir2/4.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir2/4.0]
lappend ans [list setpath MODULES_LMTAG stickysub/dir2/4.0&sticky]
testouterr_cmd_re sh {switch stickysub/dir2/4.0} $ans {}
# stickysub/dir2/4.0 is correctly seen as default when restoring
set ans [list]
lappend ans [list setpath LOADEDMODULES stickysub/dir2/4.0:foo/2.0]
lappend ans [list setpath _LMFILES_ $mp/stickysub/dir2/4.0:$mp/foo/2.0]
set tserr [msg_load foo/2.0]
testouterr_cmd_re sh {restore testsuite/home/coll-sticky-6} $ans $tserr


#
#  Cleanup
#

reset_test_env
