Skip to content

Commit bf0f82b

Browse files
committed
test: Fix tests that do not pass in --global
This also makes it so that it only checks for the bpm.toml file on subcommands that actually require it. Tests made in accordance
1 parent d656aae commit bf0f82b

File tree

4 files changed

+158
-37
lines changed

4 files changed

+158
-37
lines changed

bpm.toml

Whitespace-only changes.

pkg/lib/cmd/bpm.sh

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,11 @@ main() {
3939
esac
4040
done
4141

42-
if [ "$is_global" = 'no' ]; then
43-
if ! project_root_directory="$(
44-
while [[ ! -f "bpm.toml" && "$PWD" != / ]]; do
45-
cd ..
46-
done
47-
48-
if [[ $PWD == / ]]; then
49-
die "No 'bpm.toml' file found. Please create one to install local packages or pass the '--global' option"
50-
fi
51-
52-
printf "%s" "$PWD"
53-
)"; then
54-
exit 1
55-
fi
56-
57-
BPM_PREFIX="$project_root_directory/bpm_packages"
58-
BPM_PACKAGES_PATH="$BPM_PREFIX/packages"
59-
BPM_INSTALL_BIN="$BPM_PREFIX/bin"
60-
BPM_INSTALL_MAN="$BPM_PREFIX/man"
61-
BPM_INSTALL_COMPLETIONS="$BPM_PREFIX/completions"
62-
fi
6342

6443
case "$1" in
6544
add)
45+
may_reset_bpm_vars "$is_global"
46+
6647
shift
6748
do-add "$@"
6849
;;
@@ -79,26 +60,38 @@ main() {
7960
do-init "$@"
8061
;;
8162
link)
63+
may_reset_bpm_vars "$is_global"
64+
8265
shift
8366
do-link "$@"
8467
;;
8568
list)
69+
may_reset_bpm_vars "$is_global"
70+
8671
shift
8772
do-list "$@"
8873
;;
8974
outdated)
75+
may_reset_bpm_vars "$is_global"
76+
9077
shift
9178
bpm-outdated "$@"
9279
;;
9380
package-path)
81+
may_reset_bpm_vars "$is_global"
82+
9483
shift
9584
bpm-package-path "$@"
9685
;;
9786
remove)
87+
may_reset_bpm_vars "$is_global"
88+
9889
shift
9990
do-remove "$@"
10091
;;
10192
upgrade)
93+
may_reset_bpm_vars "$is_global"
94+
10295
shift
10396
do-upgrade "$@"
10497
;;
@@ -111,4 +104,31 @@ main() {
111104
esac
112105
}
113106

107+
may_reset_bpm_vars() {
108+
local is_global="$1"
109+
110+
if [ "$is_global" = 'no' ]; then
111+
if ! project_root_directory="$(
112+
while [[ ! -f "bpm.toml" && "$PWD" != / ]]; do
113+
cd ..
114+
done
115+
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
119+
120+
printf "%s" "$PWD"
121+
)"; then
122+
exit 1
123+
fi
124+
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"
131+
fi
132+
}
133+
114134
main "$@"

tests/do-echo.bats

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,137 @@
33
load 'util/init.sh'
44

55
@test "default BPM_ROOT" {
6-
BPM_ROOT= run bpm echo BPM_ROOT
6+
BPM_ROOT= run bpm --global echo BPM_ROOT
77
assert_output "$HOME/.local/share/bpm"
88
}
99

1010
@test "inherited BPM_ROOT" {
11-
BPM_ROOT=/tmp/bpm run bpm echo BPM_ROOT
11+
BPM_ROOT=/tmp/bpm run bpm --global echo BPM_ROOT
1212
assert_output "/tmp/bpm"
1313
}
1414

1515
@test "default BPM_PREFIX" {
16-
BPM_ROOT= BPM_PREFIX= run bpm echo BPM_PREFIX
16+
BPM_ROOT= BPM_PREFIX= run bpm --global echo BPM_PREFIX
1717
assert_output "$HOME/.local/share/bpm/cellar"
1818
}
1919

2020
@test "inherited BPM_PREFIX" {
21-
BPM_PREFIX=/usr/local run bpm echo BPM_PREFIX
21+
BPM_PREFIX=/usr/local run bpm --global echo BPM_PREFIX
2222
assert_output "/usr/local"
2323
}
2424

2525
@test "BPM_PREFIX based on BPM_ROOT" {
26-
BPM_ROOT=/tmp/bpm BPM_PREFIX= run bpm echo BPM_PREFIX
26+
BPM_ROOT=/tmp/bpm BPM_PREFIX= run bpm --global echo BPM_PREFIX
2727
assert_output "/tmp/bpm/cellar"
2828
}
2929

3030
@test "inherited BPM_PACKAGES_PATH" {
31-
BPM_PACKAGES_PATH=/usr/local/packages run bpm echo BPM_PACKAGES_PATH
31+
BPM_PACKAGES_PATH=/usr/local/packages run bpm --global echo BPM_PACKAGES_PATH
3232
assert_output "/usr/local/packages"
3333
}
3434

3535
@test "BPM_PACKAGES_PATH based on BPM_PREFIX" {
36-
BPM_PREFIX=/tmp/bpm BPM_PACKAGES_PATH= run bpm echo BPM_PACKAGES_PATH
36+
BPM_PREFIX=/tmp/bpm BPM_PACKAGES_PATH= run bpm --global echo BPM_PACKAGES_PATH
3737
assert_output "/tmp/bpm/packages"
3838
}
3939

4040
@test "default BPM_INSTALL_BIN" {
41-
BPM_ROOT= BPM_PREFIX= BPM_INSTALL_BIN= run bpm echo BPM_INSTALL_BIN
41+
BPM_ROOT= BPM_PREFIX= BPM_INSTALL_BIN= run bpm --global echo BPM_INSTALL_BIN
4242
assert_output "$HOME/.local/share/bpm/cellar/bin"
4343
}
4444

4545
@test "inherited BPM_INSTALL_BIN" {
46-
BPM_INSTALL_BIN=/opt/bin run bpm echo BPM_INSTALL_BIN
46+
BPM_INSTALL_BIN=/opt/bin run bpm --global echo BPM_INSTALL_BIN
4747
assert_output "/opt/bin"
4848
}
4949

5050
@test "BPM_INSTALL_BIN based on BPM_PREFIX" {
51-
BPM_INSTALL_BIN= BPM_ROOT=/tmp/bpm BPM_PREFIX=/usr/local run bpm echo BPM_INSTALL_BIN
51+
BPM_INSTALL_BIN= BPM_ROOT=/tmp/bpm BPM_PREFIX=/usr/local run bpm --global echo BPM_INSTALL_BIN
5252
assert_output "/usr/local/bin"
5353
}
5454

5555
@test "default BPM_INSTALL_MAN" {
56-
BPM_ROOT= BPM_PREFIX= BPM_INSTALL_MAN= run bpm echo BPM_INSTALL_MAN
56+
BPM_ROOT= BPM_PREFIX= BPM_INSTALL_MAN= run bpm --global echo BPM_INSTALL_MAN
5757
assert_output "$HOME/.local/share/bpm/cellar/man"
5858
}
5959

6060
@test "inherited BPM_INSTALL_MAN" {
61-
BPM_INSTALL_MAN=/opt/man run bpm echo BPM_INSTALL_MAN
61+
BPM_INSTALL_MAN=/opt/man run bpm --global echo BPM_INSTALL_MAN
6262
assert_output "/opt/man"
6363
}
6464

6565
@test "BPM_INSTALL_MAN based on BPM_PREFIX" {
66-
BPM_INSTALL_MAN= BPM_PREFIX=/usr/local run bpm echo BPM_INSTALL_MAN
66+
BPM_INSTALL_MAN= BPM_PREFIX=/usr/local run bpm --global echo BPM_INSTALL_MAN
6767
assert_output "/usr/local/man"
6868
}
6969

7070
@test "default BPM_INSTALL_COMPLETIONS" {
71-
BPM_ROOT= BPM_PREFIX= BPM_INSTALL_COMPLETIONS= run bpm echo BPM_INSTALL_COMPLETIONS
71+
BPM_ROOT= BPM_PREFIX= BPM_INSTALL_COMPLETIONS= run bpm --global echo BPM_INSTALL_COMPLETIONS
7272
assert_output "$HOME/.local/share/bpm/cellar/completions"
7373
}
7474

7575
@test "inherited BPM_INSTALL_COMPLETIONS" {
76-
BPM_INSTALL_COMPLETIONS=/opt/completions run bpm echo BPM_INSTALL_COMPLETIONS
76+
BPM_INSTALL_COMPLETIONS=/opt/completions run bpm --global echo BPM_INSTALL_COMPLETIONS
7777
assert_output "/opt/completions"
7878
}
7979

8080
@test "BPM_INSTALL_COMPLETIONS based on BPM_PREFIX" {
81-
BPM_INSTALL_COMPLETIONS= BPM_PREFIX=/usr/local run bpm echo BPM_INSTALL_COMPLETIONS
81+
BPM_INSTALL_COMPLETIONS= BPM_PREFIX=/usr/local run bpm --global echo BPM_INSTALL_COMPLETIONS
8282
assert_output "/usr/local/completions"
8383
}
84+
85+
86+
87+
@test "non-global default BPM_ROOT" {
88+
touch 'bpm.toml'
89+
90+
BPM_ROOT= run bpm echo BPM_ROOT
91+
92+
assert_success
93+
assert_line -p "$PWD"
94+
}
95+
96+
@test "non-global default BPM_PREFIX" {
97+
touch 'bpm.toml'
98+
99+
BPM_ROOT= BPM_PREFIX= run bpm echo BPM_PREFIX
100+
101+
assert_success
102+
assert_line -p "$PWD/bpm_packages"
103+
}
104+
105+
@test "non-global default BPM_PACKAGES_PATH" {
106+
touch 'bpm.toml'
107+
108+
BPM_ROOT= BPM_PACKAGES_PATH= run bpm echo BPM_PACKAGES_PATH
109+
110+
assert_success
111+
assert_line -p "$PWD/bpm_packages/packages"
112+
}
113+
114+
@test "non-global default BPM_INSTALL_BIN" {
115+
touch 'bpm.toml'
116+
117+
BPM_ROOT= BPM_INSTALL_BIN= run bpm echo BPM_INSTALL_BIN
118+
119+
assert_success
120+
assert_line -p "$PWD/bpm_packages/bin"
121+
}
122+
123+
@test "non-global default BPM_INSTALL_MAN" {
124+
touch 'bpm.toml'
125+
126+
BPM_ROOT= BPM_INSTALL_MAN= run bpm echo BPM_INSTALL_MAN
127+
128+
assert_success
129+
assert_line -p "$PWD/bpm_packages/man"
130+
}
131+
132+
@test "non-global default BPM_INSTALL_COMPLETIONS" {
133+
touch 'bpm.toml'
134+
135+
BPM_ROOT= BPM_INSTALL_COMPLETIONS= run bpm echo BPM_INSTALL_COMPLETIONS
136+
137+
assert_success
138+
assert_line -p "$PWD/bpm_packages/completions"
139+
}

tests/mode-global-local.bats

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bats
2+
3+
load 'util/init.sh'
4+
5+
# We only test two of all commands
6+
@test "error when not passing --global to add, list, and upgrade" {
7+
local str="No 'bpm.toml' file found. Please create one to install local packages or pass the '--global' option"
8+
9+
run bpm add foo
10+
assert_failure
11+
assert_line -p "$str"
12+
13+
run bpm list
14+
assert_failure
15+
assert_line -p "$str"
16+
17+
run bpm upgrade
18+
assert_failure
19+
assert_line -p "$str"
20+
}
21+
22+
@test "do not error with bpm.toml when not passing --global to list" {
23+
touch 'bpm.toml'
24+
25+
run bpm list
26+
assert_success
27+
assert_output ""
28+
}
29+
30+
@test "do not error when not passing --global to echo, complete, and init" {
31+
touch 'bpm.toml'
32+
33+
run bpm echo "PWD"
34+
assert_success
35+
assert_output "$PWD"
36+
37+
run bpm complete package-path
38+
assert_success
39+
assert_output ""
40+
41+
run bpm init bash
42+
assert_success
43+
assert_line -p "export PATH"
44+
assert_line -p '. "$BPM_ROOT/'
45+
}

0 commit comments

Comments
 (0)