This is an automated email from the ASF dual-hosted git repository. aw pushed a commit to branch YETUS-1034-release in repository https://gitbox.apache.org/repos/asf/yetus.git
commit 89dede67c4e9b2a793117ff89eed702b61ec92fe Author: Allen Wittenauer <[email protected]> AuthorDate: Thu Oct 29 22:34:08 2020 -0700 YETUS-1055. Add test code for yetuslib (#180) Additionally, this change: * fixes some bugs * adds shellcheck support for bats files --- precommit/src/main/shell/core.d/00-yetuslib.sh | 12 ++-- .../src/main/shell/test-patch.d/shellcheck.sh | 4 ++ .../arrayfiletest.txt} | 26 ++------ .../src/test/shell/functions_test_helper.bash | 5 +- precommit/src/test/shell/run-bats.sh | 2 +- precommit/src/test/shell/yetus_abs.bats | 66 +++++++++++++++++++ .../src/test/shell/yetus_add_array_element.bats | 1 + precommit/src/test/shell/yetus_array_contains.bats | 1 + ...rray_element.bats => yetus_array_to_comma.bats} | 37 +++++++---- .../src/test/shell/yetus_del_array_element.bats | 5 +- ...array_element.bats => yetus_file_to_array.bats} | 29 +++++---- .../test/shell/yetus_find_deepest_directory.bats | 74 ++++++++++++++++++++++ precommit/src/test/shell/yetus_relative_dir.bats | 69 ++++++++++++++++++++ ...tains.bats => yetus_sort_and_unique_array.bats} | 47 +++++++------- ...s_array_contains.bats => yetus_sort_array.bats} | 51 ++++++++------- 15 files changed, 327 insertions(+), 102 deletions(-) diff --git a/precommit/src/main/shell/core.d/00-yetuslib.sh b/precommit/src/main/shell/core.d/00-yetuslib.sh index cfea284..13754f8 100755 --- a/precommit/src/main/shell/core.d/00-yetuslib.sh +++ b/precommit/src/main/shell/core.d/00-yetuslib.sh @@ -255,7 +255,7 @@ function yetus_array_to_comma } ## @description Convert a file to an array. -## @description Comments on the beginning of the line are stripped. +## @description Comments on the beginning of the line and emtpy lines are stripped. ## @audience public ## @stability evolving ## @replaceable no @@ -280,11 +280,11 @@ function yetus_file_to_array # work due to the variable only being present in # the subshell. So MUST force the grep into the # subshell... - mapfile -t a < <("${GREP:-grep}" -v -e '^#' "${filename}" ) + mapfile -t a < <("${GREP:-grep}" -v -e '^#' -e '^$' "${filename}" ) else while read -r line; do a+=("${line}") - done < <("${GREP:-grep}" -v -e '^#' "${filename}") + done < <("${GREP:-grep}" -v -e '^#' -e '^$' "${filename}") fi eval "${var}=(\"\${a[@]}\")" return 0 @@ -464,7 +464,11 @@ function yetus_sort_and_unique_array fi } -## @description find the deepest entry of a directory array +## @description find the deepest entry of a directory array. +## @description this is different than yetus_relative_dir because +## @description the provided directory array provides where the +## @description directory breaks may occur rather than every +## @description directory being a potential cut off point. ## @description NOTE: array and filename MUST be absolute paths ## @audience public ## @stability stable diff --git a/precommit/src/main/shell/test-patch.d/shellcheck.sh b/precommit/src/main/shell/test-patch.d/shellcheck.sh index e1b683a..ae66e70 100755 --- a/precommit/src/main/shell/test-patch.d/shellcheck.sh +++ b/precommit/src/main/shell/test-patch.d/shellcheck.sh @@ -39,6 +39,10 @@ function shellcheck_filefilter # if it ends in an explicit .sh, then this is shell code. add_test shellcheck yetus_add_array_element SHELLCHECK_FILTERFILES "${filename}" + elif [[ ${filename} =~ \.bats$ ]]; then + # if it ends in an explicit .bats, then this is bats code + # which modern shellcheck can work with. + add_test shellcheck elif [[ ${BUILDTOOL} = maven && ${filename} =~ src/main/shell ]]; then # if it is maven and in src/main/shell, assume it's shell code add_test shellcheck diff --git a/precommit/src/test/shell/yetus_add_array_element.bats b/precommit/src/test/resources/arrayfiletest.txt old mode 100755 new mode 100644 similarity index 61% copy from precommit/src/test/shell/yetus_add_array_element.bats copy to precommit/src/test/resources/arrayfiletest.txt index 462f34c..3ffd070 --- a/precommit/src/test/shell/yetus_add_array_element.bats +++ b/precommit/src/test/resources/arrayfiletest.txt @@ -1,3 +1,7 @@ +1 +2 +3 + # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. @@ -12,25 +16,3 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - -load functions_test_helper - -@test "yetus_add_array_element (empty)" { - yetus_add_array_element ARRAY value - [ "${ARRAY[0]}" = value ] -} - -@test "yetus_add_array_element (exist)" { - ARRAY=("val2") - yetus_add_array_element ARRAY val1 - [ "${ARRAY[0]}" = val2 ] - [ "${ARRAY[1]}" = val1 ] -} - -@test "yetus_add_array_element (double exist)" { - ARRAY=("val2" "val1") - yetus_add_array_element ARRAY val3 - [ "${ARRAY[0]}" = val2 ] - [ "${ARRAY[1]}" = val1 ] - [ "${ARRAY[2]}" = val3 ] -} diff --git a/precommit/src/test/shell/functions_test_helper.bash b/precommit/src/test/shell/functions_test_helper.bash index 2d92edf..4ff7961 100755 --- a/precommit/src/test/shell/functions_test_helper.bash +++ b/precommit/src/test/shell/functions_test_helper.bash @@ -23,6 +23,10 @@ setup() { echo "bindir: ${TESTBINDIR}" 2>&1 + tmpres="${BATS_TEST_DIRNAME}/../resources" + # shellcheck disable=SC2034 + TESTRESOURCES=$(cd -P -- "${tmpres}" >/dev/null && pwd -P) + mkdir -p "${TMP}" # shellcheck disable=SC2034 @@ -40,7 +44,6 @@ teardown() { rm -rf "${TMP}" } - strstr() { if [ "${1#*$2}" != "${1}" ]; then echo true diff --git a/precommit/src/test/shell/run-bats.sh b/precommit/src/test/shell/run-bats.sh index 24ea79e..adc7224 100755 --- a/precommit/src/test/shell/run-bats.sh +++ b/precommit/src/test/shell/run-bats.sh @@ -15,7 +15,7 @@ # limitations under the License. targetdir=../../../target -mkdir -p ${targetdir}/surefire-reports ${targetdir}/tap +mkdir -p "${targetdir}/surefire-reports" "${targetdir}/tap" batsexe=$(command -v bats) 2>/dev/null diff --git a/precommit/src/test/shell/yetus_abs.bats b/precommit/src/test/shell/yetus_abs.bats new file mode 100755 index 0000000..9dc03db --- /dev/null +++ b/precommit/src/test/shell/yetus_abs.bats @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load functions_test_helper + +create_fake () { + mkdir "${TMP}/j" + touch "${TMP}/j/k" + ln -s j "${TMP}/l" +} + + +@test "yetus_abs (simple not exist)" { + run yetus_abs fake + [ "${status}" -eq 1 ] +} + +@test "yetus_abs (simple dir)" { + create_fake + run yetus_abs "${TMP}/j" + [ "${output}" = "${TMP}/j" ] +} + +@test "yetus_abs (simple file)" { + create_fake + run yetus_abs "${TMP}/j/k" + [ "${output}" = "${TMP}/j/k" ] +} + +@test "yetus_abs (relative file1)" { + create_fake + run yetus_abs "${TMP}/j/../j/k" + [ "${output}" = "${TMP}/j/k" ] +} + +@test "yetus_abs (relative file2)" { + create_fake + run yetus_abs "${RELTMP}/j/../j/k" + [ "${output}" = "${TMP}/j/k" ] +} + +@test "yetus_abs (relative dir)" { + create_fake + fred=$(cd -P -- ".." >/dev/null && pwd -P) + run yetus_abs ".." + [ "${output}" = "${fred}" ] +} + +@test "yetus_abs (symlink)" { + create_fake + run yetus_abs "${TMP}/l" + [ "${output}" = "${TMP}/j" ] +} diff --git a/precommit/src/test/shell/yetus_add_array_element.bats b/precommit/src/test/shell/yetus_add_array_element.bats index 462f34c..6ba687a 100755 --- a/precommit/src/test/shell/yetus_add_array_element.bats +++ b/precommit/src/test/shell/yetus_add_array_element.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. diff --git a/precommit/src/test/shell/yetus_array_contains.bats b/precommit/src/test/shell/yetus_array_contains.bats index 4151ffe..8fb5d2b 100755 --- a/precommit/src/test/shell/yetus_array_contains.bats +++ b/precommit/src/test/shell/yetus_array_contains.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. diff --git a/precommit/src/test/shell/yetus_add_array_element.bats b/precommit/src/test/shell/yetus_array_to_comma.bats similarity index 57% copy from precommit/src/test/shell/yetus_add_array_element.bats copy to precommit/src/test/shell/yetus_array_to_comma.bats index 462f34c..ef14cea 100755 --- a/precommit/src/test/shell/yetus_add_array_element.bats +++ b/precommit/src/test/shell/yetus_array_to_comma.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. @@ -15,22 +16,30 @@ load functions_test_helper -@test "yetus_add_array_element (empty)" { - yetus_add_array_element ARRAY value - [ "${ARRAY[0]}" = value ] +@test "yetus_array_to_comma (empty)" { + # shellcheck disable=SC2034 + ARRAY=() + run yetus_array_to_comma ARRAY + [ "${output}" = "" ] } -@test "yetus_add_array_element (exist)" { - ARRAY=("val2") - yetus_add_array_element ARRAY val1 - [ "${ARRAY[0]}" = val2 ] - [ "${ARRAY[1]}" = val1 ] +@test "yetus_array_to_comma (one)" { + # shellcheck disable=SC2034 + ARRAY=(one) + run yetus_array_to_comma ARRAY + [ "${output}" = "one" ] } -@test "yetus_add_array_element (double exist)" { - ARRAY=("val2" "val1") - yetus_add_array_element ARRAY val3 - [ "${ARRAY[0]}" = val2 ] - [ "${ARRAY[1]}" = val1 ] - [ "${ARRAY[2]}" = val3 ] +@test "yetus_array_to_comma (two)" { + # shellcheck disable=SC2034 + ARRAY=(one two) + run yetus_array_to_comma ARRAY + [ "${output}" = "one,two" ] } + +@test "yetus_array_to_comma (three)" { + # shellcheck disable=SC2034 + ARRAY=(one two three) + run yetus_array_to_comma ARRAY + [ "${output}" = "one,two,three" ] +} \ No newline at end of file diff --git a/precommit/src/test/shell/yetus_del_array_element.bats b/precommit/src/test/shell/yetus_del_array_element.bats index a79b584..86499ff 100755 --- a/precommit/src/test/shell/yetus_del_array_element.bats +++ b/precommit/src/test/shell/yetus_del_array_element.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. @@ -17,7 +18,7 @@ load functions_test_helper @test "yetus_del_array_element (empty array)" { yetus_del_array_element ARRAY value - [ "${#ARRAY[@]}" = 0 ] + [ "${#ARRAY[@]}" -eq 0 ] } @test "yetus_del_array_element (not exist)" { @@ -30,7 +31,7 @@ load functions_test_helper @test "yetus_add_array_element (single exist)" { ARRAY=("val1") yetus_del_array_element ARRAY val1 - echo ">${ARRAY[@]}<" + echo ">${ARRAY[*]}<" [ "${#ARRAY[@]}" -eq 0 ] } diff --git a/precommit/src/test/shell/yetus_add_array_element.bats b/precommit/src/test/shell/yetus_file_to_array.bats similarity index 61% copy from precommit/src/test/shell/yetus_add_array_element.bats copy to precommit/src/test/shell/yetus_file_to_array.bats index 462f34c..9e398fd 100755 --- a/precommit/src/test/shell/yetus_add_array_element.bats +++ b/precommit/src/test/shell/yetus_file_to_array.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. @@ -15,22 +16,22 @@ load functions_test_helper -@test "yetus_add_array_element (empty)" { - yetus_add_array_element ARRAY value - [ "${ARRAY[0]}" = value ] +@test "yetus_file_to_array (empty)" { + run yetus_file_to_array ARRAY "nonexistent" + [ "${status}" = 1 ] } -@test "yetus_add_array_element (exist)" { - ARRAY=("val2") - yetus_add_array_element ARRAY val1 - [ "${ARRAY[0]}" = val2 ] - [ "${ARRAY[1]}" = val1 ] +@test "yetus_file_to_array (basic read)" { + run yetus_file_to_array ARRAY "${TESTRESOURCES}/arrayfiletest.txt" + [ "${status}" = 0 ] } -@test "yetus_add_array_element (double exist)" { - ARRAY=("val2" "val1") - yetus_add_array_element ARRAY val3 - [ "${ARRAY[0]}" = val2 ] - [ "${ARRAY[1]}" = val1 ] - [ "${ARRAY[2]}" = val3 ] +@test "yetus_file_to_array (contents)" { + yetus_file_to_array ARRAY "${TESTRESOURCES}/arrayfiletest.txt" + [ "${ARRAY[0]}" = 1 ] } + +@test "yetus_file_to_array (count)" { + yetus_file_to_array ARRAY "${TESTRESOURCES}/arrayfiletest.txt" + [ "${#ARRAY[@]}" -eq 3 ] +} \ No newline at end of file diff --git a/precommit/src/test/shell/yetus_find_deepest_directory.bats b/precommit/src/test/shell/yetus_find_deepest_directory.bats new file mode 100755 index 0000000..0a2e3e4 --- /dev/null +++ b/precommit/src/test/shell/yetus_find_deepest_directory.bats @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load functions_test_helper + +@test "yetus_find_deepest_directory (simple find1)" { + # shellcheck disable=SC2034 + ARRAY=("/a") + run yetus_find_deepest_directory ARRAY "/a" + [ "${output}" = "/a" ] +} + +@test "yetus_find_deepest_directory (simple find2)" { + # shellcheck disable=SC2034 + ARRAY=("/a") + run yetus_find_deepest_directory ARRAY "/a/1" + [ "${output}" = "/a" ] +} + +@test "yetus_find_deepest_directory (simple fail)" { + # shellcheck disable=SC2034 + ARRAY=("/a") + run yetus_find_deepest_directory ARRAY "/b/1" + [ "${output}" = "" ] +} + +@test "yetus_find_deepest_directory (two path find)" { + # shellcheck disable=SC2034 + ARRAY=("/a/b" "/a/c") + run yetus_find_deepest_directory ARRAY "/a/b/k" + [ "${output}" = "/a/b" ] +} + +@test "yetus_find_deepest_directory (two+two path find)" { + # shellcheck disable=SC2034 + ARRAY=("/a/b/c" "/a/b/d") + run yetus_find_deepest_directory ARRAY "/a/b/d/j" + [ "${output}" = "/a/b/d" ] +} + + +@test "yetus_find_deepest_directory (complex path find1)" { + # shellcheck disable=SC2034 + ARRAY=("/a/b/c" "/a/b/d" "/a/b/e" "/a/b/d/f" "/a/b/d/g") + run yetus_find_deepest_directory ARRAY "/a/b/d" + [ "${output}" = "/a/b/d" ] +} + +@test "yetus_find_deepest_directory (complex path find2)" { + # shellcheck disable=SC2034 + ARRAY=("/a/b" "/a/b/c" "/a/b/d/f/k/j" "/a/b/d/g") + run yetus_find_deepest_directory ARRAY "/a/b/d/f/k/e" + [ "${output}" = "/a/b" ] +} + +@test "yetus_find_deepest_directory (complex path find3)" { + # shellcheck disable=SC2034 + ARRAY=("/a/b" "/a/b/c" "/a/b/d" "/a/b/d/f/k/j" "/a/b/d/g") + run yetus_find_deepest_directory ARRAY "/a/b/d/f/k/e" + [ "${output}" = "/a/b/d" ] +} \ No newline at end of file diff --git a/precommit/src/test/shell/yetus_relative_dir.bats b/precommit/src/test/shell/yetus_relative_dir.bats new file mode 100755 index 0000000..a031204 --- /dev/null +++ b/precommit/src/test/shell/yetus_relative_dir.bats @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load functions_test_helper + +create_fake () { + mkdir "${TMP}/j" + touch "${TMP}/j/k" + ln -s j "${TMP}/l" + mkdir "${TMP}/m" + touch "${TMP}/m/n" + ln -s m "${TMP}/o" +} + + +@test "yetus_relative_dir (simple dir)" { + create_fake + run yetus_relative_dir "${TMP}" "${TMP}/j" + [ "${status}" -eq 0 ] + [ "${output}" = "j" ] +} + +@test "yetus_relative_dir (simple file)" { + create_fake + run yetus_relative_dir "${TMP}/j" "${TMP}/j/k" + [ "${status}" -eq 0 ] + [ "${output}" = "k" ] +} + +@test "yetus_relative_dir (simple symlink)" { + create_fake + run yetus_relative_dir "${TMP}" "${TMP}/l" + [ "${status}" -eq 0 ] + [ "${output}" = "l" ] +} + +@test "yetus_relative_dir (fail simple dir)" { + create_fake + run yetus_relative_dir "${TMP}/j" "${TMP}/m" + [ "${status}" -eq 1 ] + [ "${output}" = "${TMP}/m" ] +} + +@test "yetus_relative_dir (fail simple file)" { + create_fake + run yetus_relative_dir "${TMP}/j" "${TMP}/m/n" + [ "${status}" -eq 1 ] + [ "${output}" = "${TMP}/m/n" ] +} + +@test "yetus_relative_dir (fail simple symlink)" { + create_fake + run yetus_relative_dir "${TMP}/j" "${TMP}/o" + [ "${status}" -eq 1 ] + [ "${output}" = "${TMP}/o" ] +} \ No newline at end of file diff --git a/precommit/src/test/shell/yetus_array_contains.bats b/precommit/src/test/shell/yetus_sort_and_unique_array.bats similarity index 50% copy from precommit/src/test/shell/yetus_array_contains.bats copy to precommit/src/test/shell/yetus_sort_and_unique_array.bats index 4151ffe..2ae8bc4 100755 --- a/precommit/src/test/shell/yetus_array_contains.bats +++ b/precommit/src/test/shell/yetus_sort_and_unique_array.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. @@ -15,33 +16,35 @@ load functions_test_helper -@test "yetus_array_contains (empty)" { - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 1 ] +@test "yetus_sort_and_unique_array (empty)" { + yetus_sort_and_unique_array ARRAY } -@test "yetus_array_contains (exist)" { +@test "yetus_sort_and_unique_array (single value)" { ARRAY=("value") - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 0 ] + yetus_sort_and_unique_array ARRAY } -@test "yetus_array_contains (notexist)" { - ARRAY=("different") - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 1 ] -} +@test "yetus_sort_and_unique_array (multiple value)" { + ARRAY=("b" "c" "a") + preifsod=$(echo "${IFS}" | od -c) + yetus_sort_and_unique_array ARRAY + postifsod=$(echo "${IFS}" | od -c) -@test "yetus_array_contains (exist, multi)" { - ARRAY=("val1" "val2" "val3") - for j in val1 val2 val3; do - run yetus_array_contains "${j}" "${ARRAY[@]}" - [ "${status}" = 0 ] - done + [ "${ARRAY[0]}" = "a" ] + [ "${ARRAY[1]}" = "b" ] + [ "${ARRAY[2]}" = "c" ] + [ "${preifsod}" = "${postifsod}" ] } -@test "yetus_array_contains (multi, not exist)" { - ARRAY=("val1" "val2" "val3") - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 1 ] -} +@test "yetus_sort_and_unique_array (multiple duplicate values)" { + ARRAY=("b" "c" "b" "a" "a" "c") + preifsod=$(echo "${IFS}" | od -c) + yetus_sort_and_unique_array ARRAY + postifsod=$(echo "${IFS}" | od -c) + + [ "${ARRAY[0]}" = "a" ] + [ "${ARRAY[1]}" = "b" ] + [ "${ARRAY[2]}" = "c" ] + [ "${preifsod}" = "${postifsod}" ] +} \ No newline at end of file diff --git a/precommit/src/test/shell/yetus_array_contains.bats b/precommit/src/test/shell/yetus_sort_array.bats similarity index 50% copy from precommit/src/test/shell/yetus_array_contains.bats copy to precommit/src/test/shell/yetus_sort_array.bats index 4151ffe..cb8cd59 100755 --- a/precommit/src/test/shell/yetus_array_contains.bats +++ b/precommit/src/test/shell/yetus_sort_array.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bash # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. @@ -15,33 +16,39 @@ load functions_test_helper -@test "yetus_array_contains (empty)" { - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 1 ] +@test "yetus_sort_array (empty)" { + yetus_sort_array ARRAY } -@test "yetus_array_contains (exist)" { +@test "yetus_sort_array (single value)" { ARRAY=("value") - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 0 ] + yetus_sort_array ARRAY } -@test "yetus_array_contains (notexist)" { - ARRAY=("different") - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 1 ] -} +@test "yetus_sort_array (multiple value)" { + ARRAY=("b" "c" "a") + preifsod=$(echo "${IFS}" | od -c) + yetus_sort_array ARRAY + postifsod=$(echo "${IFS}" | od -c) -@test "yetus_array_contains (exist, multi)" { - ARRAY=("val1" "val2" "val3") - for j in val1 val2 val3; do - run yetus_array_contains "${j}" "${ARRAY[@]}" - [ "${status}" = 0 ] - done + [ "${ARRAY[0]}" = "a" ] + [ "${ARRAY[1]}" = "b" ] + [ "${ARRAY[2]}" = "c" ] + [ "${preifsod}" = "${postifsod}" ] } -@test "yetus_array_contains (multi, not exist)" { - ARRAY=("val1" "val2" "val3") - run yetus_array_contains value "${ARRAY[@]}" - [ "${status}" = 1 ] -} +@test "yetus_sort_array (multiple duplicate values)" { + ARRAY=("b" "c" "b" "a" "a" "c") + preifsod=$(echo "${IFS}" | od -c) + yetus_sort_array ARRAY + postifsod=$(echo "${IFS}" | od -c) + + [ "${ARRAY[0]}" = "a" ] + [ "${ARRAY[1]}" = "a" ] + [ "${ARRAY[2]}" = "b" ] + [ "${ARRAY[3]}" = "b" ] + [ "${ARRAY[4]}" = "c" ] + [ "${ARRAY[5]}" = "c" ] + + [ "${preifsod}" = "${postifsod}" ] +} \ No newline at end of file
