Compare commits

...

9 Commits

9 changed files with 351 additions and 5 deletions

61
tests/test__zk_edit.sh Normal file
View File

@ -0,0 +1,61 @@
#!/bin/bash
setup() {
source ../zk
}
test__zk_edit_uses_filename_when_passed_as_parameter() {
# shellcheck disable=SC2016
fake vi 'echo vi "${FAKE_PARAMS[@]}"'
export EDITOR=""
zettel_name="11111111111111"
expected="vi $ZKDIR/$zettel_name"
result="$(_zk_edit $zettel_name)"
assert_equals "$expected" "$result"
}
test__zk_edit_uses__zk_search_when_filename_not_passed_as_parameter() {
export EDITOR=""
zettel_name="11111111111111"
expected="vi $ZKDIR/$zettel_name"
# shellcheck disable=SC2016
fake vi 'echo vi "${FAKE_PARAMS[@]}"'
fake _zk_search echo "$zettel_name"
result="$(_zk_edit)"
assert_equals "$expected" "$result"
}
test__zk_edit_runs_EDITOR_if_EDITOR_is_set() {
export EDITOR="nano"
# shellcheck disable=SC2016
fake "$EDITOR" echo '$EDITOR ${FAKE_PARAMS[@]}'
fake vi cat
filename="11111111111111"
expected="$EDITOR $ZKDIR/$filename"
result="$(_zk_edit $filename)"
assert_equals "$expected" "$result"
}
test__zk_new_runs_vi_if_EDITOR_is_not_set() {
export EDITOR=""
# shellcheck disable=SC2016
fake vi echo 'vi ${FAKE_PARAMS[@]}'
export ZKDIR="$TMP_ZETTELKASTEN"
filename="11111111111111"
fake date "echo $filename"
expected="vi $TMP_ZETTELKASTEN/$filename"
result="$(_zk_edit $filename)"
assert_equals "$expected" "$result"
}

37
tests/test__zk_list.sh Normal file
View File

@ -0,0 +1,37 @@
#!/bin/bash
TMP_ZETTELKASTEN=""
setup() {
source ../zk
TMP_ZETTELKASTEN="$(mktemp -d)"
}
teardown() {
rm -rf "$TMP_ZETTELKASTEN"
}
test__zk_list_outputs_correct_format_with_one_zettel() {
export ZKDIR="$TMP_ZETTELKASTEN"
test_zettel="aaaaaaaaaaaaaa"
echo "# Test Zettel" > "$TMP_ZETTELKASTEN/$test_zettel"
result="$(_zk_list)"
expected="$test_zettel | # Test Zettel"
assert_equals "$expected" "$result"
}
test__zk_list_outputs_correct_format_with_multiple_zettels() {
export ZKDIR="$TMP_ZETTELKASTEN"
test_zettel1="aaaaaaaaaaaaaa"
test_zettel2="bbbbbbbbbbbbbb"
echo "# Test Zettel 1" > "$TMP_ZETTELKASTEN/$test_zettel1"
echo "# Test Zettel 2" > "$TMP_ZETTELKASTEN/$test_zettel2"
result="$(_zk_list)"
expected="$test_zettel1 | # Test Zettel 1
$test_zettel2 | # Test Zettel 2"
assert_equals "$expected" "$result"
}

36
tests/test__zk_new.sh Normal file
View File

@ -0,0 +1,36 @@
#!/bin/bash
TMP_ZETTELKASTEN=""
setup() {
source ../zk
TMP_ZETTELKASTEN="$(mktemp -d)"
}
teardown() {
rm -rf "$TMP_ZETTELKASTEN"
}
test__zk_new_creates_file_in_correct_directory() {
export ZKDIR="$TMP_ZETTELKASTEN"
filename="11111111111111"
fake date "echo $filename"
fake _zk_edit echo ""
_zk_new
assert "test -e $TMP_ZETTELKASTEN/$filename"
}
test__zk_new_runs_zk_edit_on_new_file() {
export ZKDIR="$TMP_ZETTELKASTEN"
filename="11111111111111"
# shellcheck disable=SC2016
fake _zk_edit echo '_zk_edit ${FAKE_PARAMS[@]}'
fake _zk_isosec echo "$filename"
expected="_zk_edit $filename"
result="$(_zk_new)"
assert_equals "$expected" "$result"
}

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,12 +4,50 @@ 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() {
commands="list new"
expected="list" expected="list"
for arg in l li lis list; do for arg in l li lis list; do
result="$(get_command "$commands" "$arg")" result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_returns_search_for_substring() {
expected="search"
for arg in s se sea sear searc search; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_returns_new_for_substring() {
expected="new"
for arg in n ne new; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_returns_edit_for_substring() {
expected="edit"
for arg in e ed edi edit; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result" assert_equals "$expected" "$result"
done done
} }

15
tests/test_isosec.sh Normal file
View File

@ -0,0 +1,15 @@
#!/bin/bash
setup() {
source ../zk
}
test_isosec_calls_date_with_correct_parameters() {
expected="date -u +%Y%m%d%H%M%S"
# shellcheck disable=SC2016
fake date echo 'date ${FAKE_PARAMS[@]}'
result="$(_zk_isosec)"
assert_equals "$expected" "$result"
}

56
tests/test_main.sh Normal file
View File

@ -0,0 +1,56 @@
#!/bin/bash
setup() {
source ../zk
}
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"
result="$(main)"
assert_equals "$expected" "$result"
}
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"
result="$(main)"
assert_equals "$expected" "$result"
}
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"
result="$(main)"
assert_equals "$expected" "$result"
}
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"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_errors_if_unrecognized_command_given() {
fake check_zkdir test
fake get_command echo ""
assert_status_code 2 main
}

View File

@ -0,0 +1,26 @@
#!/bin/bash
setup() {
source ../zk
}
test__zk_search_calls__zk_list() {
expected="_zk_list"
fake _zk_list echo "$expected"
fake fzf cat
result="$(_zk_search)"
assert_equals "$expected" "$result"
}
test__zk_search_extracts_id_from_chosen_zettel() {
expected="1111111111111"
# shellcheck disable=SC2016
fake _zk_list 'echo "$expected | Zettel Title"'
fake fzf cat
result="$(_zk_search)"
assert_equals "$expected" "$result"
}

64
zk
View File

@ -1,21 +1,79 @@
#!/bin/sh #!/bin/sh
COMMANDS='list new delete' 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() { 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
} }
_zk_isosec() {
date -u +%Y%m%d%H%M%S
}
_zk_list() {
for zettel in "$ZKDIR"/*; do
echo "$(basename "$zettel") | $(head -1 "$zettel")"
done
}
_zk_search() {
_zk_list | fzf --prompt "ZK: " | cut -d ' ' -f 1
}
_zk_new() {
filename="$(_zk_isosec)"
touch "$ZKDIR/$filename"
_zk_edit "$filename"
}
_zk_edit() {
filename="$1"
if test -z "$filename"; then
filename="$(_zk_search)"
fi
if test -n "$EDITOR"; then
$EDITOR "$ZKDIR/$filename"
else
vi "$ZKDIR/$filename"
fi
}
main() { main() {
subcommand="$(get_commands "$COMMANDS" "$1")" check_zkdir
echo "$subcommand"
subcommand="$(get_command "$ZKCOMMANDS" "$1")"
if test -z "$subcommand"; then
echo "zk: $1 not recognized" 1>&2
exit 2
fi
_zk_"$subcommand"
} }
name="$(basename "$0")" name="$(basename "$0")"