##############################################################################
#   Modules Revision 3.0
#   Providing a flexible user environment
#
#   File:		modules.70-maint/%M%
#   Revision:		%I%
#   First Edition:	2017/11/25
#   Last Mod.:		%U%, %G%
#
#   Authors:		Xavier Delaruelle, xavier.delaruelle@cea.fr
#
#   Description:	Testuite testsequence
#   Command:        append-path
#   Modulefiles:
#   Sub-Command:
#
#   Comment:	%C{
#           Test 'append-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" "append-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" "append-path FOO $path1" $ans ""

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

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


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

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

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

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


#
# Append 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 "$path1:/modules/testsuite2.2:/modules/testsuite2.2/2.2:"]
lappend ans [list set FOO_modshare ":1:/modules/testsuite2.2/2.2:1:$path1:1:/modules/testsuite2.2:1"]
testouterr_cmd_re "sh" "append-path FOO /modules/testsuite2.2:/modules/testsuite2.2/2.2:" $ans ""
testouterr_cmd_re "sh" "append-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.org,http://foobar.net,http://foobar.com,http://foobar.io,"]
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" "append-path -d , FOO http://foobar.com,http://foobar.io," $ans ""
testouterr_cmd_re "sh" "append-path -d , FOO http://foobar.com http://foobar.io," $ans ""


#
# Append path in duplication mode
#

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 "$path1:$path1"]
lappend ans [list set FOO_modshare "$path1:2"]
testouterr_cmd_re "sh" "append-path --duplicates FOO $path1" $ans ""
testouterr_cmd_re "sh" "append-path --index --duplicates FOO $path1" $ans "$warn_msgs: --index option has no effect on add-path"


#
# Append path on DYLD-specific variables
#

set ans [list]
lappend ans [list set DYLD_FOO "$path1"]
lappend ans [list set MODULES_MODSHARE_DYLD_FOO "$path1:1"]
testouterr_cmd_re "sh" "append-path DYLD_FOO $path1" $ans ""

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

# path-variable handling not effective on OSX with DYLD variables when System
# Integrity Protection (SIP) is enabled: variables are not exported in
# subshell context, so they appear undefined to modulecmd.tcl
if {$sip_enabled} {
    set dyld_refcount 1
} else {
    set dyld_refcount 2
}
set tserr "(dyld: warning, unknown environment variable: DYLD_FOO\n|)"

set ans [list]
lappend ans [list set DYLD_FOO "$path1"]
lappend ans [list set MODULES_MODSHARE_DYLD_FOO "$path1:$dyld_refcount"]
testouterr_cmd_re "sh" "append-path DYLD_FOO $path1" $ans $tserr


#
#  Cleanup
#

reset_test_env
