##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.50-cmds/%M%
#   Revision:		%I%
#   First Edition:	1995/12/06
#   Last Mod.:		%U%, %G%
#
#   Authors:		Jens Hamisch, Jens.Hamisch@Strawberry.COM
#
#   Description:	Testuite testsequence
#   Command:		unload
#   Modulefiles:	append/1.0
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the reverse 'append-path' module subcommand for
#			all allowed shell types.
#		}C%
#
##############################################################################

#
#  Variables. This test forces a module unload command. It will result in the
#    environment variables "_LMFILES_", "LOADEDMODULES" and "PATH" to
#    be set up
#

set module "append/2.0"
set modulefile "$modpath/$module"

set old_path $env(PATH)
set ts_path "/this/is/the/modules/testsuite"
set new_path "$old_path:$ts_path"

#
#  Set up the environment for reversal ...
#

set env(PATH) "$new_path"
setenv_loaded_module $module $modulefile

#
#  For the different shells ...
#

lappend ans [list setpath PATH $old_path]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]

#
#  The tests
#

test_cmd_re "ALL" "unload $module" $ans

#
# try the empty string
#
set module "append/0.1"
set modulefile "$modpath/$module"

set ts_path "";
set new_path "$old_path:$ts_path"

#
#  Set up the environment for reversal ...
#

set env(PATH) "$new_path"
setenv_loaded_module $module $modulefile

#
#  For the different shells ...
#

# same result content as above

#
#  The tests
#

test_cmd_re "ALL" "unload $module" $ans

#
# try it again with :
#

set module "append/0.2"
set modulefile "$modpath/$module"

# different behavior than C-version: on Tcl-version removing a bare colon
# to a path produces an error since this colon cannot be distinguished
# from the colon used for path separator
set ts_err [msg_moderr "unload-path $msg_patheqsep" "append-path\tPATH\t\t:" $modulefile 24]

set ts_path ":"
set new_path "$old_path:$ts_path"

#
#  Set up the environment for reversal ...
#

# as adding bare colon leads to error, this module can never be loaded
# but anyway here we test that the unloading part of "append-path" also
# leads to error
set env(PATH) "$new_path"
setenv_loaded_module $module $modulefile

#
#  The tests
#

testouterr_cmd "ALL" "unload $module" "ERR" [msg_unload $module $ts_err]

#
# Try bad argument list: missing variable name
#

set module "append/0.3"
set modulefile "$modpath/$module"
setenv_loaded_module $module $modulefile
set ts_err [msg_moderr "unload-path $msg_needenvvarname" {append-path } $modulefile 24]
testouterr_cmd "sh" "unload $module" "ERR" [msg_unload $module $ts_err]

#
# Try bad argument list: missing variable value
#

set module "append/0.4"
set modulefile "$modpath/$module"
setenv_loaded_module $module $modulefile
set ts_err [msg_moderr "unload-path $msg_needenvvarval PATH" {append-path PATH } $modulefile 24]
testouterr_cmd "sh" "unload $module" "ERR" [msg_unload $module $ts_err]

#
# Try bad argument list: empty variable name
#

set module "append/0.5"
set modulefile "$modpath/$module"
setenv_loaded_module $module $modulefile
set ts_err [msg_moderr "unload-path $msg_validenvvarname" {append-path {} /path/to } $modulefile 24]
testouterr_cmd "sh" "unload $module" "ERR" [msg_unload $module $ts_err]

#
# Try command switches passed after variable name
#

set module "append/1.8"
set modulefile "$modpath/$module"
setenv_loaded_module $module $modulefile
setenv_path_var testsuite --delim=,
setenv_path_var testsuite2 , foo
set ans [list]
lappend ans [list unsetpath testsuite]
lappend ans [list setpath testsuite2 foo]
lappend ans [list unset testsuite3]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]
testouterr_cmd_re "sh" "unload $module" $ans {}
unsetenv_path_var testsuite
unsetenv_path_var testsuite2

#
# Append multiple paths passed as one string
#

set module "append/2.3"
set modulefile "$modpath/$module"

set env(FOO_modshare) ":1:/modules/testsuite2.2/2.2:1:/path/to/dir1:1:/modules/testsuite2.2:1"
set env(FOO) "/path/to/dir1:/modules/testsuite2.2:/modules/testsuite2.2/2.2:"
setenv_loaded_module $module $modulefile

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:1"]
lappend ans [list set FOO "/path/to/dir1"]
lappend ans [list unsetpath LOADEDMODULES]
lappend ans [list unsetpath _LMFILES_]

testouterr_cmd "sh" "unload $module" $ans ""

#
# Append multiple paths passed as multiple strings
#

set module "append/2.4"
set modulefile "$modpath/$module"

setenv_loaded_module $module $modulefile

testouterr_cmd "sh" "unload $module" $ans ""

#
#  Clean up the environment
#

set env(PATH) "$old_path"
unset env(FOO_modshare)
unset env(FOO)
unsetenv_loaded_module

#
#  Cleanup
#

reset_test_env
