Skip to content

Commit c9bbf90

Browse files
committed
Allows users to declare custom aliases
1 parent c0f6f7a commit c9bbf90

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

go-core.bash

+5-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
319319
esac
320320

321321
if [email protected]_builtin 'aliases' --exists "$cmd"; then
322-
eval "$cmd" "$@"
322+
if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd " ]]; then
323+
eval "$cmd" "$@"
324+
else
325+
"$cmd" "$@"
326+
fi
323327
return
324328
fi
325329

libexec/aliases

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ if [[ "${GO_ALIAS_CMDS_EXTRA[*]}" != '' ]]; then
2828
_GO_ALIAS_CMDS+=( "${GO_ALIAS_CMDS_EXTRA[@]}" )
2929
fi
3030

31+
if [[ "${GO_ALIAS_EXPAND_CMDS[*]}" != '' ]]; then
32+
_GO_ALIAS_CMDS+=( "${GO_ALIAS_EXPAND_CMDS[@]}" )
33+
fi
34+
3135
readonly _GO_ALIAS_CMDS
3236

3337

tests/aliases.bats

+17-6
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,33 @@ load environment
55
create_aliases_test_command_script() {
66
@go.create_test_go_script \
77
'declare -a GO_ALIAS_CMDS_EXTRA=("nvim")' \
8-
"@go $@"
8+
'declare -a GO_ALIAS_EXPAND_CMDS=("test_echo")' \
9+
'shopt -s expand_aliases' \
10+
'alias test_echo="echo -n test_string_19098e09818fad"' \
11+
"@go \$@"
912
}
1013

11-
@test "$SUITE: with no arguments, list all aliases" {
14+
@test "$SUITE: with no arguments, list all predefined aliases" {
1215
run ./go aliases
1316
assert_success
1417
assert_line_equals 0 'awk' # first alias
1518
assert_line_equals -1 'sed' # last alias
1619
}
1720

1821
@test "$SUITE: list custom aliases if defined" {
19-
create_aliases_test_command_script 'aliases'
20-
run "$TEST_GO_SCRIPT" aliases
22+
create_aliases_test_command_script
23+
run "$TEST_GO_SCRIPT" 'aliases'
2124
assert_success
2225
assert_line_equals 0 'awk' # first alias
23-
assert_line_equals -1 'nvim' # last alias
26+
assert_line_equals -2 'nvim' # second to last alias
27+
assert_line_equals -1 'test_echo' # last alias
28+
}
29+
30+
@test "$SUITE: run expanded aliases if defined" {
31+
create_aliases_test_command_script 'aliases'
32+
run "$TEST_GO_SCRIPT" 'test_echo'
33+
assert_success
34+
assert_line_equals 0 'test_string_19098e09818fad'
2435
}
2536

2637
@test "$SUITE: tab completions" {
@@ -112,7 +123,7 @@ create_aliases_test_command_script() {
112123
@test "$SUITE: leave help generic for cd, pushd when using env function" {
113124
# Setting _GO_CMD will trick the script into thinking the shell function is
114125
# running it.
115-
126+
116127
_GO_CMD='test-go' run ./go aliases --help cd
117128
[ "$status" -eq '0' ]
118129

0 commit comments

Comments
 (0)