#!/bin/bash # Tests for list command this_dir="$(cd "$(dirname "$0")" && pwd)" source "${this_dir}/common.sh" test_list_shows_secrets() { echo "value1" | szecret put "secret-a" echo "value2" | szecret put "secret-b" echo "value3" | szecret put "secret-c" local list_output list_output="$(szecret list)" assert_contains "${list_output}" "secret-a" "list output" assert_contains "${list_output}" "secret-b" "list output" assert_contains "${list_output}" "secret-c" "list output" pass "list shows all secrets" } test_list_empty_store() { local list_output list_output="$(szecret list)" assert_equals "" "${list_output}" "empty list" pass "list handles empty store" } test_list_sorted() { echo "v" | szecret put "zebra" echo "v" | szecret put "apple" echo "v" | szecret put "mango" local list_output list_output="$(szecret list)" # Check that apple comes before mango comes before zebra local apple_pos mango_pos zebra_pos apple_pos=$(echo "${list_output}" | grep -n "apple" | cut -d: -f1) mango_pos=$(echo "${list_output}" | grep -n "mango" | cut -d: -f1) zebra_pos=$(echo "${list_output}" | grep -n "zebra" | cut -d: -f1) if [[ "${apple_pos}" -lt "${mango_pos}" && "${mango_pos}" -lt "${zebra_pos}" ]]; then pass "list is sorted" else fail "list is not sorted: apple=${apple_pos}, mango=${mango_pos}, zebra=${zebra_pos}" fi } run_tests() { test_list_empty_store test_list_shows_secrets test_list_sorted } [[ "${BASH_SOURCE[0]}" == "${0}" ]] && run_tests