Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 3 | # Copyright 2005 by Rob Landley <rob@landley.net> |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | # Licensed under GPLv2, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 5 | |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 6 | # AUDIT: |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 7 | |
Mike Frysinger | caa7940 | 2009-11-04 18:41:22 -0500 | [diff] [blame] | 8 | . ./testing.sh |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 9 | |
Lauri Kasanen | 7b46220 | 2011-08-28 12:39:04 +0200 | [diff] [blame] | 10 | # testing "test name" "commands" "expected result" "file input" "stdin" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 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 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 16 | testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 17 | "1\n" "" "" |
Ron Yorston | 1f27fa9 | 2018-02-09 09:52:52 +0000 | [diff] [blame] | 18 | testing "grep (exit success)" "grep grep '$0' > /dev/null 2>&1 ; echo \$?" "0\n" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 19 | "" "" |
| 20 | # Test various data sources and destinations |
| 21 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 22 | testing "grep (default to stdin)" "grep two" "two\n" "" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 23 | "one\ntwo\nthree\nthree\nthree\n" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 24 | testing "grep - (specify stdin)" "grep two -" "two\n" "" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 25 | "one\ntwo\nthree\nthree\nthree\n" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 26 | testing "grep input (specify file)" "grep two input" "two\n" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 27 | "one\ntwo\nthree\nthree\nthree\n" "" |
| 28 | |
Denis Vlasenko | 4e79049 | 2008-05-02 13:26:18 +0000 | [diff] [blame] | 29 | # GNU grep 2.5.3 outputs a new line character after the located string |
Denis Vlasenko | 6da9b00 | 2008-05-02 12:34:59 +0000 | [diff] [blame] | 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" "" |
Bernhard Reutner-Fischer | 1e34731 | 2006-06-04 18:40:48 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 4e1e720 | 2007-11-26 05:38:20 +0000 | [diff] [blame] | 33 | >empty |
| 34 | testing "grep two files" "grep two input empty 2>/dev/null" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 35 | "input:two\n" "one\ntwo\nthree\nthree\nthree\n" "" |
Denis Vlasenko | 4e1e720 | 2007-11-26 05:38:20 +0000 | [diff] [blame] | 36 | rm empty |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 37 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 38 | testing "grep - infile (specify stdin and file)" "grep two - input" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 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)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 45 | "grep two - nonexistent 2> /dev/null ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 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)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 49 | "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 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)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 54 | "grep -q two - nonexistent ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 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)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 60 | "grep -s nomatch nonexistent ; echo \$?" "2\n" "" "" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 61 | testing "grep -s nofile - (stdin and nonexisting file, match)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 62 | "grep -s domatch nonexistent - ; echo \$?" \ |
| 63 | "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 64 | |
Denys Vlasenko | 29f354e | 2010-03-14 00:51:56 +0100 | [diff] [blame] | 65 | optional EXTRA_COMPAT |
Denis Vlasenko | 3fd15e1 | 2008-08-09 16:15:14 +0000 | [diff] [blame] | 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" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 68 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 69 | testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \ |
| 70 | "0\n" "\0\n" "" |
Denys Vlasenko | 29f354e | 2010-03-14 00:51:56 +0100 | [diff] [blame] | 71 | SKIP= |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 72 | |
| 73 | # -e regex |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 74 | testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 75 | "one\ntwo\n0\n" "one\ntwo\n" "" |
Denis Vlasenko | 4e1e720 | 2007-11-26 05:38:20 +0000 | [diff] [blame] | 76 | testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \ |
| 77 | "one\ntwo\n0\n" "one\ntwo\n" "" |
Ian Wienand | 0a2c793 | 2010-04-30 09:32:10 +0200 | [diff] [blame] | 78 | testing "grep -F handles -i" "grep -F -i foo input ; echo \$?" \ |
| 79 | "FOO\n0\n" "FOO\n" "" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | a05c071 | 2008-06-07 05:19:31 +0000 | [diff] [blame] | 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 | |
James Hogan | 5fc0585 | 2013-05-07 12:32:21 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | a77f3ec | 2020-08-15 00:39:30 +0200 | [diff] [blame] | 99 | # -L "show filenames which do not match" has inverted exitcode (if it printed something, it's "success") |
| 100 | testing "grep -L exitcode 0" "grep -L qwe input; echo \$?" \ |
| 101 | "input\n0\n" "asd\n" "" |
| 102 | testing "grep -L exitcode 0 #2" "grep -L qwe input -; echo \$?" \ |
| 103 | "(standard input)\n0\n" "qwe\n" "asd\n" |
| 104 | testing "grep -L exitcode 1" "grep -L qwe input; echo \$?" \ |
| 105 | "1\n" "qwe\n" "" |
| 106 | |
Denys Vlasenko | 1924e99 | 2016-11-14 05:09:48 +0100 | [diff] [blame] | 107 | optional EGREP |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 108 | testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \ |
| 109 | "b\ar\nfoo\nbaz" |
| 110 | testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n" |
| 111 | testing "egrep is not case insensitive" \ |
| 112 | "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n" |
Denys Vlasenko | 0944963 | 2009-07-29 01:20:09 +0200 | [diff] [blame] | 113 | testing "grep -E -o prints all matches" \ |
| 114 | "grep -E -o '([[:xdigit:]]{2}[:-]){5}[[:xdigit:]]{2}'" \ |
| 115 | "00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \ |
| 116 | "" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n" |
Denys Vlasenko | 29f354e | 2010-03-14 00:51:56 +0100 | [diff] [blame] | 117 | SKIP= |
Denys Vlasenko | 0944963 | 2009-07-29 01:20:09 +0200 | [diff] [blame] | 118 | |
Denys Vlasenko | 6dc0ace | 2009-12-04 02:48:14 +0100 | [diff] [blame] | 119 | testing "grep -o does not loop forever" \ |
| 120 | 'grep -o "[^/]*$"' \ |
| 121 | "test\n" \ |
| 122 | "" "/var/test\n" |
Denys Vlasenko | 3d8b96d | 2010-08-23 02:39:47 +0200 | [diff] [blame] | 123 | testing "grep -o does not loop forever on zero-length match" \ |
| 124 | 'grep -o "" | head -n1' \ |
| 125 | "" \ |
| 126 | "" "test\n" |
Denys Vlasenko | 6dc0ace | 2009-12-04 02:48:14 +0100 | [diff] [blame] | 127 | |
Lauri Kasanen | 7b46220 | 2011-08-28 12:39:04 +0200 | [diff] [blame] | 128 | testing "grep -f EMPTY_FILE" \ |
| 129 | "grep -f input" \ |
| 130 | "" \ |
| 131 | "" \ |
| 132 | "test\n" |
| 133 | |
| 134 | testing "grep -v -f EMPTY_FILE" \ |
| 135 | "grep -v -f input" \ |
| 136 | "test\n" \ |
| 137 | "" \ |
| 138 | "test\n" |
| 139 | |
Gray Wolf | c3295d2 | 2020-04-29 15:49:17 +0200 | [diff] [blame] | 140 | testing "grep -vxf EMPTY_FILE" \ |
| 141 | "grep -vxf input" \ |
| 142 | "test\n" \ |
| 143 | "" \ |
| 144 | "test\n" |
| 145 | |
Denys Vlasenko | 2f5b5be | 2013-01-20 16:57:19 +0100 | [diff] [blame] | 146 | testing "grep -Fw matches only words" \ |
| 147 | "grep -Fw foo input" \ |
| 148 | "" \ |
| 149 | "foop\n" \ |
| 150 | "" |
| 151 | |
| 152 | testing "grep -Fw doesn't stop on 1st mismatch" \ |
| 153 | "grep -Fw foo input" \ |
| 154 | "foop foo\n" \ |
| 155 | "foop foo\n" \ |
| 156 | "" |
| 157 | |
Bartosz Golaszewski | 414db79 | 2013-05-15 03:53:26 +0200 | [diff] [blame] | 158 | testing "grep -w doesn't stop on 1st mismatch" \ |
| 159 | "grep -w foo input" \ |
| 160 | "foop foo\n" \ |
| 161 | "foop foo\n" \ |
| 162 | "" |
| 163 | |
Denys Vlasenko | cd55f2d | 2014-01-07 14:57:42 +0100 | [diff] [blame] | 164 | testing "grep -w ^str doesn't match str not at the beginning" \ |
| 165 | "grep -w ^str input" \ |
| 166 | "" \ |
| 167 | "strstr\n" \ |
| 168 | "" |
| 169 | |
| 170 | testing "grep -w ^ doesn't hang" \ |
| 171 | "grep -w ^ input" \ |
| 172 | "" \ |
| 173 | "anything\n" \ |
| 174 | "" |
| 175 | |
Bartosz Golaszewski | 3ba2df8 | 2014-02-07 17:14:37 +0100 | [diff] [blame] | 176 | testing "grep -w word doesn't match wordword" \ |
| 177 | "grep -w word input" \ |
| 178 | "" \ |
| 179 | "wordword\n" \ |
| 180 | "" |
| 181 | |
Denys Vlasenko | 03fd7e0 | 2018-03-29 18:03:50 +0200 | [diff] [blame] | 182 | testing "grep -F -w w doesn't match ww" \ |
| 183 | "grep -F -w w input" \ |
| 184 | "" \ |
| 185 | "ww\n" \ |
| 186 | "" |
| 187 | |
Denys Vlasenko | 83e49ad | 2014-02-27 14:56:12 +0100 | [diff] [blame] | 188 | testing "grep -w word match second word" \ |
| 189 | "grep -w word input" \ |
| 190 | "bword,word\n""wordb,word\n""bwordb,word\n" \ |
| 191 | "bword,word\n""wordb,word\n""bwordb,word\n" \ |
| 192 | "" |
| 193 | |
Ari Sundholm | 9a9c6e3 | 2019-01-29 14:42:57 +0100 | [diff] [blame] | 194 | |
| 195 | testing "grep -x -v -e EXP1 -e EXP2 finds nothing if either EXP matches" \ |
| 196 | "grep -x -v -e '.*aa.*' -e 'bb.*'; echo \$?" \ |
| 197 | "1\n" \ |
| 198 | "" \ |
| 199 | " aa bb cc\n" |
| 200 | |
Sören Tempel | 42a8984 | 2020-03-30 11:02:08 +0200 | [diff] [blame] | 201 | testing "grep PATTERN can be a newline-delimited list" \ |
| 202 | 'grep -Fv "$(printf "foo\nbar\n")"' \ |
| 203 | "baz\n" \ |
| 204 | "" \ |
| 205 | "foo\nbar\nbaz\n" |
| 206 | |
| 207 | testing "grep -e PATTERN can be a newline-delimited list" \ |
| 208 | 'grep -Fv -e "$(printf "foo\nbar\n")"' \ |
| 209 | "baz\n" \ |
| 210 | "" \ |
| 211 | "foo\nbar\nbaz\n" |
| 212 | |
Denys Vlasenko | 34cc6c9 | 2014-08-28 15:50:09 +0200 | [diff] [blame] | 213 | # -r on symlink to dir should recurse into dir |
| 214 | mkdir -p grep.testdir/foo |
| 215 | echo bar > grep.testdir/foo/file |
| 216 | ln -s foo grep.testdir/symfoo |
| 217 | testing "grep -r on symlink to dir" \ |
| 218 | "grep -r . grep.testdir/symfoo" \ |
| 219 | "grep.testdir/symfoo/file:bar\n" \ |
| 220 | "" "" |
| 221 | rm -Rf grep.testdir |
| 222 | |
| 223 | # But -r on dir/symlink_to_dir should not recurse into symlink_to_dir |
| 224 | mkdir -p grep.testdir/foo |
| 225 | echo bar > grep.testdir/foo/file |
| 226 | ln -s foo grep.testdir/symfoo |
| 227 | testing "grep -r on dir/symlink to dir" \ |
| 228 | "grep -r . grep.testdir" \ |
| 229 | "grep.testdir/foo/file:bar\n" \ |
| 230 | "" "" |
| 231 | rm -Rf grep.testdir |
| 232 | |
Lauri Kasanen | 7b46220 | 2011-08-28 12:39:04 +0200 | [diff] [blame] | 233 | # testing "test name" "commands" "expected result" "file input" "stdin" |
| 234 | # file input will be file called "input" |
| 235 | # test can create a file "actual" instead of writing to stdout |
| 236 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 237 | exit $FAILCOUNT |