##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2019/10/29
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Test impact of list version specifier
#		}C%
#
##############################################################################

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

# ensure regular avail search match is set for these tests
setenv_var MODULES_SEARCH_MATCH starts_with

# ensure avail tests are made using in depth mode
setenv_var MODULES_AVAIL_INDEPTH 1

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

# ensure default tag abbreviation is defined
setenv_var MODULES_TAG_ABBREV {auto-loaded=aL:loaded=L:hidden=H:hidden-loaded=H:forbidden=F:nearly-forbidden=nF}

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

# setup specific environment
setenv_path_var MODULEPATH $mp


#
# check option enablement
#

set ans [list [list text $mp/extdfl/2.0]]
set tserr $err_path'extdfl@1.4.5,2.0'

switch -- $install_advversspec {
    n {testouterr_cmd sh {path extdfl@1.4.5,2.0} OK $tserr}
    y {testouterr_cmd sh {path extdfl@1.4.5,2.0} $ans {}}
}

setenv_var MODULES_ADVANCED_VERSION_SPEC 0
testouterr_cmd sh {path extdfl@1.4.5,2.0} OK $tserr

setenv_var MODULES_ADVANCED_VERSION_SPEC 1
testouterr_cmd sh {path extdfl@1.4.5,2.0} $ans {}

setenv_var MODULES_ADVANCED_VERSION_SPEC badvalue
switch -- $install_advversspec {
    n {testouterr_cmd sh {path extdfl@1.4.5,2.0} OK $tserr}
    y {testouterr_cmd sh {path extdfl@1.4.5,2.0} $ans {}}
}


# enable advanced version spec for next tests
setenv_var MODULES_ADVANCED_VERSION_SPEC 1


#
# check version spec parsing
#

testouterr_cmd sh {load @1,} ERR "$err_specvers'1,'"
testouterr_cmd sh {load @, @2} ERR "$err_specvers','"
testouterr_cmd sh {load @, mod@1} ERR "$err_specvers','"
testouterr_cmd sh {load mod@,} ERR "$err_specvers','"
testouterr_cmd sh {load mod@/,} ERR "$err_specvers'/,'"
testouterr_cmd sh {load mod@,/} ERR "$err_specvers',/'"
testouterr_cmd sh {load mod @,} ERR "$err_specvers','"
testouterr_cmd sh {load mod@vers/,} ERR "$err_specvers'vers/,'"
testouterr_cmd sh {load mod @,vers/} ERR "$err_specvers',vers/'"
testouterr_cmd sh {load mod@,vers} ERR "$err_specvers',vers'"
testouterr_cmd sh {load mod@vers,} ERR "$err_specvers'vers,'"
testouterr_cmd sh {load mod@vers,,} ERR "$err_specvers'vers,,'"
testouterr_cmd sh {load mod@,,vers} ERR "$err_specvers',,vers'"
testouterr_cmd sh {load mod@v1,v2,,vers} ERR "$err_specvers'v1,v2,,vers'"
testouterr_cmd sh {load mod@@v1,v2,,vers} ERR "$err_specvers'v1,v2,,vers'"
testouterr_cmd sh {load mod@v1,deep/v2,v3} ERR "$err_specvers'v1,deep/v2,v3'"
testouterr_cmd sh {load mod@deep/v1,@deep/v2} ERR "$err_specvers'deep/v1,'"
testouterr_cmd sh {load mod/1.1,1.2,1.4} ERR $err_path'mod/1.1,1.2,1.4'

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.1]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.1]
lappend ans [list setpath MODULES_LMALTNAME extdfl/1.3.1&extdfl/default&extdfl]
testouterr_cmd sh {load extdfl@1.2.3,1.3.1} $ans {}
testouterr_cmd sh {load extdfl @1.2.3,1.3.1} $ans {}

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.7]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7]
testouterr_cmd sh {load extdfl@1.2.3,1.3.7} $ans {}
testouterr_cmd sh {load extdfl @1.2.3,1.3.7} $ans {}

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/2.0]
lappend ans [list setpath _LMFILES_ $mp/extdfl/2.0]
testouterr_cmd sh {load extdfl @1.3.1,1.3.7 @2.0} $ans {}
testouterr_cmd sh {load extdfl@1.3.7 @1.3.7,2.0} $ans {}
testouterr_cmd sh {load extdfl@1.3.7@1.3.7,2.0} $ans {}

testouterr_cmd sh {load extdfl/1@1.2.3,2.0} ERR $err_path'extdfl/1@1.2.3,2.0'
testouterr_cmd sh {load extdfl/1 @1.2.3,2.0} ERR "$err_path'extdfl/1 @1.2.3,2.0'"

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/2.0:extdfl/1.2.3]
lappend ans [list setpath _LMFILES_ $mp/extdfl/2.0:$mp/extdfl/1.2.3]
testouterr_cmd_re sh {load extdfl@1.3.7 @2.0,1.3.7 extdfl@1.1.0,1.2.3} $ans {}

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.7:extdfl2/3.9.2]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7:$mp/extdfl2/3.9.2]
testouterr_cmd_re sh {load extdfl@1.1.0,1.2.10,1.3.7 extdfl2@3.1.3,3.1.7,3.9.2} $ans {}

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/2.0:extdfl2/3.9.2]
lappend ans [list setpath _LMFILES_ $mp/extdfl/2.0:$mp/extdfl2/3.9.2]
testouterr_cmd_re sh {load extdfl@1.1.10,1.3.7 @2.0,1.2.10 extdfl2 @3.9.2,3.1.7} $ans {}

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/2.0:extdfl2/3.0.2]
lappend ans [list setpath _LMFILES_ $mp/extdfl/2.0:$mp/extdfl2/3.0.2]
testouterr_cmd_re sh {load extdfl@1.3.7 @1.3.7,2.0,1.2.10 extdfl2@3.9.2,3.1.7 @3.0.0,3.0.2} $ans {}

# unconventional spec
testouterr_cmd sh {path extdfl/@1.2.10,1.3.1,1.3.7} [list [list text $mp/extdfl/1.3.1]] {}
testouterr_cmd sh {path extdfl/ @1.2.10,1.3.1,1.3.7} [list [list text $mp/extdfl/1.3.1]] {}
testouterr_cmd sh {path extdfl@/1.2.10,/1.3.1,/1.3.7} ERR $err_specvers'/1.2.10,/1.3.1,/1.3.7'
testouterr_cmd sh {path "extdfl @1.2.10,1.3.7"} OK "$err_path'\"extdfl @1.2.10,1.3.7\"'"


#
# check consistent effect over the module search context
#

# test all sub-cmd concerned by context
testouterr_cmd sh {avail -t extdfl @1.3,1.4} OK "$mp:\nextdfl/1.3.1(default)\nextdfl/1.3.7\nextdfl/1.4.5"
testouterr_cmd_re sh {whatis extdfl@1.3.7,1.4.5} OK "$modlin $mpre $modlin
\\s+extdfl/1.3.7: extdfl/1.3.7
\\s+extdfl/1.4.5: extdfl/1.4.5"
testouterr_cmd sh {paths extdfl @1.4.5,1.3.7} [list [list text $mp/extdfl/1.3.7] [list text $mp/extdfl/1.4.5]] {}

# test various modulefile kind: alias, symver, virtual, directory hidden
testouterr_cmd sh {avail -t extdfl3@1.00,1.1} OK "$mp:\nextdfl3/1.00(@)\nextdfl3/1.1/(1.2)\nextdfl3/1.1/3"
testouterr_cmd sh {avail -t extdfl7 @1.3,3.0,3.1} OK "$mp:\nextdfl7/1.3(@)\nextdfl7/2.10(3.0)\nextdfl7/3.1"
testouterr_cmd sh {avail -t nocase @ialias,virt} OK "$mp:\nnocase/ialias(@)\nnocase/virt"
testouterr_cmd sh {avail -t extdfl @.1.3.4,1.0.2} OK "$mp:\nextdfl/.1.3.4 <H>\nextdfl/1.0.2"

# test unknown module
testouterr_cmd sh {avail -t unknown @1.2,1.4} OK {}
testouterr_cmd_re sh {whatis unknown@1.3.7,1.4.5} ERR "$err_path'unknown@1.3.7,1.4.5'"
testouterr_cmd sh {paths unknown @1.4.5,1.2.10} OK {}

# wildcard character
testouterr_cmd sh {avail -t ext*l @1.3,1.4} OK "$mp:\nextdfl/1.3.1(default)\nextdfl/1.3.7\nextdfl/1.4.5"
testouterr_cmd sh {avail -t ext?fl @1.4,1.3} OK "$mp:\nextdfl/1.3.1(default)\nextdfl/1.3.7\nextdfl/1.4.5"
testouterr_cmd sh {avail -t e??.* @1.4,1.3} OK "$mp:\ne.t.fl/1.3.1\ne.t.fl/1.3.7\ne.t.fl/1.4.5"

# test explicit default enabled
testouterr_cmd sh {whatis extdfl@1.3,1.4} ERR "$err_path'extdfl@1.3,1.4'"
testouterr_cmd sh {paths extdfl @1.3,1.4} OK {}
setenv_var MODULES_EXTENDED_DEFAULT 1
testouterr_cmd_re sh {whatis extdfl@1.3,1.4} OK "$modlin $mpre $modlin
\\s+extdfl/1.3.1: extdfl/1.3.1
\\s+extdfl/1.3.7: extdfl/1.3.7
\\s+extdfl/1.4.5: extdfl/1.4.5"
testouterr_cmd sh {paths extdfl @1.4,1.3} [list [list text $mp/extdfl/1.3.1] [list text $mp/extdfl/1.3.7] [list text $mp/extdfl/1.4.5]] {}

# test implicit default disabled
if {![is_config_locked implicit_default]} {
    setenv_var MODULES_IMPLICIT_DEFAULT 0
    testouterr_cmd_re sh {whatis extdfl@1.4,1.3} OK "$modlin $mpre $modlin
    \\s+extdfl/1.3.1: extdfl/1.3.1
    \\s+extdfl/1.3.7: extdfl/1.3.7
    \\s+extdfl/1.4.5: extdfl/1.4.5"
    testouterr_cmd sh {paths extdfl @1.3,1.4} [list [list text $mp/extdfl/1.3.1] [list text $mp/extdfl/1.3.7] [list text $mp/extdfl/1.4.5]] {}
    setenv_var MODULES_IMPLICIT_DEFAULT 1
}
unsetenv_var MODULES_EXTENDED_DEFAULT

if {$is_filesystem_icase} {
    send_user "\tskipping icase tests as underlying filesystem is case-insensitive\n"
} else {
    # test icase
    testouterr_cmd_re sh {whatis -i extDfl@1.4.5,1.3.7} OK "$modlin $mpre $modlin
\\s+extdfl/1.3.7: extdfl/1.3.7
\\s+extdfl/1.4.5: extdfl/1.4.5"
    testouterr_cmd sh {avail -t -i extdfl2@2.RC.1,2.rC.2} OK "$mp:\nextdfl2/2.rc.1\nextdfl2/2.rc.2"
    testouterr_cmd sh {avail -t icase3@1.1,1.2,1.4} OK "$mp:\nicase3/1.2"
    testouterr_cmd sh {avail -t -i icase3@1.1,1.2,1.4} OK "$mp:\nICASE3/1.1\nicase3/1.2\niCaSe3/1.4"
    testouterr_cmd sh {avail -t -i iCaSe3@1.1,1.2,1.4} OK "$mp:\nICASE3/1.1\nicase3/1.2\niCaSe3/1.4"
    testouterr_cmd sh {avail -t -i ICase3@1.1,1.2,1.4} OK "$mp:\nICASE3/1.1\nicase3/1.2\niCaSe3/1.4"
    testouterr_cmd sh {avail -t -i ICase3@vErs.1,VeRS.1} OK "$mp:\nICASE3/VERS.1\nICASE3/vers.1"
}

# contains and no-indepth tests
testouterr_cmd sh {avail -t -C deep @2,3} OK "$mp:\nnocase/deep/2\nnocase/deep/3"
testouterr_cmd sh {avail -t --no-indepth extdfl @1.2,1.4} OK "$mp:\nextdfl/1.2.3\nextdfl/1.2.10\nextdfl/1.4.5"
testouterr_cmd sh {avail -t --no-indepth nocase @deep,virt} OK "$mp:\nnocase/deep/(dirsym)\nnocase/virt"
testouterr_cmd sh {avail -t --no-indepth -C deep @2,3} OK {}

# test mod@deep/vers
testouterr_cmd sh {avail -t extdfl3@1.3/3,1.3/4} ERR $err_specvers'1.3/3,1.3/4'
testouterr_cmd_re sh {whatis extdfl3@1.3/3,1.3/4} ERR $err_specvers'1.3/3,1.3/4'

# test mod@vers where a modulefile mod@vers exist
testouterr_cmd sh {avail -t advvers@2.1,2.2} OK {}
testouterr_cmd sh {whatis advvers@2.1,2.2} ERR "$err_path'advvers@2.1,2.2'"
setenv_var MODULES_ADVANCED_VERSION_SPEC 0
testouterr_cmd sh {avail -t advvers@2.1,2.2} OK {}
testouterr_cmd sh {whatis advvers@2.1,2.2} ERR "$err_path'advvers@2.1,2.2'"
setenv_var MODULES_ADVANCED_VERSION_SPEC 1


#
# check consistent effect over the one module selection context
#

# test over existing module
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl2/3.1.7]
lappend ans [list setpath _LMFILES_ $mp/extdfl2/3.1.7]
testouterr_cmd sh {load extdfl2@3.0.2,3.1.7} $ans {}
testouterr_cmd sh {load extdfl2@3.1.6,3.1.7} $ans {}
testouterr_cmd sh {load extdfl2@3.1.8,3.1.7,3.1.6} $ans {}
testouterr_cmd sh "load $mp/extdfl2 @3.0.2,3.1.7" ERR "$err_file'$mp/extdfl2 @3.0.2,3.1.7'"
testouterr_cmd sh "load $mp/extdfl2/3.1.7 @12,11" ERR "$err_file'$mp/extdfl2/3.1.7 @12,11'"
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl3/1.1/3]
lappend ans [list setpath _LMFILES_ $mp/extdfl3/1.1/3]
lappend ans [list setpath MODULES_LMALTNAME extdfl3/1.1/3&extdfl3/1.2&al|extdfl3/1.00&al|extdfl3/1.3/3&as|extdfl3/1.1/default&as|extdfl3/1.1/latest]
testouterr_cmd sh {load extdfl3@1.1,1.0} $ans {}
testouterr_cmd sh {load extdfl3@1.1/3,1.3/4} ERR $err_specvers'1.1/3,1.3/4'
testouterr_cmd sh {unload extdfl2@3.1.7,3.0.15} OK {}
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.1]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.1]
lappend ans [list setpath MODULES_LMALTNAME extdfl/1.3.1&extdfl/default&extdfl]
testouterr_cmd sh {load extdfl@1.3.1,1.3.7} $ans {}

# test over already loaded module
setenv_loaded_module [list extdfl2/3.1.7] [list $mp/extdfl2/3.1.7]
testouterr_cmd sh {load extdfl2@3.1.7,3.0.15} OK {}
testouterr_cmd sh "load $mp/extdfl2 @3.1.7,3.0.15" ERR "$err_file'$mp/extdfl2 @3.1.7,3.0.15'"
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd sh {unload extdfl2@3.1.7,3.0.15} $ans {}
testouterr_cmd sh "unload $mp/extdfl2 @3.1.7,3.0.15" OK {}

setenv_loaded_module [list extdfl/1.3.7] [list $mp/extdfl/1.3.7]
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.7:extdfl/1.3.1]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7:$mp/extdfl/1.3.1]
lappend ans [list setpath MODULES_LMALTNAME extdfl/1.3.1&extdfl/default&extdfl]
testouterr_cmd_re sh {load extdfl@1.3.1,1.3.7} $ans {}
testouterr_cmd_re sh {load extdfl@1.3.1,1.3.7,1.4.5} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.7:extdfl/1.4.5]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7:$mp/extdfl/1.4.5]
testouterr_cmd_re sh {load extdfl@1.3.7,1.4.5} $ans {}
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd_re sh {unload extdfl@1.3.1,1.3.7} $ans {}
testouterr_cmd_re sh {unload extdfl@1.3.1,1.3.7,1.4.5} $ans {}
testouterr_cmd_re sh {unload extdfl@1.3.7,1.4.5} $ans {}

setenv_loaded_module [list $mp/extdfl2/3.1.7] [list $mp/extdfl2/3.1.7]
testouterr_cmd sh {unload extdfl2@3.0.15,3.1.7} $ans {}
testouterr_cmd sh "load $mp/extdfl2 @3.1.7,3.0.15" ERR "$err_file'$mp/extdfl2 @3.1.7,3.0.15'"

testouterr_cmd sh {unload extdfl3@1.3} OK {}
testouterr_cmd sh {unload extdfl3@1.3/4} ERR $err_specvers'1.3/4'
setenv_loaded_module [list $mp/extdfl3/1.3/4] [list $mp/extdfl3/1.3/4]
testouterr_cmd sh {unload extdfl3@1.1,1.3} $ans {}
testouterr_cmd sh {unload extdfl3@1.0,1.3/3} ERR $err_specvers'1.0,1.3/3'
testouterr_cmd sh {unload extdfl3/1.3@4,3} $ans {}

setenv_loaded_module [list $mp/extdfl2/3.1.7] [list $mp/extdfl2/3.1.7]
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl3/1.1/3]
lappend ans [list setpath _LMFILES_ $mp/extdfl3/1.1/3]
lappend ans [list setpath MODULES_LMALTNAME extdfl3/1.1/3&extdfl3/1.2&al|extdfl3/1.00&al|extdfl3/1.3/3&as|extdfl3/1.1/default&as|extdfl3/1.1/latest]
testouterr_cmd sh {switch extdfl2@3.0.2,3.1.7 extdfl3@1.0,1.1} $ans {}
unsetenv_loaded_module

# test other sub-cmds concerned by context
testouterr_cmd_re sh {help extdfl2@3.10.2,3.9.2} OK "$modlin\nModule Specific Help for $mpre/extdfl2/3.10.2:\n\n$warn_msgs: Unable to find ModulesHelp in $mpre/extdfl2/3.10.2.\n$modlin"
testouterr_cmd_re sh {display extdfl2@3.9.2,3.1.3,3.10.2} OK "$modlin\n$mpre/extdfl2/3.10.2:\n\nmodule-whatis\textdfl2/3.10.2\n$modlin"
testouterr_cmd_re sh {test extdfl2@3.9.2,3.10.2,3.1.3} OK "$modlin\nModule Specific Test for $mpre/extdfl2/3.10.2:\n\n$warn_msgs: Unable to find ModulesTest in $mpre/extdfl2/3.10.2.\n$modlin"
testouterr_cmd sh {path extdfl2@3.12,3.10.2,3.9.2} [list [list text $mp/extdfl2/3.10.2]] {}
testouterr_cmd sh {is-avail extdfl2@3.9.2,3.10.2,3.1.3} OK {}
testouterr_cmd sh {is-avail extdfl2@3.12,3.10.2,3.9.2} OK {}
testouterr_cmd sh {is-avail extdfl2@3.11,3.12} ERR {}

# test unknown module
testouterr_cmd sh {load unknown@1.3.7,1.4.5} ERR $err_path'unknown@1.3.7,1.4.5'
testouterr_cmd sh {load extdfl2 @11,12} ERR "$err_path'extdfl2 @11,12'"
testouterr_cmd sh "load $mp/unknown @1.4.5,1.3.7" ERR "$err_file'$mp/unknown @1.4.5,1.3.7'"
testouterr_cmd sh "load $mp/extdfl2@11,12" ERR "$err_file'$mp/extdfl2@11,12'"

# test various modulefile kind: alias, symver, virtual, directory hidden
testouterr_cmd sh {path extdfl3@1.0,1.00} [list [list text $mp/extdfl3/1.1/3]] {}
testouterr_cmd sh {path extdfl7 @1.3,3.0} [list [list text $mp/extdfl7/2.10]] {}
testouterr_cmd sh {path nocase @regular,virt} [list [list text $mp/nocase/1]] {}
testouterr_cmd sh {path extdfl3@1.1,1.00} [list [list text $mp/extdfl3/1.1/3]] {}
testouterr_cmd sh {path extdfl @.1.3.3,.1.3.4} [list [list text $mp/extdfl/.1.3.4]] {}
# version specified over an alias which targets a module/version modulefile
testouterr_cmd sh {path advvers3 @1.0,1.2} OK "$err_path'advvers3 @1.0,1.2'"


# wildcard characters have no effect
testouterr_cmd sh {path ext*l @1.0,1.3} {} "$err_path'ext*l @1.0,1.3'"
testouterr_cmd sh {path ext?fl @1.3,1.0} {} "$err_path'ext?fl @1.3,1.0'"
testouterr_cmd sh {path extdfl @1.4.*,1.3.?} {} "$err_path'extdfl @1.4.*,1.3.?'"
testouterr_cmd sh {path extdfl @1.3.*,1.4.?} {} "$err_path'extdfl @1.3.*,1.4.?'"
testouterr_cmd sh {path extdfl @1.3*,1.4?} {} "$err_path'extdfl @1.3*,1.4?'"
testouterr_cmd sh {path e.?.* @1.4.5,1.3.7} OK "$err_path'e.?.* @1.4.5,1.3.7'"
testouterr_cmd sh {path e.t.fl @1.4.5,1.3.7} [list [list text $mp/e.t.fl/1.4.5]] {}

# test mod@vers where a modulefile mod@vers exist
testouterr_cmd sh {load advvers@2.1,2.2} ERR $err_path'advvers@2.1,2.2'
setenv_var MODULES_ADVANCED_VERSION_SPEC 0
testouterr_cmd sh {load advvers@2.1,2.2} ERR $err_path'advvers@2.1,2.2'
setenv_var MODULES_ADVANCED_VERSION_SPEC 1

# test explicit default enabled
testouterr_cmd sh {load extdfl@1.2,1.4} ERR "$err_path'extdfl@1.2,1.4'"
setenv_var MODULES_EXTENDED_DEFAULT 1
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.4.5]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.4.5]
testouterr_cmd sh {load extdfl@1.2,1.4} $ans {}

# test implicit default disabled
if {![is_config_locked implicit_default]} {
    setenv_var MODULES_IMPLICIT_DEFAULT 0
    testouterr_cmd sh {load extdfl@1.2,1.4} ERR $err_nodefault'extdfl@1.2,1.4'
    # mixing an unexistent mod with a regular one will fail load when implicit default is disabled
    testouterr_cmd sh {load extdfl@1.4.3,1.4.5} ERR $err_nodefault'extdfl@1.4.3,1.4.5'
    set ans [list]
    lappend ans [list setpath LOADEDMODULES extdfl/1.3.1]
    lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.1]
    lappend ans [list setpath MODULES_LMALTNAME extdfl/1.3.1&extdfl/default&extdfl]
    testouterr_cmd sh {load extdfl@1.4,1.3} $ans {}
    testouterr_cmd sh {load extdfl@1.4,1.2,1} $ans {}
    testouterr_cmd sh {load extdfl @1,2} $ans {}
    setenv_var MODULES_IMPLICIT_DEFAULT 1
}
unsetenv_var MODULES_EXTENDED_DEFAULT

if {$is_filesystem_icase} {
    send_user "\tskipping icase tests as underlying filesystem is case-insensitive\n"
} else {
    # test icase
    set ans [list]
    lappend ans [list setpath LOADEDMODULES extdfl/1.4.5]
    lappend ans [list setpath _LMFILES_ $mp/extdfl/1.4.5]
    testouterr_cmd sh {load -i extDfl@1.3.7,1.4.5} $ans {}
    set ans [list]
    lappend ans [list setpath LOADEDMODULES extdfl2/2.rc.2]
    lappend ans [list setpath _LMFILES_ $mp/extdfl2/2.rc.2]
    testouterr_cmd sh {load -i extdfl2@2.rC.2,2.RC.1} $ans {}
    set ans [list]
    lappend ans [list setpath LOADEDMODULES icase3/1.2]
    lappend ans [list setpath _LMFILES_ $mp/icase3/1.2]
    lappend ans [list setpath MODULES_LMALTNAME icase3/1.2&as|icase3/default&as|icase3/latest]
    testouterr_cmd sh {load -i icase3@1.1,1.2,1.4} $ans {}
    testouterr_cmd sh {load -i ICase3@1.1,1.2,1.4} $ans {}
    set ans [list]
    lappend ans [list setpath LOADEDMODULES iCaSe3/1.4]
    lappend ans [list setpath _LMFILES_ $mp/iCaSe3/1.4]
    lappend ans [list setpath MODULES_LMALTNAME iCaSe3/1.4&as|iCaSe3/default&as|iCaSe3/latest]
    testouterr_cmd sh {load -i iCaSe3@1.1,1.2,1.4} $ans {}
    testouterr_cmd sh {load -i icase3@1.1,1.4} $ans {}
    setenv_var MODULES_EXTENDED_DEFAULT 1
    testouterr_cmd sh {load -i iCaSe3@1.1,1.2,1.4} $ans {}
    testouterr_cmd sh {load -i iCaSe3@1,2} $ans {}
    set ans [list]
    lappend ans [list setpath LOADEDMODULES icase3/1.2]
    lappend ans [list setpath _LMFILES_ $mp/icase3/1.2]
    lappend ans [list setpath MODULES_LMALTNAME icase3/1.2&as|icase3/default&as|icase3/latest]
    testouterr_cmd sh {load -i ICase3@1.1,1.2,1.4} $ans {}
    testouterr_cmd sh {load -i ICase3@1,2} $ans {}
    unsetenv_var MODULES_EXTENDED_DEFAULT
    set ans [list]
    lappend ans [list setpath LOADEDMODULES ICASE3/vers.1]
    lappend ans [list setpath _LMFILES_ $mp/ICASE3/vers.1]
    lappend ans [list setpath MODULES_LMALTNAME ICASE3/vers.1&as|ICASE3/default&as|ICASE3/latest]
    testouterr_cmd sh {load -i iCaSe3@vers.1,VERS.1} $ans {}
    testouterr_cmd sh {load -i iCaSe3@vErs.1,VeRS.1} $ans {}
    testouterr_cmd sh {load -i ICASE3@vErs.1,VeRS.1} $ans {}
}

# check effect over [module-info specified] call
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers/1.2]
lappend ans [list setpath _LMFILES_ $mp/advvers/1.2]
lappend ans [list set ts advvers@1.1,1.2]
testouterr_cmd sh {load advvers@1.1,1.2} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers/1.2]
lappend ans [list setpath _LMFILES_ $mp/advvers/1.2]
lappend ans [list set ts "advvers @1.0,1.1,1.2"]
testouterr_cmd sh {load advvers @1.0,1.1,1.2} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers/1.2]
lappend ans [list setpath _LMFILES_ $mp/advvers/1.2]
lappend ans [list set ts "advvers@1.0 @1.1,1.2"]
testouterr_cmd sh {load advvers@1.0 @1.1,1.2} $ans {}


#
# check consistent effect over the module compatibility check context
#

# load prereq
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/1.0:advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/1.0:$mp/advvers4/1.3]
lappend ans [list setpath MODULES_LMNOTUASKED advvers5/1.0]
lappend ans [list setpath MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0]
testouterr_cmd_re sh {load --auto advvers4@1.3} $ans [msg_top_load advvers4/1.3 {} advvers5/1.0 {}]

# prereq already loaded
setenv_loaded_module [list advvers5/1.0] [list $mp/advvers5/1.0]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/1.0:advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/1.0:$mp/advvers4/1.3]
lappend ans [list setpath MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0]
testouterr_cmd_re sh {load --auto advvers4@1.3} $ans {}
setenv_loaded_module [list advvers5/0.9] [list $mp/advvers5/0.9]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/0.9:advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/0.9:$mp/advvers4/1.3]
lappend ans [list setpath MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0]
testouterr_cmd_re sh {load --auto advvers4@1.3} $ans {}
unsetenv_loaded_module

# with adv_version_spec disabled
setenv_var MODULES_ADVANCED_VERSION_SPEC 0
testouterr_cmd_re sh {load --auto advvers4/1.3} ERR [msg_load advvers4/1.3 $err_path'advvers5@0.9,1.0' [err_reqlo advvers5@0.9,1.0]]
setenv_loaded_module [list advvers5/1.0] [list $mp/advvers5/1.0]
testouterr_cmd_re sh {load --auto advvers4/1.3} ERR [msg_load advvers4/1.3 $err_path'advvers5@0.9,1.0' [err_reqlo advvers5@0.9,1.0]]
unsetenv_loaded_module
setenv_var MODULES_ADVANCED_VERSION_SPEC 1

# prereq requires loading module
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/1.1:advvers4/1.4]
lappend ans [list setpath _LMFILES_ $mp/advvers5/1.1:$mp/advvers4/1.4]
lappend ans [list setpath MODULES_LMNOTUASKED advvers5/1.1]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/1.1&advvers4 @1.3,1.4:advvers4/1.4&advvers5 @1.0,1.1"]
testouterr_cmd_re sh {load --auto advvers4@1.4} $ans [msg_top_load advvers4/1.4 {} advvers5/1.1 {}]

# prereq not loaded
testouterr_cmd_re sh {load --no-auto advvers4@1.3} ERR [msg_load advvers4/1.3 [err_prereq advvers5@0.9,1.0]]
testouterr_cmd_re sh {load --no-auto advvers4@1.4} ERR [msg_load advvers4/1.4 [err_prereq advvers5@1.0,1.1]]

# load module by-pass its prereq
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.3]
lappend ans [list setpath MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0]
testouterr_cmd_re sh {load --no-auto --force advvers4@1.3} $ans [msg_load advvers4/1.3 [err_reqmisf advvers5@0.9,1.0]]

# then load missing prereq
setenv_loaded_module [list advvers4/1.3] [list $mp/advvers4/1.3]
setenv_path_var MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/1.0:advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/1.0:$mp/advvers4/1.3]
lappend ans [list setpath MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0]
testouterr_cmd_re sh {load --auto advvers5@1.0} $ans [msg_top_load advvers5/1.0 {} {} advvers4/1.3]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/0.9:advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/0.9:$mp/advvers4/1.3]
lappend ans [list setpath MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0]
testouterr_cmd_re sh {load --auto advvers5@0.9} $ans [msg_top_load advvers5/0.9 {} {} advvers4/1.3]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ

# prereq fails to load
testouterr_cmd sh {load --auto advvers5@1.2} ERR [msg_load advvers4/1.5.2 $err_evalabort]\n\n[msg_load advvers5/1.2 [err_reqlo advvers4@1.5.1,1.5.2]]

# unload module and its auto loaded requirement (UReqUn)
setenv_loaded_module [list advvers5/1.0 advvers4/1.3] [list $mp/advvers5/1.0 $mp/advvers4/1.3] [list advvers5/1.0]
setenv_path_var MODULES_LMPREREQ advvers4/1.3&advvers5@0.9,1.0
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
lappend ans [list unsetpath MODULES_LMPREREQ]
testouterr_cmd sh {unload --auto advvers4@1.3} $ans [msg_top_unload advvers4/1.3 {} advvers5/1.0 {}]

# unload prereq in auto mode
testouterr_cmd_re sh {unload --auto advvers5@1.0} $ans [msg_top_unload advvers5/1.0 advvers4/1.3 {} {}]

# unload prereq in no-auto mode
testouterr_cmd_re sh {unload --no-auto advvers5@1.0} ERR [msg_unload advvers5/1.0 [err_prerequn advvers4/1.3]]

# force prereq unload
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.3]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --force --no-auto advvers5@1.0} $ans [msg_unload advvers5/1.0 [err_deplof advvers4/1.3]]

# test with alternative prereq
setenv_loaded_module [list advvers5/0.9 advvers4/1.3] [list $mp/advvers5/0.9 $mp/advvers4/1.3] [list advvers5/0.9]
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
lappend ans [list unsetpath MODULES_LMPREREQ]
testouterr_cmd sh {unload --auto advvers4@1.3} $ans [msg_top_unload advvers4/1.3 {} advvers5/0.9 {}]
testouterr_cmd_re sh {unload --auto advvers5@0.9} $ans [msg_top_unload advvers5/0.9 advvers4/1.3 {} {}]
testouterr_cmd_re sh {unload --no-auto advvers5@0.9} ERR [msg_unload advvers5/0.9 [err_prerequn advvers4/1.3]]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.3]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.3]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --force --no-auto advvers5@0.9} $ans [msg_unload advvers5/0.9 [err_deplof advvers4/1.3]]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ

# prereq with list of modules
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.2:advvers5/1.3.1]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.2:$mp/advvers5/1.3.1]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/1.3.1&advvers4 @1.1,1.2|advvers4@1.4 @1.5,1.6"]
lappend ans [list set ts "advvers4 @1.1,1.2"]
lappend ans [list setpath MODULES_LMNOTUASKED advvers4/1.2]
testouterr_cmd_re sh {load --auto advvers5@1.3.1} $ans [msg_top_load advvers5/1.3.1 {} advvers4/1.2 {}]
setenv_loaded_module [list advvers4/1.6] [list $mp/advvers4/1.6]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.6:advvers5/1.3.1]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.6:$mp/advvers5/1.3.1]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/1.3.1&advvers4 @1.1,1.2|advvers4@1.4 @1.5,1.6"]
testouterr_cmd_re sh {load --auto advvers5@1.3.1} $ans {}
unsetenv_loaded_module
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.6:advvers5/1.3.2]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.6:$mp/advvers5/1.3.2]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/1.3.2&advvers4 @1.1,1.5.1,1.2|advvers4@1.4 @1.5,1.6"]
lappend ans [list setpath MODULES_LMNOTUASKED advvers4/1.6]
lappend ans [list ERR]
testouterr_cmd_re sh {load --auto advvers5@1.3.2} $ans [msg_top_load advvers5/1.3.2 {} advvers4/1.6 {}]


# conflict not loaded
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.0]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"]
testouterr_cmd sh {load advvers5@3.0} $ans {}

# conflict loaded
setenv_loaded_module [list advvers5/3.0] [list $mp/advvers5/3.0]
setenv_path_var MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"
testouterr_cmd sh {load advvers4@1.6} ERR [msg_load advvers4/1.6 [err_conflict advvers5/3.0]]
testouterr_cmd sh {load advvers4@1.2} ERR [msg_load advvers4/1.2 [err_conflict advvers5/3.0]]
unsetenv_path_var MODULES_LMCONFLICT
setenv_loaded_module [list advvers4/1.6] [list $mp/advvers4/1.6]
testouterr_cmd sh {load advvers5@3.0} ERR [msg_load advvers5/3.0 [err_conflict advvers4 @1.1,1.2,1.6]]
setenv_loaded_module [list advvers4/1.2] [list $mp/advvers4/1.2]
testouterr_cmd sh {load advvers5@3.0} ERR [msg_load advvers5/3.0 [err_conflict advvers4 @1.1,1.2,1.6]]

# conflict loaded but conflicting module load forced
setenv_loaded_module [list advvers4/1.6] [list $mp/advvers4/1.6]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.6:advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.6:$mp/advvers5/3.0]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"]
testouterr_cmd_re sh {load --force advvers5@3.0} $ans [msg_load advvers5/3.0 [err_conlof advvers4@1.1,1.2,1.6]]
setenv_loaded_module [list advvers4/1.2] [list $mp/advvers4/1.2]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.2:advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.2:$mp/advvers5/3.0]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"]
testouterr_cmd_re sh {load --force advvers5@3.0} $ans [msg_load advvers5/3.0 [err_conlof advvers4@1.1,1.2,1.6]]

# then unload conflict
setenv_loaded_module [list advvers4/1.6 advvers5/3.0] [list $mp/advvers4/1.6 $mp/advvers5/3.0]
setenv_path_var MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.0]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"]
testouterr_cmd sh {unload --auto advvers4@1.6} $ans [msg_top_unload advvers4/1.6 {} {} advvers5/3.0]
setenv_loaded_module [list advvers4/1.6 advvers4/1.2 advvers5/3.0] [list $mp/advvers4/1.6 $mp/advvers4/1.2 $mp/advvers5/3.0]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.6:advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.6:$mp/advvers5/3.0]
lappend ans [list unset ts]
testouterr_cmd_re sh {unload --auto advvers4@1.2} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.2:advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.2:$mp/advvers5/3.0]
testouterr_cmd_re sh {unload --auto advvers4@1.6} $ans {}
# lastly loaded unload
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers4/1.6:advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers4/1.6:$mp/advvers5/3.0]
lappend ans [list unset ts]
testouterr_cmd_re sh {unload --auto advvers4@1.2,1.6} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.0]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"]
lappend ans [list unset ts]
testouterr_cmd_re sh {unload --auto advvers4@1.6 advvers4@1.2} $ans [msg_top_unload advvers4/1.2 {} {} advvers5/3.0]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/3.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.0]
lappend ans [list unset ts]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.0&advvers4 @1.1,1.2,1.6"]
testouterr_cmd_re sh {unload --auto advvers4@1.2 advvers4@1.6} $ans [msg_top_unload advvers4/1.6 {} {} advvers5/3.0]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMCONFLICT

# loading module raise conflict
testouterr_cmd_re sh {load --auto advvers5@3.1} ERR [msg_load advvers4/1.7 [err_conloi advvers5@3.1,3.0]]\n\n[msg_load advvers5/3.1 [err_reqlo advvers4@1.6,1.7]]

# conflict loaded but successful attempt to unload it
setenv_loaded_module [list advvers4/1.6] [list $mp/advvers4/1.6]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/3.2]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.2]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.2&advvers4@1.7,1.6&advvers4 @1.7,1.8"]
testouterr_cmd sh {load --auto advvers5 @3.2} $ans [msg_top_load advvers5/3.2 advvers4/1.6 {} {}]

# conflict loaded but failed attempt to unload it
setenv_loaded_module [list advvers4/1.7 advvers4/1.8] [list $mp/advvers4/1.7 $mp/advvers4/1.8]
testouterr_cmd sh {load --auto advvers5 @3.2} ERR [msg_unload advvers4/1.8 $err_evalabort]
unsetenv_loaded_module

# conflict with list of modules
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/3.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.3]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/3.3&advvers4@1.5.2,1.6&advvers4 @1.6,1.8&advvers4 @1.5.1 @1.6,1.7"]
testouterr_cmd sh {load --auto advvers5 @3.3} $ans {}
setenv_loaded_module [list advvers4/1.7] [list $mp/advvers4/1.7]
testouterr_cmd sh {load --auto advvers5 @3.3} ERR [msg_load advvers5/3.3 [err_conflict advvers4 @1.5.1 @1.6,1.7]]
setenv_loaded_module [list advvers5/3.3] [list $mp/advvers4/3.3]
setenv_path_var MODULES_LMCONFLICT "advvers5/3.3&advvers4@1.5.2,1.6&advvers4 @1.6,1.8&advvers4 @1.5.1 @1.6,1.7"
testouterr_cmd sh {load --auto advvers4@1.7} ERR [msg_load advvers4/1.7 [err_conflict advvers5/3.3]]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMCONFLICT

# check cyclic dependencies
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/4.2:advvers5/4.1:advvers5/4.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/4.2:$mp/advvers5/4.1:$mp/advvers5/4.0]
lappend ans [list setpath MODULES_LMPREREQ advvers5/4.2&advvers5@3.9,4.0:advvers5/4.1&advvers5@3.1,4.2:advvers5/4.0&advvers5@3.0,4.1]
lappend ans [list setpath MODULES_LMNOTUASKED advvers5/4.2:advvers5/4.1]
testouterr_cmd_re sh {load --auto advvers5@4.0} $ans [msg_top_load advvers5/4.0 {} [list advvers5/4.2 advvers5/4.1] {}]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/4.0:advvers5/4.2:advvers5/4.1]
lappend ans [list setpath _LMFILES_ $mp/advvers5/4.0:$mp/advvers5/4.2:$mp/advvers5/4.1]
lappend ans [list setpath MODULES_LMPREREQ advvers5/4.0&advvers5@3.0,4.1:advvers5/4.2&advvers5@3.9,4.0:advvers5/4.1&advvers5@3.1,4.2]
lappend ans [list setpath MODULES_LMNOTUASKED advvers5/4.0:advvers5/4.2]
testouterr_cmd_re sh {load --auto advvers5@4.1} $ans [msg_top_load advvers5/4.1 {} [list advvers5/4.0 advvers5/4.2] {}]
setenv_loaded_module [list advvers5/4.2 advvers5/4.1 advvers5/4.0] [list $mp/advvers5/4.2 $mp/advvers5/4.1 $mp/advvers5/4.0] [list advvers5/4.2 advvers5/4.1]
setenv_path_var MODULES_LMPREREQ advvers5/4.2&advvers5@3.9,4.0:advvers5/4.1&advvers5@3.1,4.2:advvers5/4.0&advvers5@3.0,4.1
set ans [list]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --auto advvers5@4.1} $ans [msg_top_unload advvers5/4.1 advvers5/4.0 advvers5/4.2 {}]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ

# prereq and conflict complex unload situation
setenv_loaded_module [list advvers5/5.0 advvers5/5.1 advvers5/5.2 advvers5/5.3] [list $mp/advvers5/5.0 $mp/advvers5/5.1 $mp/advvers5/5.2 $mp/advvers5/5.3] [list advvers5/5.0 advvers5/5.1]
setenv_path_var MODULES_LMPREREQ "advvers5/5.2&advvers5 @5.0,5.1" "advvers5/5.1&advvers5@4.0,5.0"
setenv_path_var MODULES_LMCONFLICT "advvers5/5.3&advvers5 @5.1,5.0"
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/5.3]
lappend ans [list setpath _LMFILES_ $mp/advvers5/5.3]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/5.3&advvers5 @5.1,5.0"]
lappend ans [list unsetpath MODULES_LMPREREQ]
lappend ans [list unsetpath MODULES_LMNOTUASKED]
testouterr_cmd_re sh {unload --auto advvers5@5.2} $ans [msg_top_unload advvers5/5.2 {} [list advvers5/5.1 advvers5/5.0] advvers5/5.3]

# prereq and conflict complex load situation
setenv_loaded_module [list advvers5/5.1 advvers5/5.2 advvers5/5.3] [list $mp/advvers5/5.1 $mp/advvers5/5.2 $mp/advvers5/5.3] [list advvers5/5.1]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/5.1:advvers5/5.2:advvers5/5.3:advvers5/5.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/5.1:$mp/advvers5/5.2:$mp/advvers5/5.3:$mp/advvers5/5.0]
testouterr_cmd_re sh {load --force --auto advvers5@5.0} $ans [msg_load advvers5/5.0 [err_conlof advvers5/5.3]]

setenv_path_var MODULES_LMCONFLICT "advvers5/5.3&advvers5 @5.1,5.0:advvers5/5.2&advvers5 @4.0,5.0"
testouterr_cmd_re sh {load --force --auto advvers5@5.0} $ans [msg_load advvers5/5.0 [err_conlof advvers5/5.2 advvers5/5.3]]

# another set with more complex prereq definition
setenv_loaded_module [list advvers5/6.0 advvers5/6.1 advvers5/6.2 advvers5/6.3 advvers5/6.5 advvers5/6.4 advvers5/6.7 advvers5/6.6] [list $mp/advvers5/6.0 $mp/advvers5/6.1 $mp/advvers5/6.2 $mp/advvers5/6.3 $mp/advvers5/6.5 $mp/advvers5/6.4 $mp/advvers5/6.7 $mp/advvers5/6.6] [list advvers5/6.0 advvers5/6.1 advvers5/6.5 advvers5/6.7]
setenv_path_var MODULES_LMPREREQ "advvers5/6.2&advvers5@5.0,6.0&advvers5 @6.0,6.1" "advvers5/6.3&advvers5@5.0,6.0" "advvers5/6.4&advvers5 @6.0,6.5|advvers5@6.0 @5.0,6.1" "advvers5/6.6&advvers5@6.1,6.7&advvers5@6.0,6.1"
setenv_path_var MODULES_LMCONFLICT "advvers5/6.3&advvers5@6.0 @6.1 @5.1,6.2"
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/6.0:advvers5/6.5:advvers5/6.3:advvers5/6.4]
lappend ans [list setpath _LMFILES_ $mp/advvers5/6.0:$mp/advvers5/6.5:$mp/advvers5/6.3:$mp/advvers5/6.4]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/6.3&advvers5@5.0,6.0:advvers5/6.4&advvers5 @6.0,6.5|advvers5@6.0 @5.0,6.1"]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/6.3&advvers5@6.0 @6.1 @5.1,6.2"]
lappend ans [list setpath MODULES_LMNOTUASKED advvers5/6.0:advvers5/6.5]
testouterr_cmd_re sh {unload --auto advvers5@6.1} $ans [msg_top_unload advvers5/6.1 [list advvers5/6.6 advvers5/6.2] advvers5/6.7 [list advvers5/6.3 advvers5/6.4]]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ
unsetenv_path_var MODULES_LMCONFLICT

# load/unload additionnal prereq
# no dependent reload triggered as requirement is already satisfied
setenv_loaded_module [list advvers5/6.0 advvers5/6.2] [list $mp/advvers5/6.0 $mp/advvers5/6.2] [list advvers5/6.0]
setenv_path_var MODULES_LMPREREQ "advvers5/6.2&advvers5@5.0,6.0&advvers5 @6.0,6.1"
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/6.0:advvers5/6.2:advvers5/5.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/6.0:$mp/advvers5/6.2:$mp/advvers5/5.0]
testouterr_cmd_re sh {load --auto advvers5@5.0} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/6.0:advvers5/6.2:advvers5/6.1]
lappend ans [list setpath _LMFILES_ $mp/advvers5/6.0:$mp/advvers5/6.2:$mp/advvers5/6.1]
testouterr_cmd_re sh {load --auto advvers5@6.1} $ans {}
unsetenv_loaded_module
unsetenv_path_var MODULES_LMPREREQ

# conflict declared on an alias name whose target unload has been attempted but failed
setenv_loaded_module [list advvers4/2.1] [list $mp/advvers4/2.1]
setenv_path_var MODULES_LMALTNAME advvers4/2.1&advvers5/9.2
testouterr_cmd_re sh {load --auto advvers5@9.0} ERR [msg_load advvers5/9.0 [err_conun advvers5@9.1,9.2,9.3]]
unsetenv_loaded_module
unsetenv_path_var MODULES_LMALTNAME

# bad specification in modulefile
testouterr_cmd sh {load --auto advvers5@2.0} ERR [msg_load advvers5/2.0 [msg_moderr "$msg_nomodnameinarg '@1,2'" {prereq @1,2} $mp/advvers5/2.0 2]]
testouterr_cmd sh {load --auto advvers5@2.1} ERR [msg_load advvers5/2.1 [msg_moderr "$msg_nomodnameinarg '@1,2'" {conflict @1,2} $mp/advvers5/2.1 2]]
testouterr_cmd sh {load --auto advvers5@2.2} ERR [msg_load advvers5/2.2 [msg_moderr "$msg_nomodnameinarg '@1,2'" {is-loaded @1,2} $mp/advvers5/2.2 2]]

# test other sub-cmd concerned by context
setenv_loaded_module [list advvers5/3.0] [list $mp/advvers5/3.0]
testouterr_cmd sh {is-loaded advvers5@2.2,3.1} ERR {}
testouterr_cmd sh {is-loaded advvers5@3.0,2.1} OK {}
testouterr_cmd sh {is-loaded advvers5@2.2,2.0 @4,5,3.0} OK {}
set ans [list]
lappend ans [list set ts1 1]
lappend ans [list set ts2 0]
lappend ans [list setpath LOADEDMODULES advvers5/3.0:advvers5/7.0]
lappend ans [list setpath _LMFILES_ $mp/advvers5/3.0:$mp/advvers5/7.0]
testouterr_cmd_re sh {load advvers5@7.0} $ans {}
unsetenv_loaded_module

# test unknown module
testouterr_cmd sh {load --auto advvers5@8.1.1} ERR [msg_load advvers5/8.1.1 $err_path'unknown@1.3.1,1.4.5' [err_reqlo unknown@1.3.1,1.4.5]]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers5/8.1.2]
lappend ans [list setpath _LMFILES_ $mp/advvers5/8.1.2]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/8.1.2&unknown @1.3.1,1.4.5"]
testouterr_cmd sh {load --auto advvers5@8.1.2} $ans {}

# test mod@deep/vers
testouterr_cmd sh {load --auto advvers5@8.2.1} ERR [msg_load advvers5/8.2.1 [msg_moderr "$msg_invversspec 'deep/1,deep/2'" {prereq advvers@deep/1,deep/2} $mp/advvers5/8.2.1 2]]
testouterr_cmd sh {load --auto advvers5@8.2.2} ERR [msg_load advvers5/8.2.2 [msg_moderr "$msg_invversspec 'deep/1,deep/2'" {conflict advvers @deep/1,deep/2} $mp/advvers5/8.2.2 2]]

setenv_loaded_module [list advvers/deep/1/1.2] [list $mp/advvers/deep/1/1.2]
testouterr_cmd sh {load --auto advvers5@8.2.1} ERR [msg_load advvers5/8.2.1 [msg_moderr "$msg_invversspec 'deep/1,deep/2'" {prereq advvers@deep/1,deep/2} $mp/advvers5/8.2.1 2]]
testouterr_cmd sh {load --auto advvers5@8.2.2} ERR [msg_load advvers5/8.2.2 [msg_moderr "$msg_invversspec 'deep/1,deep/2'" {conflict advvers @deep/1,deep/2} $mp/advvers5/8.2.2 2]]
unsetenv_loaded_module

# test mod@vers where a modulefile mod@vers exist
testouterr_cmd_re sh {load --auto advvers5@8.3.1} ERR [msg_load advvers5/8.3.1 $err_path'advvers@2.1,2.2' [err_reqlo advvers@2.1,2.2]]
setenv_loaded_module [list advvers@2.1] [list $mp/advvers@2.1]
testouterr_cmd_re sh {load --auto advvers5@8.3.1} ERR [msg_load advvers5/8.3.1 $err_path'advvers@2.1,2.2' [err_reqlo advvers@2.1,2.2]]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers@2.1:advvers5/8.3.2]
lappend ans [list setpath _LMFILES_ $mp/advvers@2.1:$mp/advvers5/8.3.2]
lappend ans [list setpath MODULES_LMCONFLICT "advvers5/8.3.2&advvers@2.1,2.2"]
testouterr_cmd_re sh {load --auto advvers5@8.3.2} $ans {}
unsetenv_loaded_module

# test explicit default enabled
setenv_var MODULES_EXTENDED_DEFAULT 1
set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.1:extdfl/1.4.5:advvers5/8.4.1]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.1:$mp/extdfl/1.4.5:$mp/advvers5/8.4.1]
lappend ans [list setpath MODULES_LMNOTUASKED extdfl/1.3.1:extdfl/1.4.5]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/8.4.1&extdfl @1.3,2&extdfl @1.4,1.2"]
lappend ans [list setpath MODULES_LMALTNAME extdfl/1.3.1&extdfl/default&extdfl]
testouterr_cmd_re sh {load --auto advvers5@8.4.1} $ans [msg_top_load advvers5/8.4.1 {} [list extdfl/1.3.1 extdfl/1.4.5] {}]
setenv_loaded_module [list extdfl/1.3.7 extdfl/1.4.5] [list $mp/extdfl/1.3.7 $mp/extdfl/1.4.5]

set ans [list]
lappend ans [list setpath LOADEDMODULES extdfl/1.3.7:extdfl/1.4.5:advvers5/8.4.1]
lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7:$mp/extdfl/1.4.5:$mp/advvers5/8.4.1]
lappend ans [list setpath MODULES_LMPREREQ "advvers5/8.4.1&extdfl @1.3,2&extdfl @1.4,1.2"]
testouterr_cmd_re sh {load --auto advvers5@8.4.1} $ans {}

testouterr_cmd_re sh {load --auto advvers5@8.4.2} ERR [msg_load advvers5/8.4.2 [err_conflict extdfl @1.3,2]]

# test implicit default disabled
if {![is_config_locked implicit_default]} {
    setenv_var MODULES_IMPLICIT_DEFAULT 0
    testouterr_cmd_re sh {load --auto advvers5@8.4.2} ERR [msg_load advvers5/8.4.2 [err_conflict extdfl @1.3,2]]
    unsetenv_loaded_module

    testouterr_cmd_re sh {load --auto advvers5@8.4.1} ERR [msg_load advvers5/8.4.1 "$err_nodefault'extdfl @1.4,1.2'" [err_reqlo extdfl@1.4,1.2]]
    setenv_var MODULES_IMPLICIT_DEFAULT 1
}
unsetenv_loaded_module
unsetenv_var MODULES_EXTENDED_DEFAULT

if {$is_filesystem_icase} {
    send_user "\tskipping icase tests as underlying filesystem is case-insensitive\n"
} else {
    # test icase
    set ans [list]
    lappend ans [list setpath LOADEDMODULES extdfl/1.3.7:advvers5/8.5.1]
    lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7:$mp/advvers5/8.5.1]
    lappend ans [list setpath MODULES_LMNOTUASKED extdfl/1.3.7]
    lappend ans [list setpath MODULES_LMPREREQ advvers5/8.5.1&ExtDfl@1.3.7,1.2.3]
    testouterr_cmd_re sh {load -i --auto advvers5@8.5.1} $ans [msg_top_load advvers5/8.5.1 {} extdfl/1.3.7 {}]

    setenv_loaded_module [list extdfl/1.3.7] [list $mp/extdfl/1.3.7]
    set ans [list]
    lappend ans [list setpath LOADEDMODULES extdfl/1.3.7:advvers5/8.5.1]
    lappend ans [list setpath _LMFILES_ $mp/extdfl/1.3.7:$mp/advvers5/8.5.1]
    lappend ans [list setpath MODULES_LMPREREQ advvers5/8.5.1&ExtDfl@1.3.7,1.2.3]
    testouterr_cmd_re sh {load -i --auto advvers5@8.5.1} $ans {}

    testouterr_cmd_re sh {load -i --auto advvers5@8.5.2} ERR [msg_load advvers5/8.5.2 [err_conflict EXTDFL@1.3.7,1.2.3]]
    unsetenv_loaded_module
}


# test dependency resolution applies the same against a non default version loaded
# whether module load/prereq is used to declared and whatever the auto_handling module enabled
setenv_loaded_module [list advvers8/1] [list $mp/advvers8/1]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers8/1:advvers10/1]
lappend ans [list setpath _LMFILES_ $mp/advvers8/1:$mp/advvers10/1]
lappend ans [list setpath MODULES_LMPREREQ advvers10/1&advvers8@1,3]
testouterr_cmd_re sh {load --auto advvers10/1} $ans {}
testouterr_cmd_re sh {load --no-auto advvers10/1} $ans {}
lappend ans [list setpath LOADEDMODULES advvers8/1:advvers10/2]
lappend ans [list setpath _LMFILES_ $mp/advvers8/1:$mp/advvers10/2]
lappend ans [list setpath MODULES_LMPREREQ advvers10/2&advvers8@1,3]
testouterr_cmd_re sh {load --auto advvers10/2} $ans {}
testouterr_cmd_re sh {load --no-auto advvers10/2} $ans {}

setenv_loaded_module [list advvers9/3] [list $mp/advvers9/3]
set ans [list]
lappend ans [list setpath LOADEDMODULES advvers9/3:advvers10/3]
lappend ans [list setpath _LMFILES_ $mp/advvers9/3:$mp/advvers10/3]
lappend ans [list setpath MODULES_LMPREREQ advvers10/3&advvers9@1,3]
testouterr_cmd_re sh {load --auto advvers10/3} $ans {}
testouterr_cmd_re sh {load --no-auto advvers10/3} $ans {}
lappend ans [list setpath LOADEDMODULES advvers9/3:advvers10/4]
lappend ans [list setpath _LMFILES_ $mp/advvers9/3:$mp/advvers10/4]
lappend ans [list setpath MODULES_LMPREREQ advvers10/4&advvers9@1,3]
testouterr_cmd_re sh {load --auto advvers10/4} $ans {}
testouterr_cmd_re sh {load --no-auto advvers10/4} $ans {}
unsetenv_loaded_module


# test with list of symbolic versions
set ans [list]
lappend ans [list setpath LOADEDMODULES autosym6/latest]
lappend ans [list setpath _LMFILES_ $mp/autosym6/2]
testouterr_cmd sh {load autosym6@latest,zzz} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES autosym6/2]
lappend ans [list setpath _LMFILES_ $mp/autosym6/2]
lappend ans [list setpath MODULES_LMALTNAME autosym6/2&autosym6/yyy]
testouterr_cmd sh {load autosym6@yyy,zzz} $ans {}
set ans [list]
lappend ans [list setpath LOADEDMODULES autosym3/1]
lappend ans [list setpath _LMFILES_ $mp/autosym3/1]
lappend ans [list setpath MODULES_LMALTNAME autosym3/1&autosym3/default&autosym3]
testouterr_cmd sh {load autosym3@default,latest} $ans {}


#
#  Cleanup
#

reset_test_env
