Newer
Older
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
#!/usr/bin/env bash
# Copyright (C) 2015, Arpinum
#
# shebang-unit is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# shebang-unit is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# shebang-unit. If not, see http://www.gnu.org/licenses/lgpl.html.
# shebang-unit all in one source file
configuration__load() {
# yes/no representation used with shebang-unit parameters to activate
# stuff like colors
SBU_YES="yes"
SBU_NO="no"
# Colors for outputs
SBU_GREEN_COLOR_CODE="\\033[1;32m"
SBU_RED_COLOR_CODE="\\033[1;31m"
SBU_YELLOW_COLOR_CODE="\\033[1;33m"
SBU_DEFAULT_COLOR_CODE="\\e[0m"
# Functions coding coventions
SBU_GLOBAL_SETUP_FUNCTION_NAME="global_setup"
SBU_GLOBAL_TEARDOWN_FUNCTION_NAME="global_teardown"
SBU_SETUP_FUNCTION_NAME="setup"
SBU_TEARDOWN_FUNCTION_NAME="teardown"
SBU_FUNCTION_DECLARATION_REGEX="^[ ]*\(function\)\{0,1\}[ ]*\([A-Za-z0-9_-]\{1,\}\)[ ]*\(([ ]*)\)\{0,1\}[ ]*{"
SBU_PRIVATE_FUNCTION_NAME_REGEX="^_.*"
# Default configuration that can be modified with shebang-unit parameters
# For more information see shebang-unit usages
SBU_TEST_FILE_PATTERN="*_test.sh"
SBU_TEST_FUNCTION_PATTERN="*"
SBU_USE_COLORS="${SBU_YES}"
SBU_RANDOM_RUN="${SBU_NO}"
SBU_REPORTERS="simple"
SBU_JUNIT_REPORTER_OUTPUT_FILE="./junit_report.xml"
# Internal constants
SBU_SUCCESS_STATUS_CODE=0
SBU_FAILURE_STATUS_CODE=1
SBU_VALUE_SEPARATOR=","
SBU_TEMP_DIR="/tmp/.shebang-unit"
SBU_LAST_ASSERTION_MSG_KEY="last_assertion_message"
SBU_NO_RUN="${SBU_NO}"
SBU_STANDARD_FD=42
SBU_ERROR_FD=43
}
assertion__equal() {
if [[ "$1" != "$2" ]]; then
_assertion__failed "Actual: <$2>, expected: <$1>."
fi
}
assertion__different() {
if [[ "$1" == "$2" ]]; then
_assertion__failed "Both values are: <$1>."
fi
}
assertion__string_contains() {
if ! system__string_contains "$1" "$2"; then
_assertion__failed "String: <$1> does not contain: <$2>."
fi
}
assertion__string_does_not_contain() {
if system__string_contains "$1" "$2"; then
_assertion__failed "String: <$1> contains: <$2>."
fi
}
assertion__string_empty() {
if [[ -n "$1" ]]; then
_assertion__failed "String: <$1> is not empty."
fi
}
assertion__string_not_empty() {
if [[ -z "$1" ]]; then
_assertion__failed "The string is empty."
fi
}
assertion__array_contains() {
local element=$1
shift 1
if ! array__contains "${element}" "$@"; then
local array_as_string="$(system__pretty_print_array "$@")"
_assertion__failed \
"Array: <${array_as_string}> does not contain: <${element}>."
fi
}
assertion__array_does_not_contain() {
local element=$1
shift 1
if array__contains "${element}" "$@"; then
local array_as_string="$(system__pretty_print_array "$@")"
_assertion__failed \
"Array: <${array_as_string}> contains: <${element}>."
fi
}
assertion__status_code_is_success() {
if (( $1 != ${SBU_SUCCESS_STATUS_CODE} )); then
_assertion__failed \
"Status code is failure instead of success." "$2"
fi
}
assertion__status_code_is_failure() {
if (( $1 == ${SBU_SUCCESS_STATUS_CODE} )); then
_assertion__failed \
"Status code is success instead of failure." "$2"
fi
}
assertion__successful() {
"$@"
if (( $? != ${SBU_SUCCESS_STATUS_CODE} )); then
_assertion__failed "Command is failing instead of successful."
fi
}
assertion__failing() {
"$@"
if (( $? == ${SBU_SUCCESS_STATUS_CODE} )); then
_assertion__failed "Command is successful instead of failing."
fi
}
_assertion__failed() {
local message_to_use="$(_assertion__get_assertion_message_to_use "$1" "$2")"
system__print_line "Assertion failed. ${message_to_use}"
exit ${SBU_FAILURE_STATUS_CODE}
}
_assertion__get_assertion_message_to_use() {
local message=$1
local custom_messsage=$2
if [[ -n "${custom_messsage}" ]]; then
system__print "${message} ${custom_messsage}"
else
system__print "${message}"
fi
}
mock__make_function_do_nothing() {
mock__make_function_call "$1" ":"
}
mock__make_function_prints() {
local function=$1
local text=$2
eval "${function}() { printf "${text}"; }"
}
mock__make_function_call() {
local function_to_mock=$1
local function_to_call=$2
shift 2
eval "${function_to_mock}() { ${function_to_call} \"\$@\"; }"
}
runner__run_all_test_files() {
SBU_BASE_TEST_DIRECTORY=$1
reporter__test_files_start_running
timer__store_current_time "global_time"
results__test_files_start_running
_runner__run_all_test_files_with_pattern_in_directory "$1"
reporter__test_files_end_running "$(timer__get_time_elapsed "global_time")"
runner__tests_are_successful
}
_runner__run_all_test_files_with_pattern_in_directory() {
local file
local files
array__from_lines files <<< "$(_runner__get_test_files_in_directory "$1")"
for file in "${files[@]}"; do
file_runner__run_test_file "${file}"
done
}
_runner__get_test_files_in_directory() {
Loading
Loading full blame...