[821] | 1 | #!/bin/sh
|
---|
| 2 |
|
---|
| 3 | # Copyright 2005 by Rob Landley <rob@landley.net>
|
---|
[2725] | 4 | # Licensed under GPLv2, see file LICENSE in this source tree.
|
---|
[821] | 5 |
|
---|
[1765] | 6 | # AUDIT:
|
---|
[821] | 7 |
|
---|
[2725] | 8 | . ./testing.sh
|
---|
[821] | 9 |
|
---|
[3232] | 10 | # testing "test name" "commands" "expected result" "file input" "stdin"
|
---|
[821] | 11 | # file input will be file called "input"
|
---|
| 12 | # test can create a file "actual" instead of writing to stdout
|
---|
| 13 |
|
---|
| 14 | # Test exit status
|
---|
| 15 |
|
---|
| 16 | testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \
|
---|
| 17 | "1\n" "" ""
|
---|
| 18 | testing "grep (exit success)" "grep grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
|
---|
| 19 | "" ""
|
---|
| 20 | # Test various data sources and destinations
|
---|
| 21 |
|
---|
| 22 | testing "grep (default to stdin)" "grep two" "two\n" "" \
|
---|
| 23 | "one\ntwo\nthree\nthree\nthree\n"
|
---|
| 24 | testing "grep - (specify stdin)" "grep two -" "two\n" "" \
|
---|
| 25 | "one\ntwo\nthree\nthree\nthree\n"
|
---|
| 26 | testing "grep input (specify file)" "grep two input" "two\n" \
|
---|
| 27 | "one\ntwo\nthree\nthree\nthree\n" ""
|
---|
| 28 |
|
---|
[2725] | 29 | # GNU grep 2.5.3 outputs a new line character after the located string
|
---|
| 30 | # even if there is no new line character in the input
|
---|
| 31 | testing "grep (no newline at EOL)" "grep bug input" "bug\n" "bug" ""
|
---|
[821] | 32 |
|
---|
[2725] | 33 | >empty
|
---|
| 34 | testing "grep two files" "grep two input empty 2>/dev/null" \
|
---|
[821] | 35 | "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
|
---|
[2725] | 36 | rm empty
|
---|
[821] | 37 |
|
---|
| 38 | testing "grep - infile (specify stdin and file)" "grep two - input" \
|
---|
| 39 | "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
|
---|
| 40 | "one\ntwo\ntoo\nthree\nthree\n"
|
---|
| 41 |
|
---|
| 42 | # Check if we see the correct return value if both stdin and non-existing file
|
---|
| 43 | # are given.
|
---|
| 44 | testing "grep - nofile (specify stdin and nonexisting file)" \
|
---|
| 45 | "grep two - nonexistent 2> /dev/null ; echo \$?" \
|
---|
| 46 | "(standard input):two\n(standard input):two\n2\n" \
|
---|
| 47 | "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
|
---|
| 48 | testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
|
---|
| 49 | "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
|
---|
| 50 | "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
|
---|
| 51 | # SUSv3: If the -q option is specified, the exit status shall be zero
|
---|
| 52 | # if an input line is selected, even if an error was detected.
|
---|
| 53 | testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
|
---|
| 54 | "grep -q two - nonexistent ; echo \$?" \
|
---|
| 55 | "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
|
---|
| 56 |
|
---|
| 57 | # Test various command line options
|
---|
| 58 | # -s no error messages
|
---|
| 59 | testing "grep -s nofile (nonexisting file, no match)" \
|
---|
| 60 | "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
|
---|
| 61 | testing "grep -s nofile - (stdin and nonexisting file, match)" \
|
---|
| 62 | "grep -s domatch nonexistent - ; echo \$?" \
|
---|
| 63 | "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
|
---|
| 64 |
|
---|
[2725] | 65 | optional EXTRA_COMPAT
|
---|
| 66 | testing "grep handles NUL in files" "grep -a foo input" "\0foo\n" "\0foo\n\n" ""
|
---|
| 67 | testing "grep handles NUL on stdin" "grep -a foo" "\0foo\n" "" "\0foo\n\n"
|
---|
[821] | 68 |
|
---|
| 69 | testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
|
---|
| 70 | "0\n" "\0\n" ""
|
---|
[2725] | 71 | SKIP=
|
---|
[821] | 72 |
|
---|
| 73 | # -e regex
|
---|
| 74 | testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \
|
---|
| 75 | "one\ntwo\n0\n" "one\ntwo\n" ""
|
---|
[2725] | 76 | testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \
|
---|
| 77 | "one\ntwo\n0\n" "one\ntwo\n" ""
|
---|
| 78 | testing "grep -F handles -i" "grep -F -i foo input ; echo \$?" \
|
---|
| 79 | "FOO\n0\n" "FOO\n" ""
|
---|
[821] | 80 |
|
---|
[2725] | 81 | # -f file/-
|
---|
| 82 | testing "grep can read regexps from stdin" "grep -f - input ; echo \$?" \
|
---|
| 83 | "two\nthree\n0\n" "tw\ntwo\nthree\n" "tw.\nthr\n"
|
---|
| 84 |
|
---|
[3621] | 85 | # -x (whole line match)
|
---|
| 86 | testing "grep -x (full match)" "grep -x foo input ; echo \$?" \
|
---|
| 87 | "foo\n0\n" "foo\n" ""
|
---|
| 88 | testing "grep -x (partial match 1)" "grep -x foo input ; echo \$?" \
|
---|
| 89 | "1\n" "foo bar\n" ""
|
---|
| 90 | testing "grep -x (partial match 2)" "grep -x foo input ; echo \$?" \
|
---|
| 91 | "1\n" "bar foo\n" ""
|
---|
| 92 | testing "grep -x -F (full match)" "grep -x -F foo input ; echo \$?" \
|
---|
| 93 | "foo\n0\n" "foo\n" ""
|
---|
| 94 | testing "grep -x -F (partial match 1)" "grep -x -F foo input ; echo \$?" \
|
---|
| 95 | "1\n" "foo bar\n" ""
|
---|
| 96 | testing "grep -x -F (partial match 2)" "grep -x -F foo input ; echo \$?" \
|
---|
| 97 | "1\n" "bar foo\n" ""
|
---|
| 98 |
|
---|
[821] | 99 | optional FEATURE_GREP_EGREP_ALIAS
|
---|
| 100 | testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
|
---|
| 101 | "b\ar\nfoo\nbaz"
|
---|
| 102 | testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
|
---|
| 103 | testing "egrep is not case insensitive" \
|
---|
| 104 | "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
|
---|
[2725] | 105 | testing "grep -E -o prints all matches" \
|
---|
| 106 | "grep -E -o '([[:xdigit:]]{2}[:-]){5}[[:xdigit:]]{2}'" \
|
---|
| 107 | "00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \
|
---|
| 108 | "" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n"
|
---|
| 109 | SKIP=
|
---|
[821] | 110 |
|
---|
[2725] | 111 | testing "grep -o does not loop forever" \
|
---|
| 112 | 'grep -o "[^/]*$"' \
|
---|
| 113 | "test\n" \
|
---|
| 114 | "" "/var/test\n"
|
---|
| 115 | testing "grep -o does not loop forever on zero-length match" \
|
---|
| 116 | 'grep -o "" | head -n1' \
|
---|
| 117 | "" \
|
---|
| 118 | "" "test\n"
|
---|
| 119 |
|
---|
[3232] | 120 | testing "grep -f EMPTY_FILE" \
|
---|
| 121 | "grep -f input" \
|
---|
| 122 | "" \
|
---|
| 123 | "" \
|
---|
| 124 | "test\n"
|
---|
| 125 |
|
---|
| 126 | testing "grep -v -f EMPTY_FILE" \
|
---|
| 127 | "grep -v -f input" \
|
---|
| 128 | "test\n" \
|
---|
| 129 | "" \
|
---|
| 130 | "test\n"
|
---|
| 131 |
|
---|
| 132 | testing "grep -Fw matches only words" \
|
---|
| 133 | "grep -Fw foo input" \
|
---|
| 134 | "" \
|
---|
| 135 | "foop\n" \
|
---|
| 136 | ""
|
---|
| 137 |
|
---|
| 138 | testing "grep -Fw doesn't stop on 1st mismatch" \
|
---|
| 139 | "grep -Fw foo input" \
|
---|
| 140 | "foop foo\n" \
|
---|
| 141 | "foop foo\n" \
|
---|
| 142 | ""
|
---|
| 143 |
|
---|
[3621] | 144 | testing "grep -w doesn't stop on 1st mismatch" \
|
---|
| 145 | "grep -w foo input" \
|
---|
| 146 | "foop foo\n" \
|
---|
| 147 | "foop foo\n" \
|
---|
| 148 | ""
|
---|
| 149 |
|
---|
| 150 | testing "grep -w ^str doesn't match str not at the beginning" \
|
---|
| 151 | "grep -w ^str input" \
|
---|
| 152 | "" \
|
---|
| 153 | "strstr\n" \
|
---|
| 154 | ""
|
---|
| 155 |
|
---|
| 156 | testing "grep -w ^ doesn't hang" \
|
---|
| 157 | "grep -w ^ input" \
|
---|
| 158 | "" \
|
---|
| 159 | "anything\n" \
|
---|
| 160 | ""
|
---|
| 161 |
|
---|
| 162 | testing "grep -w word doesn't match wordword" \
|
---|
| 163 | "grep -w word input" \
|
---|
| 164 | "" \
|
---|
| 165 | "wordword\n" \
|
---|
| 166 | ""
|
---|
| 167 |
|
---|
| 168 | testing "grep -w word match second word" \
|
---|
| 169 | "grep -w word input" \
|
---|
| 170 | "bword,word\n""wordb,word\n""bwordb,word\n" \
|
---|
| 171 | "bword,word\n""wordb,word\n""bwordb,word\n" \
|
---|
| 172 | ""
|
---|
| 173 |
|
---|
| 174 | # -r on symlink to dir should recurse into dir
|
---|
| 175 | mkdir -p grep.testdir/foo
|
---|
| 176 | echo bar > grep.testdir/foo/file
|
---|
| 177 | ln -s foo grep.testdir/symfoo
|
---|
| 178 | testing "grep -r on symlink to dir" \
|
---|
| 179 | "grep -r . grep.testdir/symfoo" \
|
---|
| 180 | "grep.testdir/symfoo/file:bar\n" \
|
---|
| 181 | "" ""
|
---|
| 182 | rm -Rf grep.testdir
|
---|
| 183 |
|
---|
| 184 | # But -r on dir/symlink_to_dir should not recurse into symlink_to_dir
|
---|
| 185 | mkdir -p grep.testdir/foo
|
---|
| 186 | echo bar > grep.testdir/foo/file
|
---|
| 187 | ln -s foo grep.testdir/symfoo
|
---|
| 188 | testing "grep -r on dir/symlink to dir" \
|
---|
| 189 | "grep -r . grep.testdir" \
|
---|
| 190 | "grep.testdir/foo/file:bar\n" \
|
---|
| 191 | "" ""
|
---|
| 192 | rm -Rf grep.testdir
|
---|
| 193 |
|
---|
[3232] | 194 | # testing "test name" "commands" "expected result" "file input" "stdin"
|
---|
| 195 | # file input will be file called "input"
|
---|
| 196 | # test can create a file "actual" instead of writing to stdout
|
---|
| 197 |
|
---|
[821] | 198 | exit $FAILCOUNT
|
---|