Compare commits

..

2 Commits

Author SHA1 Message Date
The Magician 61188b6453 Wire up "rm" subcommand 2023-12-19 12:14:34 +00:00
The Magician 9c7492bed5 Implement "rm" subcommand 2023-12-19 12:12:20 +00:00
5 changed files with 82 additions and 1 deletions

View File

@ -1,5 +1,7 @@
test:
bash_unit ./tests/test_*.sh
runtests:
ls -1 zk tests/* | entr -c bash_unit ./tests/test_*.sh
install:
test -d ~/.local/bin/ || mkdir -p ~/.local/bin/
cp zk ~/.local/bin/zk

44
tests/test__zk_rm.sh Normal file
View File

@ -0,0 +1,44 @@
#!/bin/bash
TMP_ZETTELKASTEN=""
setup() {
source ../zk
TMP_ZETTELKASTEN="$(mktemp -d)"
}
teardown() {
rm -rf "$TMP_ZETTELKASTEN"
}
test__zk_rm_gives_error_when_no_zettel_id_given() {
# shellcheck disable=SC2016
#fake rm 'echo'
assert_status_code 1 '_zk_rm'
}
test__zk_rm_gives_error_when_nonexistent_zettel_id_given() {
export ZKDIR="$TMP_ZETTELKASTEN"
zettel_id="11111111111111"
assert_status_code 2 '_zk_rm $zettel_id'
}
test__zk_rm_succeeds_when_existent_zettel_id_given() {
export ZKDIR="$TMP_ZETTELKASTEN"
zettel_id="11111111111111"
touch "$ZKDIR/$zettel_id"
assert_status_code 0 '_zk_rm 11111111111111'
}
test__zk_rm_removes_file_when_existent_zettel_id_given() {
export ZKDIR="$TMP_ZETTELKASTEN"
zettel_id="11111111111111"
echo testing >> "$ZKDIR/$zettel_id"
_zk_rm "$zettel_id"
assert_fails 'test -f "$ZKDIR/$zettel_id"'
}

View File

@ -52,6 +52,15 @@ test_get_command_returns_edit_for_substring() {
done
}
test_get_command_returns_edit_for_substring() {
expected="rm"
for arg in r rm; do
result="$(get_command "$ZKCOMMANDS" "$arg")"
assert_equals "$expected" "$result"
done
}
test_get_command_does_not_return_match_unless_at_start_of_command_string() {
commands="delete"
expected=""

View File

@ -48,6 +48,18 @@ test_main_calls_edit_when_edit_command_passed() {
assert_equals "$expected" "$result"
}
test_main_calls_rm_when_rm_command_passed() {
fake check_zkdir test
expected="_zk_rm"
fake get_command 'echo rm'
fake _zk_rm "echo $expected"
result="$(main)"
assert_equals "$expected" "$result"
}
test_main_errors_if_unrecognized_command_given() {
fake check_zkdir test
fake get_command echo ""

16
zk
View File

@ -3,7 +3,7 @@
set -e
ZKDIR="$HOME/.local/share/zk/"
ZKCOMMANDS='list search new edit'
ZKCOMMANDS='list search new edit rm'
check_zkdir() {
if ! test -d "$ZKDIR"; then
@ -63,6 +63,20 @@ _zk_edit() {
fi
}
_zk_rm() {
if test -z "$1"; then
echo "zk rm: no zettel id given" 1>&2
exit 1
fi
if ! find "$ZKDIR" -name "$1" | grep .; then
echo "zk rm: zettel "$1" not found" 1>&2
exit 2
fi
rm "$ZKDIR/$1"
}
main() {
check_zkdir