Add check to ensure directory exists

This commit is contained in:
The Magician 2023-12-08 19:37:42 +00:00
parent 0a32609d2d
commit adea4b4c97
4 changed files with 65 additions and 0 deletions

19
tests/test_check_zkdir.sh Normal file
View File

@ -0,0 +1,19 @@
#!/bin/bash
setup() {
source ../zk
}
test_check_zkdir_does_not_error_if_ZKDIR_exists() {
export ZKDIR="/"
assert_status_code 0 check_zkdir
}
test_check_zkdir_errors_if_ZKDIR_does_not_exist() {
export ZKDIR="/this/is/a/fake/directory/"
assert_status_code 1 check_zkdir
}

View File

@ -4,6 +4,18 @@ setup() {
source ../zk source ../zk
} }
test_get_command_returns_empty_if_no_input_given() {
result="$(get_command "$ZKCOMMANDS" "")"
assert_equals "" "$result"
}
test_get_command_returns_empty_if_unrecognized_input_given() {
result="$(get_command "$ZKCOMMANDS" "lkjdsaas")"
assert_equals "" "$result"
}
test_get_command_returns_list_for_substring() { test_get_command_returns_list_for_substring() {
expected="list" expected="list"

View File

@ -5,6 +5,7 @@ setup() {
} }
test_main_calls_list_when_list_command_passed() { test_main_calls_list_when_list_command_passed() {
fake check_zkdir test
expected="_zk_list" expected="_zk_list"
fake get_command 'echo list' fake get_command 'echo list'
fake _zk_list "echo $expected" fake _zk_list "echo $expected"
@ -15,6 +16,7 @@ test_main_calls_list_when_list_command_passed() {
} }
test_main_calls_search_when_search_command_passed() { test_main_calls_search_when_search_command_passed() {
fake check_zkdir test
expected="_zk_search" expected="_zk_search"
fake get_command 'echo search' fake get_command 'echo search'
fake _zk_search "echo $expected" fake _zk_search "echo $expected"
@ -25,6 +27,7 @@ test_main_calls_search_when_search_command_passed() {
} }
test_main_calls_new_when_new_command_passed() { test_main_calls_new_when_new_command_passed() {
fake check_zkdir test
expected="_zk_new" expected="_zk_new"
fake get_command 'echo new' fake get_command 'echo new'
fake _zk_new "echo $expected" fake _zk_new "echo $expected"
@ -35,6 +38,7 @@ test_main_calls_new_when_new_command_passed() {
} }
test_main_calls_edit_when_edit_command_passed() { test_main_calls_edit_when_edit_command_passed() {
fake check_zkdir test
expected="_zk_edit" expected="_zk_edit"
fake get_command 'echo edit' fake get_command 'echo edit'
fake _zk_edit "echo $expected" fake _zk_edit "echo $expected"
@ -43,3 +47,10 @@ test_main_calls_edit_when_edit_command_passed() {
assert_equals "$expected" "$result" assert_equals "$expected" "$result"
} }
test_main_errors_if_unrecognized_command_given() {
fake check_zkdir test
fake get_command echo ""
assert_status_code 2 main
}

23
zk
View File

@ -1,15 +1,30 @@
#!/bin/sh #!/bin/sh
set -e
ZKDIR="$HOME/.local/share/zk/" ZKDIR="$HOME/.local/share/zk/"
ZKCOMMANDS='list search new edit' ZKCOMMANDS='list search new edit'
check_zkdir() {
if ! test -d "$ZKDIR"; then
echo "zk: $ZKDIR directory does not exist" 1>&2
exit 1
fi
}
get_command() { get_command() {
commands="$1" commands="$1"
input="$2" input="$2"
if test -z "$input"; then
echo ""
return 0
fi
for c in $commands; do for c in $commands; do
if echo "$c" | grep "^$input" >/dev/null; then if echo "$c" | grep "^$input" >/dev/null; then
echo "$c" echo "$c"
return 0
fi fi
done done
} }
@ -49,7 +64,15 @@ _zk_edit() {
} }
main() { main() {
check_zkdir
subcommand="$(get_command "$ZKCOMMANDS" "$1")" subcommand="$(get_command "$ZKCOMMANDS" "$1")"
if test -z "$subcommand"; then
echo "zk: $1 not recognized" 1>&2
exit 2
fi
_zk_"$subcommand" _zk_"$subcommand"
} }