##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2020/07/20
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		avail, load, list, unload, purge, reload, is-loaded
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Hidding loaded modules with 'module-hide --hidden-loaded'
#           command
#		}C%
#
##############################################################################

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

# ensure avail tests have implicit default enabled
setenv_var MODULES_IMPLICIT_DEFAULT 1

# ensure regular avail search match is set for these tests
setenv_var MODULES_SEARCH_MATCH starts_with

# ensure last matching element is returned when unloading modules
setenv_var MODULES_UNLOAD_MATCH_ORDER returnlast

# ensure extended default and advanced version specifiers are enabled for these tests
setenv_var MODULES_ADVANCED_VERSION_SPEC 1
setenv_var MODULES_EXTENDED_DEFAULT 1

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

setenv_var MODULERCFILE $env(TESTSUITEDIR)/etc/modulerc.hide


# --hidden-loaded option does not change 'module-hide' basic effect when searching modules
setenv_var TESTSUITE_HIDE3_HIDDEN_LOADED_SET1 1
testouterr_cmd sh {avail -t hide3} OK {}
testouterr_cmd sh {load hide3} ERR $err_path'hide3/sub1'
unsetenv_var TESTSUITE_HIDE3_HIDDEN_LOADED_SET1
# --hidden-loaded option does not change 'module-hide --soft' basic effect when searching modules
setenv_var TESTSUITE_HIDE3_HIDDEN_LOADED_SET2 1
testouterr_cmd sh {avail -t hid?3} OK {}
unsetenv_var TESTSUITE_HIDE3_HIDDEN_LOADED_SET2


# load tests
setenv_var TESTSUITE_HIDE3_HIDDEN_LOADED_SET1 1
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded]
testouterr_cmd sh {load hide3@1.0} $ans {}
# load two hidden modules in a row
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hide3/sub1/3.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hide3/sub1/3.0]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest:hide3/sub1/3.0&as|hide3/sub1/default&as|hide3/sub1/latest]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded:hide3/sub1/3.0&hidden-loaded]
testouterr_cmd_re sh {load hide3/1.0 hide3/sub1/3.0} $ans {}

setenv_loaded_module [list hide3/1.0] [list $mp/hide3/1.0]
setenv_path_var MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded
# append new hidden module to loaded environnement
testouterr_cmd_re sh {load hide3/sub1/3.0} $ans {}
# try to load again an hidden module
testouterr_cmd sh {load hide3/1.0} OK {}
unsetenv_path_var MODULES_LMALTNAME
unsetenv_path_var MODULES_LMTAG
testouterr_cmd sh {load hide3/1.0} OK {}
# append new hidden module against a badly formed MODULES_LMTAG
setenv_path_var MODULES_LMTAG :hide3/1.0&hidden-loaded::
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hide3/sub1/3.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hide3/sub1/3.0]
lappend ans [list setpath MODULES_LMALTNAME hide3/sub1/3.0&as|hide3/sub1/default&as|hide3/sub1/latest]
lappend ans [list setpath MODULES_LMTAG :hide3/1.0&hidden-loaded:::hide3/sub1/3.0&hidden-loaded]
testouterr_cmd_re sh {load hide3/sub1/3.0} $ans {}


# is-loaded tests (hidden loaded modules are always taken into account)
setenv_loaded_module [list hide3/1.0] [list $mp/hide3/1.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded
testouterr_cmd_re sh {is-loaded hide3/1.0} OK {}
testouterr_cmd_re sh {is-loaded hide3} OK {}

# unload tests
setenv_loaded_module [list hide3/1.0] [list $mp/hide3/1.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMTAG]
testouterr_cmd_re sh {unload hide3} $ans {}
testouterr_cmd_re sh {unload hide3/1.0} $ans {}
setenv_loaded_module [list hide3/1.0 hide3/sub1/3.0] [list $mp/hide3/1.0 $mp/hide3/sub1/3.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded:hide3/sub1/3.0&hidden-loaded
testouterr_cmd_re sh {unload hide3/1.0 hide3/sub1/3.0} $ans {}
testouterr_cmd_re sh {unload hide3 hide3} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/sub1/3.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/sub1/3.0]
lappend ans [list setpath MODULES_LMTAG hide3/sub1/3.0&hidden-loaded]
testouterr_cmd_re sh {unload hide3/1.0} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded]
testouterr_cmd_re sh {unload hide3} $ans {}
# unset hidden tracking var
unsetenv_path_var MODULES_LMTAG
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0]
testouterr_cmd_re sh {unload hide3} $ans {}

# purge tests
setenv_loaded_module [list hide3/1.0 hide3/sub1/3.0] [list $mp/hide3/1.0 $mp/hide3/sub1/3.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded:hide3/sub1/3.0&hidden-loaded
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMTAG]
testouterr_cmd_re sh {purge} $ans {}

# reload tests
setenv_loaded_module [list hide3/1.0 hide3/sub1/3.0] [list $mp/hide3/1.0 $mp/hide3/sub1/3.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded:hide3/sub1/3.0&hidden-loaded
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hide3/sub1/3.0]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hide3/sub1/3.0]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded:hide3/sub1/3.0&hidden-loaded]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest:hide3/sub1/3.0&as|hide3/sub1/default&as|hide3/sub1/latest]
testouterr_cmd_re sh {reload} $ans {}
# missing hidden state on unload phase will recover on load again phase
setenv_loaded_module [list hide3/1.0 hide3/sub1/3.0] [list $mp/hide3/1.0 $mp/hide3/sub1/3.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded
testouterr_cmd_re sh {reload} $ans {}



# list tests
# only hidden modules are loaded
setenv_loaded_module [list hide3/1.0 hide3/sub1/3.0] [list $mp/hide3/1.0 $mp/hide3/sub1/3.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded:hide3/sub1/3.0&hidden-loaded
testouterr_cmd_re sh {list -t} OK $no_loaded
# mix of loaded and visible modules loaded
setenv_loaded_module [list hide3/1.0 hide3/sub1/3.0] [list $mp/hide3/1.0 $mp/hide3/sub1/3.0]
setenv_path_var MODULES_LMTAG hide3/1.0&hidden-loaded
testouterr_cmd_re sh {list -t} OK $cur_loaded\nhide3/sub1/3.0
unsetenv_loaded_module


# ensure hidden state is effective right after load
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hidereq/4.1]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hidereq/4.1]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded]
testouterr_cmd_re sh {load hide3/1.0 hidereq/4.1} $ans $no_loaded


# ensure hidden state is recovered and hidden mod load is not reported if automatically triggered
# on collection restore
testouterr_cmd_re sh {restore testsuite/home/coll-hide-once-loaded} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
# unless verbose2 level activated
testouterr_cmd_re sh {restore -v -v testsuite/home/coll-hide-once-loaded} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hidereq/4.1]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hidereq/4.1]
lappend ans [list setpath MODULES_LMNOTUASKED hide3/1.0]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded]
testouterr_cmd_re sh {restore testsuite/home/coll-hide-once-loaded-nuasked} $ans "$no_loaded\nLoading hidereq/4.1"
setenv_var MODULES_VERBOSITY verbose
testouterr_cmd_re sh {restore testsuite/home/coll-hide-once-loaded-nuasked} $ans "$no_loaded\nLoading hidereq/4.1"
testouterr_cmd_re sh {restore -v -v testsuite/home/coll-hide-once-loaded-nuasked} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
setenv_var MODULES_VERBOSITY verbose2
testouterr_cmd_re sh {restore -v testsuite/home/coll-hide-once-loaded-nuasked} $ans "$no_loaded\nLoading hidereq/4.1"
testouterr_cmd_re sh {restore testsuite/home/coll-hide-once-loaded-nuasked} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
testouterr_cmd_re sh {restore -v -v testsuite/home/coll-hide-once-loaded-nuasked} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
setenv_var MODULES_VERBOSITY normal
testouterr_cmd_re sh {restore -v -v testsuite/home/coll-hide-once-loaded-nuasked} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
testouterr_cmd_re sh {restore -vv testsuite/home/coll-hide-once-loaded-nuasked} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
testouterr_cmd_re sh {--verbose restore --verbose testsuite/home/coll-hide-once-loaded-nuasked} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"

# on scriptfile source
# work on scriptfile located in current dir to cope with the relative path definition set in it
file copy testsuite/home/coll-hide-once-loaded coll-hide-once-loaded
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hidereq/4.1]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hidereq/4.1]
lappend ans [list setpath MODULEPATH $mp]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded]
testouterr_cmd_re sh {source coll-hide-once-loaded} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
testouterr_cmd_re sh {source -vv coll-hide-once-loaded} $ans "Loading hide3/1.0\n$no_loaded\nLoading hidereq/4.1"
file delete coll-hide-once-loaded
set ans [list]
lappend ans [list setpath LOADEDMODULES hide3/1.0:hidereq/4.2]
lappend ans [list setpath _LMFILES_ $mp/hide3/1.0:$mp/hidereq/4.2]
lappend ans [list setpath MODULES_LMPREREQ hidereq/4.2&hide3/1.0]
lappend ans [list setpath MODULES_LMNOTUASKED hide3/1.0]
lappend ans [list setpath MODULES_LMALTNAME hide3/1.0&as|hide3/default&as|hide3/latest]
lappend ans [list setpath MODULES_LMTAG hide3/1.0&hidden-loaded]
testouterr_cmd_re sh {source testsuite/modulefiles.2/hidereq/4.3} $ans "Loading hidereq/4.2"
setenv_var MODULES_VERBOSITY verbose
testouterr_cmd_re sh {source testsuite/modulefiles.2/hidereq/4.3} $ans "Loading hidereq/4.2"
setenv_var MODULES_VERBOSITY verbose2
testouterr_cmd_re sh {source testsuite/modulefiles.2/hidereq/4.3} $ans [msg_load hide3/1.0]\n\n[msg_top_load hidereq/4.2 {} hide3/1.0 {}]
setenv_var MODULES_VERBOSITY normal
testouterr_cmd_re sh {source -vv testsuite/modulefiles.2/hidereq/4.3} $ans [msg_load hide3/1.0]\n\n[msg_top_load hidereq/4.2 {} hide3/1.0 {}]
testouterr_cmd_re sh {source -v -v testsuite/modulefiles.2/hidereq/4.3} $ans [msg_load hide3/1.0]\n\n[msg_top_load hidereq/4.2 {} hide3/1.0 {}]


#
#  Cleanup
#

reset_test_env
