Bashara is a "framework" (using the word loosely) for testing Bash scripts that adhere to certain parameters.
You only need to download bashara.sh
and then use it per the help:
Create a new file, e.g. my_bash_script_test.sh
and put in the following.
#!/bin/bash
source "./bashara.sh"
script_under_test="../my_bash_script.sh"
# debug=1 # Uncomment to enable debug output
init; (
# expect_script 'to_call_original default_init" "and_print \"Boot\"'
# expect_script "to_call default_init"
# it should print help when bad arguments are given
# expect_script "to_take_arguments derp" 'and_print "Help.*Unexpected.*derp"'
); cleanup
expect_script 'to_print "Finished"'
-> OK
expect_script 'to_print "Finished"' 'and_print "nished"'
-> OK
expect_script 'to_print "Unfinito"'
-> FAIL
expect_script 'to_take_arguments one two "thr ee"' 'and_print "ARG 0 = one"' 'and_print "ARG 1 = two"' 'and_print "ARG 2 = thr ee"' 'and_print "Finished"'
-> OK
expect_script 'to_take_arguments test_function' 'and_call my_function'
-> OK
expect_script 'to_take_arguments test_function' 'and_call not_my_function'
-> FAIL
expect_script 'to_take_arguments test_function' 'and_call_original my_function' 'to_print "my_function executed"'
-> OK
expect_script 'to_take_arguments test_function' 'and_call_original my_function' 'to_print "my_function executed 1"'
-> OK
expect_script 'to_take_arguments test_function' 'and_print_to /tmp/test_stdout.KqT.txt'
-> OK
expect_script 'to_take_arguments test_stdin' 'and_read_stdin_from /tmp/test_stdin.2Aj.txt' 'and_print "STDIN:.*TESTING123"'
-> OK
init; new_tempfile; mytmp=$(get_tempfile); ( expect_script 'to_read_stdin_from "'$mytmp'" ); cleanup
-> OK
All of the expectations can start with either to_
or and_
for readability.
The to_call
and to_call_original
expectations inject code into your script by using trap
to find the first line with a statement (vs. comments or function definitions). This requires that any functions you want to spy on are defined before the first statement.
This is bad, and you wont be able to test/inject on my_function()
:
#!/bin/bash
echo "Hello"
my_function() {
echo "do stuff"
}
This is good:
#!/bin/bash
my_function() {
echo "do stuff"
}
echo "Hello"
Check out test/bashara_test.sh
for the self-testing, and test/dummy_script_test.sh
for live examples.