1 | # Shell function library for test cases. |
---|
2 | # |
---|
3 | # Written by Russ Allbery <rra@stanford.edu> |
---|
4 | # Copyright 2009 Russ Allbery <rra@stanford.edu> |
---|
5 | # Copyright 2006, 2007, 2008 Board of Trustees, Leland Stanford Jr. University |
---|
6 | # |
---|
7 | # See LICENSE for licensing terms. |
---|
8 | |
---|
9 | # Print out the number of test cases we expect to run. |
---|
10 | plan () { |
---|
11 | count=1 |
---|
12 | echo "1..$1" |
---|
13 | } |
---|
14 | |
---|
15 | # Skip the entire test suite. Should be run instead of plan. |
---|
16 | skip_all () { |
---|
17 | local desc |
---|
18 | desc="$1" |
---|
19 | if [ -n "$desc" ] ; then |
---|
20 | echo "1..0 # skip $desc" |
---|
21 | else |
---|
22 | echo "1..0 # skip" |
---|
23 | fi |
---|
24 | exit 0 |
---|
25 | } |
---|
26 | |
---|
27 | # ok takes a test description and a command to run and prints success if that |
---|
28 | # command is successful, false otherwise. The count starts at 1 and is |
---|
29 | # updated each time ok is printed. |
---|
30 | ok () { |
---|
31 | local desc |
---|
32 | desc="$1" |
---|
33 | if [ -n "$desc" ] ; then |
---|
34 | desc=" - $desc" |
---|
35 | fi |
---|
36 | shift |
---|
37 | if "$@" ; then |
---|
38 | echo ok $count$desc |
---|
39 | else |
---|
40 | echo not ok $count$desc |
---|
41 | fi |
---|
42 | count=`expr $count + 1` |
---|
43 | } |
---|
44 | |
---|
45 | # Skip the next test. Takes the reason why the test is skipped. |
---|
46 | skip () { |
---|
47 | echo "ok $count # skip $*" |
---|
48 | count=`expr $count + 1` |
---|
49 | } |
---|
50 | |
---|
51 | # Report the same status on a whole set of tests. Takes the count of tests, |
---|
52 | # the description, and then the command to run to determine the status. |
---|
53 | ok_block () { |
---|
54 | local start end i desc |
---|
55 | i=$count |
---|
56 | end=`expr $count + $1` |
---|
57 | shift |
---|
58 | desc="$1" |
---|
59 | shift |
---|
60 | while [ "$i" -lt "$end" ] ; do |
---|
61 | ok "$desc" "$@" |
---|
62 | i=`expr $i + 1` |
---|
63 | done |
---|
64 | } |
---|
65 | |
---|
66 | # Skip a whole set of tests. Takes the count and then the reason for skipping |
---|
67 | # the test. |
---|
68 | skip_block () { |
---|
69 | local start end |
---|
70 | i=$count |
---|
71 | end=`expr $count + $1` |
---|
72 | shift |
---|
73 | while [ "$i" -lt "$end" ] ; do |
---|
74 | skip "$@" |
---|
75 | i=`expr $i + 1` |
---|
76 | done |
---|
77 | } |
---|
78 | |
---|
79 | # Run a program expected to succeed, and print ok if it does and produces the |
---|
80 | # correct output. Takes the description, expected exit status, the expected |
---|
81 | # output, the command to run, and then any arguments for that command. Strip |
---|
82 | # a colon and everything after it off the output if the expected status is |
---|
83 | # non-zero, since this is probably a system-specific error message. |
---|
84 | ok_program () { |
---|
85 | local desc w_status w_output output status |
---|
86 | desc="$1" |
---|
87 | shift |
---|
88 | w_status="$1" |
---|
89 | shift |
---|
90 | w_output="$1" |
---|
91 | shift |
---|
92 | output=`"$@" 2>&1` |
---|
93 | status=$? |
---|
94 | if [ "$w_status" -ne 0 ] ; then |
---|
95 | output=`echo "$output" | sed 's/^\([^:]*\):.*/\1/'` |
---|
96 | fi |
---|
97 | if [ $status = $w_status ] && [ x"$output" = x"$w_output" ] ; then |
---|
98 | ok "$desc" true |
---|
99 | else |
---|
100 | echo "# saw: ($status) $output" |
---|
101 | echo "# not: ($w_status) $w_output" |
---|
102 | ok "$desc" false |
---|
103 | fi |
---|
104 | } |
---|
105 | |
---|
106 | # Bail out with an error message. |
---|
107 | bail () { |
---|
108 | echo 'Bail out!' "$@" |
---|
109 | exit 1 |
---|
110 | } |
---|