diff --git a/nb b/nb index b25b867ee..9e57d2c48 100755 --- a/nb +++ b/nb @@ -4661,6 +4661,9 @@ _index() { _exit_1 _help index fi + _basename="${_basename//'['/\\[}" + _basename="${_basename//']'/\\]}" + if grep -q "^${_basename}$" "${_index_path}" then _sed_i -e "s/^${_basename}$//g" "${_index_path}" @@ -4944,6 +4947,9 @@ _index() { _exit_1 _help index fi + _old_basename="${_old_basename//'['/\\[}" + _old_basename="${_old_basename//']'/\\]}" + if grep -q "^${_old_basename}$" "${_index_path}" then _sed_i -e "s/^${_old_basename}$/${_new_basename}/g" "${_index_path}" diff --git a/test/index.bats b/test/index.bats index 8526fa2b4..1ee0a1bfd 100644 --- a/test/index.bats +++ b/test/index.bats @@ -277,6 +277,21 @@ load test_helper [[ "$(cat "${NB_DIR}/home/.index")" == "" ]] } +@test "'index delete ' with brackets and spaces in filename deletes an item from the index." { + { + "${_NB}" init + "${_NB}" add "[ ] first.md" --title "one" + } + + run "${_NB}" index delete "$(ls "${NB_DIR}/home")" + + printf "\${status}: '%s'\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + + [[ "${status}" -eq 0 ]] + [[ "$(cat "${NB_DIR}/home/.index")" == "" ]] +} + @test "'index delete' with no argument returns 1 and prints help." { { "${_NB}" init @@ -344,6 +359,24 @@ load test_helper [[ "$(cat "${NB_DIR}/home/.index")" == "example.md" ]] } +@test "'index update ' with brackets and spaces in filename updates the index." { + { + "${_NB}" init + "${_NB}" add "[ ] first.md" --title "# one" + } + + run "${_NB}" index update "$(ls "${NB_DIR}/home")" "example.md" + + printf "\${status}: '%s'\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + printf \ + "$(cat \"\$\{NB_DIR\}/home/.index\"): '%s'\\n" \ + "$(cat "${NB_DIR}/home/.index")" + + [[ "${status}" -eq 0 ]] + [[ "$(cat "${NB_DIR}/home/.index")" == "example.md" ]] +} + @test "'index update' with no arguments returns 1 and prints help." { { "${_NB}" init diff --git a/test/move-rename.bats b/test/move-rename.bats index 67087efa3..c377017bf 100644 --- a/test/move-rename.bats +++ b/test/move-rename.bats @@ -224,6 +224,54 @@ Moved\ to:\ .*[.*Example\ Folder/1.*].*\ .*Example\ Folder/Example\ File\.js.* # --to title / --to-title ##################################################### +@test "'move --to title' with brackets in existing filename renames todo." { + # NOTE: https://github.com/xwmx/nb/issues/292 + { + "${_NB}" init + + "${_NB}" add --filename "[ ] Example Filename.md" --title "Example Title" + + + _files=($(ls "${NB_DIR}/home/")) + printf "\${_files[0]}: '%s'\\n" "${_files[0]}" + + [[ -f "${NB_DIR}/home/[ ] Example Filename.md" ]] + [[ ! -f "${NB_DIR}/home/example_title.md" ]] + } + + run "${_NB}" rename 1 --to title <<< "y${_NEWLINE}" + + printf "\${status}: '%s'\\n" "${status}" + printf "\${output}: '%s'\\n" "${output}" + + # Returns status 0: + + [[ ${status} -eq 0 ]] + + # Moves file: + + [[ ! -f "${NB_DIR}/home/[ ] Example Filename.md" ]] + [[ -f "${NB_DIR}/home/example_title.md" ]] + + # Creates git commit: + + cd "${NB_DIR}/home" || return 1 + while [[ -n "$(git status --porcelain)" ]] + do + sleep 1 + done + git log | grep -q '\[nb\] Move' + + # Prints output: + +# [[ "${lines[0]}" =~ \ +# Moving:\ \ \ .*[.*2.*].*\ ✔️\ \ .*example_one-1.todo.md.*\ \".*[.*\ .*].*\ Example\ Two\" ]] +# [[ "${lines[1]}" =~ \ +# To:\ \ \ \ \ \ \ .*example_two.todo.md.* ]] +# [[ "${lines[2]}" =~ \ +# Moved\ to:\ .*[.*2.*].*\ .*example_two.todo.md.*\ \".*[.*\ .*].*\ Example\ Two\" ]] +} + @test "'move --to title' with duplicated, renamed todo renames title." { # NOTE: https://github.com/xwmx/nb/issues/292 {