##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2020/08/14
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		avail, load, list, unload, whatis, purge, reload,
#                   is-avail
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Disable forbidding with '--not-user' or '--not-group' 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

# ensure default tag abbreviation is defined
setenv_var MODULES_TAG_ABBREV {auto-loaded=aL:loaded=L:hidden=H:hidden-loaded=H:forbidden=F:nearly-forbidden=nF}

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 <F>"
set tserr2 $err_path'hide1@5:'
set tserr3 [err_accessdenied hide1/5.0]

# test --not-user option
setenv_var TESTSUITE_FORBID_ALLOW_USER_SET1 1
testouterr_cmd sh {load hide1@5:} $ans {}
unsetenv_var TESTSUITE_FORBID_ALLOW_USER_SET1

setenv_var TESTSUITE_FORBID_ALLOW_USER_SET2 1
testouterr_cmd sh {load hide1@5:} ERR $tserr3
testouterr_cmd sh {load hide1/5.0} ERR $tserr3
testouterr_cmd sh {avail -t hide1@5:} OK $tserr1
# fake Windows platform if expected siteconfig file is installed
if {[siteconfig_isStderrTty]} {
    setenv_var TESTSUITE_ENABLE_SITECONFIG_WINDOWS_PLATFORM 1

    if {[cmpversion $tclsh_version 8.6] == -1} {
        set custom "    invoked from within
\"if \{\[info exists env(TESTSUITE_FORBID_ALLOW_USER_SET2)\]\} \{
    module-forbid --not-user unknown hide1/5.0
\}\""
        set linenum 224
    } else {
        set custom {}
        set linenum 225
    }
    set tserr4 [escre [msg_moderr {Option '--not-user' not supported on Windows platform} {module-forbid --not-user unknown hide1/5.0} $mp/hide1/.modulerc $linenum {  } {} {} $custom]]

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


# test --not-group option
set tserr1 $mp:\nhide1/5.0
setenv_var TESTSUITE_FORBID_ALLOW_GROUP_SET5 1
testouterr_cmd sh {load hide1@5:} $ans {}
testouterr_cmd sh {avail -t hide1@5:} OK $tserr1
unsetenv_var TESTSUITE_FORBID_ALLOW_GROUP_SET5


# test cumulative use of --not-user and --not-group options
setenv_var TESTSUITE_FORBID_ALLOW_USER_GROUP_SET1 1
testouterr_cmd sh {load hide1@5.0} $ans {}
unsetenv_var TESTSUITE_FORBID_ALLOW_USER_GROUP_SET1


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_ALLOW_USER_GROUP_ERR_SET2)\]\} \{
    module-forbid --not-group hide1/5.0 --not-user
\}\""
    set linenum 236
} else {
    set custom {}
    set linenum 237
}
set tserr3 [msg_moderr {Missing value for '--not-user' option} {module-forbid --not-group hide1/5.0 --not-user} $mp/hide1/.modulerc $linenum {  } {} {} $custom]

# test erroneous specification of --not-user and --not-group options
setenv_var TESTSUITE_FORBID_ALLOW_USER_GROUP_ERR_SET2 1
testouterr_cmd sh {load hide1@5:} $ans $tserr3
testouterr_cmd sh {avail -t hide1@5:} OK $tserr1
unsetenv_var TESTSUITE_FORBID_ALLOW_USER_GROUP_ERR_SET2


#
#  Cleanup
#

reset_test_env
