##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2021/06/23
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Modulefiles:    variant
#   Sub-Command:    config, list, load, unload
#
#   Comment:	%C{
#           Test variant_shortcut mechanism
#		}C%
#
##############################################################################

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

setenv_var MODULES_ADVANCED_VERSION_SPEC 1
unsetenv_var MODULES_VARIANT_SHORTCUT


#
# config checks
#

# check expected siteconfig file is installed
set is_stderr_tty [siteconfig_isStderrTty]
if {$is_stderr_tty} {
setenv_var TESTSUITE_ENABLE_SITECONFIG_BADVARIANTSHORTCUTDFLVALUE 1

set tserr "$warn_msgs: Ignore invalid default value for 'variant_shortcut' config \\(foo\\)
$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          "
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

setenv_var MODULES_VARIANT_SHORTCUT foo
set tserr "$warn_msgs: Ignore invalid value set in MODULES_VARIANT_SHORTCUT \\(foo\\)
$warn_msgs: Ignore invalid default value for 'variant_shortcut' config \\(foo\\)
$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut           \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

unsetenv_var TESTSUITE_ENABLE_SITECONFIG_BADVARIANTSHORTCUTDFLVALUE
}

# bad env var syntax
setenv_var MODULES_VARIANT_SHORTCUT foo
set tserr "$warn_msgs: Ignore invalid value set in MODULES_VARIANT_SHORTCUT \\(foo\\)
$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          $install_variantshortcut \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

# forbidden shortcut length
setenv_var MODULES_VARIANT_SHORTCUT {foo=%:bar=##}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          foo=% \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

# forbidden shortcut character
setenv_var MODULES_VARIANT_SHORTCUT {bar=a:foo=2:baz=@}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut           \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

# shortcut superseding
setenv_var MODULES_VARIANT_SHORTCUT {bar=%:foo=%}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          foo=% \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

setenv_var MODULES_VARIANT_SHORTCUT {foo=%:bar=%}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          bar=% \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

setenv_var MODULES_VARIANT_SHORTCUT {foo=%:foo=#}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          foo=# \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

setenv_var MODULES_VARIANT_SHORTCUT {foo=%:foo=#:bar=%}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          foo=#:bar=% \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr

setenv_var MODULES_VARIANT_SHORTCUT {foo=%:foo=#:bar=#}
set tserr "$vers_reportre

- Config. name $modlin.- Value \\(set by if default overridden\\) $modlin
variant_shortcut          bar=# \\(env-var\\)"
testouterr_cmd_re sh {config variant_shortcut} OK $tserr


#
# load tests
#

setenv_var MODULES_VARIANT_SHORTCUT foo=%
setenv_var TESTSUITE_VARIANT 13

set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list set TS1 {bar=val2:foo=val1}]
lappend ans [list setpath LOADEDMODULES variant/1.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val2|0|1]
set tserr [msg_load variant/1.0{bar=val2:%val1}]
testouterr_cmd sh {load -v variant/1.0%val1 bar=val2} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 foo=val1 bar=val2} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 bar=val2%val1} $ans $tserr

setenv_var MODULES_VARIANT_SHORTCUT foo=%:bar=^
set tserr [msg_load variant/1.0{^val2:%val1}]
testouterr_cmd sh {load -v variant/1.0%val1 bar=val2} $ans $tserr
testouterr_cmd sh {load -v variant@1.0%val1^val2} $ans $tserr
testouterr_cmd sh {load -v variant@1.0 ^val2%val1} $ans $tserr

# supersede variant spec
testouterr_cmd sh {load -v variant@1.0 ^val1 %val1 ^val2} $ans $tserr
testouterr_cmd sh {load -v variant@1.0 bar=val1 %val1 ^val2} $ans $tserr
testouterr_cmd sh {load -v variant@1.0 ^val1 %val1 bar=val2} $ans $tserr

# module already loaded
setenv_loaded_module [list variant/1.0] [list $mp/variant/1.0]
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val2|0|1
set tserr [msg_load variant/1.0{%val1} [err_othervariantloaded {^val2:%val1}]]
#FIXME: should detect that omitted variant matches loaded variant with default value
testouterr_cmd sh {load -v variant/1.0%val1} ERR $tserr
testouterr_cmd sh {load -v variant/1.0 foo=val1} ERR $tserr

testouterr_cmd sh {load -v variant@1.0%val1^val2} OK {}
testouterr_cmd sh {load -v variant@1.0 foo=val1^val2} OK {}

# another variant already loaded
set tserr [msg_load variant/1.0{^val1:%val1} [err_othervariantloaded {^val2:%val1}]]
testouterr_cmd sh {load -v variant/1.0%val1^val1} ERR $tserr
testouterr_cmd sh {load -v variant@1.0 foo=val1 ^val1} ERR $tserr
set tserr [msg_load variant/1.0{^val1} [err_othervariantloaded {^val2:%val1}]]
testouterr_cmd sh {load -v variant@1.0 ^val1} ERR $tserr
set tserr [msg_load variant/1.0 [err_othervariantloaded {^val2:%val1}]]
testouterr_cmd sh {load -v variant@1.0} ERR $tserr


#
# module tags
#

unsetenv_loaded_module
unsetenv_path_var MODULES_LMVARIANT

setenv_var MODULES_VARIANT_SHORTCUT foo=%
setenv_var TESTSUITE_VARIANT 13
setenv_var TESTSUITE_VARIANT_TAG shortcut1

set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list set TS1 {bar=val2:foo=val1}]
lappend ans [list setpath LOADEDMODULES variant/1.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val2|0|1]
lappend ans [list setpath MODULES_LMTAG variant/1.0&sticky]
set tserr [msg_load variant/1.0{bar=val2:%val1}]
testouterr_cmd sh {load -v variant/1.0%val1 bar=val2} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 foo=val1 bar=val2} $ans $tserr

setenv_var TESTSUITE_VARIANT_TAG shortcut2
testouterr_cmd sh {load -v variant/1.0%val1 bar=val2} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 foo=val1 bar=val2} $ans $tserr

setenv_var MODULES_VARIANT_SHORTCUT bar=%
set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list set TS1 {bar=val2:foo=val1}]
lappend ans [list setpath LOADEDMODULES variant/1.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val2|0|1]
set tserr [msg_load variant/1.0{%val2:foo=val1}]
testouterr_cmd sh {load -v variant/1.0%val2 foo=val1} $ans $tserr

setenv_var TESTSUITE_VARIANT_TAG shortcut3
testouterr_cmd sh {load -v variant/1.0%val2 foo=val1} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 bar=val2 foo=val1} $ans $tserr

set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list set TS1 {bar=val1:foo=val1}]
lappend ans [list setpath LOADEDMODULES variant/1.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val1|0|0]
lappend ans [list setpath MODULES_LMTAG variant/1.0&sticky]
set tserr [msg_load variant/1.0{%val1:foo=val1}]
testouterr_cmd sh {load -v variant/1.0%val1 foo=val1} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 bar=val1 foo=val1} $ans $tserr

unsetenv_var TESTSUITE_VARIANT_TAG


#
# shortcut used in modulefile
#

setenv_var MODULES_VARIANT_SHORTCUT bar=%
setenv_var TESTSUITE_VARIANT shortcut1

set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list set TS1 {bar=val1:foo=val1}]
lappend ans [list setpath LOADEDMODULES variant/1.0:variant/7.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0:$mp/variant/7.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|2&bar|val1|0|0]
lappend ans [list setpath MODULES_LMPREREQ {variant/7.0&variant@1.0 %val1}]
lappend ans [list setpath MODULES_LMNOTUASKED variant/1.0]
set tserr [msg_load variant/1.0{%val1:foo=val1}]\n\n[msg_top_load variant/7.0 {} variant/1.0{%val1:foo=val1} {}]
testouterr_cmd_re sh {load --auto -v variant@7.0} $ans $tserr

setenv_var MODULES_VARIANT_SHORTCUT foo=%
setenv_var TESTSUITE_VARIANT shortcut2

set ans [list]
lappend ans [list set TS0 {bar foo}]
lappend ans [list set TS1 {bar=val2:foo=val1}]
lappend ans [list setpath LOADEDMODULES variant/1.0:variant/7.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0:$mp/variant/7.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val2|0|1]
lappend ans [list setpath MODULES_LMPREREQ {variant/7.0&variant@1.0%val1 bar=val2}]
lappend ans [list setpath MODULES_LMNOTUASKED variant/1.0]
set tserr [msg_load variant/1.0{bar=val2:%val1}]\n\n[msg_top_load variant/7.0 {} variant/1.0{bar=val2:%val1} {}]
testouterr_cmd_re sh {load --auto -v variant@7.0} $ans $tserr


#
# unload checks
#

setenv_var MODULES_VARIANT_SHORTCUT foo=%
setenv_var TESTSUITE_VARIANT 13
setenv_loaded_module [list variant/1.0] [list $mp/variant/1.0]
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val3|0|0

set ans [list]
lappend ans [list unset TS0]
lappend ans [list unset TS1]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMVARIANT]

set tserr [msg_unload variant/1.0{bar=val3:%val1}]
testouterr_cmd sh {unload -v variant@1.0} $ans $tserr
testouterr_cmd sh {unload -v variant@1.0 foo=val1} $ans $tserr
testouterr_cmd sh {unload -v variant@1.0%val1} $ans $tserr
testouterr_cmd sh {unload -v variant@1.0%val2%val1} $ans $tserr

testouterr_cmd sh {unload -v variant@1.0%val1 bar=val2} OK {}
testouterr_cmd sh {unload -v variant@1.0%val2} OK {}
testouterr_cmd sh {unload -v variant@1.0 foo=val2} OK {}
testouterr_cmd sh {unload -v variant@1.0 foo=val1 %val2} OK {}


#
# list checks
#

setenv_var MODULES_VARIANT_SHORTCUT foo=%
setenv_var TESTSUITE_VARIANT 13

setenv_loaded_module [list variant/1.0] [list $mp/variant/1.0]
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val3|0|0


testouterr_cmd sh {list} OK "$cur_loaded
 1) variant/1.0{bar=val3:%val1}  

Key:
{%value}={foo=value}  {variant=value}  "

setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|1|1|0
testouterr_cmd sh {list} OK "$cur_loaded
 1) variant/1.0{+bar:%val1}  

Key:
{+variant}={variant=on}  {%value}={foo=value}  {variant=value}  "

# json output
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val3|0|0
testouterr_cmd sh {list -j} OK "{\"variant/1.0\": { \"name\": \"variant/1.0\", \"type\": \"modulefile\", \"variants\": { \"bar\": \"val3\", \"foo\": \"val1\" }, \"symbols\": \[\], \"tags\": \[\], \"pathname\": \"$mp/variant/1.0\"}}"
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|1|1|0
testouterr_cmd sh {list -j} OK "{\"variant/1.0\": { \"name\": \"variant/1.0\", \"type\": \"modulefile\", \"variants\": { \"bar\": true, \"foo\": \"val1\" }, \"symbols\": \[\], \"tags\": \[\], \"pathname\": \"$mp/variant/1.0\"}}"

# colored output
setenv_var MODULES_COLORS "se=2:va=33"
setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|val3|0|0
testouterr_cmd_re sh {list --color=always} OK "$cur_loaded
 1\\) variant/1.0[sgr 2]{[sgr 22][sgr 33]bar=val3[sgr 0][sgr 2]:[sgr 22][sgr 33]%val1[sgr 0][sgr 2]}[sgr 22]  

Key:
[sgr 2]{[sgr 22][sgr 33]%value[sgr 0][sgr 2]}[sgr 22]=[sgr 2]{[sgr 22][sgr 33]foo=value[sgr 0][sgr 2 ]}[sgr 22]  [sgr 2]{[sgr 22][sgr 33]variant=value[sgr 0][sgr 2]}[sgr 22]  "

setenv_path_var MODULES_LMVARIANT variant/1.0&foo|val1|0|1&bar|0|1|0
testouterr_cmd_re sh {list --color=always} OK "$cur_loaded
 1\\) variant/1.0[sgr 2]{[sgr 22][sgr 33]-bar[sgr 0][sgr 2]:[sgr 22][sgr 33]%val1[sgr 0][sgr 2]}[sgr 22]  

Key:
[sgr 2]{[sgr 22][sgr 33]-variant[sgr 0][sgr 2]}[sgr 22]=[sgr 2]{[sgr 22][sgr 33]variant=off[sgr 0][sgr 2 ]}[sgr 22]  [sgr 2]{[sgr 22][sgr 33]%value[sgr 0][sgr 2]}[sgr 22]=[sgr 2]{[sgr 22][sgr 33]foo=value[sgr 0][sgr 2 ]}[sgr 22]  [sgr 2]{[sgr 22][sgr 33]variant=value[sgr 0][sgr 2]}[sgr 22]  "


#
# specific module designation
#

unsetenv_loaded_module
unsetenv_path_var MODULES_LMVARIANT

setenv_var MODULES_VARIANT_SHORTCUT foo=%
setenv_var TESTSUITE_VARIANT shortcut3

set tserr [msg_load variant/1.0{%} [err_invalvarval foo {} {val1 val2 val3}]]
testouterr_cmd sh {load -v variant/1.0%} ERR $tserr
testouterr_cmd sh {load -v variant/1.0 %} ERR $tserr
testouterr_cmd sh {load -v variant/1.0 %val1%} ERR $tserr

# shortcut on boolean variant
setenv_var TESTSUITE_VARIANT bool8
set ans [list]
lappend ans [list set TS0 {foo}]
lappend ans [list set TS1 {foo=1}]
lappend ans [list set TS2 true]
lappend ans [list setpath LOADEDMODULES variant/1.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|1|1|0]
set tserr [msg_load variant/1.0{+foo}]
testouterr_cmd sh {load -v variant/1.0%1} $ans $tserr
testouterr_cmd sh {load -v variant/1.0%on} $ans $tserr
testouterr_cmd sh {load -v variant/1.0 %true} $ans $tserr

set ans [list]
lappend ans [list set TS0 {foo}]
lappend ans [list set TS1 {foo=0}]
lappend ans [list set TS2 false]
lappend ans [list setpath LOADEDMODULES variant/1.0]
lappend ans [list setpath _LMFILES_ $mp/variant/1.0]
lappend ans [list setpath MODULES_LMVARIANT variant/1.0&foo|0|1|0]
set tserr [msg_load variant/1.0{-foo}]
testouterr_cmd sh {load -v variant/1.0 %0} $ans $tserr
testouterr_cmd sh {load -v variant/1.0%off} $ans $tserr
testouterr_cmd sh {load -v variant/1.0%false} $ans $tserr


#
#  Cleanup
#

reset_test_env
