Relax magic cookie constraint on source command

As files sourced by a 'module source' command may be managed outside of
the modulefile environment, relax the constraint of having a specific
magic cookie at the start of the file to get required clearance before
evaluation. This is typicaly helpful for the initialization modulerc
file that may loose its magic cookie after some manual edits.

Add non-regression tests in 090-source of 70-maint suite.
This commit is contained in:
Xavier Delaruelle
2017-04-17 19:09:14 +02:00
parent 26cd57f94c
commit 3fe71c0d74
5 changed files with 118 additions and 7 deletions

View File

@@ -1,3 +1,4 @@
#%Module1.0
# This file defines the initial setup for the modulefiles search path
# and modulefiles you want to load by default for all users. It should
# contains a lines of module command like 'module use path' and

View File

@@ -33,7 +33,7 @@ echo "FATAL: module: Could not find tclsh in \$PATH or in standard directories"
#
# Some Global Variables.....
#
set MODULES_CURRENT_VERSION 1.804
set MODULES_CURRENT_VERSION 1.807
set MODULES_CURRENT_RELEASE_DATE "2017-04-17"
set g_debug 0 ;# Set to 1 to enable debugging
set error_count 0 ;# Start with 0 errors
@@ -229,7 +229,7 @@ proc unset-env {var} {
}
}
proc execute-modulefile {modfile {exit_on_error 1}} {
proc execute-modulefile {modfile {exit_on_error 1} {must_have_cookie 1}} {
global g_debug g_inhibit_interp g_inhibit_errreport g_inhibit_dispreport
global ModulesCurrentModulefile
@@ -287,12 +287,14 @@ proc execute-modulefile {modfile {exit_on_error 1}} {
$g_inhibit_errreport]
interp eval $slave [list "set" "g_inhibit_dispreport"\
$g_inhibit_dispreport]
interp eval $slave [list "set" "must_have_cookie" $must_have_cookie]
}
set errorVal [interp eval $slave {
if {$g_debug} {
report "Sourcing $ModulesCurrentModulefile"
}
set modcontent [readModuleContent $ModulesCurrentModulefile 1]
set modcontent [readModuleContent $ModulesCurrentModulefile 1\
$must_have_cookie]
if {$modcontent eq ""} {
# exit after end of slave evaluation
return 2
@@ -2757,7 +2759,7 @@ proc checkValidModule {modfile} {
return 0
}
proc readModuleContent {modfile {report_read_issue 0}} {
proc readModuleContent {modfile {report_read_issue 0} {must_have_cookie 1}} {
reportDebug "readModuleContent: $modfile"
# read file
@@ -2772,8 +2774,8 @@ proc readModuleContent {modfile {report_read_issue 0}} {
return {}
}
# check module validity
if {[string first "\#%Module" $fdata] == 0} {
# check module validity if magic cookie is mandatory
if {[string first "\#%Module" $fdata] == 0 || !$must_have_cookie} {
return $fdata
} else {
reportInternalBug "Magic cookie '#%Module' missing in '$modfile'"
@@ -4008,7 +4010,11 @@ proc cmdModuleSource {args} {
pushMode "load"
pushSpecifiedName $file
pushModuleName $file
execute-modulefile $file
# relax constraint of having a magic cookie at the start of the
# modulefile to execute as sourced files may need more flexibility
# as they may be managed outside of the modulefile environment like
# the initialization modulerc file
execute-modulefile $file 1 0
popModuleName
popSpecifiedName
popMode

22
testsuite/home/rcnomagic Normal file
View File

@@ -0,0 +1,22 @@
#%Missing magic cookie
##############################################################################
# Modules Revision 3.0
# Providing a flexible user environment
#
# File: home/%M%
# Revision: %I%
# First Edition: 2017/04/17
# Last Mod.: %U%, %G%
#
# Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr
#
# Description: RC file to source
#
# Comment: %C{
# RC file to test behavior of module command 'source'
# }C%
#
##############################################################################
setenv testsuite "yes"

22
testsuite/home/rctosource Normal file
View File

@@ -0,0 +1,22 @@
#%Module1.0
##############################################################################
# Modules Revision 3.0
# Providing a flexible user environment
#
# File: home/%M%
# Revision: %I%
# First Edition: 2017/04/17
# Last Mod.: %U%, %G%
#
# Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr
#
# Description: RC file to source
#
# Comment: %C{
# RC file to test behavior of module command 'source'
# }C%
#
##############################################################################
setenv testsuite "yes"

View File

@@ -0,0 +1,60 @@
##############################################################################
# Modules Revision 3.0
# Providing a flexible user environment
#
# File: modules.70-maint/%M%
# Revision: %I%
# First Edition: 2017/04/17
# Last Mod.: %U%, %G%
#
# Authors: Xavier Delaruelle, xavier.delaruelle@cea.fr
#
# Description: Testuite testsequence
# Command: source
# Modulefiles: rctosource, rcnomagic, nonexistent
# Sub-Command:
#
# Comment: %C{
# Tests the module command 'source'
# }C%
#
##############################################################################
#
# Test variables
#
set module_1 "$env(TESTSUITEDIR)/home/rctosource"
set module_2 "$env(TESTSUITEDIR)/home/rcnomagic"
set module_3 "$env(TESTSUITEDIR)/home/nonexistent"
set out_csh "setenv testsuite yes;"
set out_csh3 "/bin/false;"
set err_csh "$error_msgs: File $module_3 does not exist"
#
# test sourcing rc file with regular module-specific magic cookie
#
testouterr_cmd "csh" "source $module_1" "$out_csh" ""
#
# test sourcing rc file with no module-specific magic cookie
#
testouterr_cmd "csh" "source $module_2" "$out_csh" ""
#
# test with nonexistent file
#
testouterr_cmd "csh" "source $module_3" "$out_csh3" "$err_csh"
#
# Cleanup
#
unset err_csh
unset out_csh3
unset out_csh
unset module_1
unset module_2
unset module_3