#!/bin/bash # main_test.sh - Don Yang (uguu.org) # # 06/16/11 set -e # Generate a temporary file name. # # We want to use tempfile(1), but it's not guaranteed to be available # on Cygwin, and MingW will not be able to access /tmp anyways. So on # Cygwin, we use Perl to generate temporary files in the current # directory. tempfile(1) is used everywhere else. function temporary_file() { if [[ "$OSTYPE" = "cygwin" ]]; then # On Cygwin, tempfile isn't always available, but Perl is, so # use Perl to generate temporary file names. Also, because the # binaries may be compiled by MingW instead of Cygwin, there is # a good chance that /tmp means two different things. For # compatibility, we generate temporary files in current directory. perl -e 'print "temporary_file.$$.", rand, "\n"' else # Use tempfile(1) for all other operating systems tempfile fi } PROGRAM=./homura INPUT_DATA=test_log.txt OPTIMIZED_INPUT_DATA=$(temporary_file) TMP_OUTPUT=$(temporary_file) perl optimize_log.pl $INPUT_DATA > $OPTIMIZED_INPUT_DATA # Collect output for all different invocation patterns $PROGRAM $INPUT_DATA $TMP_OUTPUT file_in_file_out=$(cat $TMP_OUTPUT | md5sum -) cat $INPUT_DATA | $PROGRAM - $TMP_OUTPUT pipe_in_file_out=$(cat $TMP_OUTPUT | md5sum -) $PROGRAM $OPTIMIZED_INPUT_DATA $TMP_OUTPUT opt_file_in_file_out=$(cat $TMP_OUTPUT | md5sum -) cat $OPTIMIZED_INPUT_DATA | $PROGRAM - $TMP_OUTPUT opt_pipe_in_file_out=$(cat $TMP_OUTPUT | md5sum -) file_in_pipe_out=$($PROGRAM $INPUT_DATA | md5sum -) pipe_in_pipe_out=$(cat $INPUT_DATA | $PROGRAM | md5sum -) opt_file_in_pipe_out=$($PROGRAM $OPTIMIZED_INPUT_DATA | md5sum -) opt_pipe_in_pipe_out=$(cat $OPTIMIZED_INPUT_DATA | $PROGRAM | md5sum -) # Compare checksums echo $file_in_file_out > $TMP_OUTPUT echo $pipe_in_file_out >> $TMP_OUTPUT echo $opt_file_in_file_out >> $TMP_OUTPUT echo $opt_pipe_in_file_out >> $TMP_OUTPUT echo $file_in_pipe_out >> $TMP_OUTPUT echo $pipe_in_pipe_out >> $TMP_OUTPUT echo $opt_file_in_pipe_out >> $TMP_OUTPUT echo $opt_pipe_in_pipe_out >> $TMP_OUTPUT error_status=0 if [[ $(uniq $TMP_OUTPUT | wc -l) -ne "1" ]]; then echo "Checksum mismatched" echo "$file_in_file_out file_in_file_out" echo "$pipe_in_file_out pipe_in_file_out" echo "$opt_file_in_file_out opt_file_in_file_out" echo "$opt_pipe_in_file_out opt_pipe_in_file_out" echo "$file_in_pipe_out file_in_pipe_out" echo "$pipe_in_pipe_out pipe_in_pipe_out" echo "$opt_file_in_pipe_out opt_file_in_pipe_out" echo "$opt_pipe_in_pipe_out opt_pipe_in_pipe_out" error_status=1 fi # Check that empty input is accepted. Note that pipe is used to avoid # opening /dev/null since it's not available on mingw. if ! ( echo -n | $PROGRAM > $TMP_OUTPUT ); then echo "Failed on empty input" error_status=1 fi # Validate HTML output if tidy is available if ( tidy -v >& $TMP_OUTPUT ); then $PROGRAM $OPTIMIZED_INPUT_DATA $TMP_OUTPUT if ! ( tidy -e -q $TMP_OUTPUT ); then echo "Validation failed" error_status=1 fi fi rm -f $OPTIMIZED_INPUT_DATA $TMP_OUTPUT exit $error_status