|
| 1 | +Test__MODULE_TEST_FILE="test.sh" |
| 2 | + |
| 3 | +################################################## |
| 4 | +# This function is an alias for `ash self:help`. |
| 5 | +# |
| 6 | +# @param $1: The name of the module to test |
| 7 | +################################################## |
| 8 | +Test__callable_main() { |
| 9 | + # Params |
| 10 | + local module_to_test="$1" |
| 11 | + |
| 12 | + # Get module directory |
| 13 | + local module_directory="$(Ash__find_module_directory "$module_to_test" "1")" |
| 14 | + if [[ "$module_directory" = "" ]]; then |
| 15 | + Logger__error "Module $module_to_test does not exist" |
| 16 | + return |
| 17 | + fi |
| 18 | + |
| 19 | + # Loading in name of module, as if Ash core loaded it |
| 20 | + Ash_load_callable_file "$module_to_test" |
| 21 | + Ash__import "$module_to_test" "1" |
| 22 | + |
| 23 | + # Load test file |
| 24 | + local test_file="$module_directory/$Test__MODULE_TEST_FILE" |
| 25 | + if [[ ! -f "$test_file" ]]; then |
| 26 | + Logger__error "There is no $Test__MODULE_TEST_FILE file in $module_to_test" |
| 27 | + return |
| 28 | + fi |
| 29 | + . $test_file |
| 30 | + |
| 31 | + # Loading in config |
| 32 | + local config="$module_directory/$Ash__CONFIG_FILENAME" |
| 33 | + if [[ ! -f "$config" ]]; then |
| 34 | + Logger__error "There is no $Ash__CONFIG_FILENAME file in $module_to_test" |
| 35 | + return |
| 36 | + fi |
| 37 | + eval $(YamlParse__parse "$config" "Test_module_config_") |
| 38 | + |
| 39 | + # Getting test prefix |
| 40 | + local test_prefix="$Test_module_config_test_prefix" |
| 41 | + local test_function_prefix="$test_prefix"__test_ |
| 42 | + |
| 43 | + # Loading tests |
| 44 | + tests=$(declare -F | grep "$test_function_prefix" | sed 's/declare\ -f\ //g') |
| 45 | + |
| 46 | + # Call all methods |
| 47 | + local success="$Ash__TRUE" |
| 48 | + local to_find="$test_prefix"__ |
| 49 | + for t in $tests |
| 50 | + do |
| 51 | + local test_name=$(echo "$t" | sed "s/$to_find//g") |
| 52 | + Logger__alert "Running $test_name... " -n |
| 53 | + |
| 54 | + test_output=$($t) # Can't make this local, as I need to capture the exit status! |
| 55 | + if [[ $? -eq 0 ]]; then |
| 56 | + Test__print_green_check |
| 57 | + else |
| 58 | + success="$Ash__FALSE" |
| 59 | + Test__print_red_x |
| 60 | + |
| 61 | + # Log output, if any |
| 62 | + if [[ "$test_output" != "" ]]; then |
| 63 | + echo -n " " |
| 64 | + Test__print_red_rightwards_arrow |
| 65 | + echo -n " " |
| 66 | + echo -ne '\033[1;31m' |
| 67 | + echo "${test_output}" |
| 68 | + echo -ne '\033[1;0m' |
| 69 | + fi |
| 70 | + fi |
| 71 | + done |
| 72 | + |
| 73 | + # Success |
| 74 | + if [[ "$success" = "$Ash__TRUE" ]]; then |
| 75 | + return 0 |
| 76 | + else |
| 77 | + return 1 |
| 78 | + fi |
| 79 | +} |
0 commit comments