Skip to content

Commit 6205207

Browse files
committed
feat: Add '--dry' subcommand to 'bpm-load'
1 parent fef60f3 commit 6205207

File tree

3 files changed

+70
-10
lines changed

3 files changed

+70
-10
lines changed

docs/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bash: z.sh: command not found
7070

7171
But it doesn't work - this is standard behavior. When looking for binaries, bpm _does_ look at the root directory, but only for shell scripts that are marked as _executable_ (`chmod +x z.sh`)
7272

73-
The authors of `z` did not mark the file as executable because they did not intend for you to execute the file - you are supposed to `source` it. This is why the `package-path` command exists:
73+
The authors of `z` did not mark the file as executable because they did not intend for you to execute the file - you are supposed to `source` it. This is why the `bpm-load` command exists:
7474

7575
```sh
7676
$ bpm --global package-path rupa/z

pkg/lib/cmd/bpm-load.sh

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ fi
1313
# @exitcode 1 Miscellaneous errors
1414
bpm-load() {
1515
local __bpm_flag_global='no'
16+
local __bpm_flag_dry='no'
17+
1618
for arg; do
1719
case "$arg" in
1820
--global|-g)
1921
__bpm_flag_global='yes'
2022
shift
2123
;;
24+
--dry)
25+
__bpm_flag_dry='yes'
26+
shift
27+
;;
2228
esac
2329
done
2430

@@ -88,13 +94,20 @@ bpm-load() {
8894
__bpm_bpm_load_restore_options
8995
return 3
9096
elif [ -e "$__bpm_full_path" ]; then
91-
# Ensure the error can be properly handled at the callsite, and not
92-
# bail here if errexit is set
93-
if ! source "$__bpm_full_path"; then
94-
return 2
97+
if [ "$__bpm_flag_dry" = yes ]; then
98+
printf '%s\n' "bpm-load: Would source file '$__bpm_full_path'"
99+
__bpm_bpm_load_restore_options
100+
return
101+
else
102+
# Ensure the error can be properly handled at the callsite, and not
103+
# bail here if errexit is set
104+
if ! source "$__bpm_full_path"; then
105+
__bpm_bpm_load_restore_options
106+
return 2
107+
fi
95108
fi
96109
else
97-
printf '%s\n' "bpm-load: Error: '$__bpm_full_path' does not exist"
110+
printf '%s\n' "bpm-load: Error: File '$__bpm_full_path' does not exist"
98111
__bpm_bpm_load_restore_options
99112
return 3
100113
fi
@@ -107,10 +120,17 @@ bpm-load() {
107120
if [ -f "$__bpm_full_path" ]; then
108121
__bpm_file_was_sourced='yes'
109122

110-
# Ensure the error can be properly handled at the callsite, and not
111-
# bail here if errexit is set
112-
if ! source "$__bpm_full_path"; then
113-
return 2
123+
if [ "$__bpm_flag_dry" = yes ]; then
124+
printf '%s\n' "bpm-load: Would source file '$__bpm_full_path'"
125+
__bpm_bpm_load_restore_options
126+
return
127+
else
128+
# Ensure the error can be properly handled at the callsite, and not
129+
# bail here if errexit is set
130+
if ! source "$__bpm_full_path"; then
131+
__bpm_bpm_load_restore_options
132+
return 2
133+
fi
114134
fi
115135
fi
116136
done

tests/bpm-load.bats

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

24+
@test "works without file argument in local mode" {
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 :)'" > 'load.bash'
33+
}; test_util.finish_pkg
34+
35+
echo "dependencies = ['file://$BPM_ORIGIN_DIR/$pkg']" > 'bpm.toml'
36+
BPM_MODE='local' do-add --all
37+
38+
source bpm-load
39+
BPM_MODE='local' run bpm-load "$pkg"
40+
41+
assert_success
42+
assert_output "it works :)"
43+
}
44+
45+
@test "works with --dry argument" {
46+
local site='github.com'
47+
local pkg="user/project2"
48+
49+
BPM_REPO_SOURCE="$BPM_TEST_REPO_ROOT/../source"
50+
BPM_CELLAR="$BPM_TEST_DIR/cellar"
51+
52+
test_util.setup_pkg "$pkg"; {
53+
echo "printf '%s\n' 'it works :)'" > 'load.bash'
54+
}; test_util.finish_pkg
55+
test_util.mock_add "$pkg"
56+
57+
source bpm-load
58+
run bpm-load --global --dry "$pkg"
59+
60+
assert_success
61+
assert_output -p "bpm-load: Would source file"
62+
}
63+
2464
@test "works with file argument" {
2565
local site='github.com'
2666
local pkg="user/project2"

0 commit comments

Comments
 (0)