From 4e2724472659db93386709feb4cb4e68ae3634b4 Mon Sep 17 00:00:00 2001 From: Xavier Delaruelle Date: Tue, 16 May 2017 20:17:00 +0200 Subject: [PATCH] Fix 'init*' commands to behave more like C-version Adapt tests from 60-initx to match results produced by this module flavor. Clarify differences with C-version on these module commands in diff_with_c-version documentation. --- doc/source/diff_with_c-version.pod | 18 +++ modulecmd.tcl | 124 +++++++++------------ testsuite/modules.00-init/071-args.exp | 4 - testsuite/modules.60-initx/005-init.exp | 4 +- testsuite/modules.60-initx/010-list.exp | 14 +-- testsuite/modules.60-initx/020-add.exp | 11 +- testsuite/modules.60-initx/030-switch.exp | 13 ++- testsuite/modules.60-initx/040-prepend.exp | 11 +- testsuite/modules.60-initx/050-rm.exp | 20 ++-- testsuite/modules.60-initx/060-2nd.exp | 21 ++-- testsuite/modules.60-initx/095-cleanup.exp | 2 + 11 files changed, 127 insertions(+), 115 deletions(-) diff --git a/doc/source/diff_with_c-version.pod b/doc/source/diff_with_c-version.pod index 50d04261..583e98e9 100644 --- a/doc/source/diff_with_c-version.pod +++ b/doc/source/diff_with_c-version.pod @@ -72,6 +72,24 @@ When switching on Tcl-version an I modulefile by a I one, no error is When switching on Tcl-version an I modulefile by a I one, this I modulefile does not keep the position that the I modulefile had in the B list as done on C-version but it is appended to the end of the list. Same goes for PATH-like environment variables: replaced PATH component is appended to the end or prepended to the beginning of the relative PATH-like variable, not appended or prepended relatively to the position hold by the swapped PATH component. +=item B + +=item B + +=item B + +On Tcl-version no message is displayed to give details on how list of modulefiles to load has been altered in initialization file. + +=item B + +No message is displayed on Tcl-version to inform of the modulefiles that have been removed from the loading list in initialization file. + +When last modulefile from a loading list is asked to be removed, the entire B line is removed from initialization file. + +=item B + +All B lines are removed from shell initialization files on Tcl-version when B is invoked. + =back =head2 Modules Specific Tcl Commands diff --git a/modulecmd.tcl b/modulecmd.tcl index 8cfb3634..51de9c89 100755 --- a/modulecmd.tcl +++ b/modulecmd.tcl @@ -33,8 +33,8 @@ echo "FATAL: module: Could not find tclsh in \$PATH or in standard directories" # # Some Global Variables..... # -set MODULES_CURRENT_VERSION 1.843 -set MODULES_CURRENT_RELEASE_DATE "2017-05-10" +set MODULES_CURRENT_VERSION 1.854 +set MODULES_CURRENT_RELEASE_DATE "2017-05-16" set g_debug 0 ;# Set to 1 to enable debugging set error_count 0 ;# Start with 0 errors set g_autoInit 0 @@ -961,21 +961,21 @@ proc module {command args} { cmdModuleSavelist } } - {^init(add|lo)$} { - if {[llength $args] != 1} { + {^init(a|lo)} { + if {[llength $args] == 0} { set errormsg "Unexpected number of args for 'initadd' command" } else { eval cmdModuleInit add $args } } - {^initprepend$} { - if {[llength $args] != 1} { + {^initp} { + if {[llength $args] == 0} { set errormsg "Unexpected number of args for 'initprepend' command" } else { eval cmdModuleInit prepend $args } } - {^initswitch$} { + {^initsw} { if {[llength $args] != 2} { set errormsg "Unexpected number of args for 'initswitch' command" } else { @@ -983,13 +983,13 @@ proc module {command args} { } } {^init(rm|unlo)$} { - if {[llength $args] != 1} { + if {[llength $args] == 0} { set errormsg "Unexpected number of args for 'initrm' command" } else { eval cmdModuleInit rm $args } } - {^initlist$} { + {^initl} { if {[llength $args] != 0} { set errormsg "Unexpected number of args for 'initlist' command" } else { @@ -2769,15 +2769,12 @@ if {[info commands lassign] eq ""} { } proc replaceFromList {list1 item {item2 {}}} { - set xi [lsearch -exact $list1 $item] - - while {$xi >= 0} { + while {[set xi [lsearch -exact $list1 $item]] >= 0} { if {[string length $item2] == 0} { set list1 [lreplace $list1 $xi $xi] } else { set list1 [lreplace $list1 $xi $xi $item2] } - set xi [lsearch -exact $list1 $item] } return $list1 @@ -4444,6 +4441,7 @@ proc cmdModuleInit {args} { global g_shell env set moduleinit_cmd [lindex $args 0] + set moduleinit_list [lrange $args 1 end] set notdone 1 set notclear 1 @@ -4460,17 +4458,6 @@ proc cmdModuleInit {args} { set files(fish) [list ".modules" ".config/fish/config.fish"] set files(zsh) [list ".modules" ".zshrc" ".zshenv" ".zlogin"] - array set nargs { - list 0 - add 1 - load 1 - prepend 1 - rm 1 - unload 1 - switch 2 - clear 0 - } - # Process startup files for this shell set current_files $files($g_shell) foreach filename $current_files { @@ -4484,13 +4471,6 @@ proc cmdModuleInit {args} { if {[file readable $filepath] && [file isfile $filepath]} { set fid [open $filepath r] - set temp [expr {[llength $args] -1}] - if {$temp != $nargs($moduleinit_cmd)} { - reportErrorAndExit "'module init$moduleinit_cmd' requires\ - exactly $nargs($moduleinit_cmd) arg(s)." - # cmdModuleHelp - } - # Only open the new file if we are not doing "initlist" if {[string compare $moduleinit_cmd "list"] != 0} { set newfid [open $newfilepath w] @@ -4509,48 +4489,53 @@ proc cmdModuleInit {args} { # given command switch $moduleinit_cmd { list { - report "$g_shell initialization file $filepath\ - loads modules: $modules" + if {![info exists notheader]} { + report "$g_shell initialization file\ + \$HOME/$filename loads modules:" + set notheader 0 + } + report "\t$modules" } add { - set newmodule [lindex $args 1] - set modules [replaceFromList $modules $newmodule] - append modules " $newmodule" - puts $newfid "$cmd$modules$comments" - set notdone 0 + foreach newmodule $moduleinit_list { + set modules [replaceFromList $modules $newmodule] + } + puts $newfid "$cmd$modules $moduleinit_list$comments" + # delete new modules in potential next lines + set moduleinit_cmd "rm" } prepend { - set newmodule [lindex $args 1] - set modules [replaceFromList $modules $newmodule] - set modules "$newmodule $modules" - puts $newfid "$cmd$modules$comments" - set notdone 0 + foreach newmodule $moduleinit_list { + set modules [replaceFromList $modules $newmodule] + } + puts $newfid "$cmd$moduleinit_list $modules$comments" + # delete new modules in potential next lines + set moduleinit_cmd "rm" } rm { - set oldmodule [lindex $args 1] - set modules [replaceFromList $modules $oldmodule] - if {[llength $modules] == 0} { - set modules "" + set oldmodcount [llength $modules] + foreach oldmodule $moduleinit_list { + set modules [replaceFromList $modules $oldmodule] + } + set modcount [llength $modules] + if {$modcount > 0} { + puts $newfid "$cmd$modules$comments" + } + if {$oldmodcount > $modcount} { + set notdone 0 } - puts $newfid "$cmd$modules$comments" - set notdone 0 } switch { - set oldmodule [lindex $args 1] - set newmodule [lindex $args 2] - set modules [replaceFromList $modules\ + set oldmodule [lindex $moduleinit_list 0] + set newmodule [lindex $moduleinit_list 1] + set newmodules [replaceFromList $modules\ $oldmodule $newmodule] - puts $newfid "$cmd$modules$comments" - set notdone 0 + puts $newfid "$cmd$newmodules$comments" + if {"$modules" ne "$newmodules"} { + set notdone 0 + } } clear { - set modules "" - puts $newfid "$cmd$modules$comments" - set notclear 0 - } - default { - report "Command init$moduleinit_cmd not\ - recognized" } } } else { @@ -4564,12 +4549,13 @@ proc cmdModuleInit {args} { close $fid if {[info exists newfid]} { close $newfid - if {[catch {file copy -force $filepath $filepath-OLD}] != 0} { - reportErrorAndExit "Failed to back up original\ - $filepath...exiting" + if {[catch {file copy -force $newfilepath $filepath} errMsg]} { + reportErrorAndExit "Init file $filepath cannot be\ + written.\n$errMsg" } - if {[catch {file copy -force $newfilepath $filepath}] != 0} { - reportErrorAndExit "Failed to write $filepath...exiting" + if {[catch {file delete $newfilepath} errMsg]} { + reportErrorAndExit "Temp file $filepath cannot be\ + deleted.\n$errMsg" } } } @@ -4656,11 +4642,11 @@ proc cmdModuleHelp {args} { report {Shell's initialization files handling commands:} report { initlist List all modules\ loaded from init file} - report { initadd modulefile Add modulefile to\ + report { initadd modulefile [...] Add modulefile to\ shell init file} - report { initrm modulefile Remove modulefile\ + report { initrm modulefile [...] Remove modulefile\ from shell init file} - report { initprepend modulefile Add to beginning of\ + report { initprepend modulefile [...] Add to beginning of\ list in init file} report { initswitch mod1 mod2 Switch mod1 with mod2\ from init file} diff --git a/testsuite/modules.00-init/071-args.exp b/testsuite/modules.00-init/071-args.exp index 504c158b..0d0f8fff 100644 --- a/testsuite/modules.00-init/071-args.exp +++ b/testsuite/modules.00-init/071-args.exp @@ -80,14 +80,10 @@ testouterr_cmd "sh" "saverm foo bar" "$err_sh" "${bad_arg1}saverm${bad_arg2}\n$t testouterr_cmd "sh" "saveshow foo bar" "$err_sh" "${bad_arg1}saveshow${bad_arg2}\n$type_help" testouterr_cmd "sh" "savelist foo" "$err_sh" "${bad_arg1}savelist${bad_arg2}\n$type_help" testouterr_cmd "sh" "initadd" "$err_sh" "${bad_arg1}initadd${bad_arg2}\n$type_help" -testouterr_cmd "sh" "initadd foo bar" "$err_sh" "${bad_arg1}initadd${bad_arg2}\n$type_help" testouterr_cmd "sh" "initprepend" "$err_sh" "${bad_arg1}initprepend${bad_arg2}\n$type_help" -testouterr_cmd "sh" "initprepend foo bar" "$err_sh" "${bad_arg1}initprepend${bad_arg2}\n$type_help" testouterr_cmd "sh" "initswitch" "$err_sh" "${bad_arg1}initswitch${bad_arg2}\n$type_help" testouterr_cmd "sh" "initswitch foo1 bar foo2" "$err_sh" "${bad_arg1}initswitch${bad_arg2}\n$type_help" testouterr_cmd "sh" "initrm" "$err_sh" "${bad_arg1}initrm${bad_arg2}\n$type_help" -testouterr_cmd "sh" "initrm foo bar" "$err_sh" "${bad_arg1}initrm${bad_arg2}\n$type_help" -testouterr_cmd "sh" "initrm" "$err_sh" "${bad_arg1}initrm${bad_arg2}\n$type_help" testouterr_cmd "sh" "initlist foo" "$err_sh" "${bad_arg1}initlist${bad_arg2}\n$type_help" testouterr_cmd "sh" "initclear foo" "$err_sh" "${bad_arg1}initclear${bad_arg2}\n$type_help" testouterr_cmd "sh" "debug foo" "$err_sh" "${bad_arg1}debug${bad_arg2}\n$type_help" diff --git a/testsuite/modules.60-initx/005-init.exp b/testsuite/modules.60-initx/005-init.exp index 07b27367..85431d46 100644 --- a/testsuite/modules.60-initx/005-init.exp +++ b/testsuite/modules.60-initx/005-init.exp @@ -21,8 +21,8 @@ ############################################################################## set modload "module load" -set line2 "\n$modload dot module-cvs module-info modules use.own" -set line2x "\n$modload dot module-cvs module-info use.own" +set line2 "$modload dot module-cvs module-info modules use.own" +set line2x "$modload dot module-cvs module-info use.own" # # Save the former HOME file setup diff --git a/testsuite/modules.60-initx/010-list.exp b/testsuite/modules.60-initx/010-list.exp index 383ab259..2202ea80 100644 --- a/testsuite/modules.60-initx/010-list.exp +++ b/testsuite/modules.60-initx/010-list.exp @@ -20,14 +20,14 @@ # ############################################################################## -set initlist " initialization file \$HOME/.modules loads modules:\n\tnull\n\tdot module-cvs module-info modules use.own\n" +set initlist " initialization file \$HOME/.modules loads modules:\n\tnull\n\tdot module-cvs module-info modules use.own" -testerr_cmd sh "initlist" "\nsh$initlist" -testerr_cmd ksh "initlis" "\nksh$initlist" -testerr_cmd zsh "initli" "\nzsh$initlist" -testerr_cmd csh "initl" "\ncsh$initlist" -testerr_cmd tcsh "initlist" "\ntcsh$initlist" +testerr_cmd sh "initlist" "sh$initlist" +testerr_cmd ksh "initlis" "ksh$initlist" +testerr_cmd zsh "initli" "zsh$initlist" +testerr_cmd csh "initl" "csh$initlist" +testerr_cmd tcsh "initlist" "tcsh$initlist" -_test_modules sh "initlist" "$modload null$line2" +_test_modules sh "initlist" "$modload null\n$line2" unset initlist diff --git a/testsuite/modules.60-initx/020-add.exp b/testsuite/modules.60-initx/020-add.exp index d48aa51b..b63a4835 100644 --- a/testsuite/modules.60-initx/020-add.exp +++ b/testsuite/modules.60-initx/020-add.exp @@ -21,14 +21,15 @@ ############################################################################## testerr_cmd sh "initadd foobar" "" -_test_modules sh "initadd" "$modload null foobar$line2" +_test_modules sh "initadd" "$modload null foobar\n$line2" testerr_cmd ksh "initad foo bar" "" -_test_modules ksh "initadd" "$modload null foobar foo bar$line2" +_test_modules ksh "initadd" "$modload null foobar foo bar\n$line2" testerr_cmd zsh "inita poo/2.0" "" -_test_modules zsh "initadd" "$modload null foobar foo bar poo/2.0$line2" +_test_modules zsh "initadd" "$modload null foobar foo bar poo/2.0\n$line2" -testerr_cmd csh "initadd foobar" "Moving foobar to end" -_test_modules csh "initadd" "$modload null foo bar poo/2.0 foobar$line2" +# different behavior than C-version: on Tcl-version no message is printed +testerr_cmd csh "initadd foobar" "" +_test_modules csh "initadd" "$modload null foo bar poo/2.0 foobar\n$line2" diff --git a/testsuite/modules.60-initx/030-switch.exp b/testsuite/modules.60-initx/030-switch.exp index 4d2f00ef..8a57e8f1 100644 --- a/testsuite/modules.60-initx/030-switch.exp +++ b/testsuite/modules.60-initx/030-switch.exp @@ -20,12 +20,13 @@ # ############################################################################## -testerr_cmd sh "initswitch poo/2.0 poo/1.0" "Switching poo/2.0 to poo/1.0" -_test_modules csh "initswitch" "$modload null foo bar poo/1.0 foobar$line2" +# different behavior than C-version: on Tcl-version no message is printed +testerr_cmd sh "initswitch poo/2.0 poo/1.0" "" +_test_modules csh "initswitch" "$modload null foo bar poo/1.0 foobar\n$line2" -testerr_cmd ksh "initswit foo food" "Switching foo to food" -_test_modules ksh "initswitch" "$modload null food bar poo/1.0 foobar$line2" +testerr_cmd ksh "initswit foo food" "" +_test_modules ksh "initswitch" "$modload null food bar poo/1.0 foobar\n$line2" -testerr_cmd csh "initsw bar bar/2.0" "Switching bar to bar/2.0" -_test_modules csh "initswitch" "$modload null food bar/2.0 poo/1.0 foobar$line2" +testerr_cmd csh "initsw bar bar/2.0" "" +_test_modules csh "initswitch" "$modload null food bar/2.0 poo/1.0 foobar\n$line2" diff --git a/testsuite/modules.60-initx/040-prepend.exp b/testsuite/modules.60-initx/040-prepend.exp index f07c2637..0df38900 100644 --- a/testsuite/modules.60-initx/040-prepend.exp +++ b/testsuite/modules.60-initx/040-prepend.exp @@ -26,14 +26,15 @@ file copy -force "$env(HOME)/.modules.save" "$env(HOME)/.modules" testerr_cmd sh "initprepend foobar" "" -_test_modules sh "initprepend" "$modload foobar null$line2" +_test_modules sh "initprepend" "$modload foobar null\n$line2" testerr_cmd ksh "initpre foo bar" "" -_test_modules ksh "initprepend" "$modload foo bar foobar null$line2" +_test_modules ksh "initprepend" "$modload foo bar foobar null\n$line2" testerr_cmd zsh "initpr poo/2.0" "" -_test_modules zsh "initprepend" "$modload poo/2.0 foo bar foobar null$line2" +_test_modules zsh "initprepend" "$modload poo/2.0 foo bar foobar null\n$line2" -testerr_cmd csh "initp foobar" "Moving foobar to beginning" -_test_modules csh "initprepend" "$modload foobar poo/2.0 foo bar null$line2" +# different behavior than C-version: on Tcl-version no message is printed +testerr_cmd csh "initp foobar" "" +_test_modules csh "initprepend" "$modload foobar poo/2.0 foo bar null\n$line2" diff --git a/testsuite/modules.60-initx/050-rm.exp b/testsuite/modules.60-initx/050-rm.exp index 393da79b..a72ecfc6 100644 --- a/testsuite/modules.60-initx/050-rm.exp +++ b/testsuite/modules.60-initx/050-rm.exp @@ -20,21 +20,21 @@ # ############################################################################## -testerr_cmd sh "initrm foobar" "Removed foobar" -_test_modules sh "initrm" "$modload poo/2.0 foo bar null$line2" +testerr_cmd sh "initrm foobar" "" +_test_modules sh "initrm" "$modload poo/2.0 foo bar null\n$line2" -testerr_cmd ksh "initrm foo bar" "Removed foo\nRemoved bar" -_test_modules ksh "initrm" "$modload poo/2.0 null$line2" +testerr_cmd ksh "initrm foo bar" "" +_test_modules ksh "initrm" "$modload poo/2.0 null\n$line2" -testerr_cmd zsh "initrm poo/2.0" "Removed poo/2.0" -_test_modules zsh "initrm" "$modload null$line2" +testerr_cmd zsh "initrm poo/2.0" "" +_test_modules zsh "initrm" "$modload null\n$line2" # not there testerr_cmd csh "initrm foobar" "" -_test_modules csh "initrm" "$modload null$line2" +_test_modules csh "initrm" "$modload null\n$line2" # go too far - -testerr_cmd tcsh "initrm null" "Removed null" -_test_modules tcsh "initrm" "$modload null$line2" +# different behavior than C-version: on Tcl-version entire line is removed +testerr_cmd tcsh "initrm null" "" +_test_modules tcsh "initrm" "$line2" diff --git a/testsuite/modules.60-initx/060-2nd.exp b/testsuite/modules.60-initx/060-2nd.exp index fabd5553..ea0641f7 100644 --- a/testsuite/modules.60-initx/060-2nd.exp +++ b/testsuite/modules.60-initx/060-2nd.exp @@ -21,20 +21,27 @@ # ############################################################################## -testerr_cmd sh "initrm modules" "Removed modules" -_test_modules sh "initrm" "$modload null$line2x" +# +# restore the .modules file (again) +# +file copy -force "$env(HOME)/.modules.save" "$env(HOME)/.modules" + +testerr_cmd sh "initrm modules" "" +_test_modules sh "initrm" "$modload null\n$line2x" testerr_cmd sh "initadd modules" "" -_test_modules sh "initrm" "$modload null modules$line2x" +_test_modules sh "initrm" "$modload null modules\n$line2x" -testerr_cmd sh "initadd dot" "Moving dot to end" +# different behavior than C-version: on Tcl-version no message is printed +testerr_cmd sh "initadd dot" "" _test_modules sh "initadd" "$modload null modules dot\n$modload module-cvs module-info use.own" -testerr_cmd sh "initpre use.own" "Moving use.own to beginning" +testerr_cmd sh "initpre use.own" "" _test_modules sh "initprepend" "$modload use.own null modules dot\n$modload module-cvs module-info" -testerr_cmd sh "initswitch module-info info-module" "Switching module-info to info-module" +testerr_cmd sh "initswitch module-info info-module" "" _test_modules sh "initswitch" "$modload use.own null modules dot\n$modload module-cvs info-module" +# different behavior than C-version: on Tcl-version no 'module load null' lines are left testerr_cmd sh "initclear" "" -_test_modules sh "initclear" "$modload null\n$modload null" +_test_modules sh "initclear" "" diff --git a/testsuite/modules.60-initx/095-cleanup.exp b/testsuite/modules.60-initx/095-cleanup.exp index b489ad87..0394d1ae 100644 --- a/testsuite/modules.60-initx/095-cleanup.exp +++ b/testsuite/modules.60-initx/095-cleanup.exp @@ -20,6 +20,8 @@ # ############################################################################## +file delete "$env(HOME)/.modules" + if { [info exists save_home ] } { set env(HOME) $save_home } else {