##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.20-locate/%M%
#   Revision:		%I%
#   First Edition:	2018/09/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        load, source, display, help, test, avail, whatis,
#                   path, paths
#   Modulefiles:	mcookie, mcookierc
#   Sub-Command:
#
#   Comment:	%C{
#		}C%
#
##############################################################################

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

# ensure version check is enabled
setenv_var MODULES_MCOOKIE_VERSION_CHECK 1

# get Modules release number
set sepidx [string first + $install_version]
if {$sepidx != -1} {
    set release [string range $install_version 0 [expr {$sepidx - 1}]]
} else {
    set release $install_version
}
set majorrel [lindex [split $release .] 0]

# test modules where modulefiles are all incompatible with current Modules version
set modlist [list mcookie mcookie/1 mcookierc mcookierc/1]
set tserrlist [list "$err_path'mcookie'"\
    [moderr_minverreq $modpath/mcookie/1 99]\
    "[moderr_minverreq $modpath/mcookierc/.modulerc 99]\n$err_path'mcookierc'"\
    "[moderr_minverreq $modpath/mcookierc/.modulerc 99]\n[moderr_minverreq $modpath/mcookierc/1 99.9]"\
]
set tswherrlist [list "$err_path'mcookie'"\
    [moderr_minverreq $modpath/mcookie/1 99]\
    "$err_path'mcookierc'"\
    [moderr_minverreq $modpath/mcookierc/1 99.9]\
]
set tspaerrlist [list ""\
    ""\
    [moderr_minverreq $modpath/mcookierc/.modulerc 99]\
    [moderr_minverreq $modpath/mcookierc/.modulerc 99]\
]
foreach mod $modlist tserr $tserrlist tswherr $tswherrlist tspaerr $tspaerrlist {
    foreach cmd [list load display help test] {
        testouterr_cmd "sh" "$cmd $mod" ERR $tserr
    }
    if {[string first / $mod] != -1} {
        testouterr_cmd "sh" "source $modpath/$mod" "" ""
    }
    testouterr_cmd "sh" "avail $mod" "" ""
    testouterr_cmd "sh" "whatis $mod" ERR $tswherr
    testouterr_cmd "sh" "path $mod" "" $tserr
    testouterr_cmd "sh" "paths $mod" "" $tspaerr
}

# create temporary modulefiles whose magic cookie matches current version
set tmpmodfilelist [list $modpath/mcookie/.modulerc $modpath/mcookie/2 $modpath/mcookie/3 $modpath/mcookie/4 $modpath/mcookierc/5 $modpath/mcookie/0 $modpath/mcookie/0.2]
foreach f $tmpmodfilelist r [list $release $release $majorrel " 99" $majorrel "$release#########################" "99###########"] {
    if { $verbose > 0 } {
        send_user "\tCreate test modulefile $f\n"
    }
    set fid [open $f w]
    puts $fid "#%Module$r"
    close $fid
}

# test over temporarily created modfiles some of them matching minimal version requirement
set modlist [list mcookie/2 mcookie/3 mcookie/4 mcookierc/5 mcookie/0]
set tserrlist [list "" "" "" [moderr_minverreq $modpath/mcookierc/.modulerc 99] ""]
foreach mod $modlist tserr $tserrlist {
    set ans [list]
    lappend ans [list setpath LOADEDMODULES $mod]
    lappend ans [list setpath _LMFILES_ $modpath/$mod]
    if {$tserr ne ""} {
        lappend ans [list ERR]
    }
    testouterr_cmd "sh" "load $mod" $ans $tserr
    testouterr_cmd "sh" "source $modpath/$mod" "" ""
    testouterr_cmd "sh" "avail -t $mod" "" "$modpath:\n$mod"
    testouterr_cmd "sh" "whatis $mod" "" ""
    set anspa [list [list text $modpath/$mod]]
    testouterr_cmd "sh" "path $mod" $anspa $tserr
    testouterr_cmd "sh" "paths $mod" $anspa $tserr
}
# test over temporarily created modfiles none matching minimal version requirement
foreach mod [list mcookie/0.2] tserr [list [moderr_minverreq $modpath/mcookie/0.2 99]] {
    testouterr_cmd "sh" "load $mod" ERR $tserr
    testouterr_cmd "sh" "source $modpath/$mod" "" ""
    testouterr_cmd "sh" "avail -t $mod" "" ""
    testouterr_cmd "sh" "whatis $mod" ERR $tserr
    testouterr_cmd "sh" "path $mod" "" $tserr
    testouterr_cmd "sh" "paths $mod" "" ""
}

# recheck generic modname now some valid modulefiles have been created there
foreach mod [list mcookie mcookierc] resmod [list mcookie/4 mcookierc/5] tserr [list "" [moderr_minverreq $modpath/mcookierc/.modulerc 99]] {
    set ans [list]
    lappend ans [list setpath LOADEDMODULES $resmod]
    lappend ans [list setpath _LMFILES_ $modpath/$resmod]
    if {$tserr ne ""} {
        lappend ans [list ERR]
    }
    testouterr_cmd "sh" "load $mod" [if_implicitdefault $ans ERR] $tserr[if_implicitdefault {} [expr {$tserr ne {} ? "\n" : {}}]$err_nodefault'$mod']
    testouterr_cmd_re "sh" "avail -t $mod" "" "$modpathre:\n$mod.*"
    testouterr_cmd "sh" "whatis $mod" "" ""
    set anspa [list [list text $modpath/$resmod]]
    testouterr_cmd "sh" "path $mod" [if_implicitdefault $anspa [list [list text {}]]] $tserr[if_implicitdefault {} [expr {$tserr ne {} ? "\n" : {}}]$err_nodefault'$mod']
}

# test with version check disabled
setenv_var MODULES_MCOOKIE_VERSION_CHECK 0
set ans [list]
lappend ans [list setpath LOADEDMODULES mcookie/1]
lappend ans [list setpath _LMFILES_ $modpath/mcookie/1]
testouterr_cmd sh {load mcookie/1} $ans {}
testouterr_cmd sh {avail -t mcookie/1} {} $modpath:\nmcookie/1
testouterr_cmd sh {path mcookie/1} [list [list text $modpath/mcookie/1]] {}


#
#  Cleanup
#

# delete temporary modulefile
foreach f $tmpmodfilelist {
    if { $verbose > 0 } {
        send_user "\tDelete test modulefile $f\n"
    }
    file delete $f
}

reset_test_env
