Skip to content

Commit fcb11b4

Browse files
committed
Initial commit
0 parents  commit fcb11b4

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

ash_config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: test
2+
package: github.com/ash-shell/test
3+
default_alias: test
4+
callable_prefix: Test

callable.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

lib/util.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
##################################################
2+
# Returns a unicode 'HEAVY CHECK MARK'
3+
##################################################
4+
Test__print_green_check() {
5+
echo -ne '\033[0;32m'
6+
echo -e '\xe2\x9c\x94'
7+
echo -ne '\033[1;0m'
8+
}
9+
10+
##################################################
11+
# Returns a unicode 'HEAVY BALLOT X'
12+
##################################################
13+
Test__print_red_x() {
14+
echo -ne '\033[1;31m'
15+
echo -e "\xe2\x9c\x98"
16+
echo -ne '\033[1;0m'
17+
}
18+
19+
# RIGHTWARDS ARROW
20+
Test__print_red_rightwards_arrow() {
21+
echo -ne '\033[1;31m'
22+
echo -ne "\xe2\x86\x92"
23+
echo -ne '\033[1;0m'
24+
}

0 commit comments

Comments
 (0)