Skip to content

Commit fef60f3

Browse files
committed
fix: bpm-load prints better errors
1 parent 2717d99 commit fef60f3

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

pkg/lib/cmd/bpm-load.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/usr/bin/env bash
22

3-
# TODO: print error if sourcing and passing arguments
4-
# TODO: print error if not being sourced and no arguments are passed
3+
if [[ "${BASH_SOURCE[0]}" != "${0}" && $# -ne 0 ]]; then
4+
printf '%s\n' "bpm-load: Error: Incorrect usage. See documentation"
5+
return 1
6+
fi
57

68
# @description Source Bash packages to initialize any functions
79
# that they may want to provide in the global scope

tests/bpm-load.bats

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,58 @@ load 'util/init.sh'
2121
assert_output "it works :)"
2222
}
2323

24-
@test "properly restores options" {
25-
:
24+
@test "works with file argument" {
25+
local site='github.com'
26+
local pkg="user/project2"
27+
28+
BPM_REPO_SOURCE="$BPM_TEST_REPO_ROOT/../source"
29+
BPM_CELLAR="$BPM_TEST_DIR/cellar"
30+
31+
test_util.setup_pkg "$pkg"; {
32+
echo "printf '%s\n' 'it works :)'" > 'file.bash'
33+
}; test_util.finish_pkg
34+
test_util.mock_add "$pkg"
35+
36+
source bpm-load
37+
run bpm-load --global "$pkg" 'file.bash'
38+
39+
assert_success
40+
assert_output "it works :)"
41+
}
42+
43+
@test "errors if used incorrectly (soucing with arguments passed)" {
44+
local site='github.com'
45+
local pkg="user/project2"
46+
47+
BPM_REPO_SOURCE="$BPM_TEST_REPO_ROOT/../source"
48+
BPM_CELLAR="$BPM_TEST_DIR/cellar"
49+
50+
test_util.setup_pkg "$pkg"; {
51+
echo "printf '%s\n' 'it works :)'" > 'load.bash'
52+
}; test_util.finish_pkg
53+
test_util.mock_add "$pkg"
54+
55+
run source bpm-load --global "$pkg"
56+
57+
assert_failure
58+
assert_line -p "Incorrect usage. See documentation"
59+
}
60+
61+
@test "errors if used incorrectly (running function with no arguments passed)" {
62+
local site='github.com'
63+
local pkg="user/project2"
64+
65+
BPM_REPO_SOURCE="$BPM_TEST_REPO_ROOT/../source"
66+
BPM_CELLAR="$BPM_TEST_DIR/cellar"
67+
68+
test_util.setup_pkg "$pkg"; {
69+
echo "printf '%s\n' 'it works :)'" > 'load.bash'
70+
}; test_util.finish_pkg
71+
test_util.mock_add "$pkg"
72+
73+
source bpm-load
74+
run bpm-load
75+
76+
assert_failure
77+
assert_line -p "Error: Must pass in package name as first parameter"
2678
}

0 commit comments

Comments
 (0)