##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2017/04/30
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		test
#   Modulefiles:	test, bad
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the module command 'test'
#		}C%
#
##############################################################################

# skip tests if implicit_default option could not be overridden
if {[is_config_locked implicit_default]} {
    send_user "\tskipping tests as implicit_default could not be overridden\n"
} else {

# ensure avail tests have implicit default enabled
setenv_var MODULES_IMPLICIT_DEFAULT 1

set module "test/1.2"
set modulefile "$modpathre/$module"
set modulefile_full "$modpath/$module"
set modulefile_reld "$reldmodpath/$module"
set modulefile_reldd "$relddmodpath/$module"
set module2 "test/1.0"
set modulefile2 "$modpathre/$module2"
set module3 "test/2.0"
set modulefile3 "$modpathre/$module3"
set module4 "bad"
set modulefile4 "$modpathre/$module4"
set modulefile4nore "$modpath/$module4"
set module5 "bad2"
set modulefile5 "$modpathre/$module5"
set modulefile5nore "$modpath/$module5"
set modulefile5_full "$modpath/$module5/proc"
set modulefile5_reld "$reldmodpath/$module5/proc"
set modulefile5_reldd "$relddmodpath/$module5/proc"

set lin_test "Module Specific Test for "
set missing_test "$warn_msgs: Unable to find ModulesTest in "
set fail_test "Test result: FAIL"
set ok_test "Test result: PASS"

set custom {    (procedure "ModulesTest" line 2)
    invoked from within
"ModulesTest"}
if {[cmpversion $tclsh_version 8.5] == -1} {
    append custom {<EXM>}
}
set mod5_interr [escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $modulefile5nore/proc {} {  } {} {} $custom]]
set mod4_interr "[escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $modulefile4nore/.modulerc 28 {  }]]
$modlin
$lin_test$modulefile4/before:

[escre [msg_moderr "$msg_invcmdname \"bad-command\"" bad-command $modulefile4nore/before 26 {  }]]
$modlin"

set module6 "NonExistant"
set mod6_err "$err_path'$module6'"

#
#  The tests
#

# missing procedure
testouterr_cmd_re "sh" "test $module" "OK" "$modlin\n$lin_test$modulefile:\n\n$missing_test$modulefile.\n$modlin"
# test with module passed as full path
testouterr_cmd_re "sh" "test $modulefile_full" "OK" "$modlin\n$lin_test$modulefile:\n\n$missing_test$modulefile.\n$modlin"
testouterr_cmd_re "sh" "test $modulefile_reld" "OK" "$modlin\n$lin_test$modulefile:\n\n$missing_test$modulefile.\n$modlin"
testouterr_cmd_re "sh" "test $modulefile_reldd" "OK" "$modlin\n$lin_test$modulefile:\n\n$missing_test$modulefile.\n$modlin"

# missing return value
testouterr_cmd_re "sh" "test $module2" "ERR" "$modlin\n$lin_test$modulefile2:\n\nTest_err\n$fail_test\n$modlin"

# test returns false
testouterr_cmd_re "sh" "test $module3" "ERR" "$modlin\n$lin_test$modulefile3:\n\nTest_err\n$fail_test\n$modlin"

# Set up the environment for test to return true
set env(TEST) 1

# test returns true
testouterr_cmd_re "sh" "test $module3" "OK" "$modlin\n$lin_test$modulefile3:\n\nTest_err\n$ok_test\n$modlin"

# Clean up the just changed environment
unset env(TEST)

# test with erroneous modulefile
testouterr_cmd_re "sh" "test $module4" "ERR" "$mod4_interr"

# test with erroneous ModulesTest proc in modulefile
testouterr_cmd_re "sh" "test $module5" "ERR" "$modlin\n$lin_test$modulefile5/proc:\n\n$mod5_interr\n$modlin"
# test with module passed as full path
testouterr_cmd_re "sh" "test $modulefile5_full" "ERR" "$modlin\n$lin_test$modulefile5/proc:\n\n$mod5_interr\n$modlin"
testouterr_cmd_re "sh" "test $modulefile5_reld" "ERR" "$modlin\n$lin_test$modulefile5/proc:\n\n$mod5_interr\n$modlin"
testouterr_cmd_re "sh" "test $modulefile5_reldd" "ERR" "$modlin\n$lin_test$modulefile5/proc:\n\n$mod5_interr\n$modlin"

# test on unexistant modulefile
testouterr_cmd "sh" "test $module6" "ERR" "$mod6_err"

# check error when implicit default is disabled
setenv_var MODULES_IMPLICIT_DEFAULT 0
testouterr_cmd sh "test $module5" ERR "$err_nodefault'$module5'"
setenv_var MODULES_IMPLICIT_DEFAULT 1

#
#  Cleanup
#

reset_test_env

}
