A sample test scaffold

#!/bin/bash

usage() {
    cat << EOF
usage: ${0##*/} <target_program> <expected_input_output>
EOF
}

if [ $# != 2 ]; then
    usage
    exit 1
fi

# parse arguments
INPUT_OUTPUT_FILE="input-expectedoutput"
TARGET_PROG=$1
INPUT=$2

# validate arguments
if [ ! -f $TARGET_PROG ]; then echo $TARGET_PROG does not exist; exit 1; fi
if [ ! -f $INPUT ]; then echo $INPUT does not exist; exit 1; fi
[ ! -f $INPUT_OUTPUT_FILE ] && touch $INPUT_OUTPUT_FILE
> $INPUT_OUTPUT_FILE

# generate input-expectedoutput from input
cat $INPUT | while read input; do
    expected_output=`echo $input | tr '[:lower:]' '[:upper:]'`
    echo -e "${input}\t${expected_output}" >> $INPUT_OUTPUT_FILE
done

# generate tmp prog which is used to test target prog
success=1
cat $INPUT_OUTPUT_FILE | while read input expected_output; do
    output=`./$TARGET_PROG $input`
    if [ $output != $expected_output ]; then echo "FAIL for $input"; success=0; fi
done

[ $success == 1 ] && echo "SUCCESS" || echo "FAIL"

你可能感兴趣的:(test,bash,automation,scaffold)