1 |
commit: 1d87b2e68b3419297193d871d1449f85493ab464 |
2 |
Author: Matthias Schwarzott <zzam <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Jun 8 12:50:11 2011 +0000 |
4 |
Commit: Matthias Schwarzott <zzam <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Jun 8 13:12:04 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/udev-gentoo-scripts.git;a=commit;h=1d87b2e6 |
7 |
|
8 |
Imported shunit2-2.1.6 |
9 |
|
10 |
shunit2 homepage is http://code.google.com/p/shunit2/ |
11 |
|
12 |
--- |
13 |
test/run_test.sh | 124 +++++++ |
14 |
test/shunit2 | 1048 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 |
test/versions | 227 ++++++++++++ |
16 |
3 files changed, 1399 insertions(+), 0 deletions(-) |
17 |
|
18 |
diff --git a/test/run_test.sh b/test/run_test.sh |
19 |
new file mode 100755 |
20 |
index 0000000..983cb80 |
21 |
--- /dev/null |
22 |
+++ b/test/run_test.sh |
23 |
@@ -0,0 +1,124 @@ |
24 |
+#! /bin/sh |
25 |
+# $Id: shunit2_test.sh 322 2011-04-24 00:09:45Z kate.ward@×××××××××.com $ |
26 |
+# vim:et:ft=sh:sts=2:sw=2 |
27 |
+# |
28 |
+# Copyright 2008 Kate Ward. All Rights Reserved. |
29 |
+# Released under the LGPL (GNU Lesser General Public License) |
30 |
+# Author: kate.ward@×××××××××.com (Kate Ward) |
31 |
+# |
32 |
+# shUnit2 unit test suite runner. |
33 |
+# |
34 |
+# This script runs all the unit tests that can be found, and generates a nice |
35 |
+# report of the tests. |
36 |
+ |
37 |
+MY_NAME=`basename $0` |
38 |
+MY_PATH=`dirname $0` |
39 |
+ |
40 |
+PREFIX='test_' |
41 |
+SHELLS='/bin/sh /bin/bash /bin/dash' |
42 |
+TESTS='' |
43 |
+for test in ${PREFIX}[a-z]*.sh; do |
44 |
+ TESTS="${TESTS} ${test}" |
45 |
+done |
46 |
+ |
47 |
+# load common unit test functions |
48 |
+. ./versions |
49 |
+#. ./shunit2_test_helpers |
50 |
+ |
51 |
+usage() |
52 |
+{ |
53 |
+ echo "usage: ${MY_NAME} [-e key=val ...] [-s shell(s)] [-t test(s)]" |
54 |
+} |
55 |
+ |
56 |
+env='' |
57 |
+ |
58 |
+# process command line flags |
59 |
+while getopts 'e:hs:t:' opt; do |
60 |
+ case ${opt} in |
61 |
+ e) # set an environment variable |
62 |
+ key=`expr "${OPTARG}" : '\([^=]*\)='` |
63 |
+ val=`expr "${OPTARG}" : '[^=]*=\(.*\)'` |
64 |
+ if [ -z "${key}" -o -z "${val}" ]; then |
65 |
+ usage |
66 |
+ exit 1 |
67 |
+ fi |
68 |
+ eval "${key}='${val}'" |
69 |
+ export ${key} |
70 |
+ env="${env:+${env} }${key}" |
71 |
+ ;; |
72 |
+ h) usage; exit 0 ;; # output help |
73 |
+ s) shells=${OPTARG} ;; # list of shells to run |
74 |
+ t) tests=${OPTARG} ;; # list of tests to run |
75 |
+ *) usage; exit 1 ;; |
76 |
+ esac |
77 |
+done |
78 |
+shift `expr ${OPTIND} - 1` |
79 |
+ |
80 |
+# fill shells and/or tests |
81 |
+shells=${shells:-${SHELLS}} |
82 |
+tests=${tests:-${TESTS}} |
83 |
+ |
84 |
+# error checking |
85 |
+if [ -z "${tests}" ]; then |
86 |
+ th_error 'no tests found to run; exiting' |
87 |
+ exit 1 |
88 |
+fi |
89 |
+ |
90 |
+cat <<EOF |
91 |
+#------------------------------------------------------------------------------ |
92 |
+# System data |
93 |
+# |
94 |
+ |
95 |
+# test run info |
96 |
+shells: ${shells} |
97 |
+tests: ${tests} |
98 |
+EOF |
99 |
+for key in ${env}; do |
100 |
+ eval "echo \"${key}=\$${key}\"" |
101 |
+done |
102 |
+echo |
103 |
+ |
104 |
+# output system data |
105 |
+echo "# system info" |
106 |
+echo "$ date" |
107 |
+date |
108 |
+echo |
109 |
+ |
110 |
+echo "$ uname -mprsv" |
111 |
+uname -mprsv |
112 |
+ |
113 |
+# |
114 |
+# run tests |
115 |
+# |
116 |
+ |
117 |
+for shell in ${shells}; do |
118 |
+ echo |
119 |
+ |
120 |
+ # check for existance of shell |
121 |
+ if [ ! -x ${shell} ]; then |
122 |
+ th_warn "unable to run tests with the ${shell} shell" |
123 |
+ continue |
124 |
+ fi |
125 |
+ |
126 |
+ cat <<EOF |
127 |
+ |
128 |
+#------------------------------------------------------------------------------ |
129 |
+# Running the test suite with ${shell} |
130 |
+# |
131 |
+EOF |
132 |
+ |
133 |
+ SHUNIT_SHELL=${shell} # pass shell onto tests |
134 |
+ shell_name=`basename ${shell}` |
135 |
+ shell_version=`versions_shellVersion "${shell}"` |
136 |
+ |
137 |
+ echo "shell name: ${shell_name}" |
138 |
+ echo "shell version: ${shell_version}" |
139 |
+ |
140 |
+ # execute the tests |
141 |
+ for suite in ${tests}; do |
142 |
+ suiteName=`expr "${suite}" : "${PREFIX}\(.*\).sh"` |
143 |
+ echo |
144 |
+ echo "--- Executing the '${suiteName}' test suite ---" |
145 |
+ ( exec ${shell} ./${suite} 2>&1; ) |
146 |
+ done |
147 |
+done |
148 |
|
149 |
diff --git a/test/shunit2 b/test/shunit2 |
150 |
new file mode 100644 |
151 |
index 0000000..8862ffd |
152 |
--- /dev/null |
153 |
+++ b/test/shunit2 |
154 |
@@ -0,0 +1,1048 @@ |
155 |
+#! /bin/sh |
156 |
+# $Id: shunit2 335 2011-05-01 20:10:33Z kate.ward@×××××××××.com $ |
157 |
+# vim:et:ft=sh:sts=2:sw=2 |
158 |
+# |
159 |
+# Copyright 2008 Kate Ward. All Rights Reserved. |
160 |
+# Released under the LGPL (GNU Lesser General Public License) |
161 |
+# |
162 |
+# shUnit2 -- Unit testing framework for Unix shell scripts. |
163 |
+# http://code.google.com/p/shunit2/ |
164 |
+# |
165 |
+# Author: kate.ward@×××××××××.com (Kate Ward) |
166 |
+# |
167 |
+# shUnit2 is a xUnit based unit test framework for Bourne shell scripts. It is |
168 |
+# based on the popular JUnit unit testing framework for Java. |
169 |
+ |
170 |
+# return if shunit already loaded |
171 |
+[ -n "${SHUNIT_VERSION:-}" ] && exit 0 |
172 |
+ |
173 |
+SHUNIT_VERSION='2.1.6' |
174 |
+ |
175 |
+SHUNIT_TRUE=0 |
176 |
+SHUNIT_FALSE=1 |
177 |
+SHUNIT_ERROR=2 |
178 |
+ |
179 |
+# enable strict mode by default |
180 |
+SHUNIT_STRICT=${SHUNIT_STRICT:-${SHUNIT_TRUE}} |
181 |
+ |
182 |
+_shunit_warn() { echo "shunit2:WARN $@" >&2; } |
183 |
+_shunit_error() { echo "shunit2:ERROR $@" >&2; } |
184 |
+_shunit_fatal() { echo "shunit2:FATAL $@" >&2; exit ${SHUNIT_ERROR}; } |
185 |
+ |
186 |
+# specific shell checks |
187 |
+if [ -n "${ZSH_VERSION:-}" ]; then |
188 |
+ setopt |grep "^shwordsplit$" >/dev/null |
189 |
+ if [ $? -ne ${SHUNIT_TRUE} ]; then |
190 |
+ _shunit_fatal 'zsh shwordsplit option is required for proper operation' |
191 |
+ fi |
192 |
+ if [ -z "${SHUNIT_PARENT:-}" ]; then |
193 |
+ _shunit_fatal "zsh does not pass \$0 through properly. please declare \ |
194 |
+\"SHUNIT_PARENT=\$0\" before calling shUnit2" |
195 |
+ fi |
196 |
+fi |
197 |
+ |
198 |
+# |
199 |
+# constants |
200 |
+# |
201 |
+ |
202 |
+__SHUNIT_ASSERT_MSG_PREFIX='ASSERT:' |
203 |
+__SHUNIT_MODE_SOURCED='sourced' |
204 |
+__SHUNIT_MODE_STANDALONE='standalone' |
205 |
+__SHUNIT_PARENT=${SHUNIT_PARENT:-$0} |
206 |
+ |
207 |
+# set the constants readonly |
208 |
+shunit_constants_=`set |grep '^__SHUNIT_' |cut -d= -f1` |
209 |
+echo "${shunit_constants_}" |grep '^Binary file' >/dev/null && \ |
210 |
+ shunit_constants_=`set |grep -a '^__SHUNIT_' |cut -d= -f1` |
211 |
+for shunit_constant_ in ${shunit_constants_}; do |
212 |
+ shunit_ro_opts_='' |
213 |
+ case ${ZSH_VERSION:-} in |
214 |
+ '') ;; # this isn't zsh |
215 |
+ [123].*) ;; # early versions (1.x, 2.x, 3.x) |
216 |
+ *) shunit_ro_opts_='-g' ;; # all later versions. declare readonly globally |
217 |
+ esac |
218 |
+ readonly ${shunit_ro_opts_} ${shunit_constant_} |
219 |
+done |
220 |
+unset shunit_constant_ shunit_constants_ shunit_ro_opts_ |
221 |
+ |
222 |
+# variables |
223 |
+__shunit_lineno='' # line number of executed test |
224 |
+__shunit_mode=${__SHUNIT_MODE_SOURCED} # operating mode |
225 |
+__shunit_reportGenerated=${SHUNIT_FALSE} # is report generated |
226 |
+__shunit_script='' # filename of unittest script (standalone mode) |
227 |
+__shunit_skip=${SHUNIT_FALSE} # is skipping enabled |
228 |
+__shunit_suite='' # suite of tests to execute |
229 |
+ |
230 |
+# counts of tests |
231 |
+__shunit_testSuccess=${SHUNIT_TRUE} |
232 |
+__shunit_testsTotal=0 |
233 |
+__shunit_testsPassed=0 |
234 |
+__shunit_testsFailed=0 |
235 |
+ |
236 |
+# counts of asserts |
237 |
+__shunit_assertsTotal=0 |
238 |
+__shunit_assertsPassed=0 |
239 |
+__shunit_assertsFailed=0 |
240 |
+__shunit_assertsSkipped=0 |
241 |
+ |
242 |
+# macros |
243 |
+_SHUNIT_LINENO_='eval __shunit_lineno=""; if [ "${1:-}" = "--lineno" ]; then [ -n "$2" ] && __shunit_lineno="[$2] "; shift 2; fi' |
244 |
+ |
245 |
+#----------------------------------------------------------------------------- |
246 |
+# assert functions |
247 |
+# |
248 |
+ |
249 |
+# Assert that two values are equal to one another. |
250 |
+# |
251 |
+# Args: |
252 |
+# message: string: failure message [optional] |
253 |
+# expected: string: expected value |
254 |
+# actual: string: actual value |
255 |
+# Returns: |
256 |
+# integer: success (TRUE/FALSE/ERROR constant) |
257 |
+assertEquals() |
258 |
+{ |
259 |
+ ${_SHUNIT_LINENO_} |
260 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
261 |
+ _shunit_error "assertEquals() requires two or three arguments; $# given" |
262 |
+ _shunit_error "1: ${1:+$1} 2: ${2:+$2} 3: ${3:+$3}${4:+ 4: $4}" |
263 |
+ return ${SHUNIT_ERROR} |
264 |
+ fi |
265 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
266 |
+ |
267 |
+ shunit_message_=${__shunit_lineno} |
268 |
+ if [ $# -eq 3 ]; then |
269 |
+ shunit_message_="${shunit_message_}$1" |
270 |
+ shift |
271 |
+ fi |
272 |
+ shunit_expected_=$1 |
273 |
+ shunit_actual_=$2 |
274 |
+ |
275 |
+ shunit_return=${SHUNIT_TRUE} |
276 |
+ if [ "${shunit_expected_}" = "${shunit_actual_}" ]; then |
277 |
+ _shunit_assertPass |
278 |
+ else |
279 |
+ failNotEquals "${shunit_message_}" "${shunit_expected_}" "${shunit_actual_}" |
280 |
+ shunit_return=${SHUNIT_FALSE} |
281 |
+ fi |
282 |
+ |
283 |
+ unset shunit_message_ shunit_expected_ shunit_actual_ |
284 |
+ return ${shunit_return} |
285 |
+} |
286 |
+_ASSERT_EQUALS_='eval assertEquals --lineno "${LINENO:-}"' |
287 |
+ |
288 |
+# Assert that two values are not equal to one another. |
289 |
+# |
290 |
+# Args: |
291 |
+# message: string: failure message [optional] |
292 |
+# expected: string: expected value |
293 |
+# actual: string: actual value |
294 |
+# Returns: |
295 |
+# integer: success (TRUE/FALSE/ERROR constant) |
296 |
+assertNotEquals() |
297 |
+{ |
298 |
+ ${_SHUNIT_LINENO_} |
299 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
300 |
+ _shunit_error "assertNotEquals() requires two or three arguments; $# given" |
301 |
+ return ${SHUNIT_ERROR} |
302 |
+ fi |
303 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
304 |
+ |
305 |
+ shunit_message_=${__shunit_lineno} |
306 |
+ if [ $# -eq 3 ]; then |
307 |
+ shunit_message_="${shunit_message_}$1" |
308 |
+ shift |
309 |
+ fi |
310 |
+ shunit_expected_=$1 |
311 |
+ shunit_actual_=$2 |
312 |
+ |
313 |
+ shunit_return=${SHUNIT_TRUE} |
314 |
+ if [ "${shunit_expected_}" != "${shunit_actual_}" ]; then |
315 |
+ _shunit_assertPass |
316 |
+ else |
317 |
+ failSame "${shunit_message_}" "$@" |
318 |
+ shunit_return=${SHUNIT_FALSE} |
319 |
+ fi |
320 |
+ |
321 |
+ unset shunit_message_ shunit_expected_ shunit_actual_ |
322 |
+ return ${shunit_return} |
323 |
+} |
324 |
+_ASSERT_NOT_EQUALS_='eval assertNotEquals --lineno "${LINENO:-}"' |
325 |
+ |
326 |
+# Assert that a value is null (i.e. an empty string) |
327 |
+# |
328 |
+# Args: |
329 |
+# message: string: failure message [optional] |
330 |
+# actual: string: actual value |
331 |
+# Returns: |
332 |
+# integer: success (TRUE/FALSE/ERROR constant) |
333 |
+assertNull() |
334 |
+{ |
335 |
+ ${_SHUNIT_LINENO_} |
336 |
+ if [ $# -lt 1 -o $# -gt 2 ]; then |
337 |
+ _shunit_error "assertNull() requires one or two arguments; $# given" |
338 |
+ return ${SHUNIT_ERROR} |
339 |
+ fi |
340 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
341 |
+ |
342 |
+ shunit_message_=${__shunit_lineno} |
343 |
+ if [ $# -eq 2 ]; then |
344 |
+ shunit_message_="${shunit_message_}$1" |
345 |
+ shift |
346 |
+ fi |
347 |
+ assertTrue "${shunit_message_}" "[ -z '$1' ]" |
348 |
+ shunit_return=$? |
349 |
+ |
350 |
+ unset shunit_message_ |
351 |
+ return ${shunit_return} |
352 |
+} |
353 |
+_ASSERT_NULL_='eval assertNull --lineno "${LINENO:-}"' |
354 |
+ |
355 |
+# Assert that a value is not null (i.e. a non-empty string) |
356 |
+# |
357 |
+# Args: |
358 |
+# message: string: failure message [optional] |
359 |
+# actual: string: actual value |
360 |
+# Returns: |
361 |
+# integer: success (TRUE/FALSE/ERROR constant) |
362 |
+assertNotNull() |
363 |
+{ |
364 |
+ ${_SHUNIT_LINENO_} |
365 |
+ if [ $# -gt 2 ]; then # allowing 0 arguments as $1 might actually be null |
366 |
+ _shunit_error "assertNotNull() requires one or two arguments; $# given" |
367 |
+ return ${SHUNIT_ERROR} |
368 |
+ fi |
369 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
370 |
+ |
371 |
+ shunit_message_=${__shunit_lineno} |
372 |
+ if [ $# -eq 2 ]; then |
373 |
+ shunit_message_="${shunit_message_}$1" |
374 |
+ shift |
375 |
+ fi |
376 |
+ shunit_actual_=`_shunit_escapeCharactersInString "${1:-}"` |
377 |
+ test -n "${shunit_actual_}" |
378 |
+ assertTrue "${shunit_message_}" $? |
379 |
+ shunit_return=$? |
380 |
+ |
381 |
+ unset shunit_actual_ shunit_message_ |
382 |
+ return ${shunit_return} |
383 |
+} |
384 |
+_ASSERT_NOT_NULL_='eval assertNotNull --lineno "${LINENO:-}"' |
385 |
+ |
386 |
+# Assert that two values are the same (i.e. equal to one another). |
387 |
+# |
388 |
+# Args: |
389 |
+# message: string: failure message [optional] |
390 |
+# expected: string: expected value |
391 |
+# actual: string: actual value |
392 |
+# Returns: |
393 |
+# integer: success (TRUE/FALSE/ERROR constant) |
394 |
+assertSame() |
395 |
+{ |
396 |
+ ${_SHUNIT_LINENO_} |
397 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
398 |
+ _shunit_error "assertSame() requires two or three arguments; $# given" |
399 |
+ return ${SHUNIT_ERROR} |
400 |
+ fi |
401 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
402 |
+ |
403 |
+ shunit_message_=${__shunit_lineno} |
404 |
+ if [ $# -eq 3 ]; then |
405 |
+ shunit_message_="${shunit_message_}$1" |
406 |
+ shift |
407 |
+ fi |
408 |
+ assertEquals "${shunit_message_}" "$1" "$2" |
409 |
+ shunit_return=$? |
410 |
+ |
411 |
+ unset shunit_message_ |
412 |
+ return ${shunit_return} |
413 |
+} |
414 |
+_ASSERT_SAME_='eval assertSame --lineno "${LINENO:-}"' |
415 |
+ |
416 |
+# Assert that two values are not the same (i.e. not equal to one another). |
417 |
+# |
418 |
+# Args: |
419 |
+# message: string: failure message [optional] |
420 |
+# expected: string: expected value |
421 |
+# actual: string: actual value |
422 |
+# Returns: |
423 |
+# integer: success (TRUE/FALSE/ERROR constant) |
424 |
+assertNotSame() |
425 |
+{ |
426 |
+ ${_SHUNIT_LINENO_} |
427 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
428 |
+ _shunit_error "assertNotSame() requires two or three arguments; $# given" |
429 |
+ return ${SHUNIT_ERROR} |
430 |
+ fi |
431 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
432 |
+ |
433 |
+ shunit_message_=${__shunit_lineno} |
434 |
+ if [ $# -eq 3 ]; then |
435 |
+ shunit_message_="${shunit_message_:-}$1" |
436 |
+ shift |
437 |
+ fi |
438 |
+ assertNotEquals "${shunit_message_}" "$1" "$2" |
439 |
+ shunit_return=$? |
440 |
+ |
441 |
+ unset shunit_message_ |
442 |
+ return ${shunit_return} |
443 |
+} |
444 |
+_ASSERT_NOT_SAME_='eval assertNotSame --lineno "${LINENO:-}"' |
445 |
+ |
446 |
+# Assert that a value or shell test condition is true. |
447 |
+# |
448 |
+# In shell, a value of 0 is true and a non-zero value is false. Any integer |
449 |
+# value passed can thereby be tested. |
450 |
+# |
451 |
+# Shell supports much more complicated tests though, and a means to support |
452 |
+# them was needed. As such, this function tests that conditions are true or |
453 |
+# false through evaluation rather than just looking for a true or false. |
454 |
+# |
455 |
+# The following test will succeed: |
456 |
+# assertTrue 0 |
457 |
+# assertTrue "[ 34 -gt 23 ]" |
458 |
+# The folloing test will fail with a message: |
459 |
+# assertTrue 123 |
460 |
+# assertTrue "test failed" "[ -r '/non/existant/file' ]" |
461 |
+# |
462 |
+# Args: |
463 |
+# message: string: failure message [optional] |
464 |
+# condition: string: integer value or shell conditional statement |
465 |
+# Returns: |
466 |
+# integer: success (TRUE/FALSE/ERROR constant) |
467 |
+assertTrue() |
468 |
+{ |
469 |
+ ${_SHUNIT_LINENO_} |
470 |
+ if [ $# -gt 2 ]; then |
471 |
+ _shunit_error "assertTrue() takes one two arguments; $# given" |
472 |
+ return ${SHUNIT_ERROR} |
473 |
+ fi |
474 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
475 |
+ |
476 |
+ shunit_message_=${__shunit_lineno} |
477 |
+ if [ $# -eq 2 ]; then |
478 |
+ shunit_message_="${shunit_message_}$1" |
479 |
+ shift |
480 |
+ fi |
481 |
+ shunit_condition_=$1 |
482 |
+ |
483 |
+ # see if condition is an integer, i.e. a return value |
484 |
+ shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'` |
485 |
+ shunit_return=${SHUNIT_TRUE} |
486 |
+ if [ -z "${shunit_condition_}" ]; then |
487 |
+ # null condition |
488 |
+ shunit_return=${SHUNIT_FALSE} |
489 |
+ elif [ -n "${shunit_match_}" -a "${shunit_condition_}" = "${shunit_match_}" ] |
490 |
+ then |
491 |
+ # possible return value. treating 0 as true, and non-zero as false. |
492 |
+ [ ${shunit_condition_} -ne 0 ] && shunit_return=${SHUNIT_FALSE} |
493 |
+ else |
494 |
+ # (hopefully) a condition |
495 |
+ ( eval ${shunit_condition_} ) >/dev/null 2>&1 |
496 |
+ [ $? -ne 0 ] && shunit_return=${SHUNIT_FALSE} |
497 |
+ fi |
498 |
+ |
499 |
+ # record the test |
500 |
+ if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then |
501 |
+ _shunit_assertPass |
502 |
+ else |
503 |
+ _shunit_assertFail "${shunit_message_}" |
504 |
+ fi |
505 |
+ |
506 |
+ unset shunit_message_ shunit_condition_ shunit_match_ |
507 |
+ return ${shunit_return} |
508 |
+} |
509 |
+_ASSERT_TRUE_='eval assertTrue --lineno "${LINENO:-}"' |
510 |
+ |
511 |
+# Assert that a value or shell test condition is false. |
512 |
+# |
513 |
+# In shell, a value of 0 is true and a non-zero value is false. Any integer |
514 |
+# value passed can thereby be tested. |
515 |
+# |
516 |
+# Shell supports much more complicated tests though, and a means to support |
517 |
+# them was needed. As such, this function tests that conditions are true or |
518 |
+# false through evaluation rather than just looking for a true or false. |
519 |
+# |
520 |
+# The following test will succeed: |
521 |
+# assertFalse 1 |
522 |
+# assertFalse "[ 'apples' = 'oranges' ]" |
523 |
+# The folloing test will fail with a message: |
524 |
+# assertFalse 0 |
525 |
+# assertFalse "test failed" "[ 1 -eq 1 -a 2 -eq 2 ]" |
526 |
+# |
527 |
+# Args: |
528 |
+# message: string: failure message [optional] |
529 |
+# condition: string: integer value or shell conditional statement |
530 |
+# Returns: |
531 |
+# integer: success (TRUE/FALSE/ERROR constant) |
532 |
+assertFalse() |
533 |
+{ |
534 |
+ ${_SHUNIT_LINENO_} |
535 |
+ if [ $# -lt 1 -o $# -gt 2 ]; then |
536 |
+ _shunit_error "assertFalse() quires one or two arguments; $# given" |
537 |
+ return ${SHUNIT_ERROR} |
538 |
+ fi |
539 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
540 |
+ |
541 |
+ shunit_message_=${__shunit_lineno} |
542 |
+ if [ $# -eq 2 ]; then |
543 |
+ shunit_message_="${shunit_message_}$1" |
544 |
+ shift |
545 |
+ fi |
546 |
+ shunit_condition_=$1 |
547 |
+ |
548 |
+ # see if condition is an integer, i.e. a return value |
549 |
+ shunit_match_=`expr "${shunit_condition_}" : '\([0-9]*\)'` |
550 |
+ shunit_return=${SHUNIT_TRUE} |
551 |
+ if [ -z "${shunit_condition_}" ]; then |
552 |
+ # null condition |
553 |
+ shunit_return=${SHUNIT_FALSE} |
554 |
+ elif [ -n "${shunit_match_}" -a "${shunit_condition_}" = "${shunit_match_}" ] |
555 |
+ then |
556 |
+ # possible return value. treating 0 as true, and non-zero as false. |
557 |
+ [ ${shunit_condition_} -eq 0 ] && shunit_return=${SHUNIT_FALSE} |
558 |
+ else |
559 |
+ # (hopefully) a condition |
560 |
+ ( eval ${shunit_condition_} ) >/dev/null 2>&1 |
561 |
+ [ $? -eq 0 ] && shunit_return=${SHUNIT_FALSE} |
562 |
+ fi |
563 |
+ |
564 |
+ # record the test |
565 |
+ if [ ${shunit_return} -eq ${SHUNIT_TRUE} ]; then |
566 |
+ _shunit_assertPass |
567 |
+ else |
568 |
+ _shunit_assertFail "${shunit_message_}" |
569 |
+ fi |
570 |
+ |
571 |
+ unset shunit_message_ shunit_condition_ shunit_match_ |
572 |
+ return ${shunit_return} |
573 |
+} |
574 |
+_ASSERT_FALSE_='eval assertFalse --lineno "${LINENO:-}"' |
575 |
+ |
576 |
+#----------------------------------------------------------------------------- |
577 |
+# failure functions |
578 |
+# |
579 |
+ |
580 |
+# Records a test failure. |
581 |
+# |
582 |
+# Args: |
583 |
+# message: string: failure message [optional] |
584 |
+# Returns: |
585 |
+# integer: success (TRUE/FALSE/ERROR constant) |
586 |
+fail() |
587 |
+{ |
588 |
+ ${_SHUNIT_LINENO_} |
589 |
+ if [ $# -gt 1 ]; then |
590 |
+ _shunit_error "fail() requires zero or one arguments; $# given" |
591 |
+ return ${SHUNIT_ERROR} |
592 |
+ fi |
593 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
594 |
+ |
595 |
+ shunit_message_=${__shunit_lineno} |
596 |
+ if [ $# -eq 1 ]; then |
597 |
+ shunit_message_="${shunit_message_}$1" |
598 |
+ shift |
599 |
+ fi |
600 |
+ |
601 |
+ _shunit_assertFail "${shunit_message_}" |
602 |
+ |
603 |
+ unset shunit_message_ |
604 |
+ return ${SHUNIT_FALSE} |
605 |
+} |
606 |
+_FAIL_='eval fail --lineno "${LINENO:-}"' |
607 |
+ |
608 |
+# Records a test failure, stating two values were not equal. |
609 |
+# |
610 |
+# Args: |
611 |
+# message: string: failure message [optional] |
612 |
+# expected: string: expected value |
613 |
+# actual: string: actual value |
614 |
+# Returns: |
615 |
+# integer: success (TRUE/FALSE/ERROR constant) |
616 |
+failNotEquals() |
617 |
+{ |
618 |
+ ${_SHUNIT_LINENO_} |
619 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
620 |
+ _shunit_error "failNotEquals() requires one or two arguments; $# given" |
621 |
+ return ${SHUNIT_ERROR} |
622 |
+ fi |
623 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
624 |
+ |
625 |
+ shunit_message_=${__shunit_lineno} |
626 |
+ if [ $# -eq 3 ]; then |
627 |
+ shunit_message_="${shunit_message_}$1" |
628 |
+ shift |
629 |
+ fi |
630 |
+ shunit_expected_=$1 |
631 |
+ shunit_actual_=$2 |
632 |
+ |
633 |
+ _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected:<${shunit_expected_}> but was:<${shunit_actual_}>" |
634 |
+ |
635 |
+ unset shunit_message_ shunit_expected_ shunit_actual_ |
636 |
+ return ${SHUNIT_FALSE} |
637 |
+} |
638 |
+_FAIL_NOT_EQUALS_='eval failNotEquals --lineno "${LINENO:-}"' |
639 |
+ |
640 |
+# Records a test failure, stating two values should have been the same. |
641 |
+# |
642 |
+# Args: |
643 |
+# message: string: failure message [optional] |
644 |
+# expected: string: expected value |
645 |
+# actual: string: actual value |
646 |
+# Returns: |
647 |
+# integer: success (TRUE/FALSE/ERROR constant) |
648 |
+failSame() |
649 |
+{ |
650 |
+ ${_SHUNIT_LINENO_} |
651 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
652 |
+ _shunit_error "failSame() requires two or three arguments; $# given" |
653 |
+ return ${SHUNIT_ERROR} |
654 |
+ fi |
655 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
656 |
+ |
657 |
+ shunit_message_=${__shunit_lineno} |
658 |
+ if [ $# -eq 3 ]; then |
659 |
+ shunit_message_="${shunit_message_}$1" |
660 |
+ shift |
661 |
+ fi |
662 |
+ |
663 |
+ _shunit_assertFail "${shunit_message_:+${shunit_message_} }expected not same" |
664 |
+ |
665 |
+ unset shunit_message_ |
666 |
+ return ${SHUNIT_FALSE} |
667 |
+} |
668 |
+_FAIL_SAME_='eval failSame --lineno "${LINENO:-}"' |
669 |
+ |
670 |
+# Records a test failure, stating two values were not equal. |
671 |
+# |
672 |
+# This is functionally equivalent to calling failNotEquals(). |
673 |
+# |
674 |
+# Args: |
675 |
+# message: string: failure message [optional] |
676 |
+# expected: string: expected value |
677 |
+# actual: string: actual value |
678 |
+# Returns: |
679 |
+# integer: success (TRUE/FALSE/ERROR constant) |
680 |
+failNotSame() |
681 |
+{ |
682 |
+ ${_SHUNIT_LINENO_} |
683 |
+ if [ $# -lt 2 -o $# -gt 3 ]; then |
684 |
+ _shunit_error "failNotEquals() requires one or two arguments; $# given" |
685 |
+ return ${SHUNIT_ERROR} |
686 |
+ fi |
687 |
+ _shunit_shouldSkip && return ${SHUNIT_TRUE} |
688 |
+ |
689 |
+ shunit_message_=${__shunit_lineno} |
690 |
+ if [ $# -eq 3 ]; then |
691 |
+ shunit_message_="${shunit_message_}$1" |
692 |
+ shift |
693 |
+ fi |
694 |
+ failNotEquals "${shunit_message_}" "$1" "$2" |
695 |
+ shunit_return=$? |
696 |
+ |
697 |
+ unset shunit_message_ |
698 |
+ return ${shunit_return} |
699 |
+} |
700 |
+_FAIL_NOT_SAME_='eval failNotSame --lineno "${LINENO:-}"' |
701 |
+ |
702 |
+#----------------------------------------------------------------------------- |
703 |
+# skipping functions |
704 |
+# |
705 |
+ |
706 |
+# Force remaining assert and fail functions to be "skipped". |
707 |
+# |
708 |
+# This function forces the remaining assert and fail functions to be "skipped", |
709 |
+# i.e. they will have no effect. Each function skipped will be recorded so that |
710 |
+# the total of asserts and fails will not be altered. |
711 |
+# |
712 |
+# Args: |
713 |
+# None |
714 |
+startSkipping() |
715 |
+{ |
716 |
+ __shunit_skip=${SHUNIT_TRUE} |
717 |
+} |
718 |
+ |
719 |
+# Resume the normal recording behavior of assert and fail calls. |
720 |
+# |
721 |
+# Args: |
722 |
+# None |
723 |
+endSkipping() |
724 |
+{ |
725 |
+ __shunit_skip=${SHUNIT_FALSE} |
726 |
+} |
727 |
+ |
728 |
+# Returns the state of assert and fail call skipping. |
729 |
+# |
730 |
+# Args: |
731 |
+# None |
732 |
+# Returns: |
733 |
+# boolean: (TRUE/FALSE constant) |
734 |
+isSkipping() |
735 |
+{ |
736 |
+ return ${__shunit_skip} |
737 |
+} |
738 |
+ |
739 |
+#----------------------------------------------------------------------------- |
740 |
+# suite functions |
741 |
+# |
742 |
+ |
743 |
+# Stub. This function should contains all unit test calls to be made. |
744 |
+# |
745 |
+# DEPRECATED (as of 2.1.0) |
746 |
+# |
747 |
+# This function can be optionally overridden by the user in their test suite. |
748 |
+# |
749 |
+# If this function exists, it will be called when shunit2 is sourced. If it |
750 |
+# does not exist, shunit2 will search the parent script for all functions |
751 |
+# beginning with the word 'test', and they will be added dynamically to the |
752 |
+# test suite. |
753 |
+# |
754 |
+# This function should be overridden by the user in their unit test suite. |
755 |
+# Note: see _shunit_mktempFunc() for actual implementation |
756 |
+# |
757 |
+# Args: |
758 |
+# None |
759 |
+#suite() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
760 |
+ |
761 |
+# Adds a function name to the list of tests schedule for execution. |
762 |
+# |
763 |
+# This function should only be called from within the suite() function. |
764 |
+# |
765 |
+# Args: |
766 |
+# function: string: name of a function to add to current unit test suite |
767 |
+suite_addTest() |
768 |
+{ |
769 |
+ shunit_func_=${1:-} |
770 |
+ |
771 |
+ __shunit_suite="${__shunit_suite:+${__shunit_suite} }${shunit_func_}" |
772 |
+ __shunit_testsTotal=`expr ${__shunit_testsTotal} + 1` |
773 |
+ |
774 |
+ unset shunit_func_ |
775 |
+} |
776 |
+ |
777 |
+# Stub. This function will be called once before any tests are run. |
778 |
+# |
779 |
+# Common one-time environment preparation tasks shared by all tests can be |
780 |
+# defined here. |
781 |
+# |
782 |
+# This function should be overridden by the user in their unit test suite. |
783 |
+# Note: see _shunit_mktempFunc() for actual implementation |
784 |
+# |
785 |
+# Args: |
786 |
+# None |
787 |
+#oneTimeSetUp() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
788 |
+ |
789 |
+# Stub. This function will be called once after all tests are finished. |
790 |
+# |
791 |
+# Common one-time environment cleanup tasks shared by all tests can be defined |
792 |
+# here. |
793 |
+# |
794 |
+# This function should be overridden by the user in their unit test suite. |
795 |
+# Note: see _shunit_mktempFunc() for actual implementation |
796 |
+# |
797 |
+# Args: |
798 |
+# None |
799 |
+#oneTimeTearDown() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
800 |
+ |
801 |
+# Stub. This function will be called before each test is run. |
802 |
+# |
803 |
+# Common environment preparation tasks shared by all tests can be defined here. |
804 |
+# |
805 |
+# This function should be overridden by the user in their unit test suite. |
806 |
+# Note: see _shunit_mktempFunc() for actual implementation |
807 |
+# |
808 |
+# Args: |
809 |
+# None |
810 |
+#setUp() { :; } |
811 |
+ |
812 |
+# Note: see _shunit_mktempFunc() for actual implementation |
813 |
+# Stub. This function will be called after each test is run. |
814 |
+# |
815 |
+# Common environment cleanup tasks shared by all tests can be defined here. |
816 |
+# |
817 |
+# This function should be overridden by the user in their unit test suite. |
818 |
+# Note: see _shunit_mktempFunc() for actual implementation |
819 |
+# |
820 |
+# Args: |
821 |
+# None |
822 |
+#tearDown() { :; } # DO NOT UNCOMMENT THIS FUNCTION |
823 |
+ |
824 |
+#------------------------------------------------------------------------------ |
825 |
+# internal shUnit2 functions |
826 |
+# |
827 |
+ |
828 |
+# Create a temporary directory to store various run-time files in. |
829 |
+# |
830 |
+# This function is a cross-platform temporary directory creation tool. Not all |
831 |
+# OSes have the mktemp function, so one is included here. |
832 |
+# |
833 |
+# Args: |
834 |
+# None |
835 |
+# Outputs: |
836 |
+# string: the temporary directory that was created |
837 |
+_shunit_mktempDir() |
838 |
+{ |
839 |
+ # try the standard mktemp function |
840 |
+ ( exec mktemp -dqt shunit.XXXXXX 2>/dev/null ) && return |
841 |
+ |
842 |
+ # the standard mktemp didn't work. doing our own. |
843 |
+ if [ -r '/dev/urandom' -a -x '/usr/bin/od' ]; then |
844 |
+ _shunit_random_=`/usr/bin/od -vAn -N4 -tx4 </dev/urandom \ |
845 |
+ |sed 's/^[^0-9a-f]*//'` |
846 |
+ elif [ -n "${RANDOM:-}" ]; then |
847 |
+ # $RANDOM works |
848 |
+ _shunit_random_=${RANDOM}${RANDOM}${RANDOM}$$ |
849 |
+ else |
850 |
+ # $RANDOM doesn't work |
851 |
+ _shunit_date_=`date '+%Y%m%d%H%M%S'` |
852 |
+ _shunit_random_=`expr ${_shunit_date_} / $$` |
853 |
+ fi |
854 |
+ |
855 |
+ _shunit_tmpDir_="${TMPDIR:-/tmp}/shunit.${_shunit_random_}" |
856 |
+ ( umask 077 && mkdir "${_shunit_tmpDir_}" ) || \ |
857 |
+ _shunit_fatal 'could not create temporary directory! exiting' |
858 |
+ |
859 |
+ echo ${_shunit_tmpDir_} |
860 |
+ unset _shunit_date_ _shunit_random_ _shunit_tmpDir_ |
861 |
+} |
862 |
+ |
863 |
+# This function is here to work around issues in Cygwin. |
864 |
+# |
865 |
+# Args: |
866 |
+# None |
867 |
+_shunit_mktempFunc() |
868 |
+{ |
869 |
+ for _shunit_func_ in oneTimeSetUp oneTimeTearDown setUp tearDown suite noexec |
870 |
+ do |
871 |
+ _shunit_file_="${__shunit_tmpDir}/${_shunit_func_}" |
872 |
+ cat <<EOF >"${_shunit_file_}" |
873 |
+#! /bin/sh |
874 |
+exit ${SHUNIT_TRUE} |
875 |
+EOF |
876 |
+ chmod +x "${_shunit_file_}" |
877 |
+ done |
878 |
+ |
879 |
+ unset _shunit_file_ |
880 |
+} |
881 |
+ |
882 |
+# Final cleanup function to leave things as we found them. |
883 |
+# |
884 |
+# Besides removing the temporary directory, this function is in charge of the |
885 |
+# final exit code of the unit test. The exit code is based on how the script |
886 |
+# was ended (e.g. normal exit, or via Ctrl-C). |
887 |
+# |
888 |
+# Args: |
889 |
+# name: string: name of the trap called (specified when trap defined) |
890 |
+_shunit_cleanup() |
891 |
+{ |
892 |
+ _shunit_name_=$1 |
893 |
+ |
894 |
+ case ${_shunit_name_} in |
895 |
+ EXIT) _shunit_signal_=0 ;; |
896 |
+ INT) _shunit_signal_=2 ;; |
897 |
+ TERM) _shunit_signal_=15 ;; |
898 |
+ *) |
899 |
+ _shunit_warn "unrecognized trap value (${_shunit_name_})" |
900 |
+ _shunit_signal_=0 |
901 |
+ ;; |
902 |
+ esac |
903 |
+ |
904 |
+ # do our work |
905 |
+ rm -fr "${__shunit_tmpDir}" |
906 |
+ |
907 |
+ # exit for all non-EXIT signals |
908 |
+ if [ ${_shunit_name_} != 'EXIT' ]; then |
909 |
+ _shunit_warn "trapped and now handling the (${_shunit_name_}) signal" |
910 |
+ # disable EXIT trap |
911 |
+ trap 0 |
912 |
+ # add 128 to signal and exit |
913 |
+ exit `expr ${_shunit_signal_} + 128` |
914 |
+ elif [ ${__shunit_reportGenerated} -eq ${SHUNIT_FALSE} ] ; then |
915 |
+ _shunit_assertFail 'Unknown failure encountered running a test' |
916 |
+ _shunit_generateReport |
917 |
+ exit ${SHUNIT_ERROR} |
918 |
+ fi |
919 |
+ |
920 |
+ unset _shunit_name_ _shunit_signal_ |
921 |
+} |
922 |
+ |
923 |
+# The actual running of the tests happens here. |
924 |
+# |
925 |
+# Args: |
926 |
+# None |
927 |
+_shunit_execSuite() |
928 |
+{ |
929 |
+ for _shunit_test_ in ${__shunit_suite}; do |
930 |
+ __shunit_testSuccess=${SHUNIT_TRUE} |
931 |
+ |
932 |
+ # disable skipping |
933 |
+ endSkipping |
934 |
+ |
935 |
+ # execute the per-test setup function |
936 |
+ setUp |
937 |
+ |
938 |
+ # execute the test |
939 |
+ echo "${_shunit_test_}" |
940 |
+ eval ${_shunit_test_} |
941 |
+ |
942 |
+ # execute the per-test tear-down function |
943 |
+ tearDown |
944 |
+ |
945 |
+ # update stats |
946 |
+ if [ ${__shunit_testSuccess} -eq ${SHUNIT_TRUE} ]; then |
947 |
+ __shunit_testsPassed=`expr ${__shunit_testsPassed} + 1` |
948 |
+ else |
949 |
+ __shunit_testsFailed=`expr ${__shunit_testsFailed} + 1` |
950 |
+ fi |
951 |
+ done |
952 |
+ |
953 |
+ unset _shunit_test_ |
954 |
+} |
955 |
+ |
956 |
+# Generates the user friendly report with appropriate OK/FAILED message. |
957 |
+# |
958 |
+# Args: |
959 |
+# None |
960 |
+# Output: |
961 |
+# string: the report of successful and failed tests, as well as totals. |
962 |
+_shunit_generateReport() |
963 |
+{ |
964 |
+ _shunit_ok_=${SHUNIT_TRUE} |
965 |
+ |
966 |
+ # if no exit code was provided one, determine an appropriate one |
967 |
+ [ ${__shunit_testsFailed} -gt 0 \ |
968 |
+ -o ${__shunit_testSuccess} -eq ${SHUNIT_FALSE} ] \ |
969 |
+ && _shunit_ok_=${SHUNIT_FALSE} |
970 |
+ |
971 |
+ echo |
972 |
+ if [ ${__shunit_testsTotal} -eq 1 ]; then |
973 |
+ echo "Ran ${__shunit_testsTotal} test." |
974 |
+ else |
975 |
+ echo "Ran ${__shunit_testsTotal} tests." |
976 |
+ fi |
977 |
+ |
978 |
+ _shunit_failures_='' |
979 |
+ _shunit_skipped_='' |
980 |
+ [ ${__shunit_assertsFailed} -gt 0 ] \ |
981 |
+ && _shunit_failures_="failures=${__shunit_assertsFailed}" |
982 |
+ [ ${__shunit_assertsSkipped} -gt 0 ] \ |
983 |
+ && _shunit_skipped_="skipped=${__shunit_assertsSkipped}" |
984 |
+ |
985 |
+ if [ ${_shunit_ok_} -eq ${SHUNIT_TRUE} ]; then |
986 |
+ _shunit_msg_='OK' |
987 |
+ [ -n "${_shunit_skipped_}" ] \ |
988 |
+ && _shunit_msg_="${_shunit_msg_} (${_shunit_skipped_})" |
989 |
+ else |
990 |
+ _shunit_msg_="FAILED (${_shunit_failures_}" |
991 |
+ [ -n "${_shunit_skipped_}" ] \ |
992 |
+ && _shunit_msg_="${_shunit_msg_},${_shunit_skipped_}" |
993 |
+ _shunit_msg_="${_shunit_msg_})" |
994 |
+ fi |
995 |
+ |
996 |
+ echo |
997 |
+ echo ${_shunit_msg_} |
998 |
+ __shunit_reportGenerated=${SHUNIT_TRUE} |
999 |
+ |
1000 |
+ unset _shunit_failures_ _shunit_msg_ _shunit_ok_ _shunit_skipped_ |
1001 |
+} |
1002 |
+ |
1003 |
+# Test for whether a function should be skipped. |
1004 |
+# |
1005 |
+# Args: |
1006 |
+# None |
1007 |
+# Returns: |
1008 |
+# boolean: whether the test should be skipped (TRUE/FALSE constant) |
1009 |
+_shunit_shouldSkip() |
1010 |
+{ |
1011 |
+ [ ${__shunit_skip} -eq ${SHUNIT_FALSE} ] && return ${SHUNIT_FALSE} |
1012 |
+ _shunit_assertSkip |
1013 |
+} |
1014 |
+ |
1015 |
+# Records a successful test. |
1016 |
+# |
1017 |
+# Args: |
1018 |
+# None |
1019 |
+_shunit_assertPass() |
1020 |
+{ |
1021 |
+ __shunit_assertsPassed=`expr ${__shunit_assertsPassed} + 1` |
1022 |
+ __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` |
1023 |
+} |
1024 |
+ |
1025 |
+# Records a test failure. |
1026 |
+# |
1027 |
+# Args: |
1028 |
+# message: string: failure message to provide user |
1029 |
+_shunit_assertFail() |
1030 |
+{ |
1031 |
+ _shunit_msg_=$1 |
1032 |
+ |
1033 |
+ __shunit_testSuccess=${SHUNIT_FALSE} |
1034 |
+ __shunit_assertsFailed=`expr ${__shunit_assertsFailed} + 1` |
1035 |
+ __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` |
1036 |
+ echo "${__SHUNIT_ASSERT_MSG_PREFIX}${_shunit_msg_}" |
1037 |
+ |
1038 |
+ unset _shunit_msg_ |
1039 |
+} |
1040 |
+ |
1041 |
+# Records a skipped test. |
1042 |
+# |
1043 |
+# Args: |
1044 |
+# None |
1045 |
+_shunit_assertSkip() |
1046 |
+{ |
1047 |
+ __shunit_assertsSkipped=`expr ${__shunit_assertsSkipped} + 1` |
1048 |
+ __shunit_assertsTotal=`expr ${__shunit_assertsTotal} + 1` |
1049 |
+} |
1050 |
+ |
1051 |
+# Prepare a script filename for sourcing. |
1052 |
+# |
1053 |
+# Args: |
1054 |
+# script: string: path to a script to source |
1055 |
+# Returns: |
1056 |
+# string: filename prefixed with ./ (if necessary) |
1057 |
+_shunit_prepForSourcing() |
1058 |
+{ |
1059 |
+ _shunit_script_=$1 |
1060 |
+ case "${_shunit_script_}" in |
1061 |
+ /*|./*) echo "${_shunit_script_}" ;; |
1062 |
+ *) echo "./${_shunit_script_}" ;; |
1063 |
+ esac |
1064 |
+ unset _shunit_script_ |
1065 |
+} |
1066 |
+ |
1067 |
+# Escape a character in a string. |
1068 |
+# |
1069 |
+# Args: |
1070 |
+# c: string: unescaped character |
1071 |
+# s: string: to escape character in |
1072 |
+# Returns: |
1073 |
+# string: with escaped character(s) |
1074 |
+_shunit_escapeCharInStr() |
1075 |
+{ |
1076 |
+ [ -n "$2" ] || return # no point in doing work on an empty string |
1077 |
+ |
1078 |
+ # Note: using shorter variable names to prevent conflicts with |
1079 |
+ # _shunit_escapeCharactersInString(). |
1080 |
+ _shunit_c_=$1 |
1081 |
+ _shunit_s_=$2 |
1082 |
+ |
1083 |
+ |
1084 |
+ # escape the character |
1085 |
+ echo ''${_shunit_s_}'' |sed 's/\'${_shunit_c_}'/\\\'${_shunit_c_}'/g' |
1086 |
+ |
1087 |
+ unset _shunit_c_ _shunit_s_ |
1088 |
+} |
1089 |
+ |
1090 |
+# Escape a character in a string. |
1091 |
+# |
1092 |
+# Args: |
1093 |
+# str: string: to escape characters in |
1094 |
+# Returns: |
1095 |
+# string: with escaped character(s) |
1096 |
+_shunit_escapeCharactersInString() |
1097 |
+{ |
1098 |
+ [ -n "$1" ] || return # no point in doing work on an empty string |
1099 |
+ |
1100 |
+ _shunit_str_=$1 |
1101 |
+ |
1102 |
+ # Note: using longer variable names to prevent conflicts with |
1103 |
+ # _shunit_escapeCharInStr(). |
1104 |
+ for _shunit_char_ in '"' '$' "'" '`'; do |
1105 |
+ _shunit_str_=`_shunit_escapeCharInStr "${_shunit_char_}" "${_shunit_str_}"` |
1106 |
+ done |
1107 |
+ |
1108 |
+ echo "${_shunit_str_}" |
1109 |
+ unset _shunit_char_ _shunit_str_ |
1110 |
+} |
1111 |
+ |
1112 |
+# Extract list of functions to run tests against. |
1113 |
+# |
1114 |
+# Args: |
1115 |
+# script: string: name of script to extract functions from |
1116 |
+# Returns: |
1117 |
+# string: of function names |
1118 |
+_shunit_extractTestFunctions() |
1119 |
+{ |
1120 |
+ _shunit_script_=$1 |
1121 |
+ |
1122 |
+ # extract the lines with test function names, strip of anything besides the |
1123 |
+ # function name, and output everything on a single line. |
1124 |
+ _shunit_regex_='^[ ]*(function )*test[A-Za-z0-9_]* *\(\)' |
1125 |
+ egrep "${_shunit_regex_}" "${_shunit_script_}" \ |
1126 |
+ |sed 's/^[^A-Za-z0-9_]*//;s/^function //;s/\([A-Za-z0-9_]*\).*/\1/g' \ |
1127 |
+ |xargs |
1128 |
+ |
1129 |
+ unset _shunit_regex_ _shunit_script_ |
1130 |
+} |
1131 |
+ |
1132 |
+#------------------------------------------------------------------------------ |
1133 |
+# main |
1134 |
+# |
1135 |
+ |
1136 |
+# determine the operating mode |
1137 |
+if [ $# -eq 0 ]; then |
1138 |
+ __shunit_script=${__SHUNIT_PARENT} |
1139 |
+ __shunit_mode=${__SHUNIT_MODE_SOURCED} |
1140 |
+else |
1141 |
+ __shunit_script=$1 |
1142 |
+ [ -r "${__shunit_script}" ] || \ |
1143 |
+ _shunit_fatal "unable to read from ${__shunit_script}" |
1144 |
+ __shunit_mode=${__SHUNIT_MODE_STANDALONE} |
1145 |
+fi |
1146 |
+ |
1147 |
+# create a temporary storage location |
1148 |
+__shunit_tmpDir=`_shunit_mktempDir` |
1149 |
+ |
1150 |
+# provide a public temporary directory for unit test scripts |
1151 |
+# TODO(kward): document this |
1152 |
+SHUNIT_TMPDIR="${__shunit_tmpDir}/tmp" |
1153 |
+mkdir "${SHUNIT_TMPDIR}" |
1154 |
+ |
1155 |
+# setup traps to clean up after ourselves |
1156 |
+trap '_shunit_cleanup EXIT' 0 |
1157 |
+trap '_shunit_cleanup INT' 2 |
1158 |
+trap '_shunit_cleanup TERM' 15 |
1159 |
+ |
1160 |
+# create phantom functions to work around issues with Cygwin |
1161 |
+_shunit_mktempFunc |
1162 |
+PATH="${__shunit_tmpDir}:${PATH}" |
1163 |
+ |
1164 |
+# make sure phantom functions are executable. this will bite if /tmp (or the |
1165 |
+# current $TMPDIR) points to a path on a partition that was mounted with the |
1166 |
+# 'noexec' option. the noexec command was created with _shunit_mktempFunc(). |
1167 |
+noexec 2>/dev/null || _shunit_fatal \ |
1168 |
+ 'please declare TMPDIR with path on partition with exec permission' |
1169 |
+ |
1170 |
+# we must manually source the tests in standalone mode |
1171 |
+if [ "${__shunit_mode}" = "${__SHUNIT_MODE_STANDALONE}" ]; then |
1172 |
+ . "`_shunit_prepForSourcing \"${__shunit_script}\"`" |
1173 |
+fi |
1174 |
+ |
1175 |
+# execute the oneTimeSetUp function (if it exists) |
1176 |
+oneTimeSetUp |
1177 |
+ |
1178 |
+# execute the suite function defined in the parent test script |
1179 |
+# deprecated as of 2.1.0 |
1180 |
+suite |
1181 |
+ |
1182 |
+# if no suite function was defined, dynamically build a list of functions |
1183 |
+if [ -z "${__shunit_suite}" ]; then |
1184 |
+ shunit_funcs_=`_shunit_extractTestFunctions "${__shunit_script}"` |
1185 |
+ for shunit_func_ in ${shunit_funcs_}; do |
1186 |
+ suite_addTest ${shunit_func_} |
1187 |
+ done |
1188 |
+fi |
1189 |
+unset shunit_func_ shunit_funcs_ |
1190 |
+ |
1191 |
+# execute the tests |
1192 |
+_shunit_execSuite |
1193 |
+ |
1194 |
+# execute the oneTimeTearDown function (if it exists) |
1195 |
+oneTimeTearDown |
1196 |
+ |
1197 |
+# generate the report |
1198 |
+_shunit_generateReport |
1199 |
+ |
1200 |
+# that's it folks |
1201 |
+[ ${__shunit_testsFailed} -eq 0 ] |
1202 |
+exit $? |
1203 |
|
1204 |
diff --git a/test/versions b/test/versions |
1205 |
new file mode 100755 |
1206 |
index 0000000..b874d80 |
1207 |
--- /dev/null |
1208 |
+++ b/test/versions |
1209 |
@@ -0,0 +1,227 @@ |
1210 |
+#! /bin/sh |
1211 |
+# $Id: versions 100 2008-11-15 20:24:03Z kate.ward@×××××××××.com $ |
1212 |
+# vim:et:ft=sh:sts=2:sw=2 |
1213 |
+# |
1214 |
+# Copyright 2008 Kate Ward. All Rights Reserved. |
1215 |
+# Released under the LGPL (GNU Lesser General Public License) |
1216 |
+# |
1217 |
+# Author: kate.ward@×××××××××.com (Kate Ward) |
1218 |
+# |
1219 |
+# This library provides reusable functions that determine actual names and |
1220 |
+# versions of installed shells and the OS. The library can also be run as a |
1221 |
+# script if set execuatable. |
1222 |
+ |
1223 |
+ARGV0=`basename "$0"` |
1224 |
+LSB_RELEASE='/etc/lsb-release' |
1225 |
+VERSIONS_SHELLS="/bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/sh /bin/zsh" |
1226 |
+ |
1227 |
+TRUE=0 |
1228 |
+FALSE=1 |
1229 |
+ERROR=2 |
1230 |
+ |
1231 |
+__versions_haveStrings=${ERROR} |
1232 |
+ |
1233 |
+#------------------------------------------------------------------------------ |
1234 |
+# functions |
1235 |
+# |
1236 |
+ |
1237 |
+versions_osName() |
1238 |
+{ |
1239 |
+ os_name_='unrecognized' |
1240 |
+ os_system_=`uname -s` |
1241 |
+ case ${os_system_} in |
1242 |
+ CYGWIN_NT-*) os_name_='Cygwin' ;; |
1243 |
+ Darwin) os_name_='Mac OS X' ;; |
1244 |
+ FreeBSD) os_name_='FreeBSD' ;; |
1245 |
+ Linux) os_name_='Linux' ;; |
1246 |
+ SunOS) |
1247 |
+ if grep 'OpenSolaris' /etc/release >/dev/null; then |
1248 |
+ os_name_='OpenSolaris' |
1249 |
+ else |
1250 |
+ os_name_='Solaris' |
1251 |
+ fi |
1252 |
+ ;; |
1253 |
+ esac |
1254 |
+ echo ${os_name_} |
1255 |
+ unset os_name_ os_system_ |
1256 |
+} |
1257 |
+ |
1258 |
+versions_osVersion() |
1259 |
+{ |
1260 |
+ os_version_='unrecognized' |
1261 |
+ os_system_=`uname -s` |
1262 |
+ os_release_=`uname -r` |
1263 |
+ case ${os_system_} in |
1264 |
+ CYGWIN_NT-*) |
1265 |
+ os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]\.[0-9]*\).*'` |
1266 |
+ ;; |
1267 |
+ Darwin) |
1268 |
+ major_='10' |
1269 |
+ sub_=`echo ${os_release_} |sed 's/^[0-9]*\.\([0-9]*\)\.[0-9]*$/\1/'` |
1270 |
+ case ${os_release_} in |
1271 |
+ 8.*) minor_='4' ;; |
1272 |
+ 9.*) minor_='5' ;; |
1273 |
+ 10.*) minor_='6' ;; |
1274 |
+ *) minor_='X'; sub_='X' ;; |
1275 |
+ esac |
1276 |
+ os_version_="${major_}.${minor_}.${sub_}" |
1277 |
+ ;; |
1278 |
+ FreeBSD) |
1279 |
+ os_version_=`expr "${os_release_}" : '\([0-9]*\.[0-9]*\)-.*'` |
1280 |
+ ;; |
1281 |
+ Linux) |
1282 |
+ if [ -r "${LSB_RELEASE}" ]; then |
1283 |
+ if grep -q 'DISTRIB_ID=Ubuntu' "${LSB_RELEASE}"; then |
1284 |
+ os_version_=`cat "${LSB_RELEASE}" \ |
1285 |
+ |awk -F= '$1~/DISTRIB_DESCRIPTION/{print $2}' \ |
1286 |
+ |sed 's/"//g;s/ /-/g'` |
1287 |
+ fi |
1288 |
+ elif [ -r '/etc/redhat-release' ]; then |
1289 |
+ os_version_=`cat /etc/redhat-release` |
1290 |
+ fi |
1291 |
+ ;; |
1292 |
+ SunOS) |
1293 |
+ if grep 'OpenSolaris' /etc/release >/dev/null; then |
1294 |
+ os_version_=`grep 'OpenSolaris' /etc/release |awk '{print $2"("$3")"}'` |
1295 |
+ else |
1296 |
+ major_=`echo ${os_release_} |sed 's/[0-9]*\.\([0-9]*\)/\1/'` |
1297 |
+ minor_=`grep Solaris /etc/release |sed 's/[^u]*\(u[0-9]*\).*/\1/'` |
1298 |
+ os_version_="${major_}${minor_}" |
1299 |
+ fi |
1300 |
+ ;; |
1301 |
+ esac |
1302 |
+ echo ${os_version_} |
1303 |
+ unset os_name_ os_release_ os_version_ major_ minor_ sub_ |
1304 |
+} |
1305 |
+ |
1306 |
+versions_shellVersion() |
1307 |
+{ |
1308 |
+ shell_=$1 |
1309 |
+ |
1310 |
+ if [ ! -x "${shell_}" ]; then |
1311 |
+ echo 'not installed' |
1312 |
+ return |
1313 |
+ fi |
1314 |
+ |
1315 |
+ version_='' |
1316 |
+ case ${shell_} in |
1317 |
+ */sh) |
1318 |
+ # TODO(kward): fix this |
1319 |
+ ## this could be one of any number of shells. try until one fits. |
1320 |
+ #version_=`versions_shell_bash ${shell_}` |
1321 |
+ ## dash cannot be self determined yet |
1322 |
+ #[ -z "${version_}" ] && version_=`versions_shell_ksh ${shell_}` |
1323 |
+ ## pdksh is covered in versions_shell_ksh() |
1324 |
+ #[ -z "${version_}" ] && version_=`versions_shell_zsh ${shell_}` |
1325 |
+ ;; |
1326 |
+ */bash) version_=`versions_shell_bash ${shell_}` ;; |
1327 |
+ */dash) |
1328 |
+ # simply assuming Ubuntu Linux until somebody comes up with a better |
1329 |
+ # test. the following test will return an empty string if dash is not |
1330 |
+ # installed. |
1331 |
+ version_=`versions_shell_dash` |
1332 |
+ ;; |
1333 |
+ */ksh) version_=`versions_shell_ksh ${shell_}` ;; |
1334 |
+ */pdksh) version_=`versions_shell_pdksh ${shell_}` ;; |
1335 |
+ */zsh) version_=`versions_shell_zsh ${shell_}` ;; |
1336 |
+ *) version_='invalid' |
1337 |
+ esac |
1338 |
+ |
1339 |
+ echo ${version_:-unknown} |
1340 |
+ unset shell_ version_ |
1341 |
+} |
1342 |
+ |
1343 |
+versions_shell_bash() |
1344 |
+{ |
1345 |
+ $1 --version 2>&1 |grep 'GNU bash' |sed 's/.*version \([^ ]*\).*/\1/' |
1346 |
+} |
1347 |
+ |
1348 |
+versions_shell_dash() |
1349 |
+{ |
1350 |
+ eval dpkg >/dev/null 2>&1 |
1351 |
+ [ $? -eq 127 ] && return # return if dpkg not found |
1352 |
+ |
1353 |
+ dpkg -l |grep ' dash ' |awk '{print $3}' |
1354 |
+} |
1355 |
+ |
1356 |
+versions_shell_ksh() |
1357 |
+{ |
1358 |
+ versions_shell_=$1 |
1359 |
+ |
1360 |
+ # see if --version gives a result |
1361 |
+ versions_version_=`${versions_shell_} --version 2>&1 \ |
1362 |
+ |sed 's/.*\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\).*/\1/'` |
1363 |
+ |
1364 |
+ # --version didn't work... look into the binary |
1365 |
+ if [ $? -ne ${TRUE} ]; then |
1366 |
+ _versions_have_strings |
1367 |
+ versions_version_=`strings ${versions_shell_} 2>&1 \ |
1368 |
+ |grep Version \ |
1369 |
+ |sed 's/^.*Version \(.*\)$/\1/;s/ s+ \$$//;s/ /-/g'` |
1370 |
+ [ -z "${versions_version_}" ] \ |
1371 |
+ && versions_version_=`versions_shell_pdksh ${versions_shell_}` |
1372 |
+ fi |
1373 |
+ |
1374 |
+ echo ${versions_version_} |
1375 |
+ |
1376 |
+ unset versions_shell_ versions_version_ |
1377 |
+} |
1378 |
+ |
1379 |
+versions_shell_pdksh() |
1380 |
+{ |
1381 |
+ _versions_have_strings |
1382 |
+ strings $1 2>&1 \ |
1383 |
+ |grep 'PD KSH' \ |
1384 |
+ |sed -e 's/.*PD KSH \(.*\)/\1/;s/ /-/g' |
1385 |
+} |
1386 |
+ |
1387 |
+versions_shell_zsh() |
1388 |
+{ |
1389 |
+ versions_shell_=$1 |
1390 |
+ |
1391 |
+ versions_version_=`${versions_shell_} --version 2>&1 |awk '{print $2}'` |
1392 |
+ |
1393 |
+ if [ $? -ne ${TRUE} ]; then |
1394 |
+ versions_version_=`echo 'echo ${ZSH_VERSION}' |${versions_shell_}` |
1395 |
+ fi |
1396 |
+ |
1397 |
+ echo ${versions_version_} |
1398 |
+ |
1399 |
+ unset versions_shell_ versions_version_ |
1400 |
+} |
1401 |
+ |
1402 |
+# Determine if the 'strings' binary installed. |
1403 |
+_versions_have_strings() |
1404 |
+{ |
1405 |
+ [ ${__versions_haveStrings} -ne ${ERROR} ] && return |
1406 |
+ eval strings /dev/null >/dev/null 2>&1 |
1407 |
+ if [ $? -eq 0 ]; then |
1408 |
+ __versions_haveStrings=${TRUE} |
1409 |
+ else |
1410 |
+ echo 'WARN: strings not installed. try installing binutils?' >&2 |
1411 |
+ __versions_haveStrings=${FALSE} |
1412 |
+ fi |
1413 |
+} |
1414 |
+ |
1415 |
+#------------------------------------------------------------------------------ |
1416 |
+# main |
1417 |
+# |
1418 |
+ |
1419 |
+versions_main() |
1420 |
+{ |
1421 |
+ # treat unset variables as an error |
1422 |
+ set -u |
1423 |
+ |
1424 |
+ os_name=`versions_osName` |
1425 |
+ os_version=`versions_osVersion` |
1426 |
+ echo "os: ${os_name} version: ${os_version}" |
1427 |
+ |
1428 |
+ for shell in ${VERSIONS_SHELLS}; do |
1429 |
+ shell_version=`versions_shellVersion ${shell}` |
1430 |
+ echo "shell: ${shell} version: ${shell_version}" |
1431 |
+ done |
1432 |
+} |
1433 |
+ |
1434 |
+if [ "${ARGV0}" = 'versions' ]; then |
1435 |
+ versions_main "$@" |
1436 |
+fi |