##############################################################################
#   Modules Release Tcl
#   Providing a flexible user environment
#
#   File:		modules.61-coll/%M%
#   Revision:		%I%
#   First Edition:	2016/08/16
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#			Test the module 'saveshow' command
#		}C%
#
##############################################################################

set cbadnerrmsg "$error_msgs: Collection $collbadn cannot be found"
set cbadfperrmsg "$error_msgs: Collection $collbadfp cannot be found"
set cbadterrmsg "$error_msgs: Collection $colld (for target \"$colltarget\") cannot be found"
set badcontfp "$homewithcoll/.Xresources"
set badcontfperrmsg "$error_msgs: $badcontfp is not a valid collection"


# set HOME to location containing collections
if { $verbose > 0 } {
    send_user "\tSetup HOME = $homewithcoll\n"
}
set env(HOME) "$homewithcoll"


#
# test showing nonexistent collection
#

testouterr_cmd "ALL" "saveshow $collbadn" "ERR" "$cbadnerrmsg"


#
# test showing nonexistent collection specified as a filepath
#

testouterr_cmd "ALL" "saveshow $collbadfp" "ERR" "$cbadfperrmsg"


#
# test showing existent file which is not a collection
#

testouterr_cmd "ALL" "saveshow $badcontfp" "ERR" "$badcontfperrmsg"


#
# test showing default collection
#

# test only sh shell as this test does not produce shell output
testouterr_cmd_re "sh" "saveshow" "OK" "$modlin\n$homewithcolldotmodre/$colld:\n\n$cdcontrepin\n$modlin"


#
# test showing named collection
# using old fashioned way to define paths (no --append property)
#

# test only sh shell as this test does not produce shell output
testouterr_cmd_re "sh" "saveshow $coll1" "OK" "$modlin\n$homewithcolldotmodre/$coll1:\n\n$presavedc1cont1re\n$modlin"


#
# test showing collection specified as a filepath when
# MODULES_COLLECTION_TARGET is set. target should be ignored
#

# setup environment state
if { $verbose > 0 } {
    send_user "\tSetup MODULES_COLLECTION_TARGET = $colltarget\n"
}
set env(MODULES_COLLECTION_TARGET) "$colltarget"

# test only sh shell as this test does not produce shell output
testouterr_cmd_re "sh" "saveshow $homewithcolldotmod/$coll1" "OK" "$modlin\n$homewithcolldotmodre/$coll1:\n\n$presavedc1cont1re\n$modlin"


#
# test showing collection not mentionning module to load
# when MODULES_COLLECTION_TARGET is set
#

# test only sh shell as this test does not produce shell output
testouterr_cmd_re "sh" "saveshow $coll4" "OK" "$modlin\n$homewithcolldotmodre/$coll4f:\n\n$c4contre\n$modlin"


#
# test displaying same collection than before but with a modulepath
# prepended rather appended
#

testouterr_cmd_re "sh" "saveshow $coll5fp" "OK" "$modlin\n$coll5fpre:\n\n$c4contre\n$modlin"


#
# test displaying collection with no modulepath and modulefiles loaded as fullpath
#

testouterr_cmd_re "sh" "saveshow $coll7fp" "OK" "$modlin\n$coll7fpre:\n\n$c7contre\n$modlin"


#
# test showing nonexistent default collection when
# MODULES_COLLECTION_TARGET is set
#

testouterr_cmd "ALL" "saveshow" "ERR" "$cbadterrmsg"


#
# test displaying collection with no modulepath and modulefiles loaded as fullpath
#

unsetenv_path_var MODULES_COLLECTION_TARGET

if {!$is_symlink_supported} {
    send_user "\tskipping tests over '$modpath wspace' modulepath as symbolic links are not supported on filesystem\n"
} else {

testouterr_cmd_re sh "saveshow $coll15fp" OK "$modlin\n$coll15fpre:\n\n$c15contre\n$modlin"
testouterr_cmd_re sh "saveshow $coll16fp" OK "$modlin\n$coll16fpre:\n\n$c16contre\n$modlin"

}

#
# test displaying collection with modulepath starting with variable reference
#

testouterr_cmd_re sh "saveshow $coll17fp" OK "$modlin\n$coll17fpre:\n\n$c17contre\n$modlin"

#
# variant tests
#

# ensure advanced_version_spec is enabled to record variant
setenv_var MODULES_ADVANCED_VERSION_SPEC 1

# test with collection_pin_version enabled
setenv_var MODULES_COLLECTION_PIN_VERSION 1

testouterr_cmd_re sh "saveshow $coll19fp" OK "$modlin\n$coll19fpre:\n\n$c19contre\n$modlin"
testouterr_cmd_re sh "saveshow $coll25fp" OK "$modlin\n$coll25fpre:\n\n$c25contre\n$modlin"
testouterr_cmd_re sh "saveshow $coll26fp" OK "$modlin\n$coll26fpre:\n\n$c26contre\n$modlin"

# test with collection_pin_version enabled
setenv_var MODULES_COLLECTION_PIN_VERSION 0

testouterr_cmd_re sh "saveshow $coll19fp" OK "$modlin\n$coll19fpre:\n\n$c19contre\n$modlin"
testouterr_cmd_re sh "saveshow $coll25fp" OK "$modlin\n$coll25fpre:\n\n$c25contre\n$modlin"
testouterr_cmd_re sh "saveshow $coll26fp" OK "$modlin\n$coll26fpre:\n\n$c26contre\n$modlin"

# disable advanced_version_spec
setenv_var MODULES_ADVANCED_VERSION_SPEC 0

# each variant set is considered a module to load
testouterr_cmd_re sh "saveshow $coll19fp" OK "$modlin\n$coll19fpre:\n\n$c19contnoavsre\n$modlin"
testouterr_cmd_re sh "saveshow $coll26fp" OK "$modlin\n$coll26fpre:\n\n$c26contnoavsre\n$modlin"

# test showing collection with bad module specification
setenv_var MODULES_ADVANCED_VERSION_SPEC 1
testouterr_cmd_re sh "saveshow $coll27fp" ERR "$error_msgs: $msg_nomodnameinarg 'foo=val'"
testouterr_cmd_re sh "saveshow $coll28fp" ERR "$error_msgs: $msg_nomodnameinarg '@1.0'"
testouterr_cmd_re sh "saveshow $coll29fp" OK "$modlin\n$coll29fpre:\n\n$c29contre\n$modlin"

# test showing collection with variant shortcut set
setenv_var MODULES_VARIANT_SHORTCUT foo=%
testouterr_cmd_re sh "saveshow $coll36fp" OK "$modlin\n$coll36fpre:\n\n$c36contre\n$modlin"
unsetenv_var MODULES_VARIANT_SHORTCUT

unsetenv_var MODULES_ADVANCED_VERSION_SPEC
unsetenv_var MODULES_COLLECTION_PIN_VERSION


#
# test unsetting HOME
#

if { $verbose > 0 } {
    send_user "\tUnset HOME \n"
}
unset env(HOME)

testouterr_cmd "sh" "saveshow" "ERR" $nohomeerrmsg

# restore default testsuite home
if { $verbose > 0 } {
    send_user "\tRestore HOME = $ORIG_HOME\n"
}
set env(HOME) $ORIG_HOME


#
# Cleanup
#

reset_test_env
