##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	1995/12/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Jens Hamisch, Jens.Hamisch@Strawberry.COM
#
#   Description:	Testuite testsequence
#   Command:		load, display, whatis, paths
#   Modulefiles:	loc_rc1/1.0, loc_rc2/1.0, loc_rc3/1.0, loc_rc4/1.0
#   Modulefiles:	loc_rc7, loc_rc2/2.0, loc_rc6/1.2, loc_rc6/0.9
#   Sub-Command:
#
#   Comment:	%C{
#			Check the modulecmd rc file execution
#		}C%
#
##############################################################################

#
#  Variables. This test forces a module load command. It will result in the
#    environment variables "_LMFILES_", "LOADEDMODULES" and "testsuite" to
#    be set up
#

set module_1 "loc_rc1/1.0"
set modalt_1 loc_rc1/foo
set modulefile_1 "$modpath/$module_1"
set modulefilere_1 "$modpathre/$module_1"

set module_2 "loc_rc2/1.0"
set modalt_2 loc_rc2/foo&loc_rc2/bar&loc_rc2/blah
set modulefile_2 "$modpath/$module_2"
set modulefilere_2 "$modpathre/$module_2"

set module_3 "loc_rc3/1.0"
set modalt_3 loc_rc3/default&loc_rc3
set modulefile_3 "$modpath/$module_3"
set modulefilere_3 "$modpathre/$module_3"

set module_4 "loc_rc4/2.0"
set modalt_4 loc_rc4/default&loc_rc4
set modulefile_4 "$modpath/$module_4"
set modulefilere_4 "$modpathre/$module_4"

set module_5 "loc_rc7/0.9"
if {[is_conf_enabled advversspec implicitdefault]} {
    set modalt_5 al|loc_rc7/bar&as|loc_rc7/default&as|loc_rc7/latest
} else {
    set modalt_5 al|loc_rc7/bar
}
set modulefile_5 "$modpath/$module_5"
set modulefilere_5 "$modpathre/$module_5"

set module_6 "loc_rc2/2.0"
set modalt_6 as|loc_rc2/default&as|loc_rc2/latest
set modulefile_6 "$modpath/$module_6"
set modulefilere_6 "$modpathre/$module_6"

set module_7 "loc_rc6/1.2"
set modalt_7 loc_rc6/default&loc_rc6&loc_rc6/new&al|loc_rc6/1&al|loc_rc6/bar&al|dirmodalias
if {[is_conf_enabled advversspec implicitdefault]} {
    append modalt_7 &as|loc_rc6/latest
}
set modulefile_7 "$modpath/$module_7"
set modulefilere_7 "$modpathre/$module_7"

set module_8 "loc_rc6/0.9"
set modulefile_8 "$modpath/$module_8"
set modulefilere_8 "$modpathre/$module_8"

set module_9 "dirmodalias/1.0"
set modulefile_9 "$modpath/$module_9"
set modulefilere_9 "$modpathre/$module_9"

set module_10 "loc_rcv1/1.2"
set modalt_10 loc_rcv1/default&loc_rcv1&loc_rcv1/new&al|loc_rcv1/1&al|loc_rcv1/bar
if {[is_conf_enabled advversspec implicitdefault]} {
    append modalt_10 &as|loc_rcv1/latest
}
set modulefile_10 "$modpath/loc_rcv1/.common"
set modulefilere_10 "$modpathre/loc_rcv1/.common"

set module_11 "dirmodvirt"
set modulefile_11 "$modpath/loc_rcv1/.common"
set modulefilere_11 "$modpathre/loc_rcv1/.common"

set module_12 "dirmodvirt/1.0"
set modulefile_12 "$modpath/loc_rcv1/.common"
set modulefilere_12 "$modpathre/loc_rcv1/.common"

#
#  Only checked for the csh
#

set disp_csh "setenv\t\ttestsuite yes"
set whatis_csh "module-whatis\t"
set disp_1 "$modlin\n$modulefilere_1:\n\n$whatis_csh$module_1\n$disp_csh\n$modlin"
set disp_2 "$modlin\n$modulefilere_2:\n\n$whatis_csh$module_2\n$disp_csh\n$modlin"
set disp_3 "$modlin\n$modulefilere_3:\n\n$whatis_csh$module_3\n$disp_csh\n$modlin"
set disp_4 "$modlin\n$modulefilere_4:\n\n$whatis_csh$module_4\n$disp_csh\n$modlin"
set disp_5 "$modlin\n$modulefilere_5:\n\n$whatis_csh$module_5\n$disp_csh\n$modlin"
set disp_6 "$modlin\n$modulefilere_6:\n\n$whatis_csh$module_6\n$disp_csh\n$modlin"
set disp_7 "$modlin\n$modulefilere_7:\n\n$whatis_csh$module_7\n$disp_csh\n$modlin"
set disp_10 "$modlin\n$modulefilere_10:\n\n$whatis_csh$module_10\n$modlin"
set disp_11 "$modlin\n$modulefilere_11:\n\n$whatis_csh$module_11\n$modlin"
set disp_12 "$modlin\n$modulefilere_12:\n\n$whatis_csh$module_12\n$modlin"

set header "$modlin $modpathre $modlin"
set what_1 "$header\n         $module_1: $module_1"
set what_2 "$header\n         $module_2: $module_2"
set what_7 "$header\n         $module_7: $module_7"
set what_9_7 "$header\n     $module_9: $module_9\n         $module_7: $module_7"
set what_10 "$header\n        $module_10: $module_10"
set what_11 "$header\n          $module_11: $module_11\n      $module_12: $module_12"
set what_12 "$header\n      $module_12: $module_12"

#
#  The tests
#

lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES $module_1]
lappend ans [list setpath _LMFILES_ $modulefile_1]
lappend ans [list setpath MODULES_LMALTNAME $module_1&$modalt_1]
test_cmd "csh" "load loc_rc1/foo" $ans
testouterr_cmd_re "sh" "display loc_rc1/foo" "OK" "$disp_1"
testouterr_cmd_re "sh" "whatis loc_rc1/foo" "OK" "$what_1"

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES $module_2]
lappend ans [list setpath _LMFILES_ $modulefile_2]
lappend ans [list setpath MODULES_LMALTNAME $module_2&$modalt_2]
test_cmd "csh" "load loc_rc2/foo" $ans
testouterr_cmd_re "sh" "display loc_rc2/foo" "OK" "$disp_2"
testouterr_cmd_re "sh" "whatis loc_rc2/foo" "OK" "$what_2"

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES $module_3]
lappend ans [list setpath _LMFILES_ $modulefile_3]
lappend ans [list setpath MODULES_LMALTNAME $module_3&$modalt_3]
test_cmd "csh" "load loc_rc3" $ans
testouterr_cmd_re "sh" "display loc_rc3" "OK" "$disp_3"

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES $module_4]
lappend ans [list setpath _LMFILES_ $modulefile_4]
lappend ans [list setpath MODULES_LMALTNAME $module_4&$modalt_4]
test_cmd "csh" "load loc_rc4" $ans
testouterr_cmd_re "sh" "display loc_rc4" "OK" "$disp_4"

# test loading implicit default of a module, which is an alias
set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES $module_5]
lappend ans [list setpath _LMFILES_ $modulefile_5]
lappend ans [list setpath MODULES_LMALTNAME $module_5&$modalt_5]
test_cmd "csh" "load loc_rc7" [if_implicitdefault $ans ERR]
testouterr_cmd_re "sh" "display loc_rc7" [if_implicitdefault OK ERR] [if_implicitdefault $disp_5 "$err_nodefault'loc_rc7'"]

# test loading implicit default of a module, when a symbol is the greatest version name
# this symbol should be ignored and greatest alias or file should be chosen
set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES $module_6]
lappend ans [list setpath _LMFILES_ $modulefile_6]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $module_6&$modalt_6]
}
test_cmd "csh" "load loc_rc2" [if_implicitdefault $ans ERR]
testouterr_cmd_re "sh" "display loc_rc2" [if_implicitdefault OK ERR] [if_implicitdefault $disp_6 "$err_nodefault'loc_rc2'"]

testouterr_cmd_re "sh" "whatis loc_rc6/bar" "OK" "$what_7"

# test lock effect of the implicit_default configuration
if {[is_config_locked implicit_default]} {
    setenv_var MODULES_IMPLICIT_DEFAULT 1
    testouterr_cmd_re "sh" "display loc_rc2" [if_implicitdefault OK ERR] [if_implicitdefault $disp_6 "$err_nodefault'loc_rc2'"]
    setenv_var MODULES_IMPLICIT_DEFAULT 0
    testouterr_cmd_re "sh" "display loc_rc2" [if_implicitdefault OK ERR] [if_implicitdefault $disp_6 "$err_nodefault'loc_rc2'"]
    unsetenv_var MODULES_IMPLICIT_DEFAULT
}

# test working on an alias targeting an implicit default, when another version of the
# targeted module is loaded, this loaded module should not influence alias resolution
setenv_loaded_module $module_8 $modulefile_8
set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module_8:$module_7"]
lappend ans [list setpath _LMFILES_ "$modulefile_8:$modulefile_7"]
lappend ans [list setpath MODULES_LMALTNAME $module_7&$modalt_7]
test_cmd_re "csh" "load dirmodalias" $ans
testouterr_cmd_re "sh" "display dirmodalias" "OK" "$disp_7"
testouterr_cmd_re "sh" "whatis dirmodalias" "OK" "$what_9_7"
set ans [list]
lappend ans [list text [list $modulefile_9 $modulefile_7]]
test_cmd "sh" "paths dirmodalias" $ans

# Clean up the environment
unsetenv_loaded_module

# working on virtual modules
set ans [list]
lappend ans [list setpath LOADEDMODULES $module_10]
lappend ans [list setpath _LMFILES_ $modulefile_10]
lappend ans [list setpath MODULES_LMALTNAME $module_10&$modalt_10]
test_cmd "csh" "load loc_rcv1" $ans
testouterr_cmd_re "sh" "display loc_rcv1" "OK" "$disp_10"
testouterr_cmd_re "sh" "whatis loc_rcv1/bar" "OK" "$what_10"

# virtual modules covering existing directory name
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_11"]
test_cmd_re "csh" "load dirmodvirt" $ans
testouterr_cmd_re "sh" "display dirmodvirt" "OK" "$disp_11"
testouterr_cmd_re "sh" "whatis dirmodvirt" "OK" "$what_11"
set ans [list]
lappend ans [list text [list $modulefile_11]]
test_cmd "sh" "paths dirmodvirt" $ans

# virtual modules covering existing modulefile name
set ans [list]
lappend ans [list setpath LOADEDMODULES "$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_12"]
test_cmd_re "csh" "load $module_12" $ans
testouterr_cmd_re "sh" "display $module_12" "OK" "$disp_12"
testouterr_cmd_re "sh" "whatis $module_12" "OK" "$what_12"
set ans [list]
lappend ans [list text [list $modulefile_12]]
test_cmd "sh" "paths $module_12" $ans

#
#  Cleanup
#

reset_test_env
