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> |
| 4 | # Licensed under GPL v2, see file LICENSE for details. |
| 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 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 8 | . testing.sh |
| 9 | |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 10 | # testing "test name" "options" "expected result" "file input" "stdin" |
| 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" "" "" |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +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 | |
| 65 | # This doesn't match GNU behaviour (Binary file input matches) |
| 66 | # acts like GNU grep -a |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 67 | 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] | 68 | # This doesn't match GNU behaviour (Binary file (standard input) matches) |
| 69 | # acts like GNU grep -a |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 70 | 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] | 71 | |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 72 | testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \ |
| 73 | "0\n" "\0\n" "" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 74 | |
| 75 | # -e regex |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 76 | 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] | 77 | "one\ntwo\n0\n" "one\ntwo\n" "" |
Denis Vlasenko | 4e1e720 | 2007-11-26 05:38:20 +0000 | [diff] [blame] | 78 | testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \ |
| 79 | "one\ntwo\n0\n" "one\ntwo\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 | |
Rob Landley | 48c6157 | 2005-11-07 08:50:53 +0000 | [diff] [blame] | 85 | optional FEATURE_GREP_EGREP_ALIAS |
Rob Landley | 4bb1b04 | 2006-03-16 15:20:45 +0000 | [diff] [blame] | 86 | testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \ |
| 87 | "b\ar\nfoo\nbaz" |
| 88 | testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n" |
| 89 | testing "egrep is not case insensitive" \ |
| 90 | "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n" |
Bernhard Reutner-Fischer | b47a74f | 2005-09-23 15:44:46 +0000 | [diff] [blame] | 91 | |
| 92 | exit $FAILCOUNT |