Call _zk_edit from _zk_new

This commit is contained in:
The Magician 2023-12-08 18:55:08 +00:00
parent 416d837307
commit 0a32609d2d
3 changed files with 43 additions and 35 deletions

View File

@ -30,3 +30,32 @@ test__zk_edit_uses__zk_search_when_filename_not_passed_as_parameter() {
assert_equals "$expected" "$result" 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"
}

View File

@ -12,45 +12,25 @@ teardown() {
} }
test__zk_new_creates_file_in_correct_directory() { test__zk_new_creates_file_in_correct_directory() {
fake "$EDITOR" echo ""
fake vi echo ""
export ZKDIR="$TMP_ZETTELKASTEN" export ZKDIR="$TMP_ZETTELKASTEN"
filename="aaaaaaaaaaaaaa" filename="11111111111111"
fake date "echo $filename" fake date "echo $filename"
fake _zk_edit echo ""
_zk_new _zk_new
assert "test -e $TMP_ZETTELKASTEN/$filename" assert "test -e $TMP_ZETTELKASTEN/$filename"
} }
test__zk_new_runs_EDITOR_if_EDITOR_is_set() { test__zk_new_runs_zk_edit_on_new_file() {
export EDITOR="nano"
# shellcheck disable=SC2016
fake "$EDITOR" echo '$EDITOR ${FAKE_PARAMS[@]}'
export ZKDIR="$TMP_ZETTELKASTEN" export ZKDIR="$TMP_ZETTELKASTEN"
filename="aaaaaaaaaaaaaa" filename="11111111111111"
fake date "echo $filename" # shellcheck disable=SC2016
expected="$EDITOR $TMP_ZETTELKASTEN/$filename" fake _zk_edit echo '_zk_edit ${FAKE_PARAMS[@]}'
fake _zk_isosec echo "$filename"
expected="_zk_edit $filename"
result="$(_zk_new)" result="$(_zk_new)"
assert_equals "$expected" "$result" 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="aaaaaaaaaaaaaa"
fake date "echo $filename"
expected="vi $TMP_ZETTELKASTEN/$filename"
result="$(_zk_new)"
assert_equals "$expected" "$result"
}

11
zk
View File

@ -31,12 +31,7 @@ _zk_search() {
_zk_new() { _zk_new() {
filename="$(_zk_isosec)" filename="$(_zk_isosec)"
touch "$ZKDIR/$filename" touch "$ZKDIR/$filename"
_zk_edit "$filename"
if test -n "$EDITOR"; then
$EDITOR "$ZKDIR/$filename"
else
vi "$ZKDIR/$filename"
fi
} }
_zk_edit() { _zk_edit() {
@ -46,7 +41,11 @@ _zk_edit() {
filename="$(_zk_search)" filename="$(_zk_search)"
fi fi
if test -n "$EDITOR"; then
$EDITOR "$ZKDIR/$filename"
else
vi "$ZKDIR/$filename" vi "$ZKDIR/$filename"
fi
} }
main() { main() {