From ea93b04ecf254b3a9dbaceb4ab9328b40568a276 Mon Sep 17 00:00:00 2001 From: Alexander Bayandin Date: Wed, 6 May 2020 23:03:54 +0100 Subject: [PATCH] Replace usages of associative arrays for bash 3.x in tests --- test/test_install_and_use.sh | 88 +++++++++++++++++++++--------------- test/test_uninstall.sh | 32 ++++++++----- 2 files changed, 71 insertions(+), 49 deletions(-) diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 65d608d..6eccf22 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -73,46 +73,56 @@ declare -a errors=(); log 'info' '### Test Suite: Install and Use' -declare -A string_tests=(); - -string_tests['latest version']="$(tfenv list-remote | grep -e "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | head -n 1),latest"; -string_tests['latest possibly-unstable version']="$(tfenv list-remote | head -n 1),latest:"; -string_tests['latest alpha']="$(tfenv list-remote | grep 'alpha' | head -n 1),latest:alpha"; -string_tests['latest beta']="$(tfenv list-remote | grep 'beta' | head -n 1),latest:beta"; -string_tests['latest rc']="$(tfenv list-remote | grep 'rc' | head -n 1),latest:rc"; -string_tests['latest possibly-unstable version from 0.11']="$(tfenv list-remote | grep '^0\.11\.' | head -n 1),latest:^0.11."; -string_tests['0.11.15-oci']='0.11.15-oci,0.11.15-oci'; -string_tests['latest version matching regex']='0.8.8,latest:^0.8'; -string_tests['specific version']="0.7.13,0.7.13"; - -declare kv k v; -declare -i test_num=1; - -for desc in "${!string_tests[@]}"; do +tests__desc=( + 'latest version' + 'latest possibly-unstable version' + 'latest alpha' + 'latest beta' + 'latest rc' + 'latest possibly-unstable version from 0.11' + '0.11.15-oci' + 'latest version matching regex' + 'specific version' +); +tests__kv=( + "$(tfenv list-remote | grep -e "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | head -n 1),latest" + "$(tfenv list-remote | head -n 1),latest:" + "$(tfenv list-remote | grep 'alpha' | head -n 1),latest:alpha" + "$(tfenv list-remote | grep 'beta' | head -n 1),latest:beta" + "$(tfenv list-remote | grep 'rc' | head -n 1),latest:rc" + "$(tfenv list-remote | grep '^0\.11\.' | head -n 1),latest:^0.11." + '0.11.15-oci,0.11.15-oci' + '0.8.8,latest:^0.8' + "0.7.13,0.7.13" +); +tests_count=${#tests__desc[@]} + +declare desc kv k v; + +for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do cleanup || log 'error' 'Cleanup failed?!'; - kv="${string_tests[${desc}]}"; + desc=${tests__desc[${test_num}]} + kv="${tests__kv[${test_num}]}"; v="${kv%,*}"; k="${kv##*,}"; - log 'info' "## Param Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} )"; + log 'info' "## Param Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} )"; test_install_and_use "${v}" "${k}" \ - && log info "## Param Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} ) succeeded" \ - || error_and_proceed "## Param Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} ) failed"; - test_num+=1; + && log info "## Param Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) succeeded" \ + || error_and_proceed "## Param Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) failed"; done; -test_num=1; -for desc in "${!string_tests[@]}"; do +for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do cleanup || log 'error' 'Cleanup failed?!'; - kv="${string_tests[${desc}]}"; + desc=${tests__desc[${test_num}]} + kv="${tests__kv[${test_num}]}"; v="${kv%,*}"; k="${kv##*,}"; - log 'info' "## ./.terraform-version Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} )"; + log 'info' "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} )"; log 'info' "Writing ${k} to ./.terraform-version"; echo "${k}" > ./.terraform-version; test_install_and_use "${v}" \ - && log info "## ./.terraform-version Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} ) succeeded" \ - || error_and_proceed "## ./.terraform-version Test ${test_num}/${#string_tests[*]}: ${desc} ( ${k} / ${v} ) failed"; - test_num+=1; + && log info "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) succeeded" \ + || error_and_proceed "## ./.terraform-version Test ${test_num}/${tests_count}: ${desc} ( ${k} / ${v} ) failed"; done; cleanup || log 'error' 'Cleanup failed?!'; @@ -158,20 +168,24 @@ log 'info' 'Install invalid specific version'; cleanup || log 'error' 'Cleanup failed?!'; -declare -A neg_tests=(); -neg_tests['specific version']="9.9.9"; -neg_tests['latest:word']="latest:word"; +neg_tests__desc=( + 'specific version' + 'latest:word' +); +neg_tests__kv=( + '9.9.9' + "latest:word" +); +neg_tests_count=${#neg_tests__desc[@]} -test_num=1; - -for desc in "${!neg_tests[@]}"; do +for ((test_num=0; test_num<${neg_tests_count}; ++test_num )) ; do cleanup || log 'error' 'Cleanup failed?!'; - k="${neg_tests[${desc}]}"; + desc=${neg_tests__desc[${test_num}]} + k="${neg_tests__kv[${test_num}]}"; expected_error_message="No versions matching '${k}' found in remote"; - log 'info' "## Invalid Version Test ${test_num}/${#neg_tests[*]}: ${desc} ( ${k} )"; + log 'info' "## Invalid Version Test ${test_num}/${neg_tests_count}: ${desc} ( ${k} )"; [ -z "$(tfenv install "${k}" 2>&1 | grep "${expected_error_message}")" ] \ && error_and_proceed "Installing invalid version ${k}"; - test_num+=1; done; if [ "${#errors[@]}" -gt 0 ]; then diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index ccaad83..0de2b4e 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -62,19 +62,27 @@ function test_uninstall() { log 'info' '### Test Suite: Uninstall Local Versions' cleanup || log 'error' 'Cleanup failed?!'; -declare -A tests; -tests['0.9.1']='0.9.1'; -tests['0.11.15-oci']='0.11.15-oci'; -tests['latest']="$(tfenv list-remote | head -n1)"; -tests['latest:^0.8']="$(tfenv list-remote | grep -e "^0.8" | head -n1)"; +tests__keywords=( + '0.9.1' + '0.11.15-oci' + 'latest' + 'latest:^0.8' +) +tests__versions=( + '0.9.1' + '0.11.15-oci' + "$(tfenv list-remote | head -n1)" + "$(tfenv list-remote | grep -e "^0.8" | head -n1)" +) +tests_count=${#tests__keywords[@]} -declare -i test_num=1; -for k in "${!tests[@]}"; do - log 'info' "Test ${test_num}/${#tests[@]}: Testing uninstall of version ${tests[${k}]} via keyword ${k}"; - test_uninstall "${k}" "${tests[${k}]}" \ - && log info "Test uninstall of version ${tests[${k}]} succeeded" \ - || error_and_proceed "Test uninstall of version ${tests[${k}]} failed"; - test_num+=1; +for ((test_num=0; test_num<${tests_count}; ++test_num )) ; do + keyword=${tests__keywords[${test_num}]} + version=${tests__versions[${test_num}]} + log 'info' "Test $(( ${test_num} + 1 ))/${tests_count}: Testing uninstall of version ${version} via keyword ${keyword}"; + test_uninstall "${keyword}" "${version}" \ + && log info "Test uninstall of version ${version} (via ${keyword}) succeeded" \ + || error_and_proceed "Test uninstall of version ${version} (via ${keyword}) failed"; done; if [ "${#errors[@]}" -gt 0 ]; then