##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2017/12/07
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load
#   Modulefiles:	conflict/module, conflict/fullpath, trace/all_on, setenv/1.0
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the 'conflict' command using full path modulefile
#           or reacting to loaded full path modulefile.
#		}C%
#
##############################################################################

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

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

set module1 "conflict/fullpath"
set modulefile1 "$modpath/$module1"
set module2 "conflict/module"
set modulefile2 "$modpath/$module2"
set module3 "conflict/relpath"
set modulefile3 "$modpath/$module3"

set module_tron "trace/all_on"
set modulefile_tron "$modpath/$module_tron"
set module_set1 "setenv/1.0"
set modulefile_set1 "$modpath/$module_set1"

#
#  Error messages
#

set err_con1 [msg_load $module1 [err_conflict "$modpath/setenv/1.0"]]
set err_con1re [regsub -all "\(\[.+?\]\)" $err_con1 {\\\1}]
set err_con2 [msg_load $module2 [err_conflict "trace"]]
set err_con3 [msg_load $module3 [err_conflict "../setenv/1.0"]]
set err_con3re [regsub -all "\(\[.+?\]\)" $err_con3 {\\\1}]
set err_conf1 [msg_load $modulefile1 [err_conflict "$modpath/setenv/1.0"]]
set err_conf1re [regsub -all "\(\[.+?\]\)" $err_conf1 {\\\1}]
set err_conf2 [msg_load $modulefile2 [err_conflict "trace"]]
set err_conf2re [regsub -all "\(\[.+?\]\)" $err_conf2 {\\\1}]
set err_conf3 [msg_load $modulefile3 [err_conflict "../setenv/1.0"]]
set err_conf3re [regsub -all "\(\[.+?\]\)" $err_conf3 {\\\1}]


#
#  The tests
#

lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$module1:$module2:$module3"]
lappend ans [list setpath _LMFILES_ "$modulefile1:$modulefile2:$modulefile3"]
lappend ans [list setpath MODULES_LMCONFLICT "$module1&$modulefile_set1:$module2&trace:$module3&../$module_set1"]
testouterr_cmd_re "sh" "load $module1 $module2 $module3" $ans ""

set ans [list]
lappend ans [list set testsuite yes]
lappend ans [list setpath LOADEDMODULES "$modulefile1:$modulefile2:$modulefile3"]
lappend ans [list setpath _LMFILES_ "$modulefile1:$modulefile2:$modulefile3"]
lappend ans [list setpath MODULES_LMCONFLICT "$modulefile1&$modulefile_set1:$modulefile2&trace:$modulefile3&../$module_set1"]
testouterr_cmd_re "sh" "load $modulefile1 $modulefile2 $modulefile3" $ans ""


# test with conflicts loaded as short path
setenv_loaded_module [list $module_tron $module_set1] [list $modulefile_tron $modulefile_set1]

testouterr_cmd_re "sh" "load $module1 $module2 $module3" "ERR" "$err_con1re\n\n$err_con2\n\n$err_con3"
testouterr_cmd_re "sh" "load $modulefile1 $modulefile2 $modulefile3" "ERR" "$err_conf1re\n\n$err_conf2re\n\n$err_conf3re"


# test with conflicts loaded as short path, no more modulepath set
unsetenv_path_var MODULEPATH

testouterr_cmd_re "sh" "load $modulefile1 $modulefile2 $modulefile3" "ERR" "$err_conf1re\n\n$err_conf2re\n\n$err_conf3re"


# test with conflicts loaded as full path
setenv_path_var MODULEPATH $modpath
setenv_loaded_module [list $modulefile_tron $modulefile_set1] [list $modulefile_tron $modulefile_set1]

testouterr_cmd_re "sh" "load $module1 $module2 $module3" "ERR" "$err_con1re\n\n$err_con2\n\n$err_con3"
testouterr_cmd_re "sh" "load $modulefile1 $modulefile2 $modulefile3" "ERR" "$err_conf1re\n\n$err_conf2re\n\n$err_conf3re"


# test with conflicts loaded as full path, no more modulepath set
unsetenv_path_var MODULEPATH

set ans [list]
lappend ans [list set LOADEDMODULES_modshare "(.*)"]
lappend ans [list set _LMFILES__modshare "(.*)"]
lappend ans [list set MODULES_LMCONFLICT_modshare "(.*)"]
lappend ans [list set testsuite yes]
lappend ans [list set LOADEDMODULES "$modulefile_tron:$modulefile_set1:$modulefile2"]
lappend ans [list set _LMFILES_ "$modulefile_tron:$modulefile_set1:$modulefile2"]
lappend ans [list set MODULES_LMCONFLICT "$modulefile2&trace"]
lappend ans [list ERR]
testouterr_cmd_re "sh" "load $modulefile1 $modulefile2 $modulefile3" $ans "$err_conf1re\n\n$err_conf3re"


#
#  Cleanup
#

reset_test_env
