...
1#!/bin/bash
2
3root="$TEST_TMPDIR/root"
4TEST_log="$TEST_TMPDIR/log"
5PKG="pkg"
6
7function set_up() {
8 ERROR=0
9 mkdir -p "$root"
10 cd "$root"
11 touch WORKSPACE
12}
13
14# Runs buildozer, including saving the log and error messages, by sending the
15# BUILD file contents ($1) to STDIN.
16function run() {
17 options=(--buildifier=)
18 while [[ "$1" == --* ]];
19 do
20 options+=("$1")
21 shift
22 done
23
24 input="$1"
25 shift
26 mkdir -p "$PKG"
27
28 pwd
29 echo "$input" > "$PKG/BUILD"
30 run_with_current_workspace "$buildozer ${options[*]}" "$@"
31}
32
33# Runs buildozer, including saving the log and error messages.
34function run_with_current_workspace() {
35 log="$TEST_TMPDIR/log"
36 log_err="$TEST_TMPDIR/log_err"
37 cmd="$1"
38 shift
39 echo "$cmd $*"
40 ret=0
41 $cmd "$@" > "$log" 2> "$log_err" || ret=$?
42 if [ "$ret" -ne "$ERROR" ]; then
43 cat "$log"
44 cat "$log_err"
45 fail "Expected error code $ERROR, got $ret"
46 fi
47 # There must be an error message if error code is 1 or 2.
48 if [ "$ret" -eq "1" -o "$ret" -eq "2" ]; then
49 [ -s "$log_err" ] || fail "No error message, despite error code $ret"
50 fi
51}
52
53function assert_equals() {
54 expected="$1"
55 pkg="${2-}"
56 if [ -z "$pkg" ]; then
57 pkg="$PKG"
58 fi
59 echo "$expected" > "$pkg/expected"
60 diff -u "$root/$pkg/expected" "$root/$pkg/BUILD" || fail "Output didn't match"
61}
62
63function assert_err() {
64 if ! grep "$1" "$log_err"; then
65 cat "$log_err"
66 fail "Error log doesn't contain '$1'"
67 fi
68}
69
70function assert_no_err() {
71 if grep "$1" "$log_err"; then
72 cat "$log_err"
73 fail "Error log contains '$1'"
74 fi
75}
76
77function fail() {
78 __show_log >&2
79 echo "$TEST_name FAILED:" "$@" "." >&2
80 echo "$@" >$TEST_TMPDIR/__fail
81 TEST_passed="false"
82 # Cleanup as we are leaving the subshell now
83 exit 1
84}
85
86function __show_log() {
87 echo "-- Test log: -----------------------------------------------------------"
88 [[ -e $TEST_log ]] && cat $TEST_log || echo "(Log file did not exist.)"
89 echo "------------------------------------------------------------------------"
90}
91
92function __pad() {
93 local title=$1
94 local pad=$2
95 {
96 echo -n "$pad$pad $title "
97 printf "%80s" " " | tr ' ' "$pad"
98 } | head -c 80
99 echo
100}
101
102function __trap_with_arg() {
103 func="$1" ; shift
104 for sig ; do
105 trap "$func $sig" "$sig"
106 done
107}
108
109function run_suite() {
110 local message="$1"
111
112 echo >&2
113 echo "$message" >&2
114 echo >&2
115
116 local total=0
117 local passed=0
118
119 TESTS=$(declare -F | awk '{print $3}' | grep ^test_ || true)
120
121 for TEST_name in ${TESTS[@]}; do
122 >$TEST_log # Reset the log.
123 TEST_passed="true"
124
125 total=$(($total + 1))
126 __pad $TEST_name '*' >&2
127
128 if [ "$(type -t $TEST_name)" = function ]; then
129 # Run test in a subshell.
130 rm -f $TEST_TMPDIR/__err_handled
131 __trap_with_arg __test_terminated INT KILL PIPE TERM ABRT FPE ILL QUIT SEGV
132 (
133 set_up
134 eval $TEST_name
135 test $TEST_passed == "true"
136 ) 2>&1 | tee $TEST_TMPDIR/__log
137 # Note that tee will prevent the control flow continuing if the test
138 # spawned any processes which are still running and have not closed
139 # their stdout.
140
141 test_subshell_status=${PIPESTATUS[0]}
142 if [ "$test_subshell_status" != 0 ]; then
143 TEST_passed="false"
144 fi
145
146 else # Bad test explicitly specified in $TESTS.
147 fail "Not a function: '$TEST_name'"
148 fi
149
150 local red='\033[0;31m'
151 local green='\033[0;32m'
152 local no_color='\033[0m'
153
154 if [[ "$TEST_passed" == "true" ]]; then
155 echo -e "${green}PASSED${no_color}: $TEST_name" >&2
156 passed=$(($passed + 1))
157 else
158 echo -e "${red}FAILED${no_color}: $TEST_name" >&2
159 # end marker in CDATA cannot be escaped, we need to split the CDATA sections
160 log=$(cat $TEST_TMPDIR/__log | sed 's/]]>/]]>]]><![CDATA[/g')
161 fi
162
163 echo >&2
164 done
165}
View as plain text