##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	2020/10/21
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Modulefiles:    notreq, foo, bar
#   Sub-Command:    load, unload, switch, reload, purge
#
#   Comment:	%C{
#           Test implicit_requirement configuration disablement via
#           --not-req command-line switch.
#		}C%
#
##############################################################################

# ensure avail -t tests have implicit default enabled
setenv_var MODULES_IMPLICIT_DEFAULT 1

# ensure last matching element is returned when unloading modules
setenv_var MODULES_UNLOAD_MATCH_ORDER returnlast

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

set mp $modpath.3
set mpre [regsub -all "\(\[.+?\]\)" $mp {\\\1}]

# setup specific environment
setenv_path_var MODULEPATH $mp


#
# misuse of --not-req option
#

set tserr "$error_msgs: Invalid option '--not-req'\n$err_typehelp"
testouterr_cmd_re sh {load --not-req foo} ERR $tserr
testouterr_cmd_re sh {ml --not-req foo} ERR $tserr

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES notreq/use]
lappend ans0 [list setpath _LMFILES_ $mp/notreq/use]
lappend ans0 [list setpath MODULEPATH $env(TESTSUITEDIR):$mp]
set tserr [msg_load notreq/use [msg_moderr {Unsupported option '--not-req' on use sub-command} {module use --not-req $env(TESTSUITEDIR)} $mp/notreq/use 2]]
testouterr_cmd sh {load notreq/use} ERR $tserr


#
# module load test
#

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:notreq/load]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/notreq/load]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/2.0]

set tserr [msg_top_load notreq/load {} foo/2.0 {}]

# test default implicit_requirement configuration
unsetenv_var MODULES_IMPLICIT_REQUIREMENT
if {$install_implicitrequirement eq {y}} {
    testouterr_cmd_re sh {load notreq/load} $ans0 $tserr
} else {
    testouterr_cmd_re sh {load notreq/load} $ans0 $tserr
}
# option disabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 0
testouterr_cmd_re sh {load notreq/load} $ans0 $tserr
# option enabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 1
testouterr_cmd_re sh {load notreq/load} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/load} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/load} $ans0 $tserr

# module to load already loaded
setenv_loaded_module [list foo/2.0] [list $mp/foo/2.0]
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:notreq/load]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/notreq/load]
set tserr {}
testouterr_cmd_re sh {load --no-auto notreq/load} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/load} $ans0 $tserr
unsetenv_loaded_module

# multiple load on single command
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:bar/2.0:notreq/loadmul]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/bar/2.0:$mp/notreq/loadmul]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/2.0:bar/2.0]
set tserr [msg_top_load notreq/loadmul {} [list foo/2.0 bar/2.0] {}]
testouterr_cmd_re sh {load --no-auto notreq/loadmul} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/loadmul} $ans0 $tserr

# unloading tests
setenv_loaded_module [list foo/2.0 notreq/load] [list $mp/foo/2.0 $mp/notreq/load]
setenv_path_var MODULES_LMNOTUASKED foo/2.0

# UReqUn
set tserr [msg_top_unload notreq/load {} foo/2.0 {}]
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --auto notreq/load} $ans0 $tserr
testouterr_cmd_re sh {unload --no-auto notreq/load} $ans0 $tserr

# no auto-loaded flag, no unload
unsetenv_path_var MODULES_LMNOTUASKED
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0]
set tserr {}
testouterr_cmd_re sh {unload --auto notreq/load} $ans0 $tserr
testouterr_cmd_re sh {unload --no-auto notreq/load} $ans0 $tserr

# DepUn (not triggered as requirement not recorded)
setenv_path_var MODULES_LMNOTUASKED foo/2.0
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES notreq/load]
lappend ans0 [list setpath _LMFILES_ $mp/notreq/load]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
set tserr {}
testouterr_cmd_re sh {unload foo/2.0} $ans0 $tserr

# multiple load on single command
setenv_loaded_module [list foo/2.0 bar/2.0 notreq/loadmul] [list $mp/foo/2.0 $mp/bar/2.0 $mp/notreq/loadmul]
setenv_path_var MODULES_LMNOTUASKED foo/2.0:bar/2.0
set tserr [msg_top_unload notreq/loadmul {} [list bar/2.0 foo/2.0] {}]
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --auto notreq/loadmul} $ans0 $tserr
testouterr_cmd_re sh {unload --no-auto notreq/loadmul} $ans0 $tserr

unsetenv_loaded_module
unsetenv_path_var MODULES_LMNOTUASKED


#
# module unload tests
#

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES notreq/unload]
lappend ans0 [list setpath _LMFILES_ $mp/notreq/unload]

set tserr {}

# test default implicit_requirement configuration
unsetenv_var MODULES_IMPLICIT_REQUIREMENT
if {$install_implicitrequirement eq {y}} {
    testouterr_cmd_re sh {load notreq/unload} $ans0 $tserr
} else {
    testouterr_cmd_re sh {load notreq/unload} $ans0 $tserr
}
# option disabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 0
testouterr_cmd_re sh {load notreq/unload} $ans0 $tserr
# option enabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 1
testouterr_cmd_re sh {load notreq/unload} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/unload} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/unload} $ans0 $tserr

# with module to unload loaded
setenv_loaded_module [list foo/2.0] [list $mp/foo/2.0]
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES notreq/unload]
lappend ans0 [list setpath _LMFILES_ $mp/notreq/unload]
set tserr [msg_top_load notreq/unload [list foo/2.0] {} {}]
testouterr_cmd_re sh {load --auto notreq/unload} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/unload} $ans0 $tserr

# multiple unload on same command
unsetenv_loaded_module
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES notreq/unloadmul]
lappend ans0 [list setpath _LMFILES_ $mp/notreq/unloadmul]
set tserr {}
testouterr_cmd_re sh {load --no-auto notreq/unloadmul} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/unloadmul} $ans0 $tserr

# with module to unload loaded
setenv_loaded_module [list foo/2.0 bar/2.0] [list $mp/foo/2.0 $mp/bar/2.0]
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES notreq/unloadmul]
lappend ans0 [list setpath _LMFILES_ $mp/notreq/unloadmul]
set tserr [msg_top_load notreq/unloadmul [list foo/2.0 bar/2.0] {} {}]
testouterr_cmd_re sh {load --auto notreq/unloadmul} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/unloadmul} $ans0 $tserr
unsetenv_loaded_module


#
# module switch tests
#

# 1-arg form
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:notreq/switch1]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/notreq/switch1]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/2.0]

set tserr [msg_top_load notreq/switch1 {} foo/2.0 {}]

# test default implicit_requirement configuration
unsetenv_var MODULES_IMPLICIT_REQUIREMENT
if {$install_implicitrequirement eq {y}} {
    testouterr_cmd_re sh {load notreq/switch1} $ans0 $tserr
} else {
    testouterr_cmd_re sh {load notreq/switch1} $ans0 $tserr
}
# option disabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 0
testouterr_cmd_re sh {load notreq/switch1} $ans0 $tserr
# option enabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 1
testouterr_cmd_re sh {load notreq/switch1} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/switch1} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/switch1} $ans0 $tserr

# with module to unload loaded
setenv_loaded_module [list foo/1.0] [list $mp/foo/1.0]
set tserr [msg_top_load notreq/switch1 foo/1.0 foo/2.0 {}]
testouterr_cmd_re sh {load --auto notreq/switch1} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/switch1} $ans0 $tserr
unsetenv_loaded_module

# with module to load loaded
setenv_loaded_module [list foo/2.0] [list $mp/foo/2.0]
set tserr [msg_top_load notreq/switch1 foo/2.0 foo/2.0 {}]
testouterr_cmd_re sh {load --auto notreq/switch1} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/switch1} $ans0 $tserr
unsetenv_loaded_module

# 2-args form
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:notreq/switch2]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/notreq/switch2]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/2.0]

set tserr [msg_top_load notreq/switch2 {} foo/2.0 {}]

# test default implicit_requirement configuration
unsetenv_var MODULES_IMPLICIT_REQUIREMENT
if {$install_implicitrequirement eq {y}} {
    testouterr_cmd_re sh {load notreq/switch2} $ans0 $tserr
} else {
    testouterr_cmd_re sh {load notreq/switch2} $ans0 $tserr
}
# option disabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 0
testouterr_cmd_re sh {load notreq/switch2} $ans0 $tserr
# option enabled
setenv_var MODULES_IMPLICIT_REQUIREMENT 1
testouterr_cmd_re sh {load notreq/switch2} $ans0 $tserr
testouterr_cmd_re sh {load --auto notreq/switch2} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/switch2} $ans0 $tserr

# with module to unload loaded
setenv_loaded_module [list bar/1.0] [list $mp/bar/1.0]
set tserr [msg_top_load notreq/switch2 bar/1.0 foo/2.0 {}]
testouterr_cmd_re sh {load --auto notreq/switch2} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/switch2} $ans0 $tserr
unsetenv_loaded_module

# unloading tests
setenv_loaded_module [list foo/2.0 notreq/switch2] [list $mp/foo/2.0 $mp/notreq/switch2]
setenv_path_var MODULES_LMNOTUASKED foo/2.0
set tserr [msg_top_unload notreq/switch2 {} foo/2.0 {}]
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --auto notreq/switch2} $ans0 $tserr
testouterr_cmd_re sh {unload --no-auto notreq/switch2} $ans0 $tserr
unsetenv_loaded_module
unsetenv_path_var MODULES_LMNOTUASKED


#
# mix
#

setenv_loaded_module [list bar/2.0] [list $mp/bar/2.0]
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:baz/2.0:notreq/mix]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/baz/2.0:$mp/notreq/mix]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/2.0:baz/2.0]
set tserr [msg_top_load notreq/mix bar/2.0 {} {} {Loading requirement: foo/2.0 baz/2.0}]
testouterr_cmd_re sh {load --no-auto notreq/mix} $ans0 $tserr
unsetenv_loaded_module


#
# loaded or unloaded modules themselves load or unload modules
#

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/1.0:foo/0.prereq:notreq/loadsubp]
lappend ans0 [list setpath _LMFILES_ $mp/foo/1.0:$mp/foo/0.prereq:$mp/notreq/loadsubp]
lappend ans0 [list setpath MODULES_LMPREREQ foo/0.prereq&foo/1.0]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/1.0:foo/0.prereq]
set tserr [msg_top_load notreq/loadsubp {} [list foo/1.0 foo/0.prereq] {}]
testouterr_cmd_re sh {load --auto notreq/loadsubp} $ans0 $tserr

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/1.0:foo/0.load:notreq/loadsubm]
lappend ans0 [list setpath _LMFILES_ $mp/foo/1.0:$mp/foo/0.load:$mp/notreq/loadsubm]
lappend ans0 [list setpath MODULES_LMPREREQ foo/0.load&foo/1.0]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/1.0:foo/0.load]
set tserr [msg_top_load notreq/loadsubm {} [list foo/1.0 foo/0.load] {}]
testouterr_cmd_re sh {load --auto notreq/loadsubm} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/loadsubm} $ans0 $tserr

setenv_loaded_module [list foo/1.0 foo/0.prereq notreq/loadsubp] [list $mp/foo/1.0 $mp/foo/0.prereq $mp/notreq/loadsubp]
setenv_path_var MODULES_LMNOTUASKED foo/1.0:foo/0.prereq
setenv_path_var MODULES_LMPREREQ foo/0.prereq&foo/1.0
set tserr [msg_top_unload notreq/loadsubp {} [list foo/0.prereq foo/1.0] {}]
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --auto notreq/loadsubp} $ans0 $tserr

setenv_loaded_module [list foo/1.0 foo/0.load notreq/loadsubm] [list $mp/foo/1.0 $mp/foo/0.load $mp/notreq/loadsubm]
setenv_path_var MODULES_LMNOTUASKED foo/1.0:foo/0.load
setenv_path_var MODULES_LMPREREQ foo/0.load&foo/1.0
set tserr [msg_top_unload notreq/loadsubm {} [list foo/0.load foo/1.0] {}]
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --no-auto notreq/loadsubm} $ans0 $tserr

setenv_loaded_module [list foo/1.0 foo/0.prereq] [list $mp/foo/1.0 $mp/foo/0.prereq]
setenv_path_var MODULES_LMNOTUASKED foo/1.0
setenv_path_var MODULES_LMPREREQ foo/0.prereq&foo/1.0
# auto-loaded requirement of foo/0.prereq is not auto unloaded when foo/0.prereq is
# unloaded by notreq/unload load
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/1.0:notreq/unload]
lappend ans0 [list setpath _LMFILES_ $mp/foo/1.0:$mp/notreq/unload]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
set tserr [msg_top_load notreq/unload foo/0.prereq {} {}]
testouterr_cmd_re sh {load --auto notreq/unload} $ans0 $tserr
unsetenv_loaded_module
unsetenv_path_var MODULES_LMNOTUASKED
unsetenv_path_var MODULES_LMPREREQ

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/1.0:foo/0.prereq:notreq/switchsubp]
lappend ans0 [list setpath _LMFILES_ $mp/foo/1.0:$mp/foo/0.prereq:$mp/notreq/switchsubp]
lappend ans0 [list setpath MODULES_LMPREREQ foo/0.prereq&foo/1.0]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/1.0:foo/0.prereq]
set tserr [msg_top_load notreq/switchsubp {} [list foo/1.0 foo/0.prereq] {}]
testouterr_cmd_re sh {load --auto notreq/switchsubp} $ans0 $tserr

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/1.0:foo/0.load:notreq/switchsubm]
lappend ans0 [list setpath _LMFILES_ $mp/foo/1.0:$mp/foo/0.load:$mp/notreq/switchsubm]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/1.0:foo/0.load]
lappend ans0 [list setpath MODULES_LMPREREQ foo/0.load&foo/1.0]
set tserr [msg_top_load notreq/switchsubm {} [list foo/1.0 foo/0.load] {}]
testouterr_cmd_re sh {load --auto notreq/switchsubm} $ans0 $tserr
testouterr_cmd_re sh {load --no-auto notreq/switchsubm} $ans0 $tserr

setenv_loaded_module [list foo/1.0 foo/0.prereq notreq/switchsubp] [list $mp/foo/1.0 $mp/foo/0.prereq $mp/notreq/switchsubp]
setenv_path_var MODULES_LMNOTUASKED foo/1.0:foo/0.prereq
setenv_path_var MODULES_LMPREREQ foo/0.prereq&foo/1.0
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
set tserr [msg_top_unload notreq/switchsubp {} [list foo/0.prereq foo/1.0] {}]
testouterr_cmd_re sh {unload --auto notreq/switchsubp} $ans0 $tserr

setenv_loaded_module [list foo/1.0 foo/0.load notreq/switchsubm] [list $mp/foo/1.0 $mp/foo/0.load $mp/notreq/switchsubm]
setenv_path_var MODULES_LMNOTUASKED foo/1.0:foo/0.load
setenv_path_var MODULES_LMPREREQ foo/0.load&foo/1.0
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
set tserr [msg_top_unload notreq/switchsubm {} [list foo/0.load foo/1.0] {}]
testouterr_cmd_re sh {unload --no-auto notreq/switchsubm} $ans0 $tserr
unsetenv_loaded_module
unsetenv_path_var MODULES_LMNOTUASKED
unsetenv_path_var MODULES_LMPREREQ


#
# loaded or unloaded modules do not exist or have error
#

set tserr [msg_load foo/0.err [msg_moderr {invalid command name "foo"} foo $mp/foo/0.err 2]]\n\n[msg_load notreq/loaderr [err_reqlo foo/0.err]]
testouterr_cmd sh {load --no-auto notreq/loaderr} ERR $tserr

setenv_loaded_module [list foo/0.err] [list $mp/foo/0.err]
set tserr [msg_unload foo/0.err [msg_moderr {invalid command name "foo"} foo $mp/foo/0.err 2]]
testouterr_cmd sh {load --no-auto notreq/unloaderr} ERR $tserr

unsetenv_loaded_module
set tserr [msg_load foo/0.err [msg_moderr {invalid command name "foo"} foo $mp/foo/0.err 2]]\n\n[msg_load notreq/switch1err [err_swon foo/0.err] [err_reqlo foo/0.err]]
testouterr_cmd sh {load --no-auto notreq/switch1err} ERR $tserr

set tserr [msg_load notreq/loadunk [err_unablelocate foo/0.unk] [err_reqlo foo/0.unk]]
testouterr_cmd sh {load --no-auto notreq/loadunk} ERR $tserr

set tserr [msg_switch foo/0.unk foo/0.unk [err_unablelocate foo/0.unk]]\n\n[msg_load notreq/switch1unk [err_reqlo foo/0.unk]]
testouterr_cmd sh {load --no-auto notreq/switch1unk} ERR $tserr


#
# purge/reload/restore
#

setenv_loaded_module [list foo/1.0 foo/0.load notreq/loadsubm foo/2.0 notreq/switch2] [list $mp/foo/1.0 $mp/foo/0.load $mp/notreq/loadsubm $mp/foo/2.0 $mp/notreq/switch2]
setenv_path_var MODULES_LMNOTUASKED foo/1.0:foo/0.load:foo/2.0
setenv_path_var MODULES_LMPREREQ foo/0.load&foo/1.0

set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
set tserr {}
testouterr_cmd_re sh {purge} $ans0 $tserr

set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/1.0:foo/0.load:notreq/loadsubm:foo/2.0:notreq/switch2]
lappend ans0 [list setpath _LMFILES_ $mp/foo/1.0:$mp/foo/0.load:$mp/notreq/loadsubm:$mp/foo/2.0:$mp/notreq/switch2]
lappend ans0 [list setpath MODULES_LMPREREQ foo/0.load&foo/1.0]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/1.0:foo/0.load:foo/2.0]
testouterr_cmd_re sh {reload} $ans0 $tserr

testouterr_cmd_re sh {save ./testcoll} OK {}

unsetenv_path_var MODULES_LMNOTUASKED
unsetenv_path_var MODULES_LMPREREQ
unsetenv_loaded_module

set tserr [msg_load foo/1.0]\n[msg_load foo/0.load]\n[msg_load notreq/loadsubm]\n[msg_load foo/2.0]\n[msg_switch bar foo/2.0]\n[msg_load notreq/switch2]
testouterr_cmd_re sh {restore ./testcoll} $ans0 $tserr

file delete ./testcoll


#
# module with implicit requirement dependency of another module
#

# ReqLo
set ans0 [list]
lappend ans0 [list setpath LOADEDMODULES foo/2.0:notreq/load:notreq/prereq]
lappend ans0 [list setpath _LMFILES_ $mp/foo/2.0:$mp/notreq/load:$mp/notreq/prereq]
lappend ans0 [list setpath MODULES_LMPREREQ notreq/prereq&notreq/load]
lappend ans0 [list setpath MODULES_LMNOTUASKED foo/2.0:notreq/load]
set tserr [msg_top_load notreq/prereq {} [list foo/2.0 notreq/load] {}]
testouterr_cmd_re sh {load --auto notreq/prereq} $ans0 $tserr

# UreqUn
setenv_loaded_module [list foo/2.0 notreq/load notreq/prereq] [list $mp/foo/2.0 $mp/notreq/load $mp/notreq/prereq]
setenv_path_var MODULES_LMPREREQ notreq/prereq&notreq/load
setenv_path_var MODULES_LMNOTUASKED foo/2.0:notreq/load
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
set tserr [msg_top_unload notreq/prereq {} [list notreq/load foo/2.0] {}]
testouterr_cmd_re sh {unload --auto notreq/prereq} $ans0 $tserr

# DepUn
set ans0 [list]
lappend ans0 [list unsetpath LOADEDMODULES]
lappend ans0 [list unsetpath _LMFILES_]
lappend ans0 [list unsetpath MODULES_LMPREREQ]
lappend ans0 [list unsetpath MODULES_LMNOTUASKED]
set tserr [msg_top_unload notreq/load notreq/prereq foo/2.0 {}]
testouterr_cmd_re sh {unload --auto notreq/load} $ans0 $tserr

# DepRe
# no common use case to sketch

unsetenv_path_var MODULES_LMPREREQ
unsetenv_path_var MODULES_LMNOTUASKED
unsetenv_loaded_module


#
#  Cleanup
#

reset_test_env
