##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2020/10/26
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Modulefiles:    tag
#   Sub-Command:    load, unload, list
#
#   Comment:	%C{
#           Test tag mechanism
#		}C%
#
##############################################################################

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

# setup specific environment
setenv_path_var MODULEPATH $mp

# ensure avail tests have implicit default enabled
setenv_var MODULES_IMPLICIT_DEFAULT 1

# ensure we get nearly-forbidden notices
setenv_var MODULES_NEARLY_FORBIDDEN_DAYS 2

# 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}

#
# primary tests with 'nearly-forbidden' tag
#

# skip tests on tcl versions where --after option of module-forbid is not supported
if {[cmpversion $tclsh_version 8.5] > -1} {

#
# load tests
#

set ans [list]
lappend ans [list setpath LOADEDMODULES tag/4.0]
lappend ans [list setpath _LMFILES_ $mp/tag/4.0]
lappend ans [list setpath MODULES_LMTAG tag/4.0&nearly-forbidden]
set tomorrow [clock format [expr {[clock seconds]+86400}] -format %Y-%m-%d]
set tserr [msg_load tag/4.0 [err_accessnearlydenied $tomorrow]]
testouterr_cmd sh {load tag/4.0} $ans $tserr

# with empty element in variable
setenv_path_var MODULES_LMTAG {}
set ans [list]
lappend ans [list setpath LOADEDMODULES tag/4.0]
lappend ans [list setpath _LMFILES_ $mp/tag/4.0]
lappend ans [list setpath MODULES_LMTAG :tag/4.0&nearly-forbidden]
testouterr_cmd_re sh {load tag/4.0} $ans $tserr

setenv_loaded_module [list tag/2.0] [list $mp/tag/1.0]
setenv_path_var MODULES_LMTAG tag/2.0&forbidden
set ans [list]
lappend ans [list setpath LOADEDMODULES tag/2.0:tag/4.0]
lappend ans [list setpath _LMFILES_ $mp/tag/1.0:$mp/tag/4.0]
lappend ans [list setpath MODULES_LMTAG tag/2.0&forbidden:tag/4.0&nearly-forbidden]
testouterr_cmd_re sh {load tag/4.0} $ans $tserr


#
# unload tests
#

setenv_loaded_module [list tag/4.0] [list $mp/tag/4.0]
setenv_path_var MODULES_LMTAG tag/4.0&nearly-forbidden
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMTAG]
testouterr_cmd sh {unload tag/4.0} $ans {}

setenv_path_var MODULES_LMTAG tag/4.0&nearly-forbidden&othertag
testouterr_cmd sh {unload tag/4.0} $ans {}

setenv_loaded_module [list tag/4.0 tag/2.0] [list $mp/tag/4.0 $mp/tag/1.0]
setenv_path_var MODULES_LMTAG tag/4.0&nearly-forbidden&othertag:tag/2.0&forbidden
set ans [list]
lappend ans [list setpath LOADEDMODULES tag/2.0]
lappend ans [list setpath _LMFILES_ $mp/tag/1.0]
lappend ans [list setpath MODULES_LMTAG tag/2.0&forbidden]
testouterr_cmd sh {unload tag/4.0} $ans {}

# with empty element in variable
setenv_loaded_module [list tag/4.0] [list $mp/tag/4.0]
setenv_path_var MODULES_LMTAG :tag/4.0&nearly-forbidden
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list setpath MODULES_LMTAG {}]
testouterr_cmd sh {unload tag/4.0} $ans {}


#
# list tests
#

setenv_loaded_module [list tag/4.0 tag/2.0 tag/5.0] [list $mp/tag/4.0 $mp/tag/1.0 $mp/tag/5.0]
setenv_path_var MODULES_LMTAG tag/4.0&nearly-forbidden&othertag:tag/2.0&forbidden:tag/5.0&hidden-loaded
testouterr_cmd sh {list} OK "$cur_loaded
 1) tag/4.0 <nF:othertag>   2) tag/2.0 <F>  

Key:
<module-tag>  <nF>=nearly-forbidden  <F>=forbidden  "

# terse mode just output modulename and header
testouterr_cmd sh {list -t} OK "$cur_loaded
tag/4.0
tag/2.0"

# no change on long output not to break the 80-column limit
set tserr "- Package $modlin.- Versions $modlin.- Last mod. $modlin
$cur_loaded
tag/4.0\\s+\[0-9\/]{10} \[0-9:]{8}
tag/2.0\\s+\[0-9\/]{10} \[0-9:]{8}"
testouterr_cmd_re sh {list -l} OK $tserr

set tserr "{\"tag/4.0\": { \"name\": \"tag/4.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"nearly-forbidden\", \"othertag\" \], \"pathname\": \"$mp/tag/4.0\"},
\"tag/2.0\": { \"name\": \"tag/2.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"forbidden\" \], \"pathname\": \"$mp/tag/1.0\"}}"
testouterr_cmd sh {list -j} OK $tserr

# display hidden entries
testouterr_cmd sh {list -a} OK "$cur_loaded
 1) tag/4.0 <nF:othertag>   2) tag/2.0 <F>   3) tag/5.0 <H>  

Key:
<module-tag>  <H>=hidden-loaded  <nF>=nearly-forbidden  <F>=forbidden  "

set tserr "{\"tag/4.0\": { \"name\": \"tag/4.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"nearly-forbidden\", \"othertag\" \], \"pathname\": \"$mp/tag/4.0\"},
\"tag/2.0\": { \"name\": \"tag/2.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"forbidden\" \], \"pathname\": \"$mp/tag/1.0\"},
\"tag/5.0\": { \"name\": \"tag/5.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"hidden-loaded\" \], \"pathname\": \"$mp/tag/5.0\"}}"
testouterr_cmd sh {list -j --all} OK $tserr

unsetenv_loaded_module
unsetenv_path_var MODULES_LMTAG
unset tomorrow

}

#
# 'hidden' tag test is covered by 20/11*
# this tag is not exported in MODULES_LMTAG so cannot be reported on 'list'
#

#
# 'forbidden' tag test is covered by 20/115
# by definition this tag should not be seen on 'list' output as module cannot be loaded
#

#
# test loaded/auto-loaded tags
#

set ans [list]
lappend ans [list setpath LOADEDMODULES foo/2.0:impreq/load:impreq/prereq]
lappend ans [list setpath _LMFILES_ $mp/foo/2.0:$mp/impreq/load:$mp/impreq/prereq]
lappend ans [list setpath MODULES_LMPREREQ impreq/load&foo:impreq/prereq&impreq/load]
lappend ans [list setpath MODULES_LMNOTUASKED foo/2.0:impreq/load]
if {$install_advversspec eq {y}} {
    lappend ans [list setpath MODULES_LMALTNAME foo/2.0&as|foo/default&as|foo/latest]
}
testouterr_cmd_re sh {load --auto impreq/prereq} $ans [msg_top_load impreq/prereq {} [list foo/2.0 impreq/load] {}]

setenv_loaded_module [list foo/2.0 impreq/load impreq/prereq] [list $mp/foo/2.0 $mp/impreq/load $mp/impreq/prereq] [list foo/2.0 impreq/load]
setenv_path_var MODULES_LMPREREQ impreq/load&foo:impreq/prereq&impreq/load
setenv_path_var MODULES_LMNOTUASKED foo/2.0:impreq/load

testouterr_cmd sh {list} OK "$cur_loaded
 1) foo/2.0 <aL>   2) impreq/load <aL>   3) impreq/prereq  

Key:
<module-tag>  <aL>=auto-loaded  "

set tserr "{\"foo/2.0\": { \"name\": \"foo/2.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"auto-loaded\" \], \"pathname\": \"$mp/foo/2.0\"},
\"impreq/load\": { \"name\": \"impreq/load\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"auto-loaded\" \], \"pathname\": \"$mp/impreq/load\"},
\"impreq/prereq\": { \"name\": \"impreq/prereq\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[\], \"pathname\": \"$mp/impreq/prereq\"}}"
testouterr_cmd sh {list -j} OK $tserr

set tserr "$modlin $mpre $modlin
foo/2.0 <aL>  

Key:
<module-tag>  <aL>=auto-loaded  "
testouterr_cmd_re sh {avail foo/2.0} OK $tserr

set tserr "$modlin $mpre $modlin
impreq/load <aL>  .*"
testouterr_cmd_re sh {avail impreq/load} OK $tserr

set tserr "$modlin $mpre $modlin
impreq/prereq <L>  

Key:
<module-tag>  <L>=loaded  "
testouterr_cmd_re sh {avail impreq/prereq} OK $tserr

# set an additional tag
setenv_path_var MODULES_LMTAG foo/2.0&othertag

set test_cols 120
if {![info exists term_cols]} {
    # skip tests if current terminal witdh is unknown
    send_user "\tskipping terminal width-specific tests\n"
# set a specific terminal width
} elseif {[catch {exec stty cols $test_cols}] || [getTtyCols] ne $test_cols} {
    send_user "\tskipping terminal width-specific tests, cannot set width\n"
} else {

testouterr_cmd sh {list} OK "$cur_loaded
 1) foo/2.0 <aL:othertag>   2) impreq/load <aL>   3) impreq/prereq  

Key:
<module-tag>  <aL>=auto-loaded  "

# restore terminal width
exec stty cols $term_cols

}

set tserr "{\"foo/2.0\": { \"name\": \"foo/2.0\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"auto-loaded\", \"othertag\" \], \"pathname\": \"$mp/foo/2.0\"},
\"impreq/load\": { \"name\": \"impreq/load\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[ \"auto-loaded\" \], \"pathname\": \"$mp/impreq/load\"},
\"impreq/prereq\": { \"name\": \"impreq/prereq\", \"type\": \"modulefile\", \"variants\": {}, \"symbols\": \[\], \"tags\": \[\], \"pathname\": \"$mp/impreq/prereq\"}}"
testouterr_cmd sh {list -j} OK $tserr

set tserr "$modlin $mpre $modlin
foo/2.0 <aL:othertag>  

Key:
<module-tag>  <aL>=auto-loaded  "
testouterr_cmd_re sh {avail foo/2.0} OK $tserr

unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ
unsetenv_path_var MODULES_LMNOTUASKED
unsetenv_path_var MODULES_LMTAG


#
#  Cleanup
#

reset_test_env
