##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2017/12/02
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, unload
#   Modulefiles:	info/isavail
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the 'source' modulefile subcommand for all
#			allowed shell types.
#		}C%
#
##############################################################################

set module "source/1.0"
set modulefile "$modpath/$module"
set modulefilere "$modpathre/$module"
set path $env(PATH)
set pathnew "$path:/modules/testsuite2.2:/modules/testsuite2.2/2.2"
set module2 "source/0.9"
set modulefile2 "$modpath/$module2"
set module3 "source/1.1"
set modulefile3 "$modpath/$module3"
set modulefile3re "$modpathre/$module3"
set module4 "source/1.2"
set modulefile4 "$modpath/$module4"


#
#  The tests
#

# load test
lappend ans [list set testsuite "yes"]
lappend ans [list setpath PATH $pathnew]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd_re "ALL" "load $module" $ans ""

# test with relative path source
set ans [list]
lappend ans [list set testsuite "yes"]
lappend ans [list setpath PATH $pathnew]
lappend ans [list setpath LOADEDMODULES $module3]
lappend ans [list setpath _LMFILES_ $modulefile3]

testouterr_cmd_re "sh" "load $module3" $ans ""

# test empty file name
set custom {module source {}}
if {[cmpversion $tclsh_version 8.5] == -1} {
    set custom "<EXM>$custom"
}
set errsource [msg_moderr $msg_filenameempty $custom $modulefile4 27]
testouterr_cmd_re "sh" "load $module4" "ERR" [escre [msg_load $module4 $errsource]]

# display test
set dispout "module\t\tsource $modpathre/setenv/1.0 $modpathre/append/2.2"
testouterr_cmd_re "ALL" "display $module" "OK" "$modlin\n$modulefilere:\n\n$dispout\n$modlin"

# test with relative path source
set dispout "module\t\tsource ../setenv/1.0 ../append/2.2"
testouterr_cmd_re "sh" "display $module3" "OK" "$modlin\n$modulefile3re:\n\n$dispout\n$modlin"

# unload test
if { $verbose > 0 } {
    send_user "\tSetup PATH = '$pathnew'\n"
}
set env(PATH) $pathnew
set env(testsuite) "yes"
setenv_loaded_module $module $modulefile

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath PATH $path]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]

testouterr_cmd_re "ALL" "unload $module" $ans ""

# test with relative path source
if { $verbose > 0 } {
    send_user "\tSetup PATH = '$pathnew'\n"
}
set env(PATH) $pathnew
set env(testsuite) "yes"
setenv_loaded_module $module3 $modulefile3

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath PATH $path]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]

testouterr_cmd_re "sh" "unload $module3" $ans ""

# test empty file name
setenv_loaded_module $module4 $modulefile4

set custom {module source {}}
if {[cmpversion $tclsh_version 8.5] == -1} {
    set custom "<EXM>$custom"
}
set errsource [msg_moderr $msg_filenameempty $custom $modulefile4 27]
testouterr_cmd_re "sh" "unload $module4" "ERR" [escre [msg_unload $module4 $errsource]]

# load test with source command targeting bad file
set custom {module source unknown/1.0}
if {[cmpversion $tclsh_version 8.5] == -1} {
    set custom "<EXM>$custom"
}
set errsource [msg_moderr {File unknown/1.0 does not exist} $custom $modulefile2 27]
testouterr_cmd_re "sh" "load $module2" "ERR" [escre [msg_load $module2 $errsource]]

# unload test with bad sourced file
setenv_loaded_module $module2 $modulefile2

testouterr_cmd_re "sh" "unload $module2" "ERR" [escre [msg_unload $module2 $errsource]]


#
#  Cleanup
#

reset_test_env
