Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-command handling and add tests #12

Merged
merged 5 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# symlinks created by tests/run.sh
tests/cmssw-env
/cmsset_default.sh
tests/cmsos
14 changes: 10 additions & 4 deletions cmssw-env
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ while [ "$#" != 0 ]; do
-h|--help)
HELP_ARG=""
if [ "${CMS_IMAGE}" = "${BASE_SCRIPT}" ] ; then HELP_ARG="[--cmsos <image>] "; fi
echo "Usage: $0 [-h|--help] ${HELP_ARG}[extra-options] [--ignore-mount <dir1[,dir2[,...]]>] [--command-to-run|-- <command to run>]"
echo "Usage: $0 [-h|--help] ${HELP_ARG}[extra-options] [--ignore-mount <dir1[,dir2[,...]]>] [--command-to-run|-- <command(s)>]"
echo "Environment variable UNPACKED_IMAGE can be set to point to either valid docker/singularity image or unpacked image path"
echo "If <command> 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 ;;
Expand Down Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions tests/testArgs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
ARGS=("$@")
echo ${#ARGS[@]}