##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2018/05/27
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, unload
#   Modulefiles:
#   Sub-Command:    prereq, conflict
#
#   Comment:	%C{
#           Test automatic resolution of modulefile dependencies when the
#           auto-handling mode is enabled
#		}C%
#
##############################################################################

# enable auto_handling with environment variable
setenv_var MODULES_AUTO_HANDLING 1

set mp "$modpath.deps"

# setup specific environment
setenv_path_var MODULEPATH $mp

# load attempt triggering a registered conflict
setenv_loaded_module a "$mp/a"
setenv_path_var MODULES_LMCONFLICT "a&b"
testouterr_cmd "sh" "load b" "ERR" [msg_load b [err_conflict a]]

# unload attempt triggering a registered prereq
setenv_loaded_module [list c b] [list "$mp/c" "$mp/b"] [list c]
setenv_path_var MODULES_LMCONFLICT "c&c"
setenv_path_var MODULES_LMPREREQ "b&c"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
lappend ans [list unsetpath MODULES_LMCONFLICT]
lappend ans [list unsetpath MODULES_LMPREREQ]
testouterr_cmd "sh" "unload c" $ans [msg_top_unload c b {} {}]

unsetenv_path_var MODULES_LMCONFLICT
unsetenv_path_var MODULES_LMPREREQ
unsetenv_loaded_module

# load tests
set ans [list]
lappend ans [list setpath LOADEDMODULES "c:b"]
lappend ans [list setpath _LMFILES_ "$mp/c:$mp/b"]
lappend ans [list setpath MODULES_LMCONFLICT "c&c"]
lappend ans [list setpath MODULES_LMPREREQ "b&c"]
lappend ans [list setpath MODULES_LMNOTUASKED "c"]
testouterr_cmd_re "sh" "load b" $ans [msg_top_load b {} c {}]

testouterr_cmd "sh" "load d" "ERR" [msg_load c [err_conloi d]]\n\n[msg_load d [err_reqlo c]]

testouterr_cmd "sh" "load e" "ERR" [msg_load f [err_conlo c]]\n\n[msg_load e [err_reqlo f]]

testouterr_cmd "sh" "load g" "ERR" [msg_load b [err_conlo a]]\n\n[msg_load g [err_reqlo b]]

testouterr_cmd "sh" "load h" "ERR" [msg_load a [err_conlo b]]\n\n[msg_load h [err_reqlo a]]

testouterr_cmd "sh" "load i" "ERR" [msg_load a [err_conlo b]]\n\n[msg_load i [err_reqlo a]]

testouterr_cmd "sh" "load k" "ERR" [msg_load k [err_conlo c]]

testouterr_cmd "sh" "load l" "ERR" [msg_load c [err_conlo f]]\n\n[msg_load b [err_reqmis c]]\n\n[msg_load l [err_reqlo c] [err_reqlo b]]

testouterr_cmd "sh" "load m" "ERR" [msg_load b [err_conlo a]]\n\n[msg_load m [err_reqlo b]]

testouterr_cmd "sh" "load n" "ERR" [msg_load o [err_conloi n]]\n\n[msg_load n [err_reqlo o]]

testouterr_cmd "sh" "load o" "ERR" [msg_load n [err_conloi o]]\n\n[msg_load o [err_reqlo n]]

testouterr_cmd "sh" "load p" "ERR" [msg_load a [err_conloi p]]\n\n[msg_load p [err_reqlo a]]

testouterr_cmd "sh" "load q" "ERR" [msg_load a [err_conlo b]]\n\n[msg_load i [err_reqmis a]]\n\n[msg_load q [err_reqlo a] [err_reqlo i]]

set ans [list]
lappend ans [list setpath LOADEDMODULES "c:b:j:r"]
lappend ans [list setpath _LMFILES_ "$mp/c:$mp/b:$mp/j:$mp/r"]
lappend ans [list setpath MODULES_LMCONFLICT "c&c"]
lappend ans [list setpath MODULES_LMPREREQ "b&c:r&b&j"]
lappend ans [list setpath MODULES_LMNOTUASKED "c:b:j"]
testouterr_cmd_re "sh" "load r" $ans [msg_top_load r {} [list c b j] {}]

# is-loading conflict check
testouterr_cmd "sh" "load t" "ERR" [msg_load s [err_conloi t]]\n\n[msg_load t [err_reqlo s]]


# unload tests
setenv_loaded_module [list c b] [list "$mp/c" "$mp/b"] [list c]
setenv_path_var MODULES_LMCONFLICT "c&c"
setenv_path_var MODULES_LMPREREQ "b&c"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
lappend ans [list unsetpath MODULES_LMCONFLICT]
lappend ans [list unsetpath MODULES_LMPREREQ]
testouterr_cmd "sh" "unload b" $ans [msg_top_unload b {} c {}]

setenv_loaded_module [list c b j r] [list "$mp/c" "$mp/b" "$mp/j" "$mp/r"] [list c b j]
setenv_path_var MODULES_LMCONFLICT "c&c"
setenv_path_var MODULES_LMPREREQ "b&c:r&b&j"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
lappend ans [list unsetpath MODULES_LMCONFLICT]
lappend ans [list unsetpath MODULES_LMPREREQ]
testouterr_cmd "sh" "unload r" $ans [msg_top_unload r {} [list j b c] {}]


# check broken module does not spread its prereq/conflict defs to next evaluated module
setenv_loaded_module [list c] [list $mp/c]
setenv_path_var MODULES_LMCONFLICT c&c
unsetenv_path_var MODULES_LMPREREQ
set ans [list]
lappend ans [list setpath LOADEDMODULES c:j]
lappend ans [list setpath _LMFILES_ $mp/c:$mp/j]
lappend ans [list ERR]
testouterr_cmd_re sh {load u j} $ans [msg_load u $err_evalabort]

set ans [list]
lappend ans [list setpath LOADEDMODULES c:b:j:r]
lappend ans [list setpath _LMFILES_ $mp/c:$mp/b:$mp/j:$mp/r]
lappend ans [list setpath MODULES_LMPREREQ b&c:r&b&j]
lappend ans [list setpath MODULES_LMNOTUASKED b:j]
lappend ans [list ERR]
testouterr_cmd_re sh {load u r} $ans [msg_load u $err_evalabort]\n\n[msg_top_load r {} [list b j] {}]


#
#  Cleanup
#

reset_test_env
