##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2021/05/25
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		module-info, is-loaded, is-avail
#   Modulefiles:    variant
#   Sub-Command:
#
#   Comment:	%C{
#           Test module-info/is-loaded with variant modules
#		}C%
#
##############################################################################

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


setenv_var MODULES_ADVANCED_VERSION_SPEC 1
setenv_var MODULES_EXTENDED_DEFAULT 1


#
# is-loaded tests
#

# test from command-line
testouterr_cmd sh {is-loaded variant} ERR {}
testouterr_cmd sh {is-loaded variant bar=2} ERR {}
testouterr_cmd sh {is-loaded variant bar=1} ERR {}
testouterr_cmd sh {is-loaded variant foo=val2} ERR {}
testouterr_cmd sh {is-loaded variant foo=val1} ERR {}
testouterr_cmd sh {is-loaded variant bar=1 foo=val2} ERR {}
testouterr_cmd sh {is-loaded variant bar=2 foo=val1} ERR {}
testouterr_cmd sh {is-loaded variant bar=2 foo=val2} ERR {}
testouterr_cmd sh {is-loaded variant/1.0} ERR {}
testouterr_cmd sh {is-loaded variant@1:1} ERR {}
testouterr_cmd sh {is-loaded variant/1.0 bar=2} ERR {}
testouterr_cmd sh {is-loaded variant/1.0 bar=2 foo=val1} ERR {}
testouterr_cmd sh {is-loaded variant@1 bar=2 foo=val2} ERR {}
testouterr_cmd sh {is-loaded variant@1.0 bar=2 foo=val2} ERR {}

# test from modulefile
setenv_var TESTSUITE_VARIANT info1
set ans [list]
lappend ans [list set TS1 {0 0 0 0 0 0 0 0 0 0 0 0 0 0}]
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|1|0|0]
testouterr_cmd sh {load variant/6.0 bar=1 foo=val1} $ans {}


# load one module to test it
setenv_loaded_module [list variant/1.0] [list $mp/variant/1.0]
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val2|0|1&bar|2|0|0

testouterr_cmd sh {is-loaded variant} OK {}
testouterr_cmd sh {is-loaded variant bar=2} OK {}
testouterr_cmd sh {is-loaded variant bar=1} ERR {}
testouterr_cmd sh {is-loaded variant foo=val2} OK {}
testouterr_cmd sh {is-loaded variant foo=val1} ERR {}
testouterr_cmd sh {is-loaded variant bar=1 foo=val2} ERR {}
testouterr_cmd sh {is-loaded variant bar=2 foo=val1} ERR {}
testouterr_cmd sh {is-loaded variant bar=2 foo=val2} OK {}
testouterr_cmd sh {is-loaded variant/1.0} OK {}
testouterr_cmd sh {is-loaded variant@1:2} OK {}
testouterr_cmd sh {is-loaded variant/1.0 bar=2} OK {}
testouterr_cmd sh {is-loaded variant/1.0 bar=2 foo=val1} ERR {}
testouterr_cmd sh {is-loaded variant@1 bar=2 foo=val2} OK {}
testouterr_cmd sh {is-loaded variant@1.0 bar=2 foo=val2} OK {}

set ans [list]
lappend ans [list set TS1 {1 1 0 1 0 0 0 1 1 1 1 0 1 1}]
lappend ans [list setpath LOADEDMODULES variant/1.0:variant/6.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0:$mp/variant/6.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val2|0|1&bar|2|0|0:variant/6.0&foo|val1|0|0&bar|1|0|0]
testouterr_cmd_re sh {load variant/6.0 bar=1 foo=val1} $ans {}


#
# info-loaded/module-info loaded tests
#

setenv_var TESTSUITE_VARIANT info2
unsetenv_loaded_module
unsetenv_path_var MODULES_LMVARIANT
set tserr "$error_msgs: Unexpected number of args for 'info-loaded' command\n$err_typehelp"

# test from command-line
testouterr_cmd sh {info-loaded variant} OK {}
testouterr_cmd sh {info-loaded variant bar=2} ERR $tserr
testouterr_cmd sh {info-loaded variant bar=2 foo=val2} ERR $tserr
testouterr_cmd sh {info-loaded variant\ bar=2} OK {}
testouterr_cmd sh {info-loaded variant\ bar=2\ foo=val2} OK {}
testouterr_cmd sh {info-loaded variant/1.0} OK {}
testouterr_cmd sh {info-loaded variant@1:1} OK {}
testouterr_cmd sh {info-loaded variant/1.0 bar=2} ERR $tserr
testouterr_cmd sh {info-loaded variant@1 bar=2 foo=val2} ERR $tserr
testouterr_cmd sh {info-loaded variant/1.0\ bar=2} OK {}
testouterr_cmd sh {info-loaded variant@1\ bar=2\ foo=val2} OK {}

set ans [list]
lappend ans [list set TS1 {::::::}]
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|1|0|0]
testouterr_cmd sh {load variant/6.0 bar=1 foo=val1} $ans {}


# load one module to test it
setenv_loaded_module [list variant/1.0] [list $mp/variant/1.0]
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val2|0|1&bar|2|0|0

set ans [list [list text variant/1.0]]
testouterr_cmd sh {info-loaded variant} $ans {}
testouterr_cmd sh {info-loaded variant bar=2} ERR $tserr
testouterr_cmd sh {info-loaded variant bar=2 foo=val2} ERR $tserr
testouterr_cmd sh {info-loaded variant\ bar=2} OK {}
testouterr_cmd sh {info-loaded variant\ bar=2\ foo=val2} OK {}
testouterr_cmd sh {info-loaded variant/1.0} $ans {}
testouterr_cmd sh {info-loaded variant@1:1} OK {}
testouterr_cmd sh {info-loaded variant/1.0 bar=2} ERR $tserr
testouterr_cmd sh {info-loaded variant@1 bar=2 foo=val2} ERR $tserr
testouterr_cmd sh {info-loaded variant/1.0\ bar=2} OK {}
testouterr_cmd sh {info-loaded variant@1\ bar=2\ foo=val2} OK {}

set ans [list]
lappend ans [list set TS1 {variant/1.0:::variant/1.0:::}]
lappend ans [list setpath LOADEDMODULES variant/1.0:variant/6.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0:$mp/variant/6.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val2|0|1&bar|2|0|0:variant/6.0&foo|val1|0|0&bar|1|0|0]
testouterr_cmd_re sh {load variant/6.0 bar=1 foo=val1} $ans {}


#
# is-avail tests
#

setenv_var TESTSUITE_VARIANT info3
unsetenv_loaded_module
unsetenv_path_var MODULES_LMVARIANT
setenv_var MODULES_IMPLICIT_DEFAULT 1

# from command-line tests are covered by 70/370

set ans [list]
lappend ans [list set TS1 {1 1 1 1 1 1 1 1 1 1 1 1 1 1}]
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|1|0|0]
testouterr_cmd sh {load variant/6.0 bar=1 foo=val1} $ans {}

# load one module (should not change result)
setenv_loaded_module [list variant/1.0] [list $mp/variant/1.0]
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val2|0|1&bar|2|0|0

set ans [list]
lappend ans [list set TS1 {1 1 1 1 1 1 1 1 1 1 1 1 1 1}]
lappend ans [list setpath LOADEDMODULES variant/1.0:variant/6.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0:$mp/variant/6.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val2|0|1&bar|2|0|0:variant/6.0&foo|val1|0|0&bar|1|0|0]
testouterr_cmd_re sh {load variant/6.0 bar=1 foo=val1} $ans {}

unsetenv_var MODULES_IMPLICIT_DEFAULT


#
# module-info tags tests
#

setenv_var TESTSUITE_VARIANT info4
unsetenv_loaded_module
unsetenv_path_var MODULES_LMVARIANT

set ans [list]
lappend ans [list set TS1 {t4}]
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|1|0|0]
lappend ans [list setpath MODULES_LMTAG variant/6.0&t4]
testouterr_cmd sh {load variant/6.0 bar=1 foo=val1} $ans {}

set ans [list]
lappend ans [list set TS1 {t1 t2 t4}]
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]
lappend ans [list setpath MODULES_LMTAG variant/6.0&t1&t2&t4]
testouterr_cmd sh {load variant/6.0 bar=2 foo=val1} $ans {}

set ans [list]
lappend ans [list set TS1 {t2 t3 t5}]
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|1&bar|2|0|0]
lappend ans [list setpath MODULES_LMTAG variant/6.0&t2&t3&t5]
testouterr_cmd sh {load variant/6.0 bar=2 foo=val2} $ans {}

set ans [list]
lappend ans [list set TS1 {t2 t3 t5}]
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|2&bar|2|0|0]
lappend ans [list setpath MODULES_LMTAG variant/6.0&t2&t3&t5]
testouterr_cmd sh {load variant/6.0 bar=2} $ans {}


#
# module-info specified tests
#

setenv_var TESTSUITE_VARIANT info5

set ans [list]
lappend ans [list set TS1 {variant/6.0 bar=1 foo=val1}]
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|1|0|0]
testouterr_cmd sh {load variant/6.0 bar=1 foo=val1} $ans {}

set ans [list]
lappend ans [list set TS1 {variant/6.0 bar=2}]
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|2&bar|2|0|0]
testouterr_cmd sh {load variant/6.0 bar=2} $ans {}


#
# module-info name tests
#

setenv_var TESTSUITE_VARIANT info6

set ans [list]
lappend ans [list set TS1 {variant/6.0}]
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|2&bar|2|0|0]
testouterr_cmd sh {load variant/6.0 bar=2} $ans {}

set ans [list]
lappend ans [list set TS1 {variant/6.0}]
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|val3|0|0&bar|1|0|0]
testouterr_cmd sh {load variant/6.0 bar=1 foo=val3} $ans {}

setenv_var MODULES_IMPLICIT_DEFAULT 1

set ans [list]
lappend ans [list set TS1 {variant/6.0}]
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|val3|0|0&bar|1|0|0]
testouterr_cmd sh {load variant@6:6.3 bar=1 foo=val3} $ans {}

unsetenv_var MODULES_IMPLICIT_DEFAULT


#
#  Cleanup
#

reset_test_env
