4 # Copyright 2005 by Rob Landley <rob@landley.net>
5 # Licensed under GPL v2, see file LICENSE for details.
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
17 testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \
19 testing "grep (exit success)" "grep grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
21 # Test various data sources and destinations
23 testing "grep (default to stdin)" "grep two" "two\n" "" \
24 "one\ntwo\nthree\nthree\nthree\n"
25 testing "grep - (specify stdin)" "grep two -" "two\n" "" \
26 "one\ntwo\nthree\nthree\nthree\n"
27 testing "grep input (specify file)" "grep two input" "two\n" \
28 "one\ntwo\nthree\nthree\nthree\n" ""
30 testing "grep (no newline at EOL)" "grep bug" "bug" "bug" ""
33 testing "grep two files" "grep two input empty 2>/dev/null" \
34 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
37 testing "grep - infile (specify stdin and file)" "grep two - input" \
38 "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
39 "one\ntwo\ntoo\nthree\nthree\n"
41 # Check if we see the correct return value if both stdin and non-existing file
43 testing "grep - nofile (specify stdin and nonexisting file)" \
44 "grep two - nonexistent 2> /dev/null ; echo \$?" \
45 "(standard input):two\n(standard input):two\n2\n" \
46 "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
47 testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
48 "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
49 "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
50 # SUSv3: If the -q option is specified, the exit status shall be zero
51 # if an input line is selected, even if an error was detected.
52 testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
53 "grep -q two - nonexistent ; echo \$?" \
54 "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
56 # Test various command line options
57 # -s no error messages
58 testing "grep -s nofile (nonexisting file, no match)" \
59 "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
60 testing "grep -s nofile - (stdin and nonexisting file, match)" \
61 "grep -s domatch nonexistent - ; echo \$?" \
62 "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
64 # This doesn't match GNU behaviour (Binary file input matches)
65 # acts like GNU grep -a
66 testing "grep handles binary files" "grep foo input" "foo\n" "\0foo\n\n" ""
67 # This doesn't match GNU behaviour (Binary file (standard input) matches)
68 # acts like GNU grep -a
69 testing "grep handles binary stdin" "grep foo" "foo\n" "" "\0foo\n\n"
71 testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
75 testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \
76 "one\ntwo\n0\n" "one\ntwo\n" ""
77 testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \
78 "one\ntwo\n0\n" "one\ntwo\n" ""
80 optional FEATURE_GREP_EGREP_ALIAS
81 testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
83 testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
84 testing "egrep is not case insensitive" \
85 "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"