##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2021/03/27
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		unload, switch
#   Modulefiles:    variant
#   Sub-Command:
#
#   Comment:	%C{
#           Test 'variant' modulefile command on unload/switch context
#		}C%
#
##############################################################################

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


setenv_var MODULES_ADVANCED_VERSION_SPEC 1

#
# basic unload tests
#

setenv_var TESTSUITE_VARIANT 1
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&foo|val1|0|0&bar|2|0|0
setenv_var TS0 {bar foo}
setenv_path_var TS1 bar=2 foo=val1
setenv_path_var TS2 bar-foo

# unload module specifying it fully or partially
set ans [list]
lappend ans [list unset TS0]
lappend ans [list unsetpath TS1]
lappend ans [list unsetpath TS2]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMVARIANT]
testouterr_cmd sh {unload variant} $ans {}
testouterr_cmd sh {unload variant bar=2} $ans {}
testouterr_cmd sh {unload variant bar=2 foo=val1} $ans {}
testouterr_cmd sh {unload variant/4.0} $ans {}
testouterr_cmd sh {unload variant/4.0 bar=2} $ans {}
testouterr_cmd sh {unload variant/4.0 foo=val1 bar=2} $ans {}
testouterr_cmd sh {unload variant/4.0 bar=2 foo=val1} $ans {}

# no unload if specification does not correspond
testouterr_cmd sh {unload variant/2.0} OK {}
testouterr_cmd sh {unload variant/4.0 bar=1} OK {}
testouterr_cmd sh {unload variant/4.0 foo=val1 bar=1} OK {}
testouterr_cmd sh {unload variant/4.0 foo=val2 bar=0} OK {}

# test @loaded version specifier
testouterr_cmd sh {unload variant@loaded} $ans {}
testouterr_cmd sh {unload variant@loaded bar=1} $ans {}


#
# impact of default variant value and output of [module-info specified]
#

setenv_var TESTSUITE_VARIANT 2
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&foo|val1|0|0&bar|2|0|1
setenv_var TS0 {bar foo}
setenv_path_var TS1 bar=2 foo=val1
setenv_path_var TS2 bar-foo

testouterr_cmd sh {unload variant} $ans {variant}
testouterr_cmd sh {unload variant/4.0} $ans {variant/4.0}
testouterr_cmd sh {unload variant/4.0 bar=2} $ans {variant/4.0 bar=2}
testouterr_cmd sh {unload variant/4.0 foo=val1} $ans {variant/4.0 foo=val1}
testouterr_cmd sh {unload variant foo=val1} $ans {variant foo=val1}

# no unload if specification does not correspond
testouterr_cmd sh {unload variant/4.0 foo=val2} OK {}
testouterr_cmd sh {unload variant/4.0 foo=val2 bar=1} OK {}


#
# variant name or value containing special or space character
#

setenv_var TESTSUITE_VARIANT 3
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT {variant/4.0&f* o|v?l1 |0|0&b?r |v*l1|0|0}
setenv_var TS0 {b?r  f* o}
setenv_path_var TS1 {b?r =v*l1} {f* o=v?l1 }
setenv_path_var TS2 {b?r -f* o}

# unload module specifying it fully or partially
testouterr_cmd sh {unload variant} $ans {}
testouterr_cmd sh {unload variant/4.0} $ans {}
testouterr_cmd sh {unload variant/4.0 b?r\ =v*l1} $ans {}
testouterr_cmd sh {unload variant/4.0 f*\ o=v?l1\  b?r\ =v*l1} $ans {}

# no unload if specification does not correspond
testouterr_cmd sh {unload variant/4.0 b?r\ =val5} OK {}
testouterr_cmd sh {unload variant/4.0 b?r\ =v*l1 f*\ o=v?l1} OK {}
testouterr_cmd sh {unload variant/4.0 b?r\ =v\ l1 f*\ o=v?l1\ } OK {}


#
# multiple loaded modules match unload module specification
#

setenv_var TESTSUITE_VARIANT 1
setenv_loaded_module [list variant/1.0 variant/4.0] [list $mp/variant/1.0 $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT {variant/1.0&foo|val1|0|0:variant/4.0&foo|val1|0|0&bar|2|0|0}
unsetenv_var TS0
unsetenv_path_var TS1
unsetenv_path_var TS2

set ans1 [list]
lappend ans1 [list unset TS0]
lappend ans1 [list unset TS1]
lappend ans1 [list unset TS2]
lappend ans1 [list setpath LOADEDMODULES variant/1.0]
lappend ans1 [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans1 [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|0]

set ans2 [list]
lappend ans2 [list unset TS0]
lappend ans2 [list unset TS1]
lappend ans2 [list setpath LOADEDMODULES variant/4.0]
lappend ans2 [list setpath _LMFILES_ $mp/variant/4.0]
lappend ans2 [list setpath MODULES_LMVARIANT variant/4.0&foo|val1|0|0&bar|2|0|0]

setenv_var MODULES_UNLOAD_MATCH_ORDER returnlast
testouterr_cmd sh {unload variant} $ans1 {}
testouterr_cmd sh {unload variant foo=val1} $ans1 {}
testouterr_cmd sh {unload variant bar=2} $ans1 {}
testouterr_cmd sh {unload variant bar=1} OK {}

setenv_var MODULES_UNLOAD_MATCH_ORDER returnfirst
testouterr_cmd sh {unload variant} $ans2 {}
testouterr_cmd sh {unload variant foo=val1} $ans2 {}
testouterr_cmd sh {unload variant bar=2} $ans1 {}
testouterr_cmd sh {unload variant bar=1} OK {}

unsetenv_var MODULES_UNLOAD_MATCH_ORDER


#
# variant issues
#

# missing variant value in persistency during unload

# without default value for variant
setenv_var TESTSUITE_VARIANT 1
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|0
setenv_var TS0 {bar}
setenv_path_var TS1 {bar=2}
setenv_path_var TS2 {bar}
set tserr [msg_unload variant/4.0{bar=2} [err_novarval foo {val1 val2 val3}]]
testouterr_cmd sh {unload variant} ERR $tserr
testouterr_cmd sh {unload variant bar=2} ERR $tserr
testouterr_cmd sh {unload variant/4.0 bar=2} ERR $tserr
testouterr_cmd sh {unload variant/4.0 foo=val1 bar=2} OK {}

# with default value for variant
setenv_var TESTSUITE_VARIANT 2
set ans [list]
lappend ans [list unset TS0]
lappend ans [list unsetpath TS1]
# TS2 is kept set as value computed on unload evaluation differs from loaded value
lappend ans [list setpath TS2 bar]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMVARIANT]
testouterr_cmd sh {unload variant} $ans {variant}
testouterr_cmd sh {unload variant bar=2} $ans {variant bar=2}
testouterr_cmd sh {unload variant/4.0 foo=val1 bar=2} OK {}


# bad variant value set in persistency information
# equivalent to same variant set defined between load and unload but with different value list

# without default value for variant
setenv_var TESTSUITE_VARIANT 1
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|0&foo|val5|0|0
setenv_var TS0 {bar foo}
setenv_path_var TS1 {bar=2:foo=val5}
setenv_path_var TS2 {bar-foo}
set tserr [msg_unload variant/4.0{bar=2:foo=val5} [err_invalvarval foo val5 {val1 val2 val3}]]
testouterr_cmd sh {unload variant} ERR $tserr
testouterr_cmd sh {unload variant bar=2} ERR $tserr
testouterr_cmd sh {unload variant/4.0 bar=2 foo=val5} ERR $tserr

# with default value for variant
setenv_var TESTSUITE_VARIANT 2
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|2&foo|val5|0|0
testouterr_cmd sh {unload variant} ERR $tserr
testouterr_cmd sh {unload variant bar=2} ERR $tserr
testouterr_cmd sh {unload variant/4.0 bar=2 foo=val5} ERR $tserr


# variant set in persistency but unknown when unloading module
# equivalent to different variant set defined between load and unload
setenv_var TESTSUITE_VARIANT 1
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|0&foo|val1|0|0&baz|val1|0|0
setenv_var TS0 {bar baz foo}
setenv_path_var TS1 {bar=2:baz=val1:foo=val1}
setenv_path_var TS2 {bar-baz-foo}
set tserr [msg_unload variant/4.0{bar=2:baz=val1:foo=val1} [err_unkvar baz]]
testouterr_cmd sh {unload variant} ERR $tserr
testouterr_cmd sh {unload variant baz=val1} ERR $tserr
testouterr_cmd sh {unload variant/4.0 bar=2 foo=val1 baz=val1} ERR $tserr


#
# switch tests
#

setenv_var TESTSUITE_VARIANT 1
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|0&foo|val1|0|0
setenv_var TS0 {bar foo}
setenv_path_var TS1 {bar=2:foo=val1}
setenv_path_var TS2 {bar-foo}

# switch to same modulefile but different variant value
set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list setpath TS1 {bar=1:foo=val2}]
lappend ans [list setpath TS2 bar-foo]
lappend ans [list setpath LOADEDMODULES variant/4.0]
lappend ans [list setpath _LMFILES_ $mp/variant/4.0]
lappend ans [list setpath MODULES_LMVARIANT variant/4.0&foo|val2|0|0&bar|1|0|0]
testouterr_cmd_re sh {switch variant/4.0 bar=1 foo=val2} $ans {}

# unknown variant on switched-on module
set ans [list]
lappend ans [list unsetpath MODULES_LMVARIANT]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath TS1]
lappend ans [list unsetpath TS2]
lappend ans [list unset TS0]
lappend ans [list ERR]
set tserr [msg_load variant/4.0{bar=2:baz=val1:foo=val1} [err_unkvar baz]]\n\n[msg_switch variant/4.0{bar=2:foo=val1} variant/4.0{bar=2:baz=val1:foo=val1} [err_swon variant/4.0{bar=2:baz=val1:foo=val1}]]
testouterr_cmd sh {switch variant/4.0 bar=2 foo=val1 baz=val1} $ans $tserr
testouterr_cmd sh {switch variant/4.0 bar=2 variant/4.0 bar=2 foo=val1 baz=val1} $ans $tserr

# bad variant value on switched-on module
set tserr [msg_load variant/4.0{bar=1:foo=val5} [err_invalvarval foo val5 {val1 val2 val3}]]\n\n[msg_switch variant/4.0{bar=2:foo=val1} variant/4.0{bar=1:foo=val5} [err_swon variant/4.0{bar=1:foo=val5}]]
testouterr_cmd sh {switch variant/4.0 bar=1 foo=val5} $ans $tserr
testouterr_cmd sh {switch variant/4.0 foo=val1 variant/4.0 bar=1 foo=val5} $ans $tserr

# no value set for variant on switched-on module
set tserr [msg_load variant/4.0{bar=1} [err_novarval foo {val1 val2 val3}]]\n\n[msg_switch variant/4.0{bar=2:foo=val1} variant/4.0{bar=1} [err_swon variant/4.0{bar=1}]]
testouterr_cmd sh {switch variant/4.0 bar=1} $ans $tserr
testouterr_cmd sh {switch variant/4.0 foo=val1 variant/4.0 bar=1} $ans $tserr

# missing variant value on switched-off module
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|0
setenv_var TS0 {bar}
setenv_path_var TS1 {bar=2}
setenv_path_var TS2 {bar}
set tserr [msg_unload variant/4.0{bar=2} [err_novarval foo {val1 val2 val3}]]\n\n[msg_switch variant/4.0{bar=2} variant/4.0{bar=1} [err_swoff variant/4.0{bar=2}]]
testouterr_cmd sh {switch variant/4.0 bar=1} ERR $tserr
set tserr [msg_unload variant/4.0{bar=2} [err_novarval foo {val1 val2 val3}]]\n\n[msg_switch variant/4.0{bar=2} variant/4.0{foo=val1} [err_swoff variant/4.0{bar=2}]]
testouterr_cmd sh {switch variant/4.0 bar=2 variant/4.0 foo=val1} ERR $tserr

# test close unload match with 1-arg form
setenv_loaded_module [list variant/4.0] [list $mp/variant/4.0]
setenv_path_var MODULES_LMVARIANT variant/4.0&bar|2|0|0&foo|val1|0|0
setenv_var TS0 {bar foo}
setenv_path_var TS1 {bar=2:foo=val1}
setenv_path_var TS2 {bar-foo}
set ans [list]
lappend ans [list unset TS0]
lappend ans [list unsetpath TS1]
lappend ans [list unsetpath TS2]
lappend ans [list setpath LOADEDMODULES variant/6.0]
lappend ans [list setpath _LMFILES_ $mp/variant/6.0]
lappend ans [list setpath MODULES_LMVARIANT variant/6.0&foo|val1|0|0&bar|2|0|0]
testouterr_cmd sh {switch variant/6.0 bar=2 foo=val1} $ans {}
set ans [list]
lappend ans [list unset TS0]
lappend ans [list unsetpath TS1]
lappend ans [list unsetpath TS2]
lappend ans [list setpath LOADEDMODULES variant/6.0]
lappend ans [list setpath _LMFILES_ $mp/variant/6.0]
lappend ans [list setpath MODULES_LMVARIANT variant/6.0&foo|val2|0|0&bar|1|0|0]
testouterr_cmd sh {switch variant/6.0 bar=1 foo=val2} $ans {}

setenv_var TESTSUITE_VARIANT 10
set ans [list]
lappend ans [list unset TS0]
lappend ans [list unsetpath TS1]
lappend ans [list unsetpath TS2]
lappend ans [list setpath LOADEDMODULES variant/6.0]
lappend ans [list setpath _LMFILES_ $mp/variant/6.0]
lappend ans [list setpath MODULES_LMVARIANT variant/6.0&baz|val3|0|0]
testouterr_cmd sh {switch variant/6.0 baz=val3} $ans {}


#
#  Cleanup
#

reset_test_env
