##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2018/04/01
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, unload, display, test, switch, source, whatis,
#                   apropos, purge, reload, aliases, savelist, save,
#                   restore, saveshow, saverm, use, unuse, avail 
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Test module commands called from modulefile with an empty string
#           argument called
#		}C%
#
##############################################################################

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

set module "modemptyarg"
set modulepath "$modpath.2"
set modulepathre "$modpathre.2"

# setup specific environment
setenv_path_var MODULEPATH $modulepath

# no test of module commands that can only be called from the top level
# (command-line). these commands are already checked in 151-module-onlytop.exp

#
# test load mode
#

# wrong arg number if command does not accept any argument
foreach cmd [list list purge reload aliases savelist] {
    testouterr_cmd "sh" "load $module/$cmd" "ERR" [msg_load $module/$cmd [msg_moderr "Unexpected number of args for '$cmd' command" "module $cmd {}" $modulepath/$module/$cmd 27]]
}

# on most module commands, empty arg leads to error
# test both empty names {} and "" on same module command
if {$install_autohandling eq {y}} {
testouterr_cmd "sh" "load $module/load" "ERR" [msg_load $module/load $err_emptymodname [err_reqlo {}]]
} else {
testouterr_cmd "sh" "load $module/load" "ERR" [msg_load $module/load $err_emptymodname $err_emptymodname [err_reqlo {}]]
}
testouterr_cmd "sh" "load $module/unload" "ERR" [msg_load $module/unload $err_emptymodname $err_emptymodname]

set ans [list]
lappend ans [list setpath LOADEDMODULES "$module/try-load"]
lappend ans [list setpath _LMFILES_ "$modulepath/$module/try-load"]
testouterr_cmd "sh" "load $module/try-load" $ans {}

# for some module commands, error is not fatal and modulefile is loaded
foreach cmd [list display test] {
    set ans [list]
    lappend ans [list setpath LOADEDMODULES "$module/$cmd"]
    lappend ans [list setpath _LMFILES_ "$modulepath/$module/$cmd"]
    lappend ans [list ERR]
    testouterr_cmd "sh" "load $module/$cmd" $ans [msg_load $module/$cmd $err_emptymodname]
}
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module/swap"]
lappend ans [list setpath _LMFILES_ "$modulepath/$module/swap"]
lappend ans [list ERR]
testouterr_cmd "sh" "load $module/swap" $ans "[msg_switch {} barunk $err_emptymodname]"
# switch: an empty second arg leads to one name rule
set ans [list]
lappend ans [list ERR]
testouterr_cmd_re "sh" "load $module/swap2" $ans "[msg_switch barunk barunk "$err_path'barunk'"]\n\n[msg_load $module/swap2 [err_reqlo barunk]]"
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module/swap3"]
lappend ans [list setpath _LMFILES_ "$modulepath/$module/swap3"]
lappend ans [list ERR]
testouterr_cmd "sh" "load $module/swap3" $ans "[msg_switch {} {} $err_emptymodname]"
foreach cmd [list use unuse] {
    set ans [list]
    lappend ans [list setpath LOADEDMODULES "$module/$cmd"]
    lappend ans [list setpath _LMFILES_ "$modulepath/$module/$cmd"]
    lappend ans [list ERR]
    testouterr_cmd "sh" "load $module/$cmd" $ans [msg_load $module/$cmd $err_emptydirname]
}

# or error is reported as modulefile error rather global error
foreach cmd [list save restore saveshow saverm] {
    if {[cmpversion $tclsh_version 8.5] == -1} {
        set custom [msg_moderr $msg_collnameempty "<EXM>module $cmd {}" $modulepath/$module/$cmd 27]
        testouterr_cmd_re "sh" "load $module/$cmd" "ERR" [escre [msg_load $module/$cmd $custom]]
    } else {
        testouterr_cmd "sh" "load $module/$cmd" "ERR" [msg_load $module/$cmd [msg_moderr $msg_collnameempty "module $cmd {}" $modulepath/$module/$cmd 27]]
    }
}
if {[cmpversion $tclsh_version 8.5] == -1} {
    set custom [msg_moderr $msg_filenameempty "<EXM>module source {}" $modulepath/$module/source 27]
    testouterr_cmd_re "sh" "load $module/source" "ERR" [escre [msg_load $module/source $custom]]
} else {
    testouterr_cmd "sh" "load $module/source" "ERR" [msg_load $module/source [msg_moderr $msg_filenameempty "module source {}" $modulepath/$module/source 27]]
}

# empty string currently understood like wildcard
foreach cmd [list whatis apropos] {
    set ans [list]
    lappend ans [list set _LMFILES__modshare "$modulepath/$module/$cmd:1"]
    lappend ans [list set LOADEDMODULES_modshare "$module/$cmd:1"]
    lappend ans [list set _LMFILES_ "$modulepath/$module/$cmd"]
    lappend ans [list set LOADEDMODULES "$module/$cmd"]
    # other evaluated modulefiles lead to an error result on stdout
    lappend ans [list ERR]
    testouterr_cmd_re "sh" "load $module/$cmd" $ans {.+}
}
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module/avail"]
lappend ans [list setpath _LMFILES_ "$modulepath/$module/avail"]
testouterr_cmd_re "sh" "load $module/avail" $ans ".+"


#
# test unload mode
#

# wrong arg number if command does not accept any argument
foreach cmd [list list purge reload aliases savelist] {
    setenv_loaded_module "$module/$cmd" "$modulepath/$module/$cmd"
    testouterr_cmd "sh" "unload $module/$cmd" "ERR" [msg_unload $module/$cmd [msg_moderr "Unexpected number of args for '$cmd' command" "module $cmd {}" $modulepath/$module/$cmd 27]]
}

# on most module commands, empty arg leads to error
foreach cmd [list load try-load unload] {
    setenv_loaded_module "$module/$cmd" "$modulepath/$module/$cmd"
    set ans [list]
    lappend ans [list unsetpath LOADEDMODULES]
    lappend ans [list unsetpath _LMFILES_]
    testouterr_cmd "sh" "unload $module/$cmd" $ans ""
}

# for some module commands, error is not fatal and modulefile is unloaded
foreach cmd [list display test] {
    setenv_loaded_module "$module/$cmd" "$modulepath/$module/$cmd"
    set ans [list]
    lappend ans [list unsetpath LOADEDMODULES]
    lappend ans [list unsetpath _LMFILES_]
    lappend ans [list ERR]
    testouterr_cmd "sh" "unload $module/$cmd" $ans [msg_unload $module/$cmd $err_emptymodname]
}
setenv_loaded_module "$module/swap" "$modulepath/$module/swap"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd "sh" "unload $module/swap" $ans {}
# switch: an empty second arg leads to one name rule
setenv_loaded_module "$module/swap2" "$modulepath/$module/swap2"
testouterr_cmd_re "sh" "unload $module/swap2" $ans {}
setenv_loaded_module "$module/swap3" "$modulepath/$module/swap3"
testouterr_cmd_re "sh" "unload $module/swap3" $ans {}
lappend ans [list ERR]
foreach cmd [list use unuse] {
    setenv_loaded_module "$module/$cmd" "$modulepath/$module/$cmd"
    testouterr_cmd "sh" "unload $module/$cmd" $ans [msg_unload $module/$cmd $err_emptydirname]
}

# or error is reported as modulefile error rather global error
foreach cmd [list save restore saveshow saverm] {
    setenv_loaded_module "$module/$cmd" "$modulepath/$module/$cmd"
    if {[cmpversion $tclsh_version 8.5] == -1} {
        set custom [msg_moderr $msg_collnameempty "<EXM>module $cmd {}" $modulepath/$module/$cmd 27]
        testouterr_cmd_re "sh" "unload $module/$cmd" "ERR" [escre [msg_unload $module/$cmd $custom]]
    } else {
        testouterr_cmd "sh" "unload $module/$cmd" "ERR" [msg_unload $module/$cmd [msg_moderr $msg_collnameempty "module $cmd {}" $modulepath/$module/$cmd 27]]
    }
}
setenv_loaded_module "$module/source" "$modulepath/$module/source"
set env(_LMFILES_) "$modulepath/$module/source"
if {[cmpversion $tclsh_version 8.5] == -1} {
    set custom [msg_moderr $msg_filenameempty "<EXM>module source {}" $modulepath/$module/source 27]
    testouterr_cmd_re "sh" "unload $module/source" "ERR" [escre [msg_unload $module/source $custom]]
} else {
    testouterr_cmd "sh" "unload $module/source" "ERR" [msg_unload $module/source [msg_moderr $msg_filenameempty "module source {}" $modulepath/$module/source 27]]
}

# empty string currently understood like wildcard
foreach cmd [list whatis apropos] {
    setenv_loaded_module "$module/$cmd" "$modulepath/$module/$cmd"
    set ans [list]
    lappend ans [list unset _LMFILES__modshare]
    lappend ans [list unset LOADEDMODULES_modshare]
    lappend ans [list unset _LMFILES_]
    lappend ans [list unset LOADEDMODULES]
    # other evaluated modulefiles lead to an error result on stdout
    lappend ans [list ERR]
    testouterr_cmd_re "sh" "unload $module/$cmd" $ans {.+}
}
setenv_loaded_module "$module/avail" "$modulepath/$module/avail"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd_re "sh" "unload $module/avail" $ans ".+"

# restore environment
unsetenv_loaded_module


#
# test display mode
#

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

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


# on all module commands, command line with empty arg is printed
foreach cmd [list load try-load unload] {
    # test both empty names {} and "" on same module command
    testouterr_cmd_re "sh" "display $module/$cmd" "OK" [display_stderr_content "$modulepathre/$module/$cmd" "module\t\t$cmd {} {}"]
}
foreach cmd [list use unuse source list purge reload aliases savelist display test save restore saveshow saverm whatis apropos avail] {
    testouterr_cmd_re "sh" "display $module/$cmd" "OK" [display_stderr_content "$modulepathre/$module/$cmd" "module\t\t$cmd {}"]
}
testouterr_cmd_re "sh" "display $module/swap" "OK" [display_stderr_content "$modulepathre/$module/swap" "module\t\tswap {} barunk"]
testouterr_cmd_re "sh" "display $module/swap2" "OK" [display_stderr_content "$modulepathre/$module/swap2" "module\t\tswap barunk {}"]


#
# test help mode
#

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

# on all module commands, command line is ignored
foreach cmd [list load try-load unload use unuse source list purge reload aliases savelist display test swap swap2 save restore saveshow saverm whatis apropos avail] {
    testouterr_cmd_re "sh" "help $module/$cmd" "OK" [help_stderr_content "$modulepathre/$module/$cmd" "" 1]
}


#
# test test mode
#

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

# on all module commands, command line is ignored
foreach cmd [list load try-load unload use unuse source list purge reload aliases savelist display test swap swap2 save restore saveshow saverm whatis apropos avail] {
    testouterr_cmd_re "sh" "test $module/$cmd" "OK" [test_stderr_content "$modulepathre/$module/$cmd" "" 1]
}


#
# test whatis mode
#

testouterr_cmd "sh" "whatis $module" "OK" ""


#
#  Cleanup
#

reset_test_env

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