#!/bin/sh -u # Exhaustively test the 12_optional_arguments_demo.sh shell script. # ------------------------------------------------------------------ # Syntax: $0 scriptname # ------------------------------------------------------------------ # Purpose: Run a series of tests on the 12_optional_arguments_demo.sh # shell script, to make sure it works and generates correct output. # Print helpful and appropriate error messages if any tests fail. # The script is given as the first argument. # ------------------------------------------------------------------ echo "*** FIXME: MISSING CODE MUST BE ADDED HERE (omitted for brevity in this example):" # - FIXME: Insert code to set PATH and umask. # - FIXME: Insert code to prompt for and read a missing script name. # - FIXME: Insert code to validate the $scriptname before using it # (must be an existing regular file, readable, etc.) scriptname="$1" # transfer first argument to a more readable variable name #------------------------------------------------------------------------ # Start of function definition section: # Function Name: ExpectGoodOutput # Function Arguments: 4 # First argument: the name of the test being performed # Second argument: the name of a file containing Standard Output # Third argument: the name of a file containing Standard Error # Fourth argument: an extended Regular Expression pattern # # Run this function after collecting the standard output and standard # error of the script being tested. This function does two tests: # # 1. Look for the extended regexp pattern in the standard output file. # 2. Make sure the standard error file is empty. # # Return status 0 if all the tests pass, non-zero otherwise. # ExpectGoodOutput () { # Make sure we have the correct number of args to this function. # if [ $# -ne 4 ] ; then echo 1>&2 "$0: Function ExpectGoodOutput needs 4 args, you gave $# ($*)" exit 2 fi # Transfer the numbered function arguments to readable variable names. # testname="$1" stdout="$2" stderr="$3" regexp="$4" # Use egrep and a regexp to find the correct output in the $stdout file. # Throw the output of egrep away; we only want the good/bad exit status. # if egrep "$regexp" "$stdout" >/dev/null ; then echo "$testname: PASSED" # we found the pattern in the stdout file else echo "$testname: FAILED" # oops - we did not find the pattern echo "Cannot find correct output '$pattern' in file '$stdout'" echo "STANDARD OUTPUT:" cat "$stdout" echo "STANDARD ERROR:" cat "$stderr" return 1 fi # We are testing for good output; so, we should never have any stderr # output redirected into file $stderr. If the stderr file is not empty, # then something is wrong in the script. # if [ -s "$stderr" ] ; then echo "$testname: FAILED" echo "Unexpected output found on Standard Error" echo "STANDARD OUTPUT:" cat "$stdout" echo "STANDARD ERROR:" cat "$stderr" return 1 fi return 0 # no errors - passed the tests - return good status } # End of function definitions. #------------------------------------------------------------------------ # Start of actual script code. ################################################################## # In this part of the code, we test our script with different numbers # of arguments. We call the ExpectGoodOutput function to validate each # of the expected outputs of the script. The ExpectGoodOutput function # takes four arguments: # - a title for the test we are performing # - the name of the file containing standard output from the script # - the name of the file containing standard error from the script # - an extended regular expression that must be matched in the stdout file # to know that the script worked and that the test "passed" ################################################################## echo "Testing script '$scriptname'" ################################################################## # Test 1: Execute the script with no arguments. # Save stdout and stderr in files and pass the file names # and extended regular expression to the testing function. # # With no arguments, the output should be $PWD $MAIL and $HOME. # "$scriptname" 1>out 2>errs pattern="$PWD $MAIL $HOME" # FIXME: improve this regexp if ExpectGoodOutput 'Test 1 (no arguments)' out errs "$pattern" ; then echo "Okay so far." fi echo "FIXME: Fix the rest of this script below (and remove this line)." echo "FIXME: You must fix the pattern in Test 2 and write Test 3 and 4" exit 99 # FIXME: remove all these lines when you fix the script, below. ################################################################## # Test 2: Execute the script with one argument. # Save stdout and stderr in files and pass the file names # and extended regular expression to the testing function. # # We choose an argument containing an asterisk, to see if we can # cause the script we are testing to fail due to unquoted variables. # The asterisk also happens to be a regular expression character; # you will have to deal with that correctly when you write $pattern. # "$scriptname" "first * arg" 1>out 2>errs pattern=' FIXME: you must write this extended regexp pattern yourself ' if ExpectGoodOutput 'Test 2 (one argument)' out errs "$pattern" ; then echo "Okay so far." fi ################################################################## # Test 3: Execute the script with two arguments. echo "FIXME: Write this missing code for two arguments" ################################################################## # Test 4: Execute the script with three arguments. echo "FIXME: Write this missing code for three arguments"