cleanup style ... just because you use less spaces doesnt mean the resulting code...
[oweals/busybox.git] / testsuite / grep.tests
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
7 # AUDIT: 
8
9 [ ${#COMMAND} -eq 0 ] && COMMAND=grep
10 . testing.sh
11
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
15
16 # Test exit status
17
18 testing "grep (exit with error)" "nonexistent 2> /dev/null ; echo \$?" \
19         "1\n" "" ""
20 testing "grep (exit success)" "grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
21         "" ""
22 # Test various data sources and destinations
23
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" ""
30
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" ""
34
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"
38
39 # Check if we see the correct return value if both stdin and non-existing file
40 # are given.
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"
53
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"
61
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"
68
69 testing "grep matches NUL" ". input > /dev/null 2>&1 ; echo \$?" "0\n" "\0\n" ""
70
71 # -e regex
72 testing "grep handles multiple regexps" "-e one -e two input ; echo \$?" \
73         "one\ntwo\n0\n" "one\ntwo\n" ""
74
75 optional FEATURE_GREP_EGREP_ALIAS
76 testing "grep -E supports extended regexps" "-E fo+" "foo\n" "" "b\ar\nfoo\nbaz"
77
78 exit $FAILCOUNT