Add check to ensure directory exists
This commit is contained in:
parent
0a32609d2d
commit
adea4b4c97
|
@ -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
|
||||
}
|
||||
|
|
@ -4,6 +4,18 @@ setup() {
|
|||
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() {
|
||||
expected="list"
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ setup() {
|
|||
}
|
||||
|
||||
test_main_calls_list_when_list_command_passed() {
|
||||
fake check_zkdir test
|
||||
expected="_zk_list"
|
||||
fake get_command 'echo list'
|
||||
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() {
|
||||
fake check_zkdir test
|
||||
expected="_zk_search"
|
||||
fake get_command 'echo search'
|
||||
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() {
|
||||
fake check_zkdir test
|
||||
expected="_zk_new"
|
||||
fake get_command 'echo new'
|
||||
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() {
|
||||
fake check_zkdir test
|
||||
expected="_zk_edit"
|
||||
fake get_command 'echo edit'
|
||||
fake _zk_edit "echo $expected"
|
||||
|
@ -43,3 +47,10 @@ test_main_calls_edit_when_edit_command_passed() {
|
|||
|
||||
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
23
zk
|
@ -1,15 +1,30 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
|
||||
ZKDIR="$HOME/.local/share/zk/"
|
||||
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() {
|
||||
commands="$1"
|
||||
input="$2"
|
||||
|
||||
if test -z "$input"; then
|
||||
echo ""
|
||||
return 0
|
||||
fi
|
||||
|
||||
for c in $commands; do
|
||||
if echo "$c" | grep "^$input" >/dev/null; then
|
||||
echo "$c"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
@ -49,7 +64,15 @@ _zk_edit() {
|
|||
}
|
||||
|
||||
main() {
|
||||
check_zkdir
|
||||
|
||||
subcommand="$(get_command "$ZKCOMMANDS" "$1")"
|
||||
|
||||
if test -z "$subcommand"; then
|
||||
echo "zk: $1 not recognized" 1>&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
_zk_"$subcommand"
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue