Skip to content

Commit b2e23b4

Browse files
committed
fix: Ensure zsh completions (#compdef-style) always have prepended
underscore. Fixes #58
1 parent e4cdfc2 commit b2e23b4

File tree

5 files changed

+66
-47
lines changed

5 files changed

+66
-47
lines changed

Doc

Whitespace-only changes.

docs/tutorials/local-development.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11

22
## Local Package Development
33

4-
If you are working on a project, and want to pull in a dependency, say [bash-args](https://github.com/eankeen/bash-args), the workflow is slightly different
4+
If you are working on a project, and want to pull in a dependency, say [bash-args](https://github.com/eankeen/bash-args), the workflow is similar to installing packages globally
55

66
To use the packages, simply append to the `PATH` variable in your script entrypoint (`script.sh` in the example below). Note that exporting it isn't required because it's already an exported variable
77

8-
TODO: Note that right now, you have to manually add `eankeen/bash-args` to your dependencies.toml. In the future, `dependencies` will likely be removed in favor of storing the information within the Git repository as submodules (a free lockfile)
9-
108
```sh
119
mkdir 'my-project' && cd 'my-project'
1210

pkg/lib/util/abstract-completions.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,25 @@ abstract.completions_do_action_zsh() {
155155
local file="$2"
156156

157157
abstract.completions_do_echo
158-
158+
159159
if grep -qs "^#compdef" "$file"; then
160+
local fileName="${file##*/}"
161+
if [ "${fileName::1}" != _ ]; then
162+
fileName="${fileName/#/_}"
163+
fi
164+
160165
case "$action" in
161-
link)
162-
if [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}" ]; then
166+
link)
167+
if [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/$fileName" ]; then
163168
log.error "Skipping '$fileName' since an existing symlink with the same name already exists"
164169
else
165170
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
166-
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
171+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/$fileName"
167172
fi
168173
;;
169174
unlink)
170-
if [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}" ]; then
171-
unlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
175+
if [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/$fileName" ]; then
176+
unlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/$fileName"
172177
fi
173178
;;
174179
esac

tests/do-plumbing-link-completions.bats

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,18 @@ load 'util/init.sh'
304304

305305
test_util.setup_pkg "$pkg"; {
306306
mkdir -p ./{contrib/,}completion{,s}
307-
touch "completion/c1.zsh"
308-
echo '#compdef' > "completions/c2.zsh"
309-
touch "contrib/completion/c3.zsh"
310-
echo '#compdef' > "contrib/completions/c4.zsh"
307+
touch "completion/_c1.zsh"
308+
echo '#compdef' > "completions/_c2.zsh"
309+
touch "contrib/completion/_c3.zsh"
310+
echo '#compdef' > "contrib/completions/_c4.zsh"
311311
}; test_util.finish_pkg
312312
test_util.mock_add "$pkg"
313313

314314
assert_success
315-
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/c1.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completion/c1.zsh" ]
316-
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys//c2.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completions/c2.zsh" ]
317-
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/c3.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/contrib/completion/c3.zsh" ]
318-
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys//c4.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/contrib/completions/c4.zsh" ]
315+
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_c1.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completion/_c1.zsh" ]
316+
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_c2.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completions/_c2.zsh" ]
317+
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_c3.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/contrib/completion/_c3.zsh" ]
318+
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_c4.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/contrib/completions/_c4.zsh" ]
319319
}
320320

321321
@test "adds zsh completions determined with heuristics (root directory)" {
@@ -342,16 +342,16 @@ load 'util/init.sh'
342342
test_util.setup_pkg "$pkg"; {
343343
echo 'BASH_COMPLETIONS=""' > 'package.sh'
344344
mkdir completion{,s}
345-
touch "completion/c1.zsh"
346-
echo '#compdef' > "completions/c2.zsh"
345+
touch "completion/_c1.zsh"
346+
echo '#compdef' > "completions/_c2.zsh"
347347
}; test_util.finish_pkg
348348
test_util.mock_add "$pkg"
349349

350350
run do-plumbing-link-completions "$site/$pkg"
351351

352352
assert_success
353-
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/c1.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completion/c1.zsh" ]
354-
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys//c2.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completions/c2.zsh" ]
353+
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_c1.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completion/_c1.zsh" ]
354+
assert [ "$(readlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_c2.zsh")" = "$BPM_PACKAGES_PATH/$site/$pkg/completions/_c2.zsh" ]
355355
}
356356

357357
@test "do not add zsh completions from heuristics when ZSH_COMPLETIONS is specified in package.sh" {
@@ -390,6 +390,22 @@ load 'util/init.sh'
390390
assert [ ! -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/prog.zsh" ]
391391
}
392392

393+
@test "prepend underscore to zsh completions if one is not present" {
394+
local site='github.com'
395+
local pkg='username/package'
396+
397+
test_util.setup_pkg "$pkg"; {
398+
mkdir 'completion'
399+
echo '#compdef _prog' > "completion/prog.zsh"
400+
}; test_util.finish_pkg
401+
test_util.mock_add "$pkg"
402+
403+
run do-plumbing-link-completions "$site/$pkg"
404+
405+
assert_success
406+
assert [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_prog.zsh" ]
407+
}
408+
393409
## FISH ##
394410

395411
@test "adds fish completions determined from bpm.toml" {
@@ -675,7 +691,7 @@ load 'util/init.sh'
675691
run do-plumbing-link-completions "$site/$pkg2"
676692

677693
assert_success
678-
assert_line -p "Skipping 'file2-completion.zsh' since an existing symlink with the same name already exists"
694+
assert_line -p "Skipping '_file2-completion.zsh' since an existing symlink with the same name already exists"
679695
}
680696

681697
@test "warns link fish completions if completion already exists" {

tests/do-plumbing-unlink-completions.bats

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,21 @@ load 'util/init.sh'
8787
local pkg='username/package'
8888

8989
test_util.setup_pkg "$pkg"; {
90-
echo 'ZSH_COMPLETIONS="somedir/comp1.zsh:otherdir/comp3.zsh"' > 'package.sh'
90+
echo 'ZSH_COMPLETIONS="somedir/_comp1.zsh:otherdir/_comp3.zsh"' > 'package.sh'
9191
mkdir 'somedir' 'otherdir'
92-
touch 'somedir/comp1.zsh'
93-
echo '#compdef' > 'otherdir/comp3.zsh'
92+
touch 'somedir/_comp1.zsh'
93+
echo '#compdef' > 'otherdir/_comp3.zsh'
9494
}; test_util.finish_pkg
9595
test_util.mock_add "$pkg"
9696

97-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp1.zsh" ]
98-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp3.zsh" ]
97+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp1.zsh" ]
98+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp3.zsh" ]
9999

100100
run do-plumbing-unlink-completions "$site/$pkg"
101101

102102
assert_success
103-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp1.zsh" ]
104-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp3.zsh" ]
103+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp1.zsh" ]
104+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp3.zsh" ]
105105
}
106106

107107
@test "unlinks zsh completions determined from bpm.toml" {
@@ -111,19 +111,19 @@ load 'util/init.sh'
111111
test_util.setup_pkg "$pkg"; {
112112
echo 'completionDirs = [ "somedir", "otherdir" ]' > 'bpm.toml'
113113
mkdir 'somedir' 'otherdir'
114-
touch 'somedir/comp1.zsh'
115-
echo '#compdef' > 'otherdir/comp3.zsh'
114+
touch 'somedir/_comp1.zsh'
115+
echo '#compdef' > 'otherdir/_comp3.zsh'
116116
}; test_util.finish_pkg
117117
test_util.mock_add "$pkg"
118118

119-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp1.zsh" ]
120-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp3.zsh" ]
119+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp1.zsh" ]
120+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp3.zsh" ]
121121

122122
run do-plumbing-unlink-completions "$site/$pkg"
123123

124124
assert_success
125-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp1.zsh" ]
126-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp3.zsh" ]
125+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp1.zsh" ]
126+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp3.zsh" ]
127127
}
128128

129129
@test "unlinks zsh completions determined from heuristics (completion?(s) directory)" {
@@ -132,19 +132,19 @@ load 'util/init.sh'
132132

133133
test_util.setup_pkg "$pkg"; {
134134
mkdir -p completion{,s}
135-
touch 'completion/comp.zsh'
136-
echo '#compdef' > 'completions/comp2.zsh'
135+
touch 'completion/_comp.zsh'
136+
echo '#compdef' > 'completions/_comp2.zsh'
137137
}; test_util.finish_pkg
138138
test_util.mock_add "$pkg"
139139

140-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp.zsh" ]
141-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp2.zsh" ]
140+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp.zsh" ]
141+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp2.zsh" ]
142142

143143
run do-plumbing-unlink-completions "$site/$pkg"
144144

145145
assert_success
146-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp.zsh" ]
147-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp2.zsh" ]
146+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp.zsh" ]
147+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp2.zsh" ]
148148
}
149149

150150
@test "unlinks zsh completions determined from heuristics (contrib/completion?(s) directory)" {
@@ -153,19 +153,19 @@ load 'util/init.sh'
153153

154154
test_util.setup_pkg "$pkg"; {
155155
mkdir -p contrib/completion{,s}
156-
touch 'contrib/completion/comp.zsh'
157-
echo '#compdef' > 'contrib/completions/comp2.zsh'
156+
touch 'contrib/completion/_comp.zsh'
157+
echo '#compdef' > 'contrib/completions/_comp2.zsh'
158158
}; test_util.finish_pkg
159159
test_util.mock_add "$pkg"
160160

161-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp.zsh" ]
162-
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp2.zsh" ]
161+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp.zsh" ]
162+
assert [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp2.zsh" ]
163163

164164
run do-plumbing-unlink-completions "$site/$pkg"
165165

166166
assert_success
167-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/comp.zsh" ]
168-
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/comp2.zsh" ]
167+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compctl/_comp.zsh" ]
168+
assert [ ! -e "$BPM_INSTALL_COMPLETIONS/zsh/compsys/_comp2.zsh" ]
169169
}
170170

171171
@test "bpm.toml has presidence over package.sh unlink completions" {

0 commit comments

Comments
 (0)