##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2020/02/29
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Modulefiles:
#   Sub-Command:    load, source, help, display, test
#
#   Comment:	%C{
#           Check error stack trace printed during failed evaluation
#		}C%
#
##############################################################################

set mp $modpath.2
set mpre [regsub -all "\(\[.+?\]\)" $mp {\\\1}]

# setup specific environment
setenv_path_var MODULEPATH $mp

# load tests

set custom {    (procedure "good-command" line 2)
    invoked from within
"good-command"}
set tserr [msg_load bad3/bodyproc [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodyproc 6 {} {} {} $custom]]
testouterr_cmd sh "load bad3/bodyproc" ERR $tserr

set custom "    (procedure \"good-command\" line 2)
    invoked from within
\"good-command\"
    (file \"$mp/bad3/.bodycommon\" line 5)
    invoked from within
\"source \$env(TESTSUITEDIR)/modulefiles.2/bad3/.bodycommon\""
set tserr [msg_load bad3/bodysource [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodysource 2 {} {} {} $custom]]
testouterr_cmd sh "load bad3/bodysource" ERR $tserr

# error in conditional block
if {[cmpversion $tclsh_version 8.6] == -1} {
    set custom "
    invoked from within
\"if \{\$i == 1 \} \{
    bad-command
\}\""
    set linenum 3
} else {
    set custom {}
    set linenum 4
}
set tserr [msg_load bad3/bodyif [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodyif $linenum {} {} {} $custom]]
testouterr_cmd sh "load bad3/bodyif" ERR $tserr
if {[cmpversion $tclsh_version 8.6] == -1} {
    set custom "
    (\"for\" body line 2)
    invoked from within
\"for \{set i 0\} \{\$i<10\} \{incr i\} \{
    bad-command
\}\""
    set linenum 2
} else {
    set custom {}
    set linenum 3
}
set tserr [msg_load bad3/bodyfor [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodyfor $linenum {} {} {} $custom]]
testouterr_cmd sh "load bad3/bodyfor" ERR $tserr
if {[cmpversion $tclsh_version 8.6] == -1} {
    set custom "
    (\"while\" body line 2)
    invoked from within
\"while \{\$i == 1\} \{
    bad-command
\}\""
    set linenum 3
} else {
    set custom {}
    set linenum 4
}
set tserr [msg_load bad3/bodywhile [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodywhile $linenum {} {} {} $custom]]
testouterr_cmd sh "load bad3/bodywhile" ERR $tserr



# error in modulerc
setenv_var BADRC3_CASE 1
set custom {    (procedure "good-command" line 2)
    invoked from within
"good-command"}
if {[cmpversion $tclsh_version 8.6] == -1} {
    append custom "
    (\"1\" arm line 5)
    invoked from within
\"switch -- \$case \{
1 \{
    proc good-command \{\} \{
        bad-command
    \}
    good-command
\}
2 \{
    source \$env(TESTSUITEDIR)/modulefiles.2/bad3/.bo...\""
    set tserr [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/badrc3/.modulerc 8 {  } {} {} $custom]
} else {
    set tserr [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/badrc3/.modulerc 13 {  } {} {} $custom]
}
set ans [list]
lappend ans [list setpath LOADEDMODULES badrc3/1]
lappend ans [list setpath _LMFILES_ $mp/badrc3/1]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME badrc3/1&as|badrc3/default&as|badrc3/latest]
}
lappend ans [list ERR]
testouterr_cmd sh {load badrc3/1} $ans $tserr

setenv_var BADRC3_CASE 2
set custom "    (procedure \"good-command\" line 2)
    invoked from within
\"good-command\"
    (file \"$mp/bad3/.bodycommon\" line 5)
    invoked from within
\"source \$env(TESTSUITEDIR)/modulefiles.2/bad3/.bodycommon\""
if {[cmpversion $tclsh_version 8.6] == -1} {
    append custom "
    (\"2\" arm line 2)
    invoked from within
\"switch -- \$case \{
1 \{
    proc good-command \{\} \{
        bad-command
    \}
    good-command
\}
2 \{
    source \$env(TESTSUITEDIR)/modulefiles.2/bad3/.bo...\""
    set tserr [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/badrc3/.modulerc 8 {  } {} {} $custom]
} else {
    set tserr [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/badrc3/.modulerc 16 {  } {} {} $custom]
}
testouterr_cmd sh {load badrc3/1} $ans $tserr

setenv_var BADRC3_CASE 3
if {[cmpversion $tclsh_version 8.5] == -1} {
    set linenum 8
    set tserr [escre [msg_moderr {module-info bad not supported} "<EXM>module-info bad<EXM>" $mp/badrc3/.modulerc $linenum {  }]]
    testouterr_cmd_re sh {load badrc3/1} $ans $tserr
} else {
    if {[cmpversion $tclsh_version 8.6] == -1} {
        set custom "    (\"3\" arm line 2)
    invoked from within
\"switch -- \$case \{
1 \{
    proc good-command \{\} \{
        bad-command
    \}
    good-command
\}
2 \{
    source \$env(TESTSUITEDIR)/modulefiles.2/bad3/.bo...\""
        set linenum 8
    } else {
        set custom {}
        set linenum 19
    }
    set tserr [msg_moderr {module-info bad not supported} "module-info bad" $mp/badrc3/.modulerc $linenum {  } {} {} $custom]
    testouterr_cmd sh {load badrc3/1} $ans $tserr
}
unsetenv_var BADRC3_CASE


# source tests

set custom {    (procedure "good-command" line 2)
    invoked from within
"good-command"}
set tserr [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodyproc 6 {  } {} {} $custom]
testouterr_cmd sh "source $mp/bad3/bodyproc" ERR $tserr

set custom "    (procedure \"good-command\" line 2)
    invoked from within
\"good-command\"
    (file \"$mp/bad3/.bodycommon\" line 5)
    invoked from within
\"source \$env(TESTSUITEDIR)/modulefiles.2/bad3/.bodycommon\""
set tserr [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/bodysource 2 {  } {} {} $custom]
testouterr_cmd sh "source $mp/bad3/bodysource" ERR $tserr

# display tests

set custom {    (procedure "good-command" line 2)
    invoked from within
"good-command"
    (procedure "ModulesDisplay" line 2)
    invoked from within
"ModulesDisplay"}
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set custom [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/procproc {} {  } {} {} $custom]]
set tserr "$modlin
$mpre/bad3/procproc:

$custom
$modlin"
testouterr_cmd_re sh {display bad3/procproc} ERR $tserr

set custom "    (procedure \"good-command\" line 2)
    invoked from within
\"good-command\"
    (procedure \"ModulesDisplay\" line 2)
    invoked from within
\"ModulesDisplay\""
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set custom [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/procsource {} {  } {} {} $custom]]
set tserr "$modlin
$mpre/bad3/procsource:

$custom
$modlin"
testouterr_cmd_re sh {display bad3/procsource} ERR $tserr


# help tests

set custom {    (procedure "good-command" line 2)
    invoked from within
"good-command"
    (procedure "ModulesHelp" line 2)
    invoked from within
"ModulesHelp"}
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set custom [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/procproc {} {  } {} {} $custom]]
set tserr "$modlin
Module Specific Help for $mpre/bad3/procproc:

$custom
$modlin"
testouterr_cmd_re sh {help bad3/procproc} ERR $tserr

set custom "    (procedure \"good-command\" line 2)
    invoked from within
\"good-command\"
    (procedure \"ModulesHelp\" line 2)
    invoked from within
\"ModulesHelp\""
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set custom [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/procsource {} {  } {} {} $custom]]
set tserr "$modlin
Module Specific Help for $mpre/bad3/procsource:

$custom
$modlin"
testouterr_cmd_re sh {help bad3/procsource} ERR $tserr


# test tests

set custom {    (procedure "good-command" line 2)
    invoked from within
"good-command"
    (procedure "ModulesTest" line 2)
    invoked from within
"ModulesTest"}
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set custom [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/procproc {} {  } {} {} $custom]]
set tserr "$modlin
Module Specific Test for $mpre/bad3/procproc:

$custom
$modlin"
testouterr_cmd_re sh {test bad3/procproc} ERR $tserr

set custom "    (procedure \"good-command\" line 2)
    invoked from within
\"good-command\"
    (procedure \"ModulesTest\" line 2)
    invoked from within
\"ModulesTest\""
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set custom [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $mp/bad3/procsource {} {  } {} {} $custom]]
set tserr "$modlin
Module Specific Test for $mpre/bad3/procsource:

$custom
$modlin"
testouterr_cmd_re sh {test bad3/procsource} ERR $tserr

# wrongly call a legit modulefile command from a modulefile procedure
set custom "$moderr_msgs: wrong # args: should be \"setenv var val\"
    invoked from within
\"setenv\"
    (procedure \"my-proc\" line 2)
    invoked from within
\"my-proc\"
    (file \"$mp/bad3/proc2\" line 6)
Please contact <root@localhost>"
set tserr [msg_load bad3/proc2 $custom]
testouterr_cmd sh {load bad3/proc2} ERR $tserr

# try to reach a modulecmd.tcl internal procedure from a modulefile evaluation
# "unknown command"-error will preserve internal command name reference in error stack
set custom {
    (procedure "my-proc" line 2)
    invoked from within
"my-proc"}
set tserr [msg_load bad3/proc3 [msg_moderr {invalid command name "getModshareVarName"} {getModshareVarName} $mp/bad3/proc3 6 {} {} {} $custom]]
testouterr_cmd sh {load bad3/proc3} ERR $tserr


#
#  Cleanup
#

reset_test_env
