Skip to content

Commit c290ef3

Browse files
committed
feat: Print warning and skip if symlink would override another. Closes #54
1 parent 9d368a9 commit c290ef3

File tree

6 files changed

+183
-14
lines changed

6 files changed

+183
-14
lines changed

pkg/lib/util/abstract-bins.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ abstract.bins() {
99
ensure.non_zero 'id' "$id"
1010

1111
local -a bins=()
12-
local remove_extension=
12+
local remove_extensions=
1313

1414
local bpm_toml_file="$BPM_PACKAGES_PATH/$id/bpm.toml"
1515
local package_sh_file="$BPM_PACKAGES_PATH/$id/package.sh"
@@ -118,8 +118,13 @@ abstract.bins_do_action() {
118118
case "$action" in
119119
link)
120120
mkdir -p "$BPM_INSTALL_BIN"
121-
ln -sf "$fullBinFile" "$BPM_INSTALL_BIN/$binName"
122-
chmod +x "$BPM_INSTALL_BIN/$binName"
121+
122+
if [ -L "$BPM_INSTALL_BIN/$binName" ]; then
123+
log.error "Skipping '$binName' since an existing symlink with the same name already exists"
124+
else
125+
ln -sf "$fullBinFile" "$BPM_INSTALL_BIN/$binName"
126+
chmod +x "$BPM_INSTALL_BIN/$binName"
127+
fi
123128
;;
124129
unlink)
125130
if [ -f "$BPM_INSTALL_BIN/$binName" ]; then

pkg/lib/util/abstract-completions.sh

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ abstract.completions_do_action_bash() {
134134

135135
case "$action" in
136136
link)
137-
mkdir -p "$BPM_INSTALL_COMPLETIONS/bash"
138-
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/bash/$fileName"
137+
if [ -L "$BPM_INSTALL_COMPLETIONS/bash/$fileName" ]; then
138+
log.error "Skipping '$fileName' since an existing symlink with the same name already exists"
139+
else
140+
mkdir -p "$BPM_INSTALL_COMPLETIONS/bash"
141+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/bash/$fileName"
142+
fi
139143
;;
140144
unlink)
141145
if [ -f "$BPM_INSTALL_COMPLETIONS/bash/$fileName" ]; then
@@ -155,8 +159,12 @@ abstract.completions_do_action_zsh() {
155159
if grep -qs "^#compdef" "$file"; then
156160
case "$action" in
157161
link)
158-
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
159-
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
162+
if [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}" ]; then
163+
log.error "Skipping '$fileName' since an existing symlink with the same name already exists"
164+
else
165+
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
166+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
167+
fi
160168
;;
161169
unlink)
162170
if [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}" ]; then
@@ -167,8 +175,12 @@ abstract.completions_do_action_zsh() {
167175
else
168176
case "$action" in
169177
link)
170-
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compctl"
171-
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
178+
if [ -L "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}" ]; then
179+
log.error "Skipping '$fileName' since an existing symlink with the same name already exists"
180+
else
181+
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compctl"
182+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
183+
fi
172184
;;
173185
unlink)
174186
if [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}" ]; then
@@ -187,8 +199,12 @@ abstract.completions_do_action_fish() {
187199

188200
case "$action" in
189201
link)
190-
mkdir -p "$BPM_INSTALL_COMPLETIONS/fish"
191-
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/fish/${file##*/}"
202+
if [ -L "$BPM_INSTALL_COMPLETIONS/fish/${file##*/}" ]; then
203+
log.error "Skipping '$fileName' since an existing symlink with the same name already exists"
204+
else
205+
mkdir -p "$BPM_INSTALL_COMPLETIONS/fish"
206+
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/fish/${file##*/}"
207+
fi
192208
;;
193209
unlink)
194210
if [ -f "$BPM_INSTALL_COMPLETIONS/fish/${file##*/}" ]; then

pkg/lib/util/abstract-mans.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ abstract.mans_do_action() {
8989

9090
case "$action" in
9191
link)
92-
mkdir -p "$BPM_INSTALL_MAN/man$n"
93-
ln -sf "$full_man_file" "$BPM_INSTALL_MAN/man$n/$manFile"
92+
if [ -L "$BPM_INSTALL_MAN/man$n/$manFile" ]; then
93+
log.error "Skipping '$manFile' since an existing symlink with the same name already exists"
94+
else
95+
mkdir -p "$BPM_INSTALL_MAN/man$n"
96+
ln -sf "$full_man_file" "$BPM_INSTALL_MAN/man$n/$manFile"
97+
fi
9498
;;
9599
unlink)
96100
# Because 'abstract.mans_search_heuristics' sometimes repeats

tests/do-plumbing-link-bins.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,26 @@ load 'util/init.sh'
362362

363363
assert_line -p "Directory 'dir' with executable files not found. Skipping"
364364
}
365+
366+
@test "warns link bins if binary already exists" {
367+
local site='github.com'
368+
local pkg1="username/package"
369+
local pkg2='username/package2'
370+
371+
test_util.setup_pkg "$pkg1"; {
372+
touch 'file2.bash'
373+
chmod +x 'file2.bash'
374+
}; test_util.finish_pkg
375+
test_util.fake_clone "$site/$pkg1"
376+
377+
test_util.setup_pkg "$pkg2"; {
378+
touch 'file2.bash'
379+
chmod +x 'file2.bash'
380+
}; test_util.finish_pkg
381+
test_util.fake_clone "$site/$pkg2"
382+
383+
do-plumbing-link-bins "$site/$pkg1"
384+
run do-plumbing-link-bins "$site/$pkg2"
385+
386+
assert_line -p "Skipping 'file.sh' since an existing symlink with the same name already exists"
387+
}

tests/do-plumbing-link-completions.bats

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ load 'util/init.sh'
561561

562562
run do-plumbing-link-completions "$site/$pkg"
563563

564+
assert_success
564565
assert_line -p "Completion file 'some_file' not found. Skipping"
565566
}
566567

@@ -576,10 +577,11 @@ load 'util/init.sh'
576577

577578
run do-plumbing-link-completions "$site/$pkg"
578579

580+
assert_success
579581
assert_line -p "Completion file 'some_file' not found. Skipping"
580582
}
581583

582-
@test "fails link completions when specifying file in bpm.toml" {
584+
@test "warns link completions when specifying file in bpm.toml" {
583585
local site='github.com'
584586
local pkg="username/package"
585587

@@ -590,6 +592,7 @@ load 'util/init.sh'
590592

591593
run do-plumbing-link-completions "$site/$pkg"
592594

595+
assert_success
593596
assert_line -p "Directory 'dir' with executable files not found. Skipping"
594597
}
595598

@@ -604,5 +607,102 @@ load 'util/init.sh'
604607

605608
run do-plumbing-link-completions "$site/$pkg"
606609

610+
assert_success
607611
assert_line -p "Directory 'dir' with executable files not found. Skipping"
608612
}
613+
614+
@test "warns link bash completions if completion already exists" {
615+
local site='github.com'
616+
local pkg1="username/package"
617+
local pkg2='username/package2'
618+
619+
test_util.setup_pkg "$pkg1"; {
620+
touch 'file2-completion.bash'
621+
chmod +x 'file2-completion.bash'
622+
}; test_util.finish_pkg
623+
test_util.fake_clone "$site/$pkg1"
624+
625+
test_util.setup_pkg "$pkg2"; {
626+
touch 'file2-completion.bash'
627+
chmod +x 'file2-completion.bash'
628+
}; test_util.finish_pkg
629+
test_util.fake_clone "$site/$pkg2"
630+
631+
do-plumbing-link-completions "$site/$pkg1"
632+
run do-plumbing-link-completions "$site/$pkg2"
633+
634+
assert_success
635+
assert_line -p "Skipping 'file2-completion.bash' since an existing symlink with the same name already exists"
636+
}
637+
638+
@test "warns link zsh compctl completions if completion already exists" {
639+
local site='github.com'
640+
local pkg1="username/package"
641+
local pkg2='username/package2'
642+
643+
test_util.setup_pkg "$pkg1"; {
644+
touch 'file2-completion.zsh'
645+
chmod +x 'file2-completion.zsh'
646+
}; test_util.finish_pkg
647+
test_util.fake_clone "$site/$pkg1"
648+
649+
test_util.setup_pkg "$pkg2"; {
650+
touch 'file2-completion.zsh'
651+
chmod +x 'file2-completion.zsh'
652+
}; test_util.finish_pkg
653+
test_util.fake_clone "$site/$pkg2"
654+
655+
do-plumbing-link-completions "$site/$pkg1"
656+
run do-plumbing-link-completions "$site/$pkg2"
657+
658+
assert_success
659+
assert_line -p "Skipping 'file2-completion.zsh' since an existing symlink with the same name already exists"
660+
}
661+
662+
@test "warns link zsh compsys completions if completion already exists" {
663+
local site='github.com'
664+
local pkg1="username/package"
665+
local pkg2='username/package2'
666+
667+
test_util.setup_pkg "$pkg1"; {
668+
echo '#compdef' > "file2-completion.zsh"
669+
chmod +x 'file2-completion.zsh'
670+
}; test_util.finish_pkg
671+
test_util.fake_clone "$site/$pkg1"
672+
673+
test_util.setup_pkg "$pkg2"; {
674+
echo '#compdef' > "file2-completion.zsh"
675+
chmod +x 'file2-completion.zsh'
676+
}; test_util.finish_pkg
677+
test_util.fake_clone "$site/$pkg2"
678+
679+
do-plumbing-link-completions "$site/$pkg1"
680+
run do-plumbing-link-completions "$site/$pkg2"
681+
682+
assert_success
683+
assert_line -p "Skipping 'file2-completion.zsh' since an existing symlink with the same name already exists"
684+
}
685+
686+
@test "warns link fish completions if completion already exists" {
687+
local site='github.com'
688+
local pkg1="username/package"
689+
local pkg2='username/package2'
690+
691+
test_util.setup_pkg "$pkg1"; {
692+
touch "file2-completion.fish"
693+
chmod +x 'file2-completion.fish'
694+
}; test_util.finish_pkg
695+
test_util.fake_clone "$site/$pkg1"
696+
697+
test_util.setup_pkg "$pkg2"; {
698+
touch "file2-completion.fish"
699+
chmod +x 'file2-completion.fish'
700+
}; test_util.finish_pkg
701+
test_util.fake_clone "$site/$pkg2"
702+
703+
do-plumbing-link-completions "$site/$pkg1"
704+
run do-plumbing-link-completions "$site/$pkg2"
705+
706+
assert_success
707+
assert_line -p "Skipping 'file2-completion.fish' since an existing symlink with the same name already exists"
708+
}

tests/do-plumbing-link-man.bats

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,24 @@ load 'util/init.sh'
175175

176176
assert_line -p "Directory 'dir' with executable files not found. Skipping"
177177
}
178+
179+
@test "warns link man if man page already exists" {
180+
local site='github.com'
181+
local pkg1="username/package"
182+
local pkg2='username/package2'
183+
184+
test_util.setup_pkg "$pkg1"; {
185+
touch 'exec.3'
186+
}; test_util.finish_pkg
187+
test_util.fake_clone "$site/$pkg1"
188+
189+
test_util.setup_pkg "$pkg2"; {
190+
touch 'exec.3'
191+
}; test_util.finish_pkg
192+
test_util.fake_clone "$site/$pkg2"
193+
194+
do-plumbing-link-man "$site/$pkg1"
195+
run do-plumbing-link-man "$site/$pkg2"
196+
197+
assert_line -p "Skipping 'exec.3' since an existing symlink with the same name already exists"
198+
}

0 commit comments

Comments
 (0)