##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2018/01/11
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:		load, unload, switch, list
#   Modulefiles:	loc_virt1
#   Sub-Command:
#
#   Comment:	%C{
#			Check the 'module-virtual' command
#		}C%
#
##############################################################################

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

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

set datetime_re "\[0-9\/]{10} \[0-9:]{8}"

set modsym_1 "loc_virt1/1.0"
set module_1 $modsym_1
set modulefile_1 "$modpath/loc_virt1/.common"
set modulefilere_1 "$modpathre/loc_virt1/.common"
set modavail_1 "$module_1\\s+$datetime_re"

set modsym_2 "loc_virt1/2.0"
set module_2 $modsym_2
set modulefile_2 "$modpath/loc_virt1/.common"
set modulefilere_2 "$modpathre/loc_virt1/.common"
set modavail_2 "$module_2\\s+$datetime_re"

set modsym_3 "loc_virt1/3.0"
set module_3 $modsym_3
set modulefile_3 "$env(TESTSUITEDIR)/virttargets/loc_virt"
set modulefilere_3 [regsub -all "\(\[.+?\]\)" $modulefile_3 {\\\1}]
set modavail_3 "$module_3\\s+$datetime_re"

set modsym_4 "loc_virt1/4.0"
set module_4 $modsym_4
set modulefile_4 "$env(TESTSUITEDIR)/virttargets/loc_virt"
set modulefilere_4 [regsub -all "\(\[.+?\]\)" $modulefile_4 {\\\1}]
set modavail_4 "$module_4\\s+$datetime_re"

set modsym_5 "loc_virt1"
set module_5 $module_4
set modulefile_5 $modulefile_4
set modulefilere_5 $modulefilere_4
set modavail_5 "$modavail_1\n$modavail_2\n$modavail_3\n$modavail_4"
set modwhatis_5 "\\s+$module_1: $module_1\n\\s+$module_2: $module_2\n\\s+$module_3: $module_3\n\\s+$module_4: $module_4"
set modpaths_5 [list $modulefile_1 $modulefile_3]
set nodefault_5 "$err_nodefault'$modsym_5'"

set modsym_6 "loc_virt1/6.0"
set module_6 $modsym_6
set modulefile_6 "$modpath/loc_virt1/notexist"
set modulefilere_6 "$modpathre/loc_virt1/notexist"
set modavail_6 ""
set modwhatis_6 ""
set modpaths_6 [list]
set moderr_6 "$err_file'$modulefile_6'"
set modunerr_6 [msg_unload $module_6 $moderr_6]

set modsym_7 "loc_virt1/7.0"
set module_7 $modsym_7
set modulefile_7 "/path/to/notexist"
set modulefilere_7 $modulefile_7
set modavail_7 ""
set modwhatis_7 ""
set modpaths_7 [list]
set moderr_7 "$err_file'$modulefile_7'"
set modunerr_7 [msg_unload $module_7 $moderr_7]

set modsym_8 "loc_virt1/8.0"
set module_8 $modsym_8
set modulefile_8 "$modpath/loc_virt1"
set modulefilere_8 "$modpathre/loc_virt1"
set modavail_8 ""
set modwhatis_8 ""
set modpaths_8 [list]
# a directory can be opened on FreeBSD <= 12.1, so another err message is obtained
if {$tcl_platform(os) eq "FreeBSD" && [cmpversion [lindex [split $tcl_platform(osVersion) -] 0] 12.1] < 1} {
    set moderr_8 "$err_magic'$modulefile_8'\n$err_contact"
    set modunerr_8 [msg_unload $module_8 "$err_magicns'$modulefile_8'\n$err_contactns"]
} else {
    set moderr_8 "$err_illdir'$modulefile_8'"
    set modunerr_8 [msg_unload $module_8 $moderr_8]
}

set modsym_9 "loc_virt1/9.0"
set module_9 $modsym_9
set modulefile_9 $env(TESTSUITEDIR)
set modulefilere_9 [regsub -all "\(\[.+?\]\)" $modulefile_9 {\\\1}]
set modavail_9 ""
set modwhatis_9 ""
set modpaths_9 [list]
if {$tcl_platform(os) eq "FreeBSD" && [cmpversion [lindex [split $tcl_platform(osVersion) -] 0] 12.1] < 1} {
    set moderr_9 "$err_magic'$modulefile_9'\n$err_contact"
    set modunerr_9 [msg_unload $module_9 "$err_magicns'$modulefile_9'\n$err_contactns"]
} else {
    set moderr_9 "$err_illdir'$modulefile_9'"
    set modunerr_9 [msg_unload $module_9 $moderr_9]
}

set modsym_10 "loc_virt1/.common"
set module_10 $modsym_10
set modulefile_10 "$modpath/$module_10"
set modulefilere_10 "$modpathre/$module_10"
set modavail_10 "$module_10\\s+$datetime_re"

set modsym_11 "loc_virt2/1.0"
set module_11 $modsym_11
set modulefile_11 "$modpath/loc_virt2/2.0"
set modulefilere_11 "$modpathre/loc_virt2/2.0"
set modavail_11 "$module_11\\s+$datetime_re"
set modwhatis_11 "\\s+$module_11: $module_11"

set modsym_12 "loc_virt2"
set module_12 $modsym_12/2.0
set modulefile_12 "$modpath/$module_12"
set modulefilere_12 "$modpathre/$module_12"
set modavail_12 "$modsym_12/1.0\\s+$datetime_re\n$module_12\\s+$datetime_re"
set modwhatis_12 "\\s+$modsym_12/1.0: $modsym_12/1.0\n\\s+$module_12: $module_12"
set nodefault_12 "$err_nodefault'$modsym_12'"

set module_reg "setenv/1.0"
set modulefile_reg "$modpath/$module_reg"

set help_pre "$modlin\nModule Specific Help for "
set help_post ":\n\n$modlin"
set test_pre "$modlin\nModule Specific Test for "
set test_post ":\n\nTest result: PASS\n$modlin"
set disp_pre "$modlin\n"
set disp_wi "module-whatis\\s+"
set disp_set "\nsetenv\\s+testsuite\\s+"
set disp_post "\n$modlin"
set header_long "- Package/Alias $modlin.- Versions $modlin.- Last mod. $modlin"
set avail_pre "$header_long\n$modpathre:"
set whatis_pre "$modlin $modpathre $modlin\n"
set listh_long "- Package $modlin.- Versions $modlin.- Last mod. $modlin"

#
#  The tests
#

for {set i 1} {$i <= 12} {incr i} {
    set modsym [set "modsym_${i}"]
    set module [set "module_${i}"]
    set modulefile [set "modulefile_${i}"]
    set modulefilere [set "modulefilere_${i}"]
    set modavail [set "modavail_${i}"]
    if {[info exists "modwhatis_${i}"]} {
        set modwhatis [set "modwhatis_${i}"]
    } else {
        set modwhatis "\\s+$module: $module"
    }
    set ans_paths [list]
    if {[info exists "modpaths_${i}"]} {
        lappend ans_paths [list text [set "modpaths_${i}"]]
    } else {
        lappend ans_paths [list text $modulefile]
    }
    if {[info exists "moderr_${i}"]} {
        set moderr [set "moderr_${i}"]
    } elseif {[info exists moderr]} {
        unset moderr
    }
    if {[info exists "modunerr_${i}"]} {
        set modunerr [set "modunerr_${i}"]
    } elseif {[info exists modunerr]} {
        unset modunerr
    }
    if {[info exists "nodefault_${i}"]} {
        set nodefault [set "nodefault_${i}"]
    } else {
        set nodefault ""
    }

    # setup clean environment
    unsetenv_loaded_module

    if {[info exists moderr]} {
        testouterr_cmd "sh" "load $modsym" "ERR" "$moderr"
        testouterr_cmd "sh" "display $modsym" "ERR" "$moderr"
        testouterr_cmd "sh" "help $modsym" "ERR" "$moderr"
        testouterr_cmd "sh" "test $modsym" "ERR" "$moderr"
    } else {
        set ans [list]
        lappend ans [list set testsuite $module]
        lappend ans [list setpath LOADEDMODULES $module]
        lappend ans [list setpath _LMFILES_ $modulefile]

        set displayerr "$disp_pre$modulefilere:\n\n$disp_wi$module$disp_set$module$disp_post"
        set helperr "$help_pre$modulefilere$help_post"
        set testerr "$test_pre$modulefilere$test_post"

        if {$nodefault eq {}} {
            testouterr_cmd "sh" "load $modsym" $ans ""
            testouterr_cmd_re "sh" "display $modsym" "OK" $displayerr
            testouterr_cmd_re "sh" "help $modsym" "OK" $helperr
            testouterr_cmd_re "sh" "test $modsym" "OK" $testerr
        } else {
            testouterr_cmd "sh" "load $modsym" [if_implicitdefault $ans ERR] [if_implicitdefault {} $nodefault]
            testouterr_cmd_re "sh" "display $modsym" [if_implicitdefault OK ERR] [if_implicitdefault $displayerr $nodefault]
            testouterr_cmd_re "sh" "help $modsym" [if_implicitdefault OK ERR] [if_implicitdefault $helperr $nodefault]
            testouterr_cmd_re "sh" "test $modsym" [if_implicitdefault OK ERR] [if_implicitdefault $testerr $nodefault]
        }
    }

    if {$modavail eq ""} {
        testouterr_cmd_re "sh" "avail -l $modsym" "OK" ""
    } else {
        testouterr_cmd_re "sh" "avail -l $modsym" "OK" "$avail_pre\n$modavail"
    }
    if {$modwhatis eq ""} {
        testouterr_cmd "sh" "whatis $modsym" "ERR" "$moderr"
    } else {
        testouterr_cmd_re "sh" "whatis $modsym" "OK" "$whatis_pre$modwhatis"
    }

    set ans [list]
    if {[info exists moderr]} {
        lappend ans [list text ""]
        testouterr_cmd "sh" "path $modsym" $ans "$moderr"
    } else {
        lappend ans [list text $modulefile]
        if {$nodefault eq {}} {
            testouterr_cmd "sh" "path $modsym" $ans ""
        } else {
            testouterr_cmd "sh" "path $modsym" [if_implicitdefault $ans [list [list text {}]]] [if_implicitdefault {} $nodefault]
        }
    }

    testouterr_cmd "sh" "paths $modsym" $ans_paths ""

    # setup environment for unload action
    setenv_loaded_module $module $modulefile

    set ans [list]
    lappend ans [list unset testsuite]
    lappend ans [list unsetpath LOADEDMODULES]
    lappend ans [list unsetpath _LMFILES_]

    if {[info exists modunerr]} {
        testouterr_cmd "sh" "unload $modsym" ERR $modunerr
    } elseif {[info exists moderr]} {
        testouterr_cmd "sh" "unload $modsym" "ERR" "$moderr"
    } else {
        testouterr_cmd "sh" "unload $modsym" $ans ""
    }
}


# setup clean environment
unsetenv_loaded_module

# complex single load command of multiple virtual modules targeting same file
# full path virtual target will not be honored as virtual already loaded
set ans [list]
lappend ans [list set testsuite $module_3]
lappend ans [list setpath LOADEDMODULES "$module_4:$module_1:$module_reg:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4"]

testouterr_cmd_re "sh" "load $module_4 $modulefile_4 $module_1 $module_reg $module_3" $ans ""

set ans [list]
lappend ans [list set testsuite $module_3]
lappend ans [list setpath LOADEDMODULES "$modulefile_4:$module_4:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_4:$modulefile_4"]

# full path target loaded as is will not impact the afterward load of virtual modules targeting it
testouterr_cmd_re "sh" "load $modulefile_4 $module_4 $module_3" $ans ""

# setup environment for more complex load tests
set env(testsuite) $module_1
setenv_loaded_module $module_1 $modulefile_1

# same target file load twice
set ans [list]
lappend ans [list set _LMFILES__modshare "$modulefile_1:2"]
lappend ans [list set LOADEDMODULES_modshare "$module_1:1:$module_2:1"]
lappend ans [list set _LMFILES_ "$modulefile_1:$modulefile_2"]
lappend ans [list set LOADEDMODULES "$module_1:$module_2"]
lappend ans [list set testsuite $module_2]

testouterr_cmd "sh" "load $module_2" $ans ""

# other target from same modname loaded
set ans [list]
lappend ans [list set testsuite $module_3]
lappend ans [list setpath LOADEDMODULES "$module_1:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_1:$modulefile_3"]

testouterr_cmd_re "sh" "load $module_3" $ans ""

# load with full path name, but it does not exist as it is virtual
testouterr_cmd "sh" "load $modpath/$module_3" "ERR" "$err_file'$modpath/$module_3'"


# switch to same target file
set ans [list]
lappend ans [list set _LMFILES__modshare "$modulefile_2:1"]
lappend ans [list set LOADEDMODULES_modshare "$module_2:1"]
lappend ans [list set _LMFILES_ $modulefile_2]
lappend ans [list set LOADEDMODULES "$module_2"]
lappend ans [list set testsuite $module_2]

testouterr_cmd "sh" "switch $module_2" $ans ""

# switch to other target from same modname
set ans [list]
lappend ans [list set _LMFILES__modshare "$modulefile_3:1"]
lappend ans [list set LOADEDMODULES_modshare "$module_3:1"]
lappend ans [list set _LMFILES_ $modulefile_3]
lappend ans [list set LOADEDMODULES "$module_3"]
lappend ans [list set testsuite $module_3]

testouterr_cmd "sh" "switch $module_3" $ans ""


# setup environment for more complex unload tests
set env(testsuite) $module_2
setenv_loaded_module [list $module_1 $module_2] [list $modulefile_1 $modulefile_2]

set ansfirst [list]
lappend ansfirst [list set _LMFILES__modshare "$modulefile_1:1"]
lappend ansfirst [list set LOADEDMODULES_modshare "$module_2:1"]
lappend ansfirst [list set _LMFILES_ $modulefile_1]
lappend ansfirst [list set LOADEDMODULES "$module_2"]
lappend ansfirst [list unset testsuite]

testouterr_cmd "sh" "unload $module_1" $ansfirst ""

set anslast [list]
lappend anslast [list set _LMFILES__modshare "$modulefile_1:1"]
lappend anslast [list set LOADEDMODULES_modshare "$module_1:1"]
lappend anslast [list set _LMFILES_ $modulefile_1]
lappend anslast [list set LOADEDMODULES "$module_1"]
lappend anslast [list unset testsuite]

testouterr_cmd sh "unload $module_2" $anslast {}
testouterr_cmd sh {unload loc_virt1} [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

# unload with full path name, but it does not exist as it is virtual
testouterr_cmd "sh" "unload $modpath/$module_2" OK ""

# unload by target file name, lastly or firstly loaded using this target file will be unloaded
# depending on unload_match_order chosen behavior
testouterr_cmd sh "unload $modulefile_1" [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}


# setup environment for more even more complex load/unload tests with same
# modulefile target loaded multiple times with virtual modules and full path reference
# this setup helps to detect corner cases however it cannot happened in the wild as full
# path target cannot be loaded if one of the virtual module targeting it is already loaded
set env(testsuite) $module_3
setenv_loaded_module [list $module_4 $modulefile_4 $module_1 $module_reg $module_3] [list $modulefile_4 $modulefile_4 $modulefile_1 $modulefile_reg $modulefile_4]

# list tests
testouterr_cmd "sh" "list -t" "" "$cur_loaded\n$module_4\n$modulefile_4\n$module_1\n$module_reg\n$module_3"
testouterr_cmd_re "sh" "list -l" "" "$listh_long\n$cur_loaded\n$module_4\\s*$datetime_re\n$modulefilere_4\\s*$datetime_re\n$module_1\\s*$datetime_re\n$module_reg\\s*$datetime_re\n$module_3\\s*$datetime_re"

# info-loaded tests
set ans [list]
lappend ans [list text $module_4]
testouterr_cmd "sh" "info-loaded $module_4" $ans ""

lappend ans [list text $module_1]
lappend ans [list text $module_3]
testouterr_cmd "sh" "info-loaded loc_virt1" $ans ""

set ans [list]
lappend ans [list text $module_4]
lappend ans [list text $modulefile_4]
lappend ans [list text $module_3]
testouterr_cmd "sh" "info-loaded $modulefile_4" $ans ""

# is-loaded test
testouterr_cmd "sh" "is-loaded $module_4" "OK" ""
testouterr_cmd "sh" "is-loaded loc_virt1" "OK" ""
testouterr_cmd "sh" "is-loaded $module_2" "ERR" ""

# attempt to load already loaded virtual module
testouterr_cmd "sh" "load $module_4" "OK" ""
testouterr_cmd "sh" "load loc_virt1" [if_implicitdefault OK ERR] [if_implicitdefault {} "$err_nodefault'loc_virt1'"]

# unload tests
set ansfirst [list]
lappend ansfirst [list unset testsuite]
lappend ansfirst [list setpath LOADEDMODULES "$modulefile_4:$module_1:$module_reg:$module_3"]
lappend ansfirst [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4"]
testouterr_cmd_re sh "unload $module_4" $ansfirst {}

set anslast [list]
lappend anslast [list unset testsuite]
lappend anslast [list setpath LOADEDMODULES "$module_4:$modulefile_4:$module_1:$module_reg"]
lappend anslast [list setpath _LMFILES_ "$modulefile_4:$modulefile_4:$modulefile_1:$modulefile_reg"]
testouterr_cmd_re sh {unload loc_virt1} [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

# switch tests
set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$modulefile_4:$module_1:$module_reg:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4"]
testouterr_cmd_re "sh" "switch $module_4 $module_3" $ans ""

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$module_4:$modulefile_4:$module_reg:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_4:$modulefile_reg:$modulefile_4"]
testouterr_cmd_re "sh" "switch $module_1 $module_4" $ans ""

set ansfirst [list]
lappend ansfirst [list unset testsuite]
lappend ansfirst [list setpath LOADEDMODULES "$modulefile_4:$module_1:$module_reg:$module_3"]
lappend ansfirst [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4"]
set anslast [list]
lappend anslast [list unset testsuite]
lappend anslast [list setpath LOADEDMODULES "$module_4:$modulefile_4:$module_1:$module_reg"]
lappend anslast [list setpath _LMFILES_ "$modulefile_4:$modulefile_4:$modulefile_1:$modulefile_reg"]
testouterr_cmd_re sh "switch loc_virt1 $module_reg" [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

set ans [list]
lappend ans [list set testsuite $module_4]
lappend ans [list setpath LOADEDMODULES "$modulefile_4:$module_1:$module_reg:$module_3:$module_4"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4:$modulefile_4"]
testouterr_cmd_re "sh" "switch $module_4" $ans ""

# switch target file, when it is also loaded as a module name
set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$module_4:$module_1:$module_reg:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4"]
testouterr_cmd_re "sh" "switch $modulefile_4" $ans ""

# same test without modulepath defined
unsetenv_path_var MODULEPATH
set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$module_4:$module_1:$module_reg:$module_3"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg:$modulefile_4"]
testouterr_cmd_re "sh" "switch $modulefile_4" $ans ""

# restore modulepath
setenv_path_var MODULEPATH $modpath

# reset environment by removing the virtual target file loaded as a full path module
set env(testsuite) $module_3
setenv_loaded_module [list $module_4 $module_1 $module_reg $module_3] [list $modulefile_4 $modulefile_1 $modulefile_reg $modulefile_4]

# switch target file, when it is not also loaded as a module name
set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$module_4:$module_1:$module_reg"]
lappend ans [list setpath _LMFILES_ "$modulefile_4:$modulefile_1:$modulefile_reg"]
testouterr_cmd_re "sh" "switch $modulefile_4" $ans ""

# same test without modulepath defined
unsetenv_path_var MODULEPATH
testouterr_cmd_re "sh" "switch $modulefile_4" $ans ""

# restore modulepath
setenv_path_var MODULEPATH $modpath

# setup environment for load/unload tests on a module where versions are a mix of regular
# and virtual, virtual module targeting the regular modulefile

set env(testsuite) $module_11
setenv_loaded_module $module_11 $modulefile_11

set ans [list]
lappend ans [list set testsuite $module_12]
lappend ans [list setpath LOADEDMODULES "$module_11:$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_11:$modulefile_12"]
testouterr_cmd_re "sh" "load $module_12" $ans ""

testouterr_cmd "sh" "load $modulefile_12" "OK" ""

set ans [list]
lappend ans [list set testsuite $module_12]
lappend ans [list setpath LOADEDMODULES "$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_12"]
testouterr_cmd_re "sh" "switch $module_12" $ans ""

set ans [list]
lappend ans [list set testsuite $modulefile_12]
lappend ans [list setpath LOADEDMODULES "$modulefile_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_12"]
testouterr_cmd_re "sh" "switch $modulefile_12" $ans ""


set env(testsuite) $module_12
setenv_loaded_module $module_12 $modulefile_12

set ans [list]
lappend ans [list set testsuite $module_11]
lappend ans [list setpath LOADEDMODULES "$module_12:$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_12:$modulefile_11"]
testouterr_cmd_re "sh" "load $module_11" $ans ""

testouterr_cmd "sh" "load $modulefile_11" "OK" ""

set ans [list]
lappend ans [list set testsuite $module_11]
lappend ans [list setpath LOADEDMODULES "$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_11"]
testouterr_cmd_re "sh" "switch $module_11" $ans ""

set ans [list]
lappend ans [list set testsuite $modulefile_11]
lappend ans [list setpath LOADEDMODULES "$modulefile_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_11"]
testouterr_cmd_re "sh" "switch $modulefile_11" $ans ""


set env(testsuite) $module_12
setenv_loaded_module [list $module_11 $module_12] [list $modulefile_11 $modulefile_12]

set ansfirst [list]
lappend ansfirst [list unset testsuite]
lappend ansfirst [list setpath LOADEDMODULES "$module_12"]
lappend ansfirst [list setpath _LMFILES_ "$modulefile_12"]
testouterr_cmd_re sh "unload $module_11" $ansfirst {}

set anslast [list]
lappend anslast [list unset testsuite]
lappend anslast [list setpath LOADEDMODULES "$module_11"]
lappend anslast [list setpath _LMFILES_ "$modulefile_11"]
testouterr_cmd_re sh "unload $module_12" $anslast {}
testouterr_cmd_re sh "unload $modulefile_11" [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}
testouterr_cmd_re sh {unload loc_virt2} [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

testouterr_cmd_re sh "switch $modulefile_11" $anslast {}

set ans [list]
lappend ans [list set testsuite $module_11]
lappend ans [list setpath LOADEDMODULES "$module_12:$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_12:$modulefile_11"]
testouterr_cmd_re "sh" "switch $module_11" $ans ""

set ans [list]
lappend ans [list set testsuite $module_12]
lappend ans [list setpath LOADEDMODULES "$module_11:$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_11:$modulefile_12"]
testouterr_cmd_re "sh" "switch $module_12" $ans ""


set env(testsuite) $module_11
setenv_loaded_module [list $module_12 $module_11] [list $modulefile_12 $modulefile_11]

set anslast [list]
lappend anslast [list unset testsuite]
lappend anslast [list setpath LOADEDMODULES $module_12]
lappend anslast [list setpath _LMFILES_ $modulefile_12]
testouterr_cmd_re sh "unload $module_11" $anslast {}

set ansfirst [list]
lappend ansfirst [list unset testsuite]
lappend ansfirst [list setpath LOADEDMODULES $module_11]
lappend ansfirst [list setpath _LMFILES_ $modulefile_11]
testouterr_cmd_re sh "unload $module_12" $ansfirst {}

# remove lastly or firstly loaded match depending on unload_match_order setting
testouterr_cmd_re sh "unload $modulefile_11" [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}
testouterr_cmd_re sh {unload loc_virt2} [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_11"]
testouterr_cmd_re "sh" "switch $modulefile_11" $ans ""

set ans [list]
lappend ans [list set testsuite $module_11]
lappend ans [list setpath LOADEDMODULES "$module_12:$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_12:$modulefile_11"]
testouterr_cmd_re "sh" "switch $module_11" $ans ""

set ans [list]
lappend ans [list set testsuite $module_12]
lappend ans [list setpath LOADEDMODULES "$module_11:$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_11:$modulefile_12"]
testouterr_cmd_re "sh" "switch $module_12" $ans ""


set env(testsuite) $module_12
setenv_loaded_module [list $module_11 $modulefile_12] [list $modulefile_11 $modulefile_12]

set ansfirst [list]
lappend ansfirst [list unset testsuite]
lappend ansfirst [list setpath LOADEDMODULES $modulefile_12]
lappend ansfirst [list setpath _LMFILES_ $modulefile_12]
testouterr_cmd_re sh "unload $module_11" $ansfirst {}

set anslast [list]
lappend anslast [list unset testsuite]
lappend anslast [list setpath LOADEDMODULES $module_11]
lappend anslast [list setpath _LMFILES_ $modulefile_11]
testouterr_cmd_re sh "unload $module_12" $anslast {}
testouterr_cmd_re sh "unload $modulefile_11" [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}
testouterr_cmd_re sh {unload loc_virt2} [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

testouterr_cmd_re sh "switch $modulefile_11" $anslast {}

set ans [list]
lappend ans [list set testsuite $module_11]
lappend ans [list setpath LOADEDMODULES "$modulefile_12:$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_12:$modulefile_11"]
testouterr_cmd_re "sh" "switch $module_11" $ans ""

set ans [list]
lappend ans [list set testsuite $module_12]
lappend ans [list setpath LOADEDMODULES "$module_11:$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_11:$modulefile_12"]
testouterr_cmd_re "sh" "switch $module_12" $ans ""


set env(testsuite) $module_11
setenv_loaded_module [list $modulefile_12 $module_11] [list $modulefile_12 $modulefile_11]

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$modulefile_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_12"]
testouterr_cmd_re "sh" "unload $module_11" $ans ""

set ans [list]
lappend ans [list unset testsuite]
lappend ans [list setpath LOADEDMODULES "$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_11"]
testouterr_cmd_re "sh" "unload $module_12" $ans ""

# remove lastly or firstly loaded match depending on unload_match_order setting
set anslast [list]
lappend anslast [list unset testsuite]
lappend anslast [list setpath LOADEDMODULES $modulefile_12]
lappend anslast [list setpath _LMFILES_ $modulefile_12]
set ansfirst [list]
lappend ansfirst [list unset testsuite]
lappend ansfirst [list setpath LOADEDMODULES $module_11]
lappend ansfirst [list setpath _LMFILES_ $modulefile_11]

testouterr_cmd_re sh "unload $modulefile_11" [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}
testouterr_cmd_re sh {unload loc_virt2} [expr {$install_unloadmatchorder eq {returnlast} ? $anslast : $ansfirst}] {}

testouterr_cmd_re sh "switch $modulefile_11" $ansfirst {}

set ans [list]
lappend ans [list set testsuite $module_11]
lappend ans [list setpath LOADEDMODULES "$modulefile_12:$module_11"]
lappend ans [list setpath _LMFILES_ "$modulefile_12:$modulefile_11"]
testouterr_cmd_re "sh" "switch $module_11" $ans ""

set ans [list]
lappend ans [list set testsuite $module_12]
lappend ans [list setpath LOADEDMODULES "$module_11:$module_12"]
lappend ans [list setpath _LMFILES_ "$modulefile_11:$modulefile_12"]
testouterr_cmd_re "sh" "switch $module_12" $ans ""


#
#  Cleanup
#

reset_test_env
