Wire up "rm" subcommand

This commit is contained in:
The Magician 2023-12-19 12:14:34 +00:00
parent 9c7492bed5
commit 61188b6453
4 changed files with 66 additions and 1 deletions

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 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() { test_get_command_does_not_return_match_unless_at_start_of_command_string() {
commands="delete" commands="delete"
expected="" expected=""

View File

@ -48,6 +48,18 @@ test_main_calls_edit_when_edit_command_passed() {
assert_equals "$expected" "$result" 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() { test_main_errors_if_unrecognized_command_given() {
fake check_zkdir test fake check_zkdir test
fake get_command echo "" fake get_command echo ""

2
zk
View File

@ -3,7 +3,7 @@
set -e set -e
ZKDIR="$HOME/.local/share/zk/" ZKDIR="$HOME/.local/share/zk/"
ZKCOMMANDS='list search new edit' ZKCOMMANDS='list search new edit rm'
check_zkdir() { check_zkdir() {
if ! test -d "$ZKDIR"; then if ! test -d "$ZKDIR"; then