##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2018/04/05
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, unload, display, test, help, whatis
#   Modulefiles:    allin
#   Sub-Command:    all
#
#   Comment:	%C{
#           Test modulefiles containing all existing commands over all
#            existing mode
#		}C%
#
##############################################################################

# skip tests if external command cannot be found
if {$domainname ne ""} {

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

# ensure auto symbolic versions are not set for these tests
setenv_var MODULES_ADVANCED_VERSION_SPEC 0

# 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 module "allin"
set modulepath "$modpath.allin"
set modulepathre "$modpathre.allin"
set collpath  $env(HOME)/.module
regsub -all {([.+?()])} $collpath {\\\1} collpathre
regsub -all {([.+?()])} $env(TESTSUITEDIR) {\\\1} testsuitedirre

set whatis "$modlin $modulepathre $modlin\n\\s+$module/1: $module"

# fetch info for uname result test
set u_sysname $tcl_platform(os)
set u_release $tcl_platform(osVersion)
set u_machine $tcl_platform(machine)
set u_nodename [exec uname -n]
set u_version [exec uname -v]
set u_domain [exec domainname]
regsub -all {([.+?()])} $u_sysname {\\\1} u_sysnamere
regsub -all {([.+?()])} $u_release {\\\1} u_releasere
regsub -all {([.+?()])} $u_machine {\\\1} u_machinere
regsub -all {([.+?()])} $u_nodename {\\\1} u_nodenamere
regsub -all {([.+?()])} $u_version {\\\1} u_versionre
regsub -all {([.+?()])} $u_domain {\\\1} u_domainre

proc display_stderr_content {modfile content} {
    if {$content ne ""} {
        append content "\n"
    }

    return "$::modlin\n$modfile:\n\n$content$::modlin"
}

proc help_stderr_content {modfile content {procwarn 0}} {
    if {$content ne ""} {
        append content "\n"
    }

    if {$procwarn} {
        append content "WARNING: Unable to find ModulesHelp in $modfile.\n"
    }

    return "$::modlin\nModule Specific Help for $modfile:\n\n$content$::modlin"
}

proc test_stderr_content {modfile content {procwarn 0}} {
    if {$content ne ""} {
        append content "\n"
    }

    if {$procwarn} {
        append content "WARNING: Unable to find ModulesTest in $modfile.\n"
    }

    return "$::modlin\nModule Specific Test for $modfile:\n\n$content$::modlin"
}


# setup specific environment
setenv_path_var MODULEPATH $modulepath

#
# test load mode
#

set ans [list]
lappend ans [list set ts1 ok]
lappend ans [list set tsu ""]
lappend ans [list setpath TSPATH "dir3 dir2"]
lappend ans [list set tsp "dir3 dir2"]
lappend ans [list unset ts2]
lappend ans [list unset tsc]
lappend ans [list set ts3 ok]
lappend ans [list alias foo bar]
lappend ans [list unalias bar]
lappend ans [list xres Ileaf.popup.saveUnder True]
lappend ans [list chdir $testsuitedirre]
lappend ans [list setpath LOADEDMODULES $module/5:$module/1]
lappend ans [list setpath _LMFILES_ $modulepath/$module/5:$modulepath/$module/1]
lappend ans [list setpath MODULES_LMNOTUASKED $module/5]
lappend ans [list setpath MODULES_LMPREREQ $module/1&$module/5]
lappend ans [list setpath MODULES_LMCONFLICT $module/1&$module/4]
lappend ans [list ERR]
set tserr1 "$modulepathre:
$module/1
$module/2
$module/3
$module/4
$module/5

$modlin
$modulepathre/$module/5:

setenv\t\tts3 ok
$modlin
$no_loaded
Named collection list:
default

$modlin
$collpathre/default:

module use --append $modulepathre

$modlin
[test_stderr_content $modulepathre/$module/5 {} 0]
foo"
# if error when testing X11 setup, will get an error not a loaded module
if {[info exists x11_warn]} {
    testouterr_cmd_re "sh" "load $module/1" "ERR" $tserr1\n\n[msg_load $module/1 "WARNING: Unable to find ModulesTest in $modulepathre/$module/5." "$error_msgs: Directory '/path/to/dir' not found" $x11_warn]
} else {
    testouterr_cmd_re "sh" "load $module/1" $ans $tserr1\n\n[msg_top_load $module/1 {} $module/5 {} "WARNING: Unable to find ModulesTest in $modulepathre/$module/5." "$error_msgs: Directory '/path/to/dir' not found"]
}

# satisfy prereq
setenv_loaded_module "$module/5" "$modulepath/$module/5"
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module/5:$module/2"]
lappend ans [list setpath _LMFILES_ "$modulepath/$module/5:$modulepath/$module/2"]
lappend ans [list setpath MODULES_LMPREREQ "$module/2&$module/5"]

set tserr "_UNDEFINED_100[if_implicitdefault 1 0]Tclloadload$module/2$module/2shsh$module$module/5$u_sysnamere$u_nodenamere$u_domainre$u_releasere$u_versionre$u_machinere"

testouterr_cmd_re "sh" "load $module/2" $ans $tserr

# restore environment
unsetenv_loaded_module


testouterr_cmd_re "sh" "load $module/3" "ERR" "$whatis\n\n$whatis\n\n[msg_load $module/3 $err_evalabort]"
testouterr_cmd "sh" "load $module/4" "ERR" [msg_load $module/4 $err_evalabort]


#
# test unload mode
#

setenv_loaded_module [list $module/5 $module/1] [list $modulepath/$module/5 $modulepath/$module/1] [list $module/5]
setenv_path_var MODULES_LMALTNAME $module/1&foo
setenv_path_var MODULES_LMPREREQ $module/1&$module/5
setenv_path_var MODULES_LMCONFLICT $module/1&$module/4
setenv_var ts1 "ok"
setenv_var tsu ""
setenv_var TSPATH "dir3 dir2"
setenv_var TSPATH_modshare "dir3:1:dir2:1"
setenv_var tsp "dir3 dir2"
set ans [list]
lappend ans [list unset ts1]
lappend ans [list unset tsc]
lappend ans [list unset tsu]
lappend ans [list unsetpath TSPATH]
lappend ans [list unset tsp]
lappend ans [list set ts2 ok]
lappend ans [list unset ts3]
lappend ans [list unalias foo]
lappend ans [list unxres Ileaf.popup.saveUnder]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list unsetpath MODULES_LMCONFLICT]
lappend ans [list unsetpath MODULES_LMALTNAME]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
set tserr1 "$modulepathre:
$module/1 <L>
$module/2
$module/3
$module/4
$module/5 <aL>

$modlin
$modulepathre/$module/5:

setenv\t\tts3 ok
$modlin
$cur_loaded
$module/5
$module/1

Named collection list:
default

$modlin
$collpathre/default:

module use --append $modulepathre
module load --notuasked $module[if_implicitdefault {} /5]
module load $module/1

$modlin
[test_stderr_content $modulepathre/$module/5 {} 0]
foo"
# if error when testing X11 setup, will get an error not a loaded module
if {[info exists x11_warn]} {
    testouterr_cmd_re "sh" "unload $module/1" "ERR" $tserr1\n\n[msg_unload $module/1 "WARNING: Unable to find ModulesTest in $modulepathre/$module/5." $x11_warn]
} else {
    testouterr_cmd_re "sh" "unload $module/1" $ans $tserr1\n\n[msg_top_unload $module/1 {} $module/5 {} "WARNING: Unable to find ModulesTest in $modulepathre/$module/5."]
}

# restore environment
unsetenv_var ts1
unsetenv_var tsu
unsetenv_path_var TSPATH
unsetenv_var tsp

unsetenv_path_var MODULES_LMALTNAME
unsetenv_path_var MODULES_LMPREREQ
unsetenv_path_var MODULES_LMCONFLICT
setenv_loaded_module "$module/2" "$modulepath/$module/2"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]

set tserr "_UNDEFINED_100[if_implicitdefault 1 0]Tclunloadunload$module/2$module/2shsh$module$module/2$u_sysnamere$u_nodenamere$u_domainre$u_releasere$u_versionre$u_machinere"

testouterr_cmd_re "sh" "unload $module/2" $ans $tserr

setenv_loaded_module "$module/3" "$modulepath/$module/3"
testouterr_cmd_re "sh" "unload $module/3" "ERR" "$whatis\n\n$whatis\n\n[msg_unload $module/3 $err_evalabort]"

setenv_loaded_module "$module/4" "$modulepath/$module/4"
testouterr_cmd "sh" "unload $module/4" "ERR" [msg_unload $module/4 $err_evalabort]


# restore environment
unsetenv_loaded_module


#
# test display mode
#

set tserr1 "chdir\t\t$testsuitedirre
setenv\t\tts1 ok
unsetenv\tts2
unsetenv\tts2 ok
setenv\t\ttsc ok
unsetenv\ttsc
setenv\t\ttsu {}
append-path\t-d { } TSPATH dir1
append-path\t-d { } TSPATH dir2
prepend-path\t-d { } TSPATH dir3
remove-path\t-d { } TSPATH dir1
setenv\t\ttsp {dir3 dir2}
module\t\taliases
module\t\tavail -t allin
module\t\tdisplay allin/5
module\t\tlist -t
module\t\tsave
module\t\trestore
module\t\tsavelist -t
module\t\tsaveshow
module\t\tsaverm
module\t\tload allin/5
module\t\tsource ./5
module\t\ttest allin/5
module\t\tunload allin/4
module\t\tuse -a /path/to/dir
module\t\tunuse /path/to/dir
module-version\t/3 3.0
module-alias\tfoo $module/1
module-virtual\t/6 5
module-whatis\t$module
set-alias\tfoo bar
unset-alias\tbar
system\t\techo foo
x-resource\tIleaf.popup.saveUnder:True"

testouterr_cmd_re "sh" "display $module/1" "OK" [display_stderr_content "$modulepathre/$module/1" $tserr1]

set tserr "\\\$ts000[if_implicitdefault 1 0]Tcldisplaydisplay$module/2$module/2shsh$module$u_sysnamere$u_nodenamere$u_domainre$u_releasere$u_versionre$u_machinere"
testouterr_cmd_re "sh" "display $module/2" "OK" [display_stderr_content "$modulepathre/$module/2" "prereq\t\t$module/5\n$tserr"]

set tserr "conflict\t$module/5
module\t\tapropos $module
module\t\twhatis $module"
testouterr_cmd_re "sh" "display $module/3" "ERR" [display_stderr_content "$modulepathre/$module/3" $tserr]

set tserr "module\t\tpurge
module\t\treload
module\t\tswap $module/1 $module/5"
testouterr_cmd_re "sh" "display $module/4" "ERR" [display_stderr_content "$modulepathre/$module/4" $tserr]


#
# test help mode
#

testouterr_cmd_re "sh" "help $module/1" "OK" [help_stderr_content "$modulepathre/$module/1" "" 1]

set tserr "_UNDEFINED_000[if_implicitdefault 1 0]Tclhelphelp$module/2$module/2shsh$module$u_sysnamere$u_nodenamere$u_domainre$u_releasere$u_versionre$u_machinere"
testouterr_cmd_re "sh" "help $module/2" "OK" [help_stderr_content "$modulepathre/$module/2" "$tserr" 0]
testouterr_cmd_re "sh" "help $module/3" "ERR" [help_stderr_content "$modulepathre/$module/3" "" 0]
testouterr_cmd_re "sh" "help $module/4" "ERR" [help_stderr_content "$modulepathre/$module/4" "" 0]


#
# test test mode
#

testouterr_cmd_re "sh" "test $module/1" "OK" [test_stderr_content "$modulepathre/$module/1" "" 1]
set tserr "_UNDEFINED_000[if_implicitdefault 1 0]Tcltesttest$module/2$module/2shsh$module$u_sysnamere$u_nodenamere$u_domainre$u_releasere$u_versionre$u_machinere"
testouterr_cmd_re "sh" "test $module/2" "OK" [test_stderr_content "$modulepathre/$module/2" "$tserr" 0]
testouterr_cmd_re "sh" "test $module/3" "ERR" [test_stderr_content "$modulepathre/$module/3" "" 0]
testouterr_cmd_re "sh" "test $module/4" "ERR" [test_stderr_content "$modulepathre/$module/4" "" 0]


#
# test whatis mode
#

testouterr_cmd_re "sh" "whatis $module" "ERR" $whatis


#
#  Cleanup
#

# restore MODULEPATH
if { $verbose > 0 } {
    send_user "\tDelete $collpath\n"
}
file delete $collpath

reset_test_env

# remove procs
rename display_stderr_content {}
rename help_stderr_content {}
rename test_stderr_content {}

}
