Skip to content

Commit 1c2cb2a

Browse files
committed
test: Significantly improve test suite
This adds a bunch of tests, and refactors most of the current ones to use the `test_util.setup_pkg` and `test_util.fake_install` functions. This conversion is being done because it makes the tests significantly easier to reason about without common function helper abstractions
1 parent 4049485 commit 1c2cb2a

22 files changed

+580
-281
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ Why not use `bpkg` or `Basher`? Because `bpm`...
4949
- Is faster (bpm considers exec and subshell creation overhead)
5050
- Has a _much_ improved help output (basher)
5151
- Prints why a command failed, rather than just printing the help menu (basher)
52-
- Has actually been updated recently
5352
- Better bpm completion scripts
5453
- More flexibly parses command line arguments (basher)
5554

docs/recepies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Recepies
22

3-
### Installing packages from Github
3+
### Installing a package from Github
44

55
```sh
66
bpm install sstephenson/bats

pkg/lib/util/abstract-bins.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ abstract.bins_do_action() {
102102
chmod +x "$BPM_INSTALL_BIN/$binName"
103103
;;
104104
unlink)
105-
unlink "$BPM_INSTALL_BIN/$binName"
105+
if [ -f "$BPM_INSTALL_BIN/$binName" ]; then
106+
unlink "$BPM_INSTALL_BIN/$binName"
107+
fi
106108
;;
107109
esac
108110
}

pkg/lib/util/abstract-completions.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ abstract.completions_do_action_bash() {
9191
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/bash/${file##*/}"
9292
;;
9393
unlink)
94-
unlink "$BPM_INSTALL_COMPLETIONS/bash/${file##*/}"
94+
if [ -f "$BPM_INSTALL_COMPLETIONS/bash/${file##*/}" ]; then
95+
unlink "$BPM_INSTALL_COMPLETIONS/bash/${file##*/}"
96+
fi
9597
;;
9698
esac
9799

@@ -103,15 +105,15 @@ abstract.completions_do_action_zsh() {
103105

104106
if grep -qs "^#compdef" "$file"; then
105107
# TODO: run mkdir outside of loop
106-
# TODO: unlink?
107108
case "$action" in
108109
link)
109110
mkdir -p "$BPM_INSTALL_COMPLETIONS/zsh/compsys"
110111
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
111112
;;
112113
unlink)
113-
rm -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
114-
# unlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
114+
if [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}" ]; then
115+
unlink "$BPM_INSTALL_COMPLETIONS/zsh/compsys/${file##*/}"
116+
fi
115117
;;
116118
esac
117119
else
@@ -121,8 +123,9 @@ abstract.completions_do_action_zsh() {
121123
ln -sf "$file" "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
122124
;;
123125
unlink)
124-
rm -f "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
125-
# unlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
126+
if [ -f "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}" ]; then
127+
unlink "$BPM_INSTALL_COMPLETIONS/zsh/compctl/${file##*/}"
128+
fi
126129
;;
127130
esac
128131
fi

pkg/lib/util/abstract-mans.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ abstract.mans_search_heuristics() {
6666
done
6767
}
6868

69-
# @arg $1 The man file to symlink or remove
69+
# @arg $1 The man file to symlink or remove. Not all the files passed
70+
# in here are man pages, which is why the regex check exists, to extract
71+
# the file ending (and the man category)
7072
abstract.mans_do_action() {
7173
local action="$1"
7274
local full_man_file="$2"
@@ -83,9 +85,12 @@ abstract.mans_do_action() {
8385
ln -sf "$full_man_file" "$BPM_INSTALL_MAN/man$n/$manFile"
8486
;;
8587
unlink)
86-
# TODO unlink?
87-
rm -f "$BPM_INSTALL_MAN/man$n/$file"
88-
# unlink "$BPM_INSTALL_MAN/man$n/$manFile"
88+
# Because 'abstract.mans_search_heuristics' sometimes repeats
89+
# directories, and sometimes the stat's are out of dates, we add this
90+
# check in case a file was deleted in the meantime
91+
if [ -f "$BPM_INSTALL_MAN/man$n/$manFile" ]; then
92+
unlink "$BPM_INSTALL_MAN/man$n/$manFile"
93+
fi
8994
;;
9095
esac
9196
fi

pkg/lib/util/util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ util.extract_shell_variable() {
198198
local shellFile="$1"
199199
local variableName="$2"
200200

201-
if [ ! -f "$shellFile" ]; then
201+
if [ ! -e "$shellFile" ]; then
202202
die "File '$shellFile' not found"
203203
fi
204204

pkg/share/include.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include() {
1515
fi
1616

1717
if [ ! -d "$BPM_PREFIX/packages/$package" ]; then
18-
printf "%s" "Error: Package not installed: $package" >&2
18+
printf "%s" "Error: Package '$package' not installed" >&2
1919
return 1
2020
fi
2121

tests/do-init.bats

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ load './util/init.sh'
66
BPM_ROOT=/lol run do-init bash
77

88
assert_success
9-
assert_line -e 'export BPM_ROOT="/lol"'
9+
assert_line -p 'export BPM_ROOT="/lol"'
1010
}
1111

1212
@test "exports BPM_PREFIX" {
1313
BPM_PREFIX=/lol run do-init bash
1414

1515
assert_success
16-
assert_line -e 'export BPM_PREFIX="/lol"'
16+
assert_line -p 'export BPM_PREFIX="/lol"'
1717
}
1818

1919
@test "exports BPM_PACKAGES_PATH" {
2020
BPM_PACKAGES_PATH=/lol run do-init bash
2121

2222
assert_success
23-
assert_line -e 'export BPM_PACKAGES_PATH="/lol"'
23+
assert_line -p 'export BPM_PACKAGES_PATH="/lol"'
2424
}
2525

2626
@test "errors if shell is not available" {
2727
run do-init fakesh
2828

2929
assert_failure
30-
assert_line -e "Shell 'fakesh' is not a valid shell"
30+
assert_line -p "Shell 'fakesh' is not a valid shell"
3131
}
3232

3333
@test "bash completion works" {
@@ -47,10 +47,6 @@ load './util/init.sh'
4747
}
4848

4949
@test "is sh-compatible" {
50-
if ! command -v sh &>/dev/null; then
51-
skip "Command 'sh' not in PATH"
52-
fi
53-
54-
run sh -ec 'eval "$(bpm init - sh)"'
50+
run eval "$(do-init - sh)"
5551
assert_success
5652
}

tests/do-link.bats

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ load 'util/init.sh'
99
assert_output -p "Directory 'invalid' not found"
1010
}
1111

12-
@test "fails with a file path instead of a directory path" {
13-
touch file1
12+
@test "fails with a file" {
13+
touch 'file1'
1414

15-
run do-link file1
15+
run do-link 'file1'
1616

1717
assert_failure
1818
assert_output -p "Directory 'file1' not found"
@@ -24,11 +24,11 @@ load 'util/init.sh'
2424
test_util.mock_command do-plumbing-link-completions
2525
test_util.mock_command do-plumbing-link-man
2626

27-
mkdir -p subdir/theta
28-
do-link subdir/theta
27+
mkdir -p 'subdir/theta'
28+
do-link 'subdir/theta'
29+
mkdir 'theta'
2930

30-
mkdir theta
31-
run do-link theta
31+
run do-link 'theta'
3232

3333
assert_failure
3434
assert_line -n 0 -p "Package 'bpm-local/theta' is already present"
@@ -42,26 +42,26 @@ load 'util/init.sh'
4242

4343
mkdir -p touch "$BPM_PACKAGES_PATH/bpm-local"
4444
touch "$BPM_PACKAGES_PATH/bpm-local/theta"
45+
mkdir 'theta'
4546

46-
mkdir theta
47-
run do-link theta
47+
run do-link 'theta'
4848

4949
assert_failure
5050
assert_line -n 0 -p "Package 'bpm-local/theta' is already present"
5151
}
5252

53-
@test "links the package to packages under the correct namespace" {
53+
@test "links the package to packages under the correct namespace (bpm-local)" {
5454
test_util.mock_command do-plumbing-add-deps
5555
test_util.mock_command do-plumbing-link-bins
5656
test_util.mock_command do-plumbing-link-completions
5757
test_util.mock_command do-plumbing-link-man
5858

59-
mkdir package1
59+
mkdir 'package1'
6060

61-
run do-link package1
61+
run do-link 'package1'
6262

6363
assert_success
64-
assert [ "$(test_util.readlink $BPM_PACKAGES_PATH/bpm-local/package1)" = "$(test_util.readlink "$PWD/package1")" ]
64+
assert [ "$(readlink -f $BPM_PACKAGES_PATH/bpm-local/package1)" = "$(readlink -f "$PWD/package1")" ]
6565
}
6666

6767
@test "calls link-bins, link-completions, link-man and deps in order" {
@@ -70,9 +70,9 @@ load 'util/init.sh'
7070
test_util.mock_command do-plumbing-link-completions
7171
test_util.mock_command do-plumbing-link-man
7272

73-
mkdir package2
73+
mkdir 'package2'
7474

75-
run do-link package2
75+
run do-link 'package2'
7676

7777
assert_success
7878
assert_line -n 0 -e "Linking '/(.*)/bpm/cwd/package2'"
@@ -83,15 +83,15 @@ load 'util/init.sh'
8383

8484
}
8585

86-
@test "respects --no-deps option, in order, with --nodeps" {
86+
@test "respects the --no-deps option in the correct order" {
8787
test_util.mock_command do-plumbing-add-deps
8888
test_util.mock_command do-plumbing-link-bins
8989
test_util.mock_command do-plumbing-link-completions
9090
test_util.mock_command do-plumbing-link-man
9191

92-
mkdir package2
92+
mkdir 'package2'
9393

94-
run do-link --no-deps package2
94+
run do-link --no-deps 'package2'
9595

9696
assert_success
9797
assert_line -n 0 -e "Linking '/(.*)/bpm/cwd/package2'"
@@ -101,60 +101,59 @@ load 'util/init.sh'
101101
}
102102

103103

104-
@test "respects --no-deps option" {
104+
@test "respects the --no-deps option" {
105105
test_util.mock_command do-plumbing-add-deps
106106
test_util.mock_command do-plumbing-link-bins
107107
test_util.mock_command do-plumbing-link-completions
108108
test_util.mock_command do-plumbing-link-man
109109

110-
mkdir package2
110+
mkdir 'package2'
111111

112-
run do-link --no-deps package2
112+
run do-link --no-deps 'package2'
113113

114114
assert_success
115115
refute_line "do-plumbing-add-deps bpm-local/package2"
116116
}
117117

118-
@test "resolves current directory (dot) path" {
118+
@test "links the current directory" {
119119
test_util.mock_command do-plumbing-add-deps
120120
test_util.mock_command do-plumbing-link-bins
121121
test_util.mock_command do-plumbing-link-completions
122122
test_util.mock_command do-plumbing-link-man
123123

124-
mkdir package3
125-
cd package3
124+
mkdir 'package3'
125+
cd 'package3'
126126

127127
run do-link .
128128

129129
assert_success
130-
assert [ "$(test_util.readlink "$BPM_PACKAGES_PATH/bpm-local/package3")" = "$(test_util.readlink "$PWD")" ]
130+
assert [ "$(readlink -f "$BPM_PACKAGES_PATH/bpm-local/package3")" = "$(readlink -f "$PWD")" ]
131131
}
132132

133-
@test "resolves parent directory (dotdot) path" {
133+
@test "links the parent directory" {
134134
test_util.mock_command do-plumbing-add-deps
135135
test_util.mock_command do-plumbing-link-bins
136136
test_util.mock_command do-plumbing-link-completions
137137
test_util.mock_command do-plumbing-link-man
138138

139-
mkdir package3
140-
cd package3
139+
mkdir -p 'sierra/tango'
140+
cd 'sierra/tango'
141141

142-
run do-link ../package3
142+
run do-link ..
143143

144144
assert_success
145-
assert [ "$(test_util.readlink "$BPM_PACKAGES_PATH/bpm-local/package3")" = "$(test_util.readlink "$PWD")" ]
145+
assert [ "$(readlink -f "$BPM_PACKAGES_PATH/bpm-local/sierra")" = "$(readlink -f "$PWD/..")" ]
146146
}
147147

148-
@test "resolves arbitrary complex relative path" {
148+
@test "links an arbitrary complex relative path" {
149149
test_util.mock_command do-plumbing-add-deps
150150
test_util.mock_command do-plumbing-link-bins
151151
test_util.mock_command do-plumbing-link-completions
152152
test_util.mock_command do-plumbing-link-man
153153

154-
mkdir package3
155-
154+
mkdir 'package3'
156155
run do-link ./package3/.././package3
157156

158157
assert_success
159-
assert [ "$(test_util.readlink "$BPM_PACKAGES_PATH/bpm-local/package3")" = "$(test_util.readlink "$PWD/package3")" ]
158+
assert [ "$(readlink -f "$BPM_PACKAGES_PATH/bpm-local/package3")" = "$(readlink -f "$PWD/package3")" ]
160159
}

tests/do-list.bats

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
load 'util/init.sh'
44

5-
@test "list installed packages" {
6-
create_package username/p1
7-
create_package username2/p2
8-
create_package username2/p3
9-
test_util.fake_clone username/p1
10-
test_util.fake_clone username2/p2
5+
@test "properly list for 2 installed packages" {
6+
create_package 'username/p1'
7+
create_package 'username2/p2'
8+
create_package 'username2/p3'
9+
test_util.fake_clone 'username/p1'
10+
test_util.fake_clone 'username2/p2'
1111

1212
run do-list
1313

@@ -17,7 +17,7 @@ load 'util/init.sh'
1717
refute_line "username2/p3"
1818
}
1919

20-
@test "displays nothing if there are no packages" {
20+
@test "properly list for no installed packages" {
2121
create_package username/p1
2222

2323
run do-list
@@ -26,7 +26,7 @@ load 'util/init.sh'
2626
assert_output ""
2727
}
2828

29-
@test "displays outdated packages" {
29+
@test "properly list outdated packages" {
3030
create_package username/outdated
3131
create_package username/uptodate
3232
test_util.fake_clone username/outdated

0 commit comments

Comments
 (0)