From 7d8c619ee06127f8dd28d2238d881a3cfbfdb321 Mon Sep 17 00:00:00 2001 From: Filippo Squillace Date: Wed, 5 Jan 2022 23:47:55 +0100 Subject: [PATCH] #284 Fix copy command --- lib/core/common.sh | 8 +++++--- tests/unit-tests/test-common.sh | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/core/common.sh b/lib/core/common.sh index 7618af5..7d23d07 100644 --- a/lib/core/common.sh +++ b/lib/core/common.sh @@ -276,20 +276,22 @@ function copy_passwd_and_group(){ ! getent_cmd passwd ${USER} >> ${JUNEST_HOME}/etc/passwd then warn "getent command failed or does not exist. Binding directly from /etc/passwd." - copy_file /etc/passwd ${JUNEST_HOME}/etc/passwd + copy_file /etc/passwd fi if ! getent_cmd group > ${JUNEST_HOME}/etc/group then warn "getent command failed or does not exist. Binding directly from /etc/group." - copy_file /etc/group ${JUNEST_HOME}/etc/group + copy_file /etc/group fi return 0 } function copy_file() { local file="${1}" - [[ -r "$file" ]] && cp_cmd "$file" "${JUNEST_HOME}/$file" + # -f option ensure to remove destination file if it cannot be opened + # https://github.com/fsquillace/junest/issues/284 + [[ -r "$file" ]] && cp_cmd -f "$file" "${JUNEST_HOME}/$file" return 0 } diff --git a/tests/unit-tests/test-common.sh b/tests/unit-tests/test-common.sh index 0aba4b7..7037f98 100755 --- a/tests/unit-tests/test-common.sh +++ b/tests/unit-tests/test-common.sh @@ -197,7 +197,7 @@ function test_copy_passwd_and_group_fallback(){ echo $@ } CP=cp_cmd_mock GETENT=false LD_EXEC=false assertCommandSuccess copy_passwd_and_group - assertEquals "$(echo -e "/etc/passwd $JUNEST_HOME//etc/passwd\n/etc/group $JUNEST_HOME//etc/group")" "$(cat $STDOUTF)" + assertEquals "$(echo -e "-f /etc/passwd $JUNEST_HOME//etc/passwd\n-f /etc/group $JUNEST_HOME//etc/group")" "$(cat $STDOUTF)" } function test_copy_passwd_and_group_failure(){