Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # grep tests. |
| 4 | # Copyright 2005 by Rob Landley <rob@landley.net> |
| 5 | # Licensed under GPL v2, see file LICENSE for details. |
| 6 | |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 7 | # AUDIT: |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 8 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 9 | . testing.sh |
| 10 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 11 | # testing "test name" "options" "expected result" "file input" "stdin" |
| 12 | # file input will be file called "input" |
| 13 | # test can create a file "actual" instead of writing to stdout |
| 14 | |
| 15 | # Test exit status |
| 16 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 17 | testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 18 | "1\n" "" "" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 19 | 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] | 20 | "" "" |
| 21 | # Test various data sources and destinations |
| 22 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 23 | testing "grep (default to stdin)" "grep two" "two\n" "" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 24 | "one\ntwo\nthree\nthree\nthree\n" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 25 | testing "grep - (specify stdin)" "grep two -" "two\n" "" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 26 | "one\ntwo\nthree\nthree\nthree\n" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 27 | testing "grep input (specify file)" "grep two input" "two\n" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 28 | "one\ntwo\nthree\nthree\nthree\n" "" |
| 29 | |
Bernhard Reutner-Fischer | 1e34731 | 2006-06-04 18:40:48 +0000 | [diff] [blame] | 30 | testing "grep (no newline at EOL)" "grep bug" "bug" "bug" "" |
| 31 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 32 | # Note that this assumes actual is empty. |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 33 | testing "grep input actual (two files)" "grep two input actual 2> /dev/null" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 34 | "input:two\n" "one\ntwo\nthree\nthree\nthree\n" "" |
| 35 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 36 | testing "grep - infile (specify stdin and file)" "grep two - input" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 37 | "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \ |
| 38 | "one\ntwo\ntoo\nthree\nthree\n" |
| 39 | |
| 40 | # Check if we see the correct return value if both stdin and non-existing file |
| 41 | # are given. |
| 42 | testing "grep - nofile (specify stdin and nonexisting file)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 43 | "grep two - nonexistent 2> /dev/null ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 44 | "(standard input):two\n(standard input):two\n2\n" \ |
| 45 | "" "one\ntwo\ntwo\nthree\nthree\nthree\n" |
| 46 | testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 47 | "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 48 | "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n" |
| 49 | # SUSv3: If the -q option is specified, the exit status shall be zero |
| 50 | # if an input line is selected, even if an error was detected. |
| 51 | testing "grep -q - nofile (specify stdin and nonexisting file, match)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 52 | "grep -q two - nonexistent ; echo \$?" \ |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 53 | "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n" |
| 54 | |
| 55 | # Test various command line options |
| 56 | # -s no error messages |
| 57 | testing "grep -s nofile (nonexisting file, no match)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 58 | "grep -s nomatch nonexistent ; echo \$?" "2\n" "" "" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 59 | testing "grep -s nofile - (stdin and nonexisting file, match)" \ |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 60 | "grep -s domatch nonexistent - ; echo \$?" \ |
| 61 | "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 62 | |
| 63 | # This doesn't match GNU behaviour (Binary file input matches) |
| 64 | # acts like GNU grep -a |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 65 | testing "grep handles binary files" "grep foo input" "foo\n" "\0foo\n\n" "" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 66 | # This doesn't match GNU behaviour (Binary file (standard input) matches) |
| 67 | # acts like GNU grep -a |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 68 | testing "grep handles binary stdin" "grep foo" "foo\n" "" "\0foo\n\n" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 69 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 70 | testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \ |
| 71 | "0\n" "\0\n" "" |
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" "" |
| 76 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 77 | optional FEATURE_GREP_EGREP_ALIAS |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 78 | testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \ |
| 79 | "b\ar\nfoo\nbaz" |
| 80 | testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n" |
| 81 | testing "egrep is not case insensitive" \ |
| 82 | "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 83 | |
| 84 | exit $FAILCOUNT |