diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49b1559 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# symlinks created by tests/run.sh +tests/cmssw-env +/cmsset_default.sh +tests/cmsos diff --git a/cmssw-env b/cmssw-env index 7f2d1c1..c60a27c 100755 --- a/cmssw-env +++ b/cmssw-env @@ -18,8 +18,9 @@ while [ "$#" != 0 ]; do -h|--help) HELP_ARG="" if [ "${CMS_IMAGE}" = "${BASE_SCRIPT}" ] ; then HELP_ARG="[--cmsos ] "; fi - echo "Usage: $0 [-h|--help] ${HELP_ARG}[extra-options] [--ignore-mount ] [--command-to-run|-- ]" + echo "Usage: $0 [-h|--help] ${HELP_ARG}[extra-options] [--ignore-mount ] [--command-to-run|-- ]" echo "Environment variable UNPACKED_IMAGE can be set to point to either valid docker/singularity image or unpacked image path" + echo "If includes multiple commands separated by ; or &&, or other high-precedence shell operators like >, it must be quoted" exit 0 ;; --ignore-mount) IGNORE_MOUNTS=$(echo $2 | tr ',' ' '); shift; shift ;; @@ -56,10 +57,15 @@ for dir in /etc/tnsnames.ora /etc/pki/ca-trust /eos /build /data /afs /pool $(/b [ ! -e $dir ] || MOUNT_POINTS="${MOUNT_POINTS},${dir}" done OLD_CMSOS=$(echo ${SCRAM_ARCH} | cut -d_ -f1,2) +# necessary to preserve quotes/grouping in original CMD_TO_RUN when running multiple commands through sh -c +printf -v CMD_STR '%q ' "${CMD_TO_RUN[@]}" +# necessary to expand multi-command input given as quoted string +CMD_PREF= +if [ "${#CMD_TO_RUN[@]}" -eq 1 ]; then CMD_PREF="eval "; fi if [ -e ${THISDIR}/../cmsset_default.sh ] ; then - # necessary to preserve quotes/grouping in original CMD_TO_RUN when running multiple commands through sh -c - printf -v CMD_STR '%q ' "${CMD_TO_RUN[@]}" - CMD_TO_RUN=("[ \"${OLD_CMSOS}\" != \"\$(${THISDIR}/cmsos)\" ] && export SCRAM_ARCH=""; source ${THISDIR}/../cmsset_default.sh >/dev/null 2>&1; ${CMD_STR}") + CMD_TO_RUN=("[ \"${OLD_CMSOS}\" != \"\$(${THISDIR}/cmsos)\" ] && export SCRAM_ARCH=""; source ${THISDIR}/../cmsset_default.sh >/dev/null 2>&1; ${CMD_PREF}${CMD_STR}") +else + CMD_TO_RUN=("${CMD_PREF}${CMD_STR}") fi if [ "X${UNPACKED_IMAGE}" = "X" ] ;then diff --git a/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..9e57608 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Pass in name and status +function die { echo $1: status $2 ; exit $2; } + +function tests() { + # test single command + TEST=0 + EXPECTED="" + RESULT=$(./cmssw-env --cmsos cc7 -B $PWD --pwd $PWD -- echo) + if [ "$RESULT" != "$EXPECTED" ]; then die "Test $TEST: wrong output $RESULT (should be $EXPECTED)" 1; fi + + # test argument quoting + TEST=1 + EXPECTED="hello world" + RESULT=$(./cmssw-env --cmsos cc7 -B $PWD --pwd $PWD -- echo "$EXPECTED") + if [ "$RESULT" != "$EXPECTED" ]; then die "Test $TEST: wrong output $RESULT (should be $EXPECTED)" 1; fi + + # test argument quoting with script + TEST=2 + EXPECTED=3 + RESULT=$(./cmssw-env --cmsos cc7 -B $PWD --pwd $PWD -- ./testArgs.sh 1 "2 3" 4) + if [ "$RESULT" -ne "$EXPECTED" ]; then die "Test $TEST: incorrect number of arguments $RESULT (should be $EXPECTED)" 1; fi + + # test argument quoting with script, fully quoted + TEST=2b + RESULT=$(./cmssw-env --cmsos cc7 -B $PWD --pwd $PWD -- './testArgs.sh 1 "2 3" 4') + if [ "$RESULT" -ne "$EXPECTED" ]; then die "Test $TEST: incorrect number of arguments $RESULT (should be $EXPECTED)" 1; fi + + # test multi-command case + TEST=3 + EXPECTED=$(echo foo; echo bar) + RESULT=$(./cmssw-env --cmsos cc7 -B $PWD --pwd $PWD -- "echo foo; echo bar") + if [ "$RESULT" != "$EXPECTED" ]; then die "Test $TEST: wrong output $RESULT (should be $EXPECTED)" 1; fi +} + +# common setup +ln -sf ../cmssw-env . + +# cvmfs-like setup +ln -sf /cvmfs/cms.cern.ch/cmsset_default.sh ../ +ln -sf /cvmfs/cms.cern.ch/common/cmsos . + +echo "Testing with cvmfs-like setup" +tests + +# standalone setup +rm ../cmsset_default.sh +rm cmsos + +echo "Testing with standalone setup" +tests diff --git a/tests/testArgs.sh b/tests/testArgs.sh new file mode 100755 index 0000000..75d9541 --- /dev/null +++ b/tests/testArgs.sh @@ -0,0 +1,3 @@ +#!/bin/bash +ARGS=("$@") +echo ${#ARGS[@]}