diff --git a/.travis.yml b/.travis.yml index 66f859f..1d79584 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,13 +5,12 @@ install: - juju -f echo 'Installing juju' - sed -i -e "s/#Server/Server/" ~/.juju/etc/pacman.d/mirrorlist - juju -f pacman --noconfirm -Syy + - juju -f pacman --noconfirm -S base-devel script: - SKIP_ROOT_TESTS=1 ./tests/test_all.sh -# Test on installing package from AUR (issue #58) - #- juju -f pacman --noconfirm -S base-devel - #- juju -f yaourt --noconfirm -S tcptraceroute - #- juju -f tcptraceroute localhost +# Test on installing package from AUR + - juju -f yaourt --noconfirm -S tcptraceroute # Test on installing package from official repo - juju -f pacman --noconfirm -S sysstat - juju -f iostat diff --git a/lib/core.sh b/lib/core.sh index 8580c1e..5b5a8c1 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -150,22 +150,24 @@ function setup_from_file_juju(){ function run_juju_as_root(){ - local main_cmd="${SH[@]}" - [ "$1" != "" ] && main_cmd="$@" - local uid=$UID [ -z $SUDO_UID ] || uid=$SUDO_UID:$SUDO_GID - local cmd=" -mkdir -p ${JUJU_HOME}/${HOME} -mkdir -p /run/lock -${main_cmd} -" + local main_cmd="${SH[@]}" + [ "$1" != "" ] && main_cmd="$(insert_quotes_on_spaces "$@")" + local cmd="mkdir -p ${JUJU_HOME}/${HOME} && mkdir -p /run/lock && ${main_cmd}" - JUJU_ENV=1 ${CHROOT} $JUJU_HOME /usr/bin/bash -c "${cmd}" + trap - QUIT EXIT ABRT KILL TERM INT + trap "[ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME}" EXIT QUIT ABRT KILL TERM INT + + JUJU_ENV=1 ${CHROOT} $JUJU_HOME "${SH[@]}" "-c" "${cmd}" + local ret=$? # The ownership of the files in JuJu is assigned to the real user [ -z $uid ] || ${CHOWN} -R ${uid} ${JUJU_HOME} + + trap - QUIT EXIT ABRT KILL TERM INT + return $? } function _run_proot(){ @@ -190,7 +192,7 @@ function _run_juju_with_proot(){ if [ "$1" != "" ] then - _run_proot "${proot_args}" "${@}" + _run_proot "${proot_args}" "${SH[@]}" "-c" "$(insert_quotes_on_spaces "${@}")" else _run_proot "${proot_args}" "${SH[@]}" fi diff --git a/lib/util.sh b/lib/util.sh index 9f411a3..52e1b24 100644 --- a/lib/util.sh +++ b/lib/util.sh @@ -67,3 +67,21 @@ function ask(){ fi } + +function insert_quotes_on_spaces(){ +# It inserts quotes between arguments. +# Useful to preserve quotes on command +# to be used inside sh -c/bash -c + C='' + whitespace="[[:space:]]" + for i in "$@" + do + if [[ $i =~ $whitespace ]] + then + C="$C \"$i\"" + else + C="$C $i" + fi + done + echo $C +} diff --git a/tests/test_all.sh b/tests/test_all.sh index 684e6fd..48f9262 100755 --- a/tests/test_all.sh +++ b/tests/test_all.sh @@ -1,5 +1,7 @@ - +tests_succeded=true for tst in $(ls $(dirname $0)/test_* | grep -v $(basename $0)) do - $tst + $tst || tests_succeded=false done + +$tests_succeded diff --git a/tests/test_core.sh b/tests/test_core.sh index bf66e80..0549fff 100755 --- a/tests/test_core.sh +++ b/tests/test_core.sh @@ -105,20 +105,28 @@ function test_run_juju_as_root(){ install_mini_juju CHROOT="sudo $CHROOT" CHOWN="sudo $CHOWN" - SH=("type" "-t" "type") - local output=$(run_juju_as_root) - assertEquals $output "builtin" + local output=$(run_juju_as_root pwd) - assertEquals $output "/" - run_juju_as_root "[ -e /run/lock ]" + assertEquals "$output" "/" + run_juju_as_root [ -e /run/lock ] assertEquals $? 0 - run_juju_as_root "[ -e $HOME ]" + run_juju_as_root [ -e $HOME ] assertEquals $? 0 # test that normal user has ownership of the files created by root - run_juju_as_root "touch /a_root_file" - local output=$(run_juju_as_root "stat -c '%u' /a_root_file") - assertEquals $output "$UID" + run_juju_as_root touch /a_root_file + local output=$(run_juju_as_root stat -c '%u' /a_root_file) + assertEquals "$output" "$UID" + + SH=("sh" "--login" "-c" "type -t type") + local output=$(run_juju_as_root) + assertEquals "$output" "builtin" + SH=("sh" "--login" "-c" "[ -e /run/lock ]") + run_juju_as_root + assertEquals $? 0 + SH=("sh" "--login" "-c" "[ -e $HOME ]") + run_juju_as_root + assertEquals $? 0 } function test_run_juju_as_user(){ diff --git a/tests/test_util.sh b/tests/test_util.sh index f0a5447..c10a095 100755 --- a/tests/test_util.sh +++ b/tests/test_util.sh @@ -52,4 +52,12 @@ function test_ask(){ assertEquals $? 1 } +function test_insert_quotes_on_spaces(){ + local actual=$(insert_quotes_on_spaces this is "a test") + assertEquals "this is \"a test\"" "$actual" + + local actual=$(insert_quotes_on_spaces this is 'a test') + assertEquals "this is \"a test\"" "$actual" +} + source $(dirname $0)/shunit2