##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2017/11/26
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        prepend-path
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Test 'prepend-path' module command
#		}C%
#
##############################################################################

if {[info exists env(FOO)]} {
    if { $verbose > 0 } {
        send_user "\tUnset FOO\n"
    }
    set ORIG_FOO $env(FOO)
    unset env(FOO)
}

set path1 "/path/to/dir1"
set path2 "/path/other/dir2"

lappend ans [list set FOO $path1]
lappend ans [list set FOO_modshare "$path1:1"]

testouterr_cmd_re "sh" "prepend-path FOO $path1" $ans ""


if { $verbose > 0 } {
    send_user "\tSetup FOO = '$path1'\n"
}
set env(FOO) $path1

set ans [list]
lappend ans [list set FOO "$path1"]
lappend ans [list set FOO_modshare "$path1:2"]
testouterr_cmd_re "sh" "prepend-path FOO $path1" $ans ""

set ans [list]
lappend ans [list setpath FOO "$path2:$path1"]
testouterr_cmd_re "ALL" "prepend-path FOO $path2" $ans ""
testouterr_cmd_re "sh" "prepend-path -d : FOO $path2" $ans ""
testouterr_cmd_re "sh" "prepend-path --delim : FOO $path2" $ans ""
testouterr_cmd_re "sh" "prepend-path --delim=: FOO $path2" $ans ""

set ans [list]
lappend ans [list setpath FOO "$path2,$path1"]
testouterr_cmd_re "sh" "prepend-path -d , FOO $path2" $ans ""
testouterr_cmd_re "sh" "prepend-path --delim , FOO $path2" $ans ""
testouterr_cmd_re "sh" "prepend-path --delim=, FOO $path2" $ans ""


if { $verbose > 0 } {
    send_user "\tSetup FOO = '$path2:$path1'\n"
}
set env(FOO) "$path2:$path1"

set ans [list]
lappend ans [list setpath FOO "$path2:$path1"]
testouterr_cmd_re "sh" "prepend-path FOO $path2" $ans ""

set ans [list]
lappend ans [list setpath FOO ":$path2:$path1"]
testouterr_cmd_re "sh" "prepend-path FOO " $ans ""


# test bad argument list
testouterr_cmd_re "sh" "prepend-path FOO" "ERR" "$error_msgs: Unexpected number of args for 'prepend-path' command\n$err_typehelp"
testouterr_cmd_re "sh" "prepend-path -d : FOO" "ERR" "$error_msgs: add-path $msg_needenvvarval FOO"
testouterr_cmd_re "sh" "prepend-path -d :" "ERR" "$error_msgs: add-path $msg_needenvvarname"
testouterr_cmd_re "sh" "prepend-path -d : " "ERR" "$error_msgs: add-path $msg_validenvvarname"


#
# Prepend multiple paths passed as one string
#

set path1 "/path/to/dir1"
if { $verbose > 0 } {
    send_user "\tSetup FOO_modshare = '$path1:1'\n"
    send_user "\tSetup FOO = '$path1'\n"
}
set env(FOO_modshare) "$path1:1"
set env(FOO) $path1

set ans [list]
lappend ans [list set FOO ":/modules/testsuite2.2:/modules/testsuite2.2/2.2:$path1"]
lappend ans [list set FOO_modshare ":1:/modules/testsuite2.2/2.2:1:$path1:1:/modules/testsuite2.2:1"]
testouterr_cmd_re "sh" "prepend-path FOO :/modules/testsuite2.2:/modules/testsuite2.2/2.2" $ans ""
testouterr_cmd_re "sh" "prepend-path FOO :/modules/testsuite2.2 /modules/testsuite2.2/2.2" $ans ""

# with specific delim
if { $verbose > 0 } {
    send_user "\tSetup FOO_modshare = 'http://foobar.org:1:http://foobar.net:1'\n"
    send_user "\tSetup FOO = 'http://foobar.org,http://foobar.net'\n"
}
set env(FOO_modshare) "http\\://foobar.org:1:http\\://foobar.net:1"
set env(FOO) "http://foobar.org,http://foobar.net"

set ans [list]
lappend ans [list set FOO ",http://foobar.com,http://foobar.io,http://foobar.org,http://foobar.net"]
lappend ans [list set FOO_modshare ":1:http\\://foobar.com:1:http\\://foobar.net:1:http\\://foobar.io:1:http\\://foobar.org:1"]
testouterr_cmd_re "sh" "prepend-path -d , FOO ,http://foobar.com,http://foobar.io" $ans ""
testouterr_cmd_re "sh" "prepend-path -d , FOO ,http://foobar.com http://foobar.io" $ans ""


#
# Prepend path in duplication mode
#

if { $verbose > 0 } {
    send_user "\tSetup FOO_modshare = '$path1:1:/foo:1'\n"
    send_user "\tSetup FOO = '$path1:/foo'\n"
}
set env(FOO_modshare) "$path1:1:/foo:1"
set env(FOO) "$path1:/foo"

set ans [list]
lappend ans [list set FOO "$path1:$path1:/foo"]
lappend ans [list set FOO_modshare "$path1:2:/foo:1"]
testouterr_cmd_re "sh" "prepend-path --duplicates FOO $path1" $ans ""


#
#  Cleanup
#

reset_test_env
