Skip to content

Commit 8c30348

Browse files
committed
test: Fix 'echo' subcommand outputing wrong path information
1 parent bf0f82b commit 8c30348

File tree

4 files changed

+46
-29
lines changed

4 files changed

+46
-29
lines changed

pkg/lib/cmd/bpm.sh

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ main() {
3939
esac
4040
done
4141

42-
4342
case "$1" in
4443
add)
45-
may_reset_bpm_vars "$is_global"
44+
must_reset_bpm_vars "$is_global"
4645

4746
shift
4847
do-add "$@"
@@ -52,6 +51,8 @@ main() {
5251
do-complete "$@"
5352
;;
5453
echo)
54+
may_reset_bpm_vars "$is_global"
55+
5556
shift
5657
do-echo "$@"
5758
;;
@@ -60,37 +61,37 @@ main() {
6061
do-init "$@"
6162
;;
6263
link)
63-
may_reset_bpm_vars "$is_global"
64+
must_reset_bpm_vars "$is_global"
6465

6566
shift
6667
do-link "$@"
6768
;;
6869
list)
69-
may_reset_bpm_vars "$is_global"
70+
must_reset_bpm_vars "$is_global"
7071

7172
shift
7273
do-list "$@"
7374
;;
7475
outdated)
75-
may_reset_bpm_vars "$is_global"
76+
must_reset_bpm_vars "$is_global"
7677

7778
shift
7879
bpm-outdated "$@"
7980
;;
8081
package-path)
81-
may_reset_bpm_vars "$is_global"
82+
must_reset_bpm_vars "$is_global"
8283

8384
shift
8485
bpm-package-path "$@"
8586
;;
8687
remove)
87-
may_reset_bpm_vars "$is_global"
88+
must_reset_bpm_vars "$is_global"
8889

8990
shift
9091
do-remove "$@"
9192
;;
9293
upgrade)
93-
may_reset_bpm_vars "$is_global"
94+
must_reset_bpm_vars "$is_global"
9495

9596
shift
9697
do-upgrade "$@"
@@ -104,31 +105,35 @@ main() {
104105
esac
105106
}
106107

107-
may_reset_bpm_vars() {
108+
must_reset_bpm_vars() {
108109
local is_global="$1"
109110

110-
if [ "$is_global" = 'no' ]; then
111-
if ! project_root_directory="$(
112-
while [[ ! -f "bpm.toml" && "$PWD" != / ]]; do
113-
cd ..
114-
done
111+
local project_root_dir=
112+
if ! project_root_dir="$(util.get_project_root_dir)"; then
113+
die "No 'bpm.toml' file found. Please create one to install local packages or pass the '--global' option"
114+
fi
115115

116-
if [[ $PWD == / ]]; then
117-
die "No 'bpm.toml' file found. Please create one to install local packages or pass the '--global' option"
118-
fi
116+
do_set_bpm_vars "$project_root_dir"
117+
}
119118

120-
printf "%s" "$PWD"
121-
)"; then
122-
exit 1
123-
fi
119+
may_reset_bpm_vars() {
120+
local is_global="$1"
124121

125-
BPM_ROOT="$project_root_directory"
126-
BPM_PREFIX="$project_root_directory/bpm_packages"
127-
BPM_PACKAGES_PATH="$BPM_PREFIX/packages"
128-
BPM_INSTALL_BIN="$BPM_PREFIX/bin"
129-
BPM_INSTALL_MAN="$BPM_PREFIX/man"
130-
BPM_INSTALL_COMPLETIONS="$BPM_PREFIX/completions"
122+
local project_root_dir=
123+
if project_root_dir="$(util.get_project_root_dir)"; then
124+
do_set_bpm_vars "$project_root_dir"
131125
fi
132126
}
133127

128+
do_set_bpm_vars() {
129+
local project_root_dir="$1"
130+
131+
BPM_ROOT="$project_root_dir"
132+
BPM_PREFIX="$project_root_dir/bpm_packages"
133+
BPM_PACKAGES_PATH="$BPM_PREFIX/packages"
134+
BPM_INSTALL_BIN="$BPM_PREFIX/bin"
135+
BPM_INSTALL_MAN="$BPM_PREFIX/man"
136+
BPM_INSTALL_COMPLETIONS="$BPM_PREFIX/completions"
137+
}
138+
134139
main "$@"

pkg/lib/util/util.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,20 @@ util.extract_shell_variable() {
249249
return 1
250250
}
251251

252+
# @description Get the working directory of the project. Note
253+
# that this should always be called within a subshell
254+
util.get_project_root_dir() {
255+
while [[ ! -f "bpm.toml" && "$PWD" != / ]]; do
256+
cd ..
257+
done
258+
259+
if [[ $PWD == / ]]; then
260+
return 1
261+
fi
262+
263+
printf "%s" "$PWD"
264+
}
265+
252266
util.show_help() {
253267
cat <<"EOF"
254268
Usage:

tests/do-echo.bats

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ load 'util/init.sh'
8282
assert_output "/usr/local/completions"
8383
}
8484

85-
86-
8785
@test "non-global default BPM_ROOT" {
8886
touch 'bpm.toml'
8987

File renamed without changes.

0 commit comments

Comments
 (0)