From a2c674458b6a1947aeeafe744fb1e845881ec3b4 Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 2 Jan 2024 04:07:50 -0600 Subject: [PATCH 1/5] add unit tests --- .gitignore | 4 ++++ tests/run.sh | 35 +++++++++++++++++++++++++++++++++++ tests/testArgs.sh | 3 +++ 3 files changed, 42 insertions(+) create mode 100644 .gitignore create mode 100755 tests/run.sh create mode 100755 tests/testArgs.sh 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/tests/run.sh b/tests/run.sh new file mode 100755 index 0000000..99a3a81 --- /dev/null +++ b/tests/run.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Pass in name and status +function die { echo $1: status $2 ; exit $2; } + +function tests() { + # 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 +} + +# 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[@]} From 32e1893ea4142e8774371a2a0c110e1a3e3c6b6c Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 2 Jan 2024 04:08:14 -0600 Subject: [PATCH 2/5] fix handling of standalone case --- cmssw-env | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cmssw-env b/cmssw-env index 7f2d1c1..317e1a5 100755 --- a/cmssw-env +++ b/cmssw-env @@ -56,10 +56,12 @@ 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[@]}" 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}") +else + CMD_TO_RUN=("${CMD_STR}") fi if [ "X${UNPACKED_IMAGE}" = "X" ] ;then From 8e8c57434d39f1a26d60aff67e94e9738084a653 Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 2 Jan 2024 05:28:37 -0600 Subject: [PATCH 3/5] add single and multi command tests --- tests/run.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/run.sh b/tests/run.sh index 99a3a81..9e57608 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -4,6 +4,12 @@ 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" @@ -15,6 +21,17 @@ function tests() { 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 From c2855ad4a00a24424e182db00f15698feac38083 Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 2 Jan 2024 05:29:00 -0600 Subject: [PATCH 4/5] fix multi command handling using eval --- cmssw-env | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmssw-env b/cmssw-env index 317e1a5..e8c1718 100755 --- a/cmssw-env +++ b/cmssw-env @@ -58,10 +58,13 @@ 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 - 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_STR}") + CMD_TO_RUN=("${CMD_PREF}${CMD_STR}") fi if [ "X${UNPACKED_IMAGE}" = "X" ] ;then From 4913b9b4c5ebe8369b6c9b4b2b777787510316ad Mon Sep 17 00:00:00 2001 From: Kevin Pedro Date: Tue, 2 Jan 2024 05:30:47 -0600 Subject: [PATCH 5/5] update usage --- cmssw-env | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmssw-env b/cmssw-env index e8c1718..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 ;;