OLD | NEW |
(Empty) | |
| 1 # Adapted from |
| 2 # shfuncs : test suite common shell functions |
| 3 # which was shipped with the TET example code. |
| 4 |
| 5 . "$XDG_TEST_DIR/include/testfuncs.sh" |
| 6 |
| 7 ## NOTE: Documentation is generated AUTOMATICALLY from this file |
| 8 ## Function usage must immediately follow function delcaration |
| 9 |
| 10 assert_exit() { |
| 11 # execute command (saving output) and check exit code |
| 12 # Usage: assert_exit N command ... |
| 13 # where N is a number or a literal 'N' (for non-zero) |
| 14 # command can be an unquoted string, but be careful. |
| 15 EXPECT="$1" |
| 16 shift 1 |
| 17 |
| 18 # make sure nothing is hanging around from a prev. test. |
| 19 rm -f out.stdout out.stderr |
| 20 |
| 21 # $1 is command, $2 is expected exit code (0 or "N" for non-zero) |
| 22 ( "$@" > out.stdout 2> out.stderr ) |
| 23 CODE="$?" |
| 24 |
| 25 LASTCOMMAND="$*" |
| 26 |
| 27 if [ -z "$EXPECT" ]; then |
| 28 EXPECT=0; |
| 29 fi |
| 30 if [ "$EXPECT" = N -a "$CODE" -eq 0 ]; then |
| 31 test_fail "Command ($*) gave exit code $CODE, expected nonzero" |
| 32 elif [ "$EXPECT" != N ] && [ "$EXPECT" -ne "$CODE" ]; then |
| 33 test_fail "Command ($*) gave exit code $CODE, expected $EXPECT" |
| 34 fi |
| 35 } |
| 36 |
| 37 assert_interactive_notroot() { |
| 38 if [ `whoami` != 'root' ] ; then |
| 39 assert_interactive "$@" |
| 40 fi |
| 41 } |
| 42 |
| 43 assert_interactive() { |
| 44 # Useage: |
| 45 # assert_interactive {msg} [y|n|C|s varname] |
| 46 # |
| 47 # msg is the text to print. |
| 48 # y -> expect y for [y/n] |
| 49 # n -> expect n for [y/n] |
| 50 # s -> save y or n into varname. Then, (presumably) $varname can be |
| 51 # given in place of y or n in subsequent calls to assert_interactive |
| 52 # C -> cleanup msg. Always print "msg [enter to continue]" despite test failure. |
| 53 # if no argument is given after msg, print "msg [enter to continue]" |
| 54 |
| 55 query=$1 |
| 56 expect=$2 |
| 57 # It seems valuable to see what happens even if the test has failed. |
| 58 # (eg fail on stdout.) |
| 59 if [ "$TEST_STATUS" = 'FAIL' -a "$expect" != C ] ; then |
| 60 ## Don't waste user's time if test has already failed. |
| 61 test_infoline "Test has already failed. Not bothering to ask '$q
uery'" |
| 62 return |
| 63 fi |
| 64 |
| 65 if [ ! -z "$XDG_TEST_NO_INTERACTIVE" ] ; then |
| 66 test_infoline "Assumed '$query' is '$expect'" |
| 67 return |
| 68 fi |
| 69 |
| 70 if [ ! -z "$expect" -a "$expect" != C ] ; then |
| 71 if [ "$expect" != y -a "$expect" != n -a "$expect" != s -a "$exp
ect" != C ] ; then |
| 72 echo "TEST SYNTAX ERROR: interactive assertions require
one of (y,n,s,C) as choices. (found '$expect')" >&2 |
| 73 exit 255 |
| 74 fi |
| 75 unset result |
| 76 while [ "$result" != y -a "$result" != n ] ; do |
| 77 echo -ne "\n\t$query [y/n]: " >&2 |
| 78 read result |
| 79 done |
| 80 |
| 81 if [ "$expect" = s ] ; then |
| 82 if [ -z "$3" ] ; then |
| 83 echo "TEST SYNTAX ERROR: 's' requires a variable
name" |
| 84 exit 255 |
| 85 fi |
| 86 eval "$3=$result" |
| 87 elif [ "$result" != "$expect" ] ; then |
| 88 test_fail "User indicated '$result' instead of '$expect'
in response to '$query'" |
| 89 fi |
| 90 else |
| 91 echo -ne "\n\t$query [enter to continue] " >&2 |
| 92 read result |
| 93 fi |
| 94 } |
| 95 |
| 96 |
| 97 assert_file_in_path() { |
| 98 # Assert that some file is present in a path. |
| 99 # |
| 100 # Usage: |
| 101 # assert_file_in_path FILENAME PATH |
| 102 # where FILE is the exact name of the file and |
| 103 # PATH is a ':' separated list of directories |
| 104 search_dirs=`echo "$2" | tr ':' ' '` |
| 105 found_files=`find $search_dirs -name "$1" 2>/dev/null` |
| 106 |
| 107 if [ -z "$found_files" ] ; then |
| 108 test_fail "Did not find '$1' in '$2'" |
| 109 fi |
| 110 } |
| 111 |
| 112 assert_file_not_in_path() { |
| 113 # Assert the some file is NOT present in a path. |
| 114 # Opposite of 'assert_file_in_path' |
| 115 search_dirs=`echo "$2" | tr ':' ' '` |
| 116 found_files=`find $search_dirs -name "$1" 2>/dev/null` |
| 117 |
| 118 if [ ! -z "$found_files" ] ; then |
| 119 test_fail "Found '$found_files' in $2" |
| 120 fi |
| 121 } |
| 122 |
| 123 |
| 124 assert_file() { |
| 125 # Assert the existance of an exact filename |
| 126 # Usage: assert_file FILE |
| 127 if [ ! -e "$1" ] ; then |
| 128 test_fail "'$1' does not exist" |
| 129 return |
| 130 elif [ ! -f "$1" ] ; then |
| 131 test_fail "'$1' is not a regular file" |
| 132 return |
| 133 fi |
| 134 if [ -f "$2" ] ; then |
| 135 compare=`diff -wB "$1" "$2"` |
| 136 if [ ! -z "$compare" ] ; then |
| 137 test_fail "'$1' is different from '$2'. Diff is:\n$compa
re" |
| 138 fi |
| 139 fi |
| 140 } |
| 141 |
| 142 assert_nofile() { |
| 143 # Assert the non existance of an exact filename. |
| 144 # Opposite of 'assert_file' |
| 145 if [ -e "$1" ] ; then |
| 146 test_fail "'$1' exists." |
| 147 fi |
| 148 } |
| 149 |
| 150 |
| 151 assert_nostdout() { |
| 152 # assert nothing was written to a stdout. |
| 153 # NOTE: Failing this assertion will WARN rather than FAIL |
| 154 if [ -s out.stdout ] |
| 155 then |
| 156 test_infoline "Unexpected output from '$LASTCOMMAND' written to stdout,
as shown below:" |
| 157 infofile out.stdout stdout: |
| 158 if [ "$TEST_STATUS" = "PASS" ]; then |
| 159 test_status WARN |
| 160 fi |
| 161 fi |
| 162 } |
| 163 |
| 164 assert_nostderr() { |
| 165 # assert nothing was written to stderr. |
| 166 # NOTE: Failing this assertion will WARN rather than FAIL |
| 167 if [ -n "$XDG_UTILS_DEBUG_LEVEL" ] ; then |
| 168 if [ -s out.stderr ] ; then |
| 169 infofile out.stderr debug: |
| 170 fi |
| 171 elif [ -s out.stderr ] ; then |
| 172 test_infoline "Unexpected output from '$LASTCOMMAND' written to stderr,
as shown below:" |
| 173 infofile out.stderr stderr: |
| 174 if [ "$TEST_STATUS" = "PASS" ]; then |
| 175 test_status WARN |
| 176 fi |
| 177 fi |
| 178 } |
| 179 |
| 180 assert_stderr() { |
| 181 # check that stderr matches expected error |
| 182 # $1 is file containing regexp for expected error |
| 183 # if no argument supplied, just check out.stderr is not empty |
| 184 |
| 185 if [ ! -s out.stderr ] |
| 186 then |
| 187 test_infoline "Expected output from '$LASTCOMMAND' to stderr, but no
ne written" |
| 188 test_fail |
| 189 return |
| 190 fi |
| 191 if [ ! -z "$1" ] ; then |
| 192 expfile="$1" |
| 193 OK=Y |
| 194 exec 4<&0 0< "$expfile" 3< out.stderr |
| 195 while read expline |
| 196 do |
| 197 if read line <&3 |
| 198 then |
| 199 if expr "$line" : "$expline" > /dev/null |
| 200 then |
| 201 : |
| 202 else |
| 203 OK=N |
| 204 break |
| 205 fi |
| 206 else |
| 207 OK=N |
| 208 fi |
| 209 done |
| 210 exec 0<&4 3<&- 4<&- |
| 211 if [ "$OK" = N ] |
| 212 then |
| 213 test_infoline "Incorrect output from '$LASTCOMMAND' written to stder
r, as shown below" |
| 214 infofile "$expfile" "expected stderr:" |
| 215 infofile out.stderr "received stderr:" |
| 216 test_fail |
| 217 fi |
| 218 fi |
| 219 } |
| 220 |
| 221 assert_stdout() { |
| 222 # check that stderr matches expected error |
| 223 # $1 is file containing regexp for expected error |
| 224 # if no argument supplied, just check out.stderr is not empty |
| 225 |
| 226 if [ ! -s out.stdout ] |
| 227 then |
| 228 test_infoline "Expected output from '$LASTCOMMAND' to stdout, but no
ne written" |
| 229 test_fail |
| 230 return |
| 231 fi |
| 232 if [ ! -z "$1" ] ; then |
| 233 expfile="$1" |
| 234 |
| 235 if [ ! -e "$expfile" ] ; then |
| 236 test_status NORESULT "Could not find file '$expfile' to look up
expected pattern!" |
| 237 return |
| 238 fi |
| 239 OK=Y |
| 240 exec 4<&0 0< "$expfile" 3< out.stdout |
| 241 while read expline |
| 242 do |
| 243 if read line <&3 |
| 244 then |
| 245 if expr "$line" : "$expline" > /dev/null |
| 246 then |
| 247 : |
| 248 else |
| 249 OK=N |
| 250 break |
| 251 fi |
| 252 else |
| 253 OK=N |
| 254 fi |
| 255 done |
| 256 exec 0<&4 3<&- 4<&- |
| 257 if [ "$OK" = N ] |
| 258 then |
| 259 test_infoline "Incorrect output from '$LASTCOMMAND' written to stdou
t, as shown below" |
| 260 infofile "$expfile" "expected stdout:" |
| 261 infofile out.stdout "received stdout:" |
| 262 test_fail |
| 263 fi |
| 264 fi |
| 265 } |
| 266 |
| 267 require_interactive() { |
| 268 # if $XDG_TEST_NO_INTERACTIVE is set, test result becomes UNTESTED |
| 269 if [ ! -z "$XDG_TEST_NO_INTERACTIVE" ] ; then |
| 270 test_result UNTESTED "XDG_TEST_NO_INTERACTIVE is set, but this test need
s interactive" |
| 271 fi |
| 272 } |
| 273 |
| 274 require_root() { |
| 275 # if the test is not being run as root, test result is UNTESTED |
| 276 if [ `whoami` != 'root' ] ; then |
| 277 test_result UNTESTED "not running as root, but test requires root privil
eges" |
| 278 fi |
| 279 } |
| 280 |
| 281 require_notroot() { |
| 282 # if the test is being run as root, the test result is UNTESTED |
| 283 # opposite of 'require_root' |
| 284 if [ `whoami` = 'root' ] ; then |
| 285 test_result UNTESTED "running as root, but test must be run as a normal
user" |
| 286 fi |
| 287 } |
| 288 |
| 289 set_no_display() { |
| 290 # Clear $DISPLAY |
| 291 unset DISPLAY |
| 292 } |
| 293 |
| 294 assert_display() { |
| 295 # Assert that the $DISPLAY variable is set. |
| 296 if [ -z "$DISPLAY" ] ; then |
| 297 test_fail "DISPLAY not set!" |
| 298 fi |
| 299 } |
| 300 |
| 301 assert_util_var() { |
| 302 # Assert that the $XDGUTIL varilable is set. |
| 303 # DEPRICATED. Only used by generic tests. |
| 304 if [ "x$XDGUTIL" = x ]; then |
| 305 test_fail "XDGUTIL variable not set" |
| 306 fi |
| 307 } |
| 308 |
| 309 use_file() { |
| 310 # Copy a datafile from it's defult location into the test directory |
| 311 # Usage: |
| 312 # use_file ORIG_FILE VAR |
| 313 # Where ORIG_FILE is the name of the file, and VAR is the name of the |
| 314 # variable to create that will contain the new (unique) filename. |
| 315 # DO NOT put a '$' in front of VAR. VAR will be created via eval. |
| 316 # |
| 317 # $VAR will be set to 'xdgtestdata-$XDG_TEST_ID-$file' after the |
| 318 # directory is stripped from $file. |
| 319 src="$1" |
| 320 file=${src##/*/} |
| 321 varname="$2" |
| 322 |
| 323 if [ $# -lt 2 ] ; then |
| 324 echo "TEST SYNTAX ERROR: use_file must have two argument
s" >&2 |
| 325 exit 255 |
| 326 fi |
| 327 |
| 328 assert_file "$src" |
| 329 |
| 330 outfile="xdgtestdata-$XDG_TEST_ID-$file" |
| 331 eval "$varname=$outfile" |
| 332 |
| 333 cp "$src" "$XDG_TEST_TMPDIR/$outfile" |
| 334 } |
| 335 |
| 336 get_unique_name() { |
| 337 # Get a unique name for a file, similar to 'use_file' |
| 338 # except that no file is copied. You are left to create file $VAR. |
| 339 varname="$1" |
| 340 file="$2" |
| 341 if [ -z "$varname" ] ; then |
| 342 echo "TEST SYNAX ERROR: get_unique_name requries a variable name
" |
| 343 exit 255 |
| 344 fi |
| 345 |
| 346 outfile="xdgtestdata-$XDG_TEST_ID-$file" |
| 347 eval "$varname=$outfile" |
| 348 } |
| 349 |
| 350 edit_file() { |
| 351 # Edit file via sed. |
| 352 # Usage: |
| 353 # edit_file $FILE origstr VARNAME [newstr] |
| 354 # Where: |
| 355 # $FILE is a file, probably copied from 'use_file' |
| 356 # VARNAME is created via 'eval' to contain the newstr |
| 357 # newstr is the optional substitution. If newstr is not present, |
| 358 # it will become 'xdgtestdata-$XDG_TEST_ID-$origstr' |
| 359 file="$1" |
| 360 origstr="$2" |
| 361 varname="$3" |
| 362 newstr="$4" |
| 363 |
| 364 if [ $# -lt 3 ] ; then |
| 365 echo "TEST SYNTAX ERROR: edit_file must have at least 3 argument
s." |
| 366 exit 255 |
| 367 fi |
| 368 |
| 369 assert_file "$file" |
| 370 |
| 371 if [ -z "$newstr" ] ; then |
| 372 newstr="xdgtestdata-$XDG_TEST_ID-$origstr" |
| 373 fi |
| 374 |
| 375 eval "$varname=\"$newstr\"" |
| 376 |
| 377 sed -i -e "s|$origstr|$newstr|g" "$file" |
| 378 } |
| 379 |
OLD | NEW |