diff --git a/src/functions.sh b/src/functions.sh index 976a1f58..1c608bae 100644 --- a/src/functions.sh +++ b/src/functions.sh @@ -227,37 +227,7 @@ file_to_array () { [[ ${UNIT_TESTS:-1} -eq 0 ]] && echo "${output[@]}" - clean_array "$2" "${output[@]}" && return 0 -} - -# Function to trim spaces and new lines from array elements -# https://stackoverflow.com/a/9715377 -# https://stackoverflow.com/a/19347380 -# https://unix.stackexchange.com/a/225517 -# https://unix.stackexchange.com/a/360648 -# $1 - name of a variable where the result array will be stored -# $@ - source array -# $? - return value - 0 on success -clean_array () { - [[ $# -le 1 ]] && return 1 - local output="$1" - shift - local input=("$@") - - local extglob_state=0 - if ! shopt -q extglob; then - extglob_state=1 - shopt -s extglob - fi - - for i in "${input[@]}"; do - local cleaned_item="" - cleaned_item=$(printf '%s' "${i##+([[:space:]])}") \ - && cleaned_item=$(printf '%s' "${cleaned_item%%+([[:space:]])}") - eval "${output}"+=\("$(printf '%q' "${cleaned_item}")"\) - done - - [[ ${extglob_state} -ne 0 ]] && shopt -u extglob + eval "${2}"=\("${output[*]@Q}"\) } # Evaluate if variable contains true value diff --git a/test/clean_array.bats b/test/clean_array.bats deleted file mode 100644 index cef3ab71..00000000 --- a/test/clean_array.bats +++ /dev/null @@ -1,55 +0,0 @@ -# SPDX-License-Identifier: GPL-3.0-or-later - -setup_file () { - load 'test_helper/common-setup' - _common_setup -} - -setup () { - load 'test_helper/bats-assert/load' - load 'test_helper/bats-support/load' -} - -@test "clean_array() - arguments" { - source "${PROJECT_ROOT}/src/functions.sh" - - local cleaned_array=() - run clean_array - assert_failure 1 - - run clean_array "cleaned_array" - assert_failure 1 - - run clean_array "cleaned_array" "Something" - assert_success -} - -@test "clean_array()" { - source "${PROJECT_ROOT}/src/functions.sh" - - local cleaned_array=() - local el1=$(echo -e "Something1 \n") - local el2=$(echo -e "Something2\r ") - local el3=$(echo -e "\n Something3 \n") - - clean_array "cleaned_array" "$el1" "$el2" "$el3" - assert_equal "\"${cleaned_array[0]}\"" "\"Something1\"" - assert_equal "\"${cleaned_array[1]}\"" "\"Something2\"" - assert_equal "\"${cleaned_array[2]}\"" "\"Something3\"" -} - -@test "clean_array() - special characters" { - source "${PROJECT_ROOT}/src/functions.sh" - - local cleaned_array=() - local el1=$(echo -e "Something1 conf\n") - local el2=$(echo -e "Something2 conf\r ") - local el3=$(echo -e "\n Something3 conf\n") - local el4=$(echo -e "\n Something4\&Something4 conf \n") - - clean_array "cleaned_array" "$el1" "$el2" "$el3" "$el4" - assert_equal "\"${cleaned_array[0]}\"" "\"Something1 conf\"" - assert_equal "\"${cleaned_array[1]}\"" "\"Something2 conf\"" - assert_equal "\"${cleaned_array[2]}\"" "\"Something3 conf\"" - assert_equal "\"${cleaned_array[3]}\"" "\"Something4\&Something4 conf\"" -}