From 73b8bec8db34d1cfb51bf97934ac31a3909f0eaf Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Sun, 24 Apr 2022 19:42:10 +0000 Subject: [PATCH] Fix wrapper when passing multiple arguments and add unit tests This change come from the PR #289 --- lib/core/wrappers.sh | 6 +++--- tests/unit-tests/test-wrappers.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib/core/wrappers.sh b/lib/core/wrappers.sh index 13b206a..d6c2f52 100644 --- a/lib/core/wrappers.sh +++ b/lib/core/wrappers.sh @@ -33,12 +33,12 @@ function create_wrappers() { # Arguments inside a variable (i.e. `JUNEST_ARGS`) separated by quotes # are not recognized normally unless using `eval`. More info here: # https://github.com/fsquillace/junest/issues/262 + # https://github.com/fsquillace/junest/pull/287 cat < "${JUNEST_HOME}/usr/bin_wrappers/${file}" #!/usr/bin/env bash -JUNEST_ARGS=\${JUNEST_ARGS:-ns} - -eval junest "\${JUNEST_ARGS}" -- ${file} "\$(printf "%q" "\$@")" +eval "junest_args_array=(\${JUNEST_ARGS:-ns})" +junest "\${junest_args_array[@]}" -- ${file} "\$@" EOF chmod +x "${JUNEST_HOME}/usr/bin_wrappers/${file}" done diff --git a/tests/unit-tests/test-wrappers.sh b/tests/unit-tests/test-wrappers.sh index af7aae2..ebd1eff 100755 --- a/tests/unit-tests/test-wrappers.sh +++ b/tests/unit-tests/test-wrappers.sh @@ -43,6 +43,35 @@ function test_create_wrappers_executable_file(){ assertTrue "myfile wrapper should exist" "[ -x $JUNEST_HOME/usr/bin_wrappers/myfile ]" } +function test_create_wrappers_verify_content(){ + # Test for: + # https://github.com/fsquillace/junest/issues/262 + # https://github.com/fsquillace/junest/issues/292 + touch "$JUNEST_HOME"/usr/bin/myfile + chmod +x "$JUNEST_HOME"/usr/bin/myfile + export JUNEST_ARGS="ns --fakeroot -b '--bind /run /run2'" + assertCommandSuccess create_wrappers + assertEquals "" "$(cat "$STDOUTF")" + + # Mock junest command to capture the actual output generated from myfile script + junest(){ + for arg in "$@" + do + echo "$arg" + done + } + assertEquals "ns +--fakeroot +-b +--bind /run /run2 +-- +myfile +pacman +-Rsn +neovim +new package" "$(source "$JUNEST_HOME"/usr/bin_wrappers/myfile pacman -Rsn neovim 'new package')" +} + function test_create_wrappers_already_exist(){ touch "$JUNEST_HOME"/usr/bin/myfile chmod +x "$JUNEST_HOME"/usr/bin/myfile