##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2018/04/16
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, unload, display, test, help, whatis
#   Modulefiles:    allenvchg
#   Sub-Command:    append-path, prepend-path, setenv, remove-path,
#                   unsetenv, getenv
#
#   Comment:	%C{
#           Test all environment variable change modulefile commands against
#           the different modes
#		}C%
#
##############################################################################

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

set modulepath "$modpath.allin"
set modulepathre "$modpathre.allin"
set module "allenvchg"
set modulefile "$modulepath/$module"
set modulefilere "$modulepathre/$module"


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

    if {$premodlin} {
        set header "$::modlin\n"
    } else {
        set header ""
    }

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

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

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

    if {$premodlin} {
        set header "$::modlin\n"
    } else {
        set header ""
    }

    return "${header}Module Specific Help for $modfile:\n\n$content$::modlin"
}

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

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

    if {$premodlin} {
        set header "$::modlin\n"
    } else {
        set header ""
    }

    return "${header}Module Specific Test for $modfile:\n\n$content$::modlin"
}


# setup specific environment
setenv_path_var MODULEPATH $modulepath

#
# test load mode
#

set tsall ",dir2,dir2:dir1,,_UNDEFINED_altered,dir2,_UNDEFINED_altered,ok8,ok9"

set ans [list]
lappend ans [list unset ts1]
lappend ans [list set ts2 "dir2"]
lappend ans [list set ts3 "dir2:dir1"]
lappend ans [list set ts4 ""]
lappend ans [list setpath TSPATH dir2]
lappend ans [list set ts5 "_UNDEFINED_altered"]
lappend ans [list set ts6 "dir2"]
lappend ans [list set ts7 "_UNDEFINED_altered"]
lappend ans [list set ts8 "ok8"]
lappend ans [list set ts9 "ok9"]
lappend ans [list setpath TSPATH2 ok8]
lappend ans [list setpath TSPATH3 "ok8:ok9"]
lappend ans [list setpath LOADEDMODULES "$module/1:$module/2"]
lappend ans [list setpath _LMFILES_ "$modulefile/1:$modulefile/2"]
lappend ans [list set tsall $tsall]
# env changes spread to next loaded modulefiles
testouterr_cmd_re "sh" "load $module/1 $module/2" $ans ""

# test with value set to variable prior to load
setenv_var ts2 "dir2"
setenv_var ts3 "dir2:dir1"
setenv_var ts4 ""
setenv_path_var TSPATH "dir2"
setenv_var ts5 "_UNDEFINED_altered"
setenv_var ts6 "dir2"
setenv_var ts7 "_UNDEFINED_altered"
setenv_var ts8 "ok8"
setenv_var ts9 "ok9"
setenv_path_var TSPATH2 "ok8"
setenv_path_var TSPATH3 "ok8:ok9"
setenv_var tsall $tsall

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


#
# test unload mode
#

# test with module loaded
setenv_loaded_module [list "$module/1" "$module/2"] [list "$modulefile/1" "$modulefile/2"]

set ans [list]
lappend ans [list unset TSPATH2_modshare]
lappend ans [list unset ts1]
lappend ans [list unset ts2]
lappend ans [list unset _LMFILES_]
lappend ans [list unset LOADEDMODULES]
lappend ans [list unset ts3]
lappend ans [list unset ts4]
lappend ans [list unset TSPATH]
lappend ans [list unset ts5]
lappend ans [list unset ts6]
lappend ans [list unset TSPATH2]
lappend ans [list unset ts7]
lappend ans [list set TSPATH3 ok8]
lappend ans [list unset ts8]
lappend ans [list unset tsall]
lappend ans [list unset ts9]
lappend ans [list set TSPATH3_modshare "ok8:1"]
lappend ans [list unset _LMFILES__modshare]
lappend ans [list unset LOADEDMODULES_modshare]
lappend ans [list unset TSPATH_modshare]
testouterr_cmd "sh" "unload $module/1 $module/2" $ans ""

# test with module loaded but vanished env vars
unsetenv_var ts2 
unsetenv_var ts3
unsetenv_var ts4
unsetenv_path_var TSPATH
unsetenv_var ts5
unsetenv_var ts6
unsetenv_var ts7
unsetenv_var ts8
unsetenv_var ts9
unsetenv_path_var TSPATH2
unsetenv_path_var TSPATH3
unsetenv_var tsall

set ans [list]
lappend ans [list unset ts1]
lappend ans [list unset ts2]
lappend ans [list unset _LMFILES_]
lappend ans [list unset LOADEDMODULES]
lappend ans [list unset ts3]
lappend ans [list unset ts4]
lappend ans [list unset TSPATH]
lappend ans [list unset ts5]
lappend ans [list unset ts6]
lappend ans [list unset ts7]
lappend ans [list unset TSPATH2]
lappend ans [list unset TSPATH3]
lappend ans [list unset ts8]
lappend ans [list unset tsall]
lappend ans [list unset ts9]
lappend ans [list unset _LMFILES__modshare]
lappend ans [list unset LOADEDMODULES_modshare]
testouterr_cmd "sh" "unload $module/1 $module/2" $ans ""

# restore environment
unsetenv_loaded_module


#
# test display mode
#

# test without module loaded
set tsdisp "append-path\tTSPATH dir1
prepend-path\tTSPATH dir2
setenv\t\tts1 dir2:dir1
remove-path\tTSPATH dir1
setenv\t\tts2 dir2
setenv\t\tts3 dir2:dir1
unsetenv\tts1
setenv\t\tts4 {}
setenv\t\tts5 \\\$ts1
remove-path\tTSPATH dir1
setenv\t\tts6 dir2
setenv\t\tts7 \\\$ts5
setenv\t\tts8 ok8
setenv\t\tts9 ok9
append-path\tTSPATH2 ok8
append-path\tTSPATH3 ok8 \\\$ts9
module-whatis\t,dir2,dir2:dir1,,\\\$ts1altered,dir2,\\\$ts5,ok8,ok9"
set tserr [display_stderr_content "$modulefilere/1" $tsdisp]
set tsdisp "setenv\t\ttsall ,dir2,dir2:dir1,,\\\$ts1altered,dir2,\\\$ts5,ok8,ok9
module-whatis\t,dir2,dir2:dir1,,\\\$ts1altered,dir2,\\\$ts5,ok8,ok9"
append tserr "\n[display_stderr_content $modulefilere/2 $tsdisp 0]"
testouterr_cmd_re "sh" "display $module/1 $module/2" "" $tserr

# test with module loaded
setenv_loaded_module [list "$module/1" "$module/2"] [list "$modulefile/1" "$modulefile/2"]
setenv_var ts2 "dir2"
setenv_var ts3 "dir2:dir1"
setenv_var ts4 ""
setenv_path_var TSPATH "dir2"
setenv_var ts5 "_UNDEFINED_altered"
setenv_var ts6 "dir2"
setenv_var ts7 "_UNDEFINED_altered"
setenv_var ts8 "ok8"
setenv_var ts9 "ok9"
setenv_path_var TSPATH2 "ok8"
setenv_path_var TSPATH3 "ok8:ok9"
setenv_var tsall $tsall

testouterr_cmd_re "sh" "display $module/1 $module/2" "" $tserr

# restore environment
unsetenv_loaded_module
unsetenv_var ts2 
unsetenv_var ts3
unsetenv_var ts4
unsetenv_path_var TSPATH
unsetenv_var ts5
unsetenv_var ts6
unsetenv_var ts7
unsetenv_var ts8
unsetenv_var ts9
unsetenv_path_var TSPATH2
unsetenv_path_var TSPATH3
unsetenv_var tsall


#
# test help mode
#

# test without module loaded
set tserr [help_stderr_content "$modulefilere/1" $tsall]
append tserr "\n[help_stderr_content $modulefilere/2 $tsall 0 0]"
testouterr_cmd_re "sh" "help $module/1 $module/2" "" $tserr

# test with module loaded
setenv_loaded_module [list "$module/1" "$module/2"] [list "$modulefile/1" "$modulefile/2"]
setenv_var ts2 "dir2"
setenv_var ts3 "dir2:dir1"
setenv_var ts4 ""
setenv_path_var TSPATH "dir2"
setenv_var ts5 "_UNDEFINED_altered"
setenv_var ts6 "dir2"
setenv_var ts7 "_UNDEFINED_altered"
setenv_var ts8 "ok8"
setenv_var ts9 "ok9"
setenv_path_var TSPATH2 "ok8"
setenv_path_var TSPATH3 "ok8:ok9"
setenv_var tsall $tsall

testouterr_cmd_re "sh" "help $module/1 $module/2" "" $tserr

# restore environment
unsetenv_loaded_module
unsetenv_var ts2 
unsetenv_var ts3
unsetenv_var ts4
unsetenv_path_var TSPATH
unsetenv_var ts5
unsetenv_var ts6
unsetenv_var ts7
unsetenv_var ts8
unsetenv_var ts9
unsetenv_path_var TSPATH2
unsetenv_path_var TSPATH3
unsetenv_var tsall


#
# test test mode
#

# test without module loaded
set tserr [test_stderr_content "$modulefilere/1" $tsall]
append tserr "\n[test_stderr_content $modulefilere/2 $tsall 0 0]"
testouterr_cmd_re "sh" "test $module/1 $module/2" "" $tserr

# test with module loaded
setenv_loaded_module [list "$module/1" "$module/2"] [list "$modulefile/1" "$modulefile/2"]
setenv_var ts2 "dir2"
setenv_var ts3 "dir2:dir1"
setenv_var ts4 ""
setenv_path_var TSPATH "dir2"
setenv_var ts5 "_UNDEFINED_altered"
setenv_var ts6 "dir2"
setenv_var ts7 "_UNDEFINED_altered"
setenv_var ts8 "ok8"
setenv_var ts9 "ok9"
setenv_path_var TSPATH2 "ok8"
setenv_path_var TSPATH3 "ok8:ok9"
setenv_var tsall $tsall

testouterr_cmd_re "sh" "test $module/1 $module/2" "" $tserr

# restore environment
unsetenv_loaded_module
unsetenv_var ts2 
unsetenv_var ts3
unsetenv_var ts4
unsetenv_path_var TSPATH
unsetenv_var ts5
unsetenv_var ts6
unsetenv_var ts7
unsetenv_var ts8
unsetenv_var ts9
unsetenv_path_var TSPATH2
unsetenv_path_var TSPATH3
unsetenv_var tsall


#
# test whatis mode
#

# *setenv and *-path cmds only init var if undefined with empty value
set tsallwh1 ",,,,altered,,,,"
set tsallwh2 ",dir2,dir2:dir1,,_UNDEFINED_alteredaltered,dir2,_UNDEFINED_altered,ok8,ok9"

# test without module loaded
set whatis "$modlin $modulepathre $modlin\n\\s+$module/1: $tsallwh1\n\\s+$module/2: "
testouterr_cmd_re "sh" "whatis $module" "" $whatis

# test with module loaded
setenv_loaded_module [list "$module/1" "$module/2"] [list "$modulefile/1" "$modulefile/2"]
setenv_var ts2 "dir2"
setenv_var ts3 "dir2:dir1"
setenv_var ts4 ""
setenv_path_var TSPATH "dir2"
setenv_var ts5 "_UNDEFINED_altered"
setenv_var ts6 "dir2"
setenv_var ts7 "_UNDEFINED_altered"
setenv_var ts8 "ok8"
setenv_var ts9 "ok9"
setenv_path_var TSPATH2 "ok8"
setenv_path_var TSPATH3 "ok8:ok9"
setenv_var tsall $tsall

set whatis "$modlin $modulepathre $modlin\n\\s+$module/1: $tsallwh2\n\\s+$module/2: $tsall"
testouterr_cmd_re "sh" "whatis $module" "" $whatis

# restore environment
unsetenv_loaded_module
unsetenv_var ts2 
unsetenv_var ts3
unsetenv_var ts4
unsetenv_path_var TSPATH
unsetenv_var ts5
unsetenv_var ts6
unsetenv_var ts7
unsetenv_var ts8
unsetenv_var ts9
unsetenv_path_var TSPATH2
unsetenv_path_var TSPATH3
unsetenv_var tsall


#
#  Cleanup
#

reset_test_env

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