Skip to content

Commit

Permalink
Allows users to declare custom aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
nkakouros committed Nov 20, 2018
1 parent c0f6f7a commit c9bbf90
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 5 additions & 1 deletion go-core.bash
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
esac

if [email protected]_builtin 'aliases' --exists "$cmd"; then
eval "$cmd" "$@"
if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd " ]]; then
eval "$cmd" "$@"
else
"$cmd" "$@"
fi
return
fi

Expand Down
4 changes: 4 additions & 0 deletions libexec/aliases
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ if [[ "${GO_ALIAS_CMDS_EXTRA[*]}" != '' ]]; then
_GO_ALIAS_CMDS+=( "${GO_ALIAS_CMDS_EXTRA[@]}" )
fi

if [[ "${GO_ALIAS_EXPAND_CMDS[*]}" != '' ]]; then
_GO_ALIAS_CMDS+=( "${GO_ALIAS_EXPAND_CMDS[@]}" )
fi

readonly _GO_ALIAS_CMDS

[email protected]() {
Expand Down
23 changes: 17 additions & 6 deletions tests/aliases.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,33 @@ load environment
create_aliases_test_command_script() {
@go.create_test_go_script \
'declare -a GO_ALIAS_CMDS_EXTRA=("nvim")' \
"@go $@"
'declare -a GO_ALIAS_EXPAND_CMDS=("test_echo")' \
'shopt -s expand_aliases' \
'alias test_echo="echo -n test_string_19098e09818fad"' \
"@go \$@"
}

@test "$SUITE: with no arguments, list all aliases" {
@test "$SUITE: with no arguments, list all predefined aliases" {
run ./go aliases
assert_success
assert_line_equals 0 'awk' # first alias
assert_line_equals -1 'sed' # last alias
}

@test "$SUITE: list custom aliases if defined" {
create_aliases_test_command_script 'aliases'
run "$TEST_GO_SCRIPT" aliases
create_aliases_test_command_script
run "$TEST_GO_SCRIPT" 'aliases'
assert_success
assert_line_equals 0 'awk' # first alias
assert_line_equals -1 'nvim' # last alias
assert_line_equals -2 'nvim' # second to last alias
assert_line_equals -1 'test_echo' # last alias
}

@test "$SUITE: run expanded aliases if defined" {
create_aliases_test_command_script 'aliases'
run "$TEST_GO_SCRIPT" 'test_echo'
assert_success
assert_line_equals 0 'test_string_19098e09818fad'
}

@test "$SUITE: tab completions" {
Expand Down Expand Up @@ -112,7 +123,7 @@ create_aliases_test_command_script() {
@test "$SUITE: leave help generic for cd, pushd when using env function" {
# Setting _GO_CMD will trick the script into thinking the shell function is
# running it.

_GO_CMD='test-go' run ./go aliases --help cd
[ "$status" -eq '0' ]

Expand Down

0 comments on commit c9bbf90

Please sign in to comment.