|
| 1 | +#!./libs/bats/bin/bats |
| 2 | + |
| 3 | +load 'libs/bats-support/load' |
| 4 | +load 'libs/bats-assert/load' |
| 5 | +load 'helpers' |
| 6 | + |
| 7 | +setup() { |
| 8 | + setupNotesEnv |
| 9 | +} |
| 10 | + |
| 11 | +teardown() { |
| 12 | + teardownNotesEnv |
| 13 | +} |
| 14 | + |
| 15 | +notes="./notes" |
| 16 | + |
| 17 | +@test "Should output nothing and return non-zero if there are no notes to search" { |
| 18 | + run $notes search |
| 19 | + |
| 20 | + assert_failure |
| 21 | + echo $output |
| 22 | + assert_equal $(echo $output | wc -w) 0 |
| 23 | +} |
| 24 | + |
| 25 | +@test "Should show all notes found if no pattern is provided to search" { |
| 26 | + touch $NOTES_DIRECTORY/note1.md |
| 27 | + touch $NOTES_DIRECTORY/note2.md |
| 28 | + |
| 29 | + run $notes search |
| 30 | + assert_success |
| 31 | + assert_line "note1.md" |
| 32 | + assert_line "note2.md" |
| 33 | +} |
| 34 | + |
| 35 | +@test "Should search notes when using the search shorthand alias" { |
| 36 | + touch $NOTES_DIRECTORY/note.md |
| 37 | + |
| 38 | + run $notes s |
| 39 | + assert_success |
| 40 | + assert_line "note.md" |
| 41 | +} |
| 42 | + |
| 43 | +@test "Should show matching notes only if a pattern is provided to search" { |
| 44 | + touch $NOTES_DIRECTORY/match-note1.md |
| 45 | + touch $NOTES_DIRECTORY/hide-note2.md |
| 46 | + |
| 47 | + run $notes search "match" |
| 48 | + |
| 49 | + assert_success |
| 50 | + assert_line "match-note1.md" |
| 51 | + refute_line "hide-note2.md" |
| 52 | +} |
| 53 | + |
| 54 | +@test "Should match notes case insensitively with search" { |
| 55 | + touch $NOTES_DIRECTORY/MATCH-note1.md |
| 56 | + touch $NOTES_DIRECTORY/hide-note2.md |
| 57 | + |
| 58 | + run $notes search "match" |
| 59 | + |
| 60 | + assert_success |
| 61 | + assert_line "MATCH-note1.md" |
| 62 | + refute_line "hide-note2.md" |
| 63 | +} |
| 64 | + |
| 65 | +@test "Should match subdirectory or file names with search" { |
| 66 | + touch "$NOTES_DIRECTORY/hide-note.md" |
| 67 | + mkdir "$NOTES_DIRECTORY/match-directory" |
| 68 | + touch "$NOTES_DIRECTORY/match-directory/note.md" |
| 69 | + |
| 70 | + run $notes search "match" |
| 71 | + |
| 72 | + assert_success |
| 73 | + assert_output "match-directory/note.md" |
| 74 | +} |
| 75 | + |
| 76 | +# These are the 'grep' tests. |
| 77 | + |
| 78 | +@test "Should match only the files containing the given pattern when searching" { |
| 79 | + echo "my-pattern" > $NOTES_DIRECTORY/matching-note.md |
| 80 | + echo "some-other-pattern" > $NOTES_DIRECTORY/non-matching-note.md |
| 81 | + |
| 82 | + run $notes search my-pattern |
| 83 | + |
| 84 | + assert_success |
| 85 | + assert_line "matching-note.md" |
| 86 | + refute_line "non-matching-note.md" |
| 87 | +} |
| 88 | + |
| 89 | +@test "Should search notes when using the search shorthand alias" { |
| 90 | + echo "my-pattern" > $NOTES_DIRECTORY/matching-note.md |
| 91 | + |
| 92 | + run $notes s my-pattern |
| 93 | + |
| 94 | + assert_success |
| 95 | + assert_line "matching-note.md" |
| 96 | +} |
| 97 | + |
| 98 | +@test "Should search case-insensitively" { |
| 99 | + echo "LETTERS" > $NOTES_DIRECTORY/matching-note.md |
| 100 | + |
| 101 | + run $notes search letter |
| 102 | + |
| 103 | + assert_success |
| 104 | + assert_line "matching-note.md" |
| 105 | +} |
| 106 | + |
| 107 | +@test "Should search files with paths including spaces" { |
| 108 | + mkdir "$NOTES_DIRECTORY/path with spaces" |
| 109 | + echo 'match' > "$NOTES_DIRECTORY/path with spaces/note file.md" |
| 110 | + |
| 111 | + run $notes search match |
| 112 | + |
| 113 | + assert_success |
| 114 | + assert_output "path with spaces/note file.md" |
| 115 | +} |
0 commit comments