4 # Copyright 2005 by Rob Landley <rob@landley.net>
5 # Licensed under GPL v2, see file LICENSE for details.
9 [ ${#COMMAND} -eq 0 ] && COMMAND=grep
12 # testing "test name" "options" "expected result" "file input" "stdin"
13 # file input will be file called "input"
14 # test can create a file "actual" instead of writing to stdout
18 testing "grep (exit with error)" "nonexistent 2> /dev/null ; echo \$?" \
20 testing "grep (exit success)" "grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
22 # Test various data sources and destinations
24 testing "grep (default to stdin)" "two" "two\n" "" \
25 "one\ntwo\nthree\nthree\nthree\n"
26 testing "grep - (specify stdin)" "two -" "two\n" "" \
27 "one\ntwo\nthree\nthree\nthree\n"
28 testing "grep input (specify file)" "two input" "two\n" \
29 "one\ntwo\nthree\nthree\nthree\n" ""
31 # Note that this assumes actual is empty.
32 testing "grep input actual (two files)" "two input actual 2> /dev/null" \
33 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
35 testing "grep - infile (specify stdin and file)" "two - input" \
36 "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
37 "one\ntwo\ntoo\nthree\nthree\n"
39 # Check if we see the correct return value if both stdin and non-existing file
41 testing "grep - nofile (specify stdin and nonexisting file)" \
42 "two - nonexistent 2> /dev/null ; echo \$?" \
43 "(standard input):two\n(standard input):two\n2\n" \
44 "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
45 testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
46 "-q nomatch - nonexistent 2> /dev/null ; echo \$?" \
47 "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
48 # SUSv3: If the -q option is specified, the exit status shall be zero
49 # if an input line is selected, even if an error was detected.
50 testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
51 "-q two - nonexistent ; echo \$?" \
52 "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
54 # Test various command line options
55 # -s no error messages
56 testing "grep -s nofile (nonexisting file, no match)" \
57 "-s nomatch nonexistent ; echo \$?" "2\n" "" ""
58 testing "grep -s nofile - (stdin and nonexisting file, match)" \
59 "-s domatch nonexistent - ; echo \$?" "(standard input):domatch\n2\n" \
60 "" "nomatch\ndomatch\nend\n"
62 # This doesn't match GNU behaviour (Binary file input matches)
63 # acts like GNU grep -a
64 testing "grep handles binary files" "foo input" "foo\n" "\0foo\n\n" ""
65 # This doesn't match GNU behaviour (Binary file (standard input) matches)
66 # acts like GNU grep -a
67 testing "grep handles binary stdin" "foo" "foo\n" "" "\0foo\n\n"
69 testing "grep matches NUL" ". input > /dev/null 2>&1 ; echo \$?" "0\n" "\0\n" ""
72 testing "grep handles multiple regexps" "-e one -e two input ; echo \$?" \
73 "one\ntwo\n0\n" "one\ntwo\n" ""
75 optional FEATURE_GREP_EGREP_ALIAS
76 testing "grep -E supports extended regexps" "-E fo+" "foo\n" "" "b\ar\nfoo\nbaz"