##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2017/08/12
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, display, whatis, paths
#   Modulefiles:	symlink, symlink2, brokenlink
#   Sub-Command:
#
#   Comment:	%C{
#			Localization of filesystem symbolic link modulefiles
#		}C%
#
##############################################################################

# cannot test symlinks if not supported on filesystem
if {!$is_symlink_supported} {
    send_user "\tskipping symbolic link tests as they are not supported on filesystem\n"
} else {

set module_1 "symlink/0.9"
set modulefile_1 "$modpath/$module_1"
set modulefilere_1 "$modpathre/$module_1"

set module_2 "symlink/1.2"
set modulefile_2 "$modpath/$module_2"
set modulefilere_2 "$modpathre/$module_2"

set module_3 "symlink2/2.0"
set modulefile_3 "$modpath/$module_3"
set modulefilere_3 "$modpathre/$module_3"

set module_4 "symlink3"
set modulefile_4 "$modpath/$module_4"
set modulefilere_4 "$modpathre/$module_4"

set disp_sh "setenv\t\ttestsuite yes"
set whatis_sh "module-whatis\t"
set header "$modlin $modpathre $modlin"


#
#  Test on filesystem symbolic link with valid target
#

set disp_2 "$modlin\n$modulefilere_2:\n\n${whatis_sh}loc_rc6/1.2\n$disp_sh\n$modlin"
set what_1_2 "$header\n         $module_1: loc_rc6/0.9\n         $module_2: loc_rc6/1.2"

# symbolic link name used (not targeted name) once loaded, displayed, etc
set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module_2"]
lappend ans [list setpath _LMFILES_ "$modulefile_2"]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $module_2&symlink/default&symlink&symlink/new&al|symlink/1&al|symlink/bar&as|symlink/latest]
} else {
    lappend ans [list setpath MODULES_LMALTNAME $module_2&symlink/default&symlink&symlink/new&al|symlink/1&al|symlink/bar]
}
test_cmd "sh" "load symlink" $ans
testouterr_cmd_re "sh" "display symlink" "OK" "$disp_2"
testouterr_cmd_re "sh" "whatis symlink" "OK" "$what_1_2"
set ans [list]
lappend ans [list text [list $modulefile_1 $modulefile_2]]
test_cmd "sh" "paths symlink" $ans


#
# Test on filesystem symbolic link with valid target but issue on target module
#

set err_mag_3 "$err_magic'$modpath/symlink2/.modulerc'\n$err_contact"
set err_magre_3 "$err_magic'$modpathre/symlink2/.modulerc'\n$err_contact"
set disp_3 "$modlin\n$modulefilere_3:\n\n${whatis_sh}loc_rc5/2.0\n$disp_sh\n$modlin"
set what_3 "$header\n        $module_3: loc_rc5/2.0"

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_3"]
if {[is_conf_enabled advversspec implicitdefault]} {
    lappend ans [list setpath MODULES_LMALTNAME $module_3&as|symlink2/default&as|symlink2/latest]
}
lappend ans [list ERR]
testouterr_cmd "sh" "load symlink2" [if_implicitdefault $ans ERR] $err_mag_3[if_implicitdefault {} "\n$err_nodefault'symlink2'"]
testouterr_cmd_re "sh" "display symlink2/2.0" "ERR" "$err_magre_3\n$disp_3"
testouterr_cmd_re "sh" "whatis symlink2/2.0" "OK" "$what_3"

set ans [list]
lappend ans [list text $modulefile_3]
testouterr_cmd "sh" "paths symlink2/2.0" $ans $err_mag_3


#
# Test on filesystem symbolic link with nonexistent target
#

set err_found_4 "$err_file'$modulefile_4'"

testouterr_cmd "sh" "load $module_4" "ERR" $err_found_4
testouterr_cmd "sh" "display $module_4" "ERR" $err_found_4
testouterr_cmd "sh" "whatis $module_4" "ERR" $err_found_4

set ans [list]
lappend ans [list text ""]
testouterr_cmd_re "sh" "paths $module_4" $ans ""


#
#  Cleanup
#

reset_test_env

}
