Open Aludirk opened 7 years ago
If you use Bash 4, you can use builtin BASH_ALIASES (refer to man page for Bash) variable to emulate aliases;
Say that you want to use myAlias in test cases, then you can define a function as follows in your test helper script:
In test_helper.bash
myAlias() {
$( echo ${BASH_ALIASES[myAlias]} $@ )
}
In your test case:
load test_helper
@test "test" {
run myAlias hello world
[ $status -eq 0 ]
}
But if run command natively supports aliases, I think it would be much more great and convenient;
Why not just declare a function instead of an alias? They're more reliable and don't require escaping strings:
error_msg() {
error_msg_func "${BASH_SOURCE[0]}" "${LINENO}"
}
Of course, we all use functions. We implement all functionalities within functions, but
Also function names should avoid possible name collision with other command names: (So you may use long names or just use extra underscores...)
_core_func foo var baz hello world true false
This is good. But except one or two params, users may rarely use the rest of the params, which may happen in real world. Also, some params may be just for internal usage, which users don't have to know in the first place.
Also, making the users type extra characters (like underscores) for your function names might be annoying from the user's perspective.
One workaround is, we use aliases like so:
alias al='_core_func foo var baz hello world true false'
So users just type less characters and they don't have to focus on extra details until they seriously need to access those extra details.
Of course, you may use simpler function names and wrapper functions with less params to wrap your core functions, which may look a lot user-friendly.
But still, name collisions may happen when you stick to functions as your outermost interface.
Of course, you need to provide users a simple mechanism to replace the default alias names with whatever names suit them best.
Please, see my file search tool lf.sh for more detailed usage
Or refer to z which is a more renowned project which I referenced to build lf.sh
I have a function:
For convenient:
So I can easy to use:
and no need to type "${BASH_SOURCE[0]}" and ${LINENO} every time
When I try to test it:
I got
bats/libexec/bats-exec-test: line 58: error_msg: command not found
however, the following one is okay: