OLD | NEW |
(Empty) | |
| 1 #!/bin/sh |
| 2 |
| 3 # Copyright (c) 2006, Google Inc. |
| 4 # All rights reserved. |
| 5 # |
| 6 # Redistribution and use in source and binary forms, with or without |
| 7 # modification, are permitted provided that the following conditions are |
| 8 # met: |
| 9 # |
| 10 # * Redistributions of source code must retain the above copyright |
| 11 # notice, this list of conditions and the following disclaimer. |
| 12 # * Redistributions in binary form must reproduce the above |
| 13 # copyright notice, this list of conditions and the following disclaimer |
| 14 # in the documentation and/or other materials provided with the |
| 15 # distribution. |
| 16 # * Neither the name of Google Inc. nor the names of its |
| 17 # contributors may be used to endorse or promote products derived from |
| 18 # this software without specific prior written permission. |
| 19 # |
| 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 # --- |
| 33 # Author: Craig Silverstein |
| 34 # |
| 35 # Just tries to run gflags_unittest with various flags defined in |
| 36 # gflags.cc, and make sure they give the appropriate exit |
| 37 # status and appropriate error message. |
| 38 |
| 39 if [ -z "$1" ] |
| 40 then |
| 41 echo "USAGE: $0 <unittest exe> [top_srcdir] [tmpdir]" |
| 42 exit 1 |
| 43 fi |
| 44 |
| 45 EXE="$1" |
| 46 SRCDIR="${2:-./}" |
| 47 TMPDIR="${3:-/tmp/gflags}" |
| 48 |
| 49 # Executables built with the main source file suffixed with "-main" and "_main". |
| 50 EXE2="${EXE}2" # eg, gflags_unittest2 |
| 51 EXE3="${EXE}3" # eg, gflags_unittest3 |
| 52 |
| 53 # $1: executable |
| 54 # $2: line-number $3: expected return code. $4: substring of expected output. |
| 55 # $5: a substring you *don't* expect to find in the output. $6+ flags |
| 56 ExpectExe() { |
| 57 local executable="$1" |
| 58 shift |
| 59 local line_number="$1" |
| 60 shift |
| 61 local expected_rc="$1" |
| 62 shift |
| 63 local expected_output="$1" |
| 64 shift |
| 65 local unexpected_output="$1" |
| 66 shift |
| 67 |
| 68 # We always add --srcdir=$SRCDIR because it's needed for correctness |
| 69 "$executable" --srcdir="$SRCDIR" "$@" > "$TMPDIR/test.$line_number" 2>&1 |
| 70 local actual_rc=$? |
| 71 if [ $actual_rc != $expected_rc ]; then |
| 72 echo "Test on line $line_number failed:" \ |
| 73 "expected rc $expected_rc, got $actual_rc" |
| 74 exit 1; |
| 75 fi |
| 76 if [ -n "$expected_output" ] && |
| 77 ! fgrep -e "$expected_output" "$TMPDIR/test.$line_number" >/dev/null; then |
| 78 echo "Test on line $line_number failed:" \ |
| 79 "did not find expected substring '$expected_output'" |
| 80 exit 1; |
| 81 fi |
| 82 if [ -n "$unexpected_output" ] && |
| 83 fgrep -e "$unexpected_output" "$TMPDIR/test.$line_number" >/dev/null; then |
| 84 echo "Test line $line_number failed:" \ |
| 85 "found unexpected substring '$unexpected_output'" |
| 86 exit 1; |
| 87 fi |
| 88 } |
| 89 |
| 90 # $1: line-number $2: expected return code. $3: substring of expected output. |
| 91 # $4: a substring you *don't* expect to find in the output. $5+ flags |
| 92 Expect() { |
| 93 ExpectExe "$EXE" "$@" |
| 94 } |
| 95 |
| 96 rm -rf "$TMPDIR" |
| 97 mkdir "$TMPDIR" || exit 2 |
| 98 |
| 99 # Create a few flagfiles we can use later |
| 100 echo "--version" > "$TMPDIR/flagfile.1" |
| 101 echo "--foo=bar" > "$TMPDIR/flagfile.2" |
| 102 echo "--nounused_bool" >> "$TMPDIR/flagfile.2" |
| 103 echo "--flagfile=$TMPDIR/flagfile.2" > "$TMPDIR/flagfile.3" |
| 104 |
| 105 # Set a few environment variables (useful for --tryfromenv) |
| 106 export FLAGS_undefok=foo,bar |
| 107 export FLAGS_weirdo= |
| 108 export FLAGS_version=true |
| 109 export FLAGS_help=false |
| 110 |
| 111 # First, just make sure the unittest works as-is |
| 112 Expect $LINENO 0 "PASS" "" |
| 113 |
| 114 # --help should show all flags, including flags from gflags_reporting.cc |
| 115 Expect $LINENO 1 "/gflags_reporting.cc" "" --help |
| 116 |
| 117 # Make sure --help reflects flag changes made before flag-parsing |
| 118 Expect $LINENO 1 \ |
| 119 "-changed_bool1 (changed) type: bool default: true" "" --help |
| 120 Expect $LINENO 1 \ |
| 121 "-changed_bool2 (changed) type: bool default: true" "" --help |
| 122 |
| 123 # --nohelp and --help=false should be as if we didn't say anything |
| 124 Expect $LINENO 0 "PASS" "" --nohelp |
| 125 Expect $LINENO 0 "PASS" "" --help=false |
| 126 |
| 127 # --helpfull is the same as help |
| 128 Expect $LINENO 1 "/gflags_reporting.cc" "" -helpfull |
| 129 |
| 130 # --helpshort should show only flags from the unittest itself |
| 131 Expect $LINENO 1 "/gflags_unittest.cc" "/gflags_reporting.cc" --helpshort |
| 132 |
| 133 # --helpshort should show the tldflag we created in the unittest dir |
| 134 Expect $LINENO 1 "tldflag1" "/google.cc" --helpshort |
| 135 Expect $LINENO 1 "tldflag2" "/google.cc" --helpshort |
| 136 |
| 137 # --helpshort should work if the main source file is suffixed with [_-]main |
| 138 ExpectExe "$EXE2" $LINENO 1 "/gflags_unittest-main.cc" "/gflags_reporting.cc" \ |
| 139 --helpshort |
| 140 ExpectExe "$EXE3" $LINENO 1 "/gflags_unittest_main.cc" "/gflags_reporting.cc" \ |
| 141 --helpshort |
| 142 |
| 143 # --helpon needs an argument |
| 144 Expect $LINENO 1 \ |
| 145 "'--helpon' is missing its argument; flag description: show help on" \ |
| 146 "" --helpon |
| 147 |
| 148 # --helpon argument indicates what file we'll show args from |
| 149 Expect $LINENO 1 "/gflags.cc" "/gflags_unittest.cc" --helpon=gflags |
| 150 |
| 151 # another way of specifying the argument |
| 152 Expect $LINENO 1 "/gflags.cc" "/gflags_unittest.cc" --helpon gflags |
| 153 |
| 154 # test another argument |
| 155 Expect $LINENO 1 "/gflags_unittest.cc" "/gflags.cc" \ |
| 156 --helpon gflags_unittest |
| 157 |
| 158 # helpmatch is like helpon but takes substrings |
| 159 Expect $LINENO 1 "/gflags_reporting.cc" "/gflags_unittest.cc" \ |
| 160 -helpmatch reporting |
| 161 Expect $LINENO 1 "/gflags_unittest.cc" "/gflags.cc" \ |
| 162 -helpmatch=unittest |
| 163 |
| 164 # if no flags are found with helpmatch or helpon, suggest --help |
| 165 Expect $LINENO 1 "No modules matched" "/gflags_unittest.cc" \ |
| 166 -helpmatch=nosuchsubstring |
| 167 Expect $LINENO 1 "No modules matched" "/gflags_unittest.cc" \ |
| 168 -helpon=nosuchmodule |
| 169 |
| 170 # helppackage shows all the flags in the same dir as this unittest |
| 171 # --help should show all flags, including flags from google.cc |
| 172 Expect $LINENO 1 "/gflags_reporting.cc" "" --helppackage |
| 173 |
| 174 # xml! |
| 175 Expect $LINENO 1 "/gflags_unittest.cc</file>" \ |
| 176 "/gflags_unittest.cc:" --helpxml |
| 177 |
| 178 # just print the version info and exit |
| 179 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" --version |
| 180 |
| 181 # --undefok is a fun flag... |
| 182 Expect $LINENO 1 "unknown command line flag 'foo'" "" --undefok= --foo --unused_
bool |
| 183 Expect $LINENO 0 "PASS" "" --undefok=foo --foo --unused_bool |
| 184 # It's ok if the foo is in the middle |
| 185 Expect $LINENO 0 "PASS" "" --undefok=fee,fi,foo,fum --foo --unused_bool |
| 186 # But the spelling has to be just right... |
| 187 Expect $LINENO 1 "unknown command line flag 'foo'" "" --undefok=fo --foo --unuse
d_bool |
| 188 Expect $LINENO 1 "unknown command line flag 'foo'" "" --undefok=foot --foo --unu
sed_bool |
| 189 |
| 190 # See if we can successfully load our flags from the flagfile |
| 191 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" \ |
| 192 --flagfile="$TMPDIR/flagfile.1" |
| 193 Expect $LINENO 0 "PASS" "" --flagfile="$TMPDIR/flagfile.2" |
| 194 Expect $LINENO 0 "PASS" "" --flagfile="$TMPDIR/flagfile.3" |
| 195 |
| 196 # Also try to load flags from the environment |
| 197 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" --fromenv=version |
| 198 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" --tryfromenv=version |
| 199 Expect $LINENO 0 "PASS" "" --fromenv=help |
| 200 Expect $LINENO 0 "PASS" "" --tryfromenv=help |
| 201 Expect $LINENO 1 "helpfull not found in environment" "" --fromenv=helpfull |
| 202 Expect $LINENO 0 "PASS" "" --tryfromenv=helpfull |
| 203 Expect $LINENO 0 "PASS" "" --tryfromenv=undefok --foo |
| 204 Expect $LINENO 1 "unknown command line flag" "" --tryfromenv=weirdo |
| 205 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" \ |
| 206 --tryfromenv=test_bool,version,unused_bool |
| 207 Expect $LINENO 1 "not found in environment" "" --fromenv=test_bool |
| 208 Expect $LINENO 1 "unknown command line flag" "" --fromenv=test_bool,ok |
| 209 # Here, the --version overrides the fromenv |
| 210 Expect $LINENO 0 "gflags_unittest" "gflags_unittest.cc" \ |
| 211 --fromenv=test_bool,version,ok |
| 212 |
| 213 # Make sure -- by itself stops argv processing |
| 214 Expect $LINENO 0 "PASS" "" -- --help |
| 215 |
| 216 # Make sure boolean flags gives warning when type of default value is not bool |
| 217 Expect $LINENO 0 "Flag test_bool_string is of type bool, but its default value i
s not a boolean." "" |
| 218 Expect $LINENO 0 "Flag test_bool_float is of type bool, but its default value is
not a boolean." "" |
| 219 Expect $LINENO 0 "Flag test_bool_int is of type bool, but its default value is n
ot a boolean." "" |
| 220 |
| 221 # Make sure that boolean flags don't give warning when default value is bool |
| 222 Expect $LINENO 0 "" "Flag test_bool_bool is of type bool, but its default value
is not a boolean." |
| 223 |
| 224 # And we should die if the flag value doesn't pas the validator |
| 225 Expect $LINENO 1 "ERROR: failed validation of new value 'true' for flag 'always_
fail'" "" --always_fail |
| 226 |
| 227 echo "PASS" |
| 228 exit 0 |
OLD | NEW |