forked from raven-computing/project-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·225 lines (198 loc) · 6.07 KB
/
test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# Test script for the Project Init system.
USAGE="Usage: test.sh [options]";
HELP_TEXT=$(cat << EOS
Tests the Project Init system.
${USAGE}
Run this script to execute the Project Init test suite. You can check whether your Bash
interpreter is compatible, perform compatibility tests for the utility commands that
Project Init has as a dependency, and validate the functionality of the program.
If no arguments are given, the entire test suite is run in the previously mentioned order.
Options:
[--check-bash] Only check compatibility of the available Bash interpreter.
[-c|--compatibility] Only perform compatibility tests for Bash and dependent commands.
[-f|--functionality] [tests]
Only perform functionality tests. The optional argument [tests] can
specify a comma-separated list of test case names. If provided, only
those functionality test cases will be executed. If no argument is
provided to this option, then all available functionality test cases
are executed. For example, specifying '-f c_library' will only
execute the test provided by the 'tests/test_func_c_library.sh' file.
[--keep-output] Do not automatically remove files generated by functionality tests.
Subsequent runs will still remove any residue files prior to
the next execution.
[--lint] Perform static code analysis with a linter.
[--test-path] TEST_PATH
The path to the source root directory of the instance to test.
This option is used to instruct the testing facility to run tests for
an addons resource instead of the base resources. The mandatory option
argument must be the filesystem path to the addons resource directory.
[-?|--help] Show this help message.
EOS
)
# Arg flags
ARG_CHECK_BASH=false;
ARG_TEST_COMPAT=false;
ARG_TEST_FUNCT=false;
ARG_TEST_FUNCT_ARGS=();
ARG_KEEP_OUTPUT=false;
ARG_LINT=false;
ARG_TEST_PATH=false;
ARG_SHOW_HELP=false;
# Arg helper vars
arg_check_optarg=false;
arg_optarg_key="";
arg_optarg_required="";
# Parse all arguments given to this script
for arg in "$@"; do
if [[ $arg_check_optarg == true ]]; then
arg_check_optarg=false;
if [[ "$arg" != -* ]]; then
ARG_TEST_FUNCT_ARGS+=("${arg_optarg_key}${arg}");
arg_optarg_required="";
shift;
continue;
fi
fi
if [ -n "$arg_optarg_required" ]; then
echo "Missing required option argument '$arg_optarg_required'";
exit 1;
fi
case $arg in
--check-bash)
ARG_CHECK_BASH=true;
shift
;;
-c|--compatibility)
ARG_TEST_COMPAT=true;
shift
;;
-f|--functionality)
ARG_TEST_FUNCT=true;
arg_check_optarg=true;
arg_optarg_key="--filter=";
shift
;;
--keep-output)
ARG_KEEP_OUTPUT=true;
ARG_TEST_FUNCT_ARGS+=("--keep-output");
shift
;;
--lint)
ARG_LINT=true;
shift
;;
--test-path)
ARG_TEST_PATH=true;
arg_check_optarg=true;
arg_optarg_required="TEST_PATH";
arg_optarg_key="--test-path=";
shift
;;
-\?|--help)
ARG_SHOW_HELP=true;
shift
;;
*)
# Unknown Argument
echo "Unknown argument: '$arg'";
echo "$USAGE";
echo "";
echo "Run 'test.sh --help' for more information";
exit 1;
;;
esac
done
# Check if help is triggered
if [[ $ARG_SHOW_HELP == true ]]; then
echo "$HELP_TEXT";
exit 0;
fi
if [ -n "$arg_optarg_required" ]; then
echo "Missing required option argument '$arg_optarg_required'";
exit 1;
fi
# The test.sh might be called from an addon, in which case the current
# working directory might not actually be the expected Project Init base
# source root. Ensure the correct CWD is set
BASEPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)";
cd "$BASEPATH";
run_bash_compat=true;
run_compat=true;
run_funct=true;
if [[ $ARG_LINT == true ]]; then
if ! command -v "shellcheck" &> /dev/null; then
echo "Could not find command 'shellcheck'";
echo "Please make sure that ShellCheck is correctly installed";
echo "See https://github.com/koalaman/shellcheck#installing";
exit 1;
fi
files_to_lint=();
# Add library and implementation files.
files_to_lint+=("libinit.sh");
files_to_lint+=("libform.sh");
files_to_lint+=("initmain.sh");
files_to_lint+=("quickstart.sh");
shopt -s globstar;
# Add init.sh files.
for initfile in **/init.sh; do
# Do not attempt to lint example files from the 'addons' subdir.
if [[ "$initfile" != "addons/"* ]]; then
files_to_lint+=("$initfile");
fi
done
# Add all test script files.
for testfile in tests/*.sh; do
files_to_lint+=("$testfile");
done
echo "Performing static code analysis";
shellcheck --shell=bash "${files_to_lint[@]}";
sc_status=$?;
if (( sc_status == 0 )); then
echo "[OK] No issues found in source files";
fi
exit $sc_status;
fi
if [[ $ARG_CHECK_BASH == true ]]; then
if [[ $ARG_TEST_COMPAT == true || $ARG_TEST_FUNCT == true ]]; then
echo "Argument '--check-bash' cannot be used with '-c' or '-f'";
exit 1;
fi
run_compat=false;
run_funct=false;
fi
if [[ $ARG_TEST_COMPAT == true ]]; then
if [[ $ARG_TEST_FUNCT == true ]]; then
echo "Argument '-c' cannot be used with '-f'";
exit 1;
fi
run_funct=false;
fi
if [[ $ARG_TEST_FUNCT == true ]]; then
run_bash_compat=false;
run_compat=false;
fi
test_result=0;
if [[ $ARG_TEST_PATH == false ]]; then
if [[ $run_bash_compat == true ]]; then
bash "tests/bash_tests.sh";
test_result=$?;
fi
if [[ $run_compat == true ]]; then
if (( $test_result == 0 )); then
bash "tests/compatibility_tests.sh";
test_result=$?;
fi
fi
fi
if [[ $run_funct == true ]]; then
if (( $test_result == 0 )); then
ftest_args="";
if (( ${#ARG_TEST_FUNCT_ARGS[@]} > 0 )); then
ftest_args="${ARG_TEST_FUNCT_ARGS[@]}";
fi
bash "tests/functionality_tests.sh" $ftest_args;
test_result=$?;
fi
fi
exit $test_result;