##############################################################################
#   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:		load
#   Modulefiles:	remove/2.0
#   Sub-Command:
#
#   Comment:	%C{
#			Tests the 'remove-path' module subcommand for all
#			allowed shell types.
#		}C%
#
##############################################################################

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

set module "remove/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"

#
#  For the different shells ...
#

lappend ans [list setpath PATH $old_path]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

#
#  The tests
#

test_cmd_re "ALL" "load $module" $ans

#
# Try bad argument list: missing variable name
#

set module "remove/0.3"
set modulefile "$modpath/$module"
set ts_err [msg_moderr "unload-path $msg_needenvvarname" {remove-path } $modulefile 24]
testouterr_cmd "sh" "load $module" "ERR" [msg_load $module $ts_err]

#
# Try bad argument list: missing variable value
#

set module "remove/0.4"
set modulefile "$modpath/$module"
set ts_err [msg_moderr "unload-path $msg_needenvvarval PATH" {remove-path PATH} $modulefile 24]
testouterr_cmd "sh" "load $module" "ERR" [msg_load $module $ts_err]

#
# Try bad argument list: empty variable name
#

set module "remove/0.5"
set modulefile "$modpath/$module"
set ts_err [msg_moderr "unload-path $msg_validenvvarname" {remove-path {}} $modulefile 24]
testouterr_cmd "sh" "load $module" "ERR" [msg_load $module $ts_err]

#
# Try command switches passed after variable name
#

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


#
# Remove multiple paths passed as one string
#

set module "remove/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:"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:1"]
lappend ans [list set FOO "/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

#
# Remove multiple paths passed as multiple strings
#

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

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:1"]
lappend ans [list set FOO "/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""


#
# Remove unknown path on a var set empty
#

set module "remove/3.0"
set modulefile "$modpath/$module"

set env(FOO_modshare) ":1"
set env(FOO) ""

set ans [list]
lappend ans [list set FOO_modshare ":1"]
lappend ans [list set FOO ""]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""


#
# Remove path added in duplication mode
#

set module "remove/3.0"
set modulefile "$modpath/$module"

unset env(FOO_modshare)
set env(FOO) "/path/to/dir1:/path/to/dir2:/path/to/dir1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir2"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set env(FOO_modshare) "/path/to/dir1:1:/path/to/dir2:1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir2"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set env(FOO_modshare) "/path/to/dir1:2:/path/to/dir2:1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:1:/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir2"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set env(FOO_modshare) "/path/to/dir1:3:/path/to/dir2:1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:2:/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir2:/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set module "remove/3.1"
set modulefile "$modpath/$module"

set env(FOO_modshare) "/path/to/dir1:2:/path/to/dir2:1"
set env(FOO) "/path/to/dir1:/path/to/dir2:/path/to/dir1:/path/to/dir1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:1:/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir2"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans [msg_load $module "$warn_msgs: --duplicates option has no effect on unload-path"]

#
# Remove path in index mode
#

set module "remove/4.0"
set modulefile "$modpath/$module"
set ts_err [msg_moderr "unload-path $msg_valididxvalue" {remove-path --index FOO /path/to/dir1} $modulefile 24]

# not a number
testouterr_cmd "sh" "load $module" "ERR" [msg_load $module $ts_err]

set module "remove/4.1"
set modulefile "$modpath/$module"

set env(FOO_modshare) ":1"
set env(FOO) ""

set ans [list]
lappend ans [list set FOO_modshare ":1"]
lappend ans [list set FOO ""]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

# out of bound
testouterr_cmd "sh" "load $module" $ans ""

set env(FOO_modshare) "/path/to/dir1:2:/path/to/dir2:1"
set env(FOO) "/path/to/dir1:/path/to/dir2:/path/to/dir1:/path/to/dir1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:2"]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir1:/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set env(FOO_modshare) "/path/to/dir1:2:/path/to/dir2:2"
set env(FOO) "/path/to/dir1:/path/to/dir2:/path/to/dir1:/path/to/dir1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:2:/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir2:/path/to/dir1:/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set module "remove/4.2"
set modulefile "$modpath/$module"

set env(FOO_modshare) "/path/to/dir1:2:/path/to/dir2:2"
set env(FOO) "/path/to/dir1:/path/to/dir2:/path/to/dir1:/path/to/dir1"

set ans [list]
lappend ans [list set FOO_modshare "/path/to/dir1:1:/path/to/dir2:1"]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir2:/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""

set env(FOO_modshare) "/path/to/dir1:1"
set env(FOO) "/path/to/dir1:/path/to/dir2:/path/to/dir1:/path/to/dir1"

set ans [list]
lappend ans [list unset FOO_modshare]
lappend ans [list set FOO "/path/to/dir1:/path/to/dir1"]
lappend ans [list setpath LOADEDMODULES $module]
lappend ans [list setpath _LMFILES_ $modulefile]

testouterr_cmd "sh" "load $module" $ans ""


#
#  Clean up the environment
#

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

#
#  Cleanup
#

reset_test_env
