##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2020/08/15
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		avail, load
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Disable forbidding with '--before' or '--after' options of
#           'module-forbid' command
#		}C%
#
##############################################################################

# ensure avail tests are made using in depth mode
setenv_var MODULES_AVAIL_INDEPTH 1

# ensure avail tests have implicit default enabled
setenv_var MODULES_IMPLICIT_DEFAULT 1

# ensure regular avail search match is set for these tests
setenv_var MODULES_SEARCH_MATCH starts_with

# ensure extended default and advanced version specifiers are enabled for these tests
setenv_var MODULES_ADVANCED_VERSION_SPEC 1
setenv_var MODULES_EXTENDED_DEFAULT 1

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

setenv_var MODULERCFILE $env(TESTSUITEDIR)/etc/modulerc.hide


set ans [list]
lappend ans [list setpath LOADEDMODULES hide1/5.0]
lappend ans [list setpath _LMFILES_ $mp/hide1/5.0]
lappend ans [list setpath MODULES_LMALTNAME hide1/5.0&as|hide1/latest]

set ans2 [list]
lappend ans2 [list setpath LOADEDMODULES hide1/5.0]
lappend ans2 [list setpath _LMFILES_ $mp/hide1/5.0]
lappend ans2 [list setpath MODULES_LMALTNAME hide1/5.0&as|hide1/latest]
lappend ans2 [list ERR]

set tserr1 $mp:\nhide1/5.0
set tserr3 [err_accessdenied hide1/5.0]

if {[cmpversion $tclsh_version 8.5] == -1} {

# test --before option
setenv_var TESTSUITE_FORBID_BEFORE_SET1 1
set custom "    invoked from within
\"if \{\[info exists env(TESTSUITE_FORBID_BEFORE_SET1)\]\} \{
    module-forbid --before 2020-01-01 hide1/5.0
\}\""
set linenum 339
set tserr3 [escre [msg_moderr {Option '--before' not supported on Tcl<8.5} {module-forbid --before 2020-01-01 hide1/5.0} $mp/hide1/.modulerc $linenum {  } {} {} $custom]]

testouterr_cmd_re sh {load hide1@5:} $ans2 $tserr3
testouterr_cmd sh {avail -t hide1@5:} OK $mp:\nhide1/5.0
unsetenv_var TESTSUITE_FORBID_BEFORE_SET1

# test --after option
setenv_var TESTSUITE_FORBID_AFTER_SET5 1
set custom "    invoked from within
\"if \{\[info exists env(TESTSUITE_FORBID_AFTER_SET5)\]\} \{
    module-forbid --after 2029-09-12T13:14 hide1/5.0
\}\""
set linenum 347
set tserr3 [escre [msg_moderr {Option '--after' not supported on Tcl<8.5} {module-forbid --after 2029-09-12T13:14 hide1/5.0} $mp/hide1/.modulerc $linenum {  } {} {} $custom]]

testouterr_cmd_re sh {load hide1@5:} $ans2 $tserr3
testouterr_cmd sh {avail -t hide1@5:} OK $mp:\nhide1/5.0
unsetenv_var TESTSUITE_FORBID_AFTER_SET5


} else {

# test --before option
setenv_var TESTSUITE_FORBID_BEFORE_SET1 1
testouterr_cmd sh {load hide1@5:} $ans {}
testouterr_cmd sh {avail -t hide1@5:} OK $tserr1
unsetenv_var TESTSUITE_FORBID_BEFORE_SET1

setenv_var TESTSUITE_FORBID_BEFORE_SET2 1
testouterr_cmd sh {load hide1/5.0} ERR $tserr3
testouterr_cmd sh {avail -t hide1@5:} OK {}
unsetenv_var TESTSUITE_FORBID_BEFORE_SET2


# test --after option
setenv_var TESTSUITE_FORBID_AFTER_SET5 1
testouterr_cmd sh {load hide1@5:} $ans {}
unsetenv_var TESTSUITE_FORBID_AFTER_SET5


# test cumulative use of --before and --after options
setenv_var TESTSUITE_FORBID_BEFORE_AFTER_SET3 1
testouterr_cmd sh {load hide1@5:} ERR $tserr3
testouterr_cmd sh {load hide1/5.0} ERR $tserr3
unsetenv_var TESTSUITE_FORBID_BEFORE_AFTER_SET3


set ans [list]
lappend ans [list setpath LOADEDMODULES hide1/5.0]
lappend ans [list setpath _LMFILES_ $mp/hide1/5.0]
lappend ans [list setpath MODULES_LMALTNAME hide1/5.0&as|hide1/latest]
lappend ans [list ERR]

if {[cmpversion $tclsh_version 8.6] == -1} {
    set custom "    invoked from within
\"if \{\[info exists env(TESTSUITE_FORBID_BEFORE_AFTER_ERR_SET2)\]\} \{
    module-forbid --before hide1/5.0 --after
\}\""
    set linenum 356
} else {
    set custom {}
    set linenum 357
}
set tserr3 [msg_moderr {Incorrect --before value 'hide1/5.0' (valid date time format is 'YYYY-MM-DD[THH:MM]')} {module-forbid --before hide1/5.0 --after} $mp/hide1/.modulerc $linenum {  } {} {} $custom]

# test erroneous specification of --before and --after options
setenv_var TESTSUITE_FORBID_BEFORE_AFTER_ERR_SET2 1
testouterr_cmd sh {load hide1@5:} $ans $tserr3
testouterr_cmd sh {avail -t hide1@5:} OK $tserr1
unsetenv_var TESTSUITE_FORBID_BEFORE_AFTER_ERR_SET2

}


#
#  Cleanup
#

reset_test_env
