Repository navigation
Expand file tree
/
Copy pathapprove
More file actions
executable file
·177 lines (139 loc) · 5.46 KB
/
Copy pathapprove
File metadata and controls
executable file
·177 lines (139 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
cd test > /dev/null
source test_helper.bash
context "when the shell is non-interactive"
describe "cd"
it "is a builtin"
approve "type -t cd" "type_builtin"
context "when the shell is interactive"
export FUZZYCD_TTY_FORCE=1
source ../fuzzycd
describe "cd"
it "is a function"
approve "type -t cd" "type_function"
describe "cd -h"
it "shows help"
approve "cd -h"
describe "cd -v"
it "shows version"
approve "cd -v"
describe "cd -c"
it "shows completions function"
approve "cd -c"
it "handles spaces when _cd is unavailable"
mkdir -p "tmp/space dir"
printf '%s\n' "$PWD/tmp/space dir" > "$FUZZYCD_HISTORY_FILE"
eval "$(fuzzycd_run -c)"
unset -f _cd 2>/dev/null || true
COMP_WORDS=(cd space)
COMP_CWORD=1
COMPREPLY=()
_fuzzycd_completions
[[ ${#COMPREPLY[@]} == 1 ]] || fail "Expected one completion, got ${#COMPREPLY[@]}"
[[ "${COMPREPLY[0]}" == "$PWD/tmp/space dir" ]] || fail "Expected spaced path completion"
[[ "$(fuzzycd_run -c)" != *mapfile* ]] || fail "Expected completion function to avoid mapfile dependency"
it "puts standard cd completions before fuzzy matches and removes duplicates"
mkdir -p "tmp/fuzzy one" "tmp/fuzzy two" "tmp/native three"
printf '%s\n%s\n' "$PWD/tmp/fuzzy one" "$PWD/tmp/fuzzy two" > "$FUZZYCD_HISTORY_FILE"
eval "$(fuzzycd_run -c)"
_cd() { COMPREPLY=("$PWD/tmp/native three" "$PWD/tmp/fuzzy two"); }
COMP_WORDS=(cd fuzzy)
COMP_CWORD=1
COMPREPLY=()
_fuzzycd_completions
[[ ${#COMPREPLY[@]} == 3 ]] || fail "Expected three merged completions, got ${#COMPREPLY[@]}"
[[ "${COMPREPLY[0]}" == "$PWD/tmp/native three" ]] || fail "Expected native completion first"
[[ "${COMPREPLY[1]}" == "$PWD/tmp/fuzzy two" ]] || fail "Expected duplicate fuzzy/native match second"
[[ "${COMPREPLY[2]}" == "$PWD/tmp/fuzzy one" ]] || fail "Expected remaining fuzzy match last"
it "keeps fuzzycd registered as the cd completion function"
printf '%s\n' "$PWD/tmp/one/two" > "$FUZZYCD_HISTORY_FILE"
eval "$(fuzzycd_run -c)"
_cd() {
COMPREPLY=("$PWD/tmp/one/two")
complete -F _cd cd
}
COMP_WORDS=(cd one)
COMP_CWORD=1
COMPREPLY=()
_fuzzycd_completions
[[ "$(complete -p cd)" == *"_fuzzycd_completions"* ]] || fail "Expected fuzzycd completion to remain registered"
describe "cd DIR"
it "adds it to history"
cd tmp/one/two > /dev/null
[[ $PWD == *tmp/one/two ]] || fail "Expected tmp/one/twp, got $PWD"
grep -q one/two "$FUZZYCD_HISTORY_FILE" || fail "Expected history file to contain one/two"
describe "cd DIR with a fuzzy string"
it "changes to the requested directory"
cd ontwo > /dev/null
[[ $PWD == *tmp/one/two ]] || fail "Expected tmp/one/twp, got $PWD"
context "when the history file contains some entries"
rm -f "$FUZZYCD_HISTORY_FILE"
cd /usr/local/bin > /dev/null
cd /usr/local/lib > /dev/null
cd /etc > /dev/null
EDITOR="cat"
describe "cd -s"
it "shows history file"
approve "cd -s"
describe "cd -e"
it "opens the history file"
approve "cd -e"
describe "cd -d DIR"
it "removes DIR from history"
approve "cd -d /etc"
approve "cd -s" "cd_s_after_delete"
describe "cd -d"
it "removes current directory from history"
cd /usr/local/lib
approve "cd -d"
approve "cd -s" "cd_s_after_delete2"
cd "$basedir"
rm -f "$FUZZYCD_HISTORY_FILE"
context "when the history file contains gone directories"
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
echo /no/such/dir >> "$FUZZYCD_HISTORY_FILE"
echo /another/gone/dir >> "$FUZZYCD_HISTORY_FILE"
echo /and/another/gone/dir >> "$FUZZYCD_HISTORY_FILE"
describe "cd DIR"
it "deletes the gone directory"
approve "cd suchdir" || true
expect_exit_code 1
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile"
describe "cd -p"
it "removes all gone directories"
approve "cd -p"
expect_exit_code 0
approve "cat $FUZZYCD_HISTORY_FILE" "cat_histfile_pruned"
it "states that nothing was pruned if there is nothing to do"
approve "cd -p" "cd_p_nothing_pruned"
expect_exit_code 0
context "when using a custom history file path"
export FUZZYCD_HISTORY_FILE="$PWD/tmp/custom/history"
mkdir -p "$(dirname "$FUZZYCD_HISTORY_FILE")"
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
echo /usr/local/lib >> "$FUZZYCD_HISTORY_FILE"
rm -f "$HOME/.fuzzycd-history.tmp"
describe "cd -d DIR"
it "rewrites the configured history file without using a fixed HOME temp file"
cd -d /usr/local/bin > /dev/null
[[ ! -e "$HOME/.fuzzycd-history.tmp" ]] || fail "Expected no temp file in HOME"
[[ "$(cat "$FUZZYCD_HISTORY_FILE")" == "/usr/local/lib" ]] || fail "Expected custom history file to be rewritten"
export FUZZYCD_HISTORY_FILE="$PWD/tmp/histfile"
context "when CDPATH contains entries"
echo /usr/local/bin > "$FUZZYCD_HISTORY_FILE"
export CDPATH=".:/usr:/etc"
describe "cd DIR"
it "prefers paths that are sub directories of a CDPATH path"
approve "cd local"
unset CDPATH
context "when FUZZYCD_MODE=m"
export FUZZYCD_MODE=m
rm -f "$FUZZYCD_HISTORY_FILE"
cd /usr/local/bin > /dev/null
cd /usr/local/lib > /dev/null
describe "cd with a fuzzy string"
it "goes to best match"
approve "cd localb"
cd "$basedir"
rm -f "$FUZZYCD_HISTORY_FILE"
unset FUZZYCD_MODE