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