blob: 2a7a7b9017a24a514a8334fbcb1221ed287ed124 [file] [log] [blame]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00001#!/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
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000012# 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
18testing "grep (exit with error)" "nonexistent 2> /dev/null ; echo \$?" \
19 "1\n" "" ""
20testing "grep (exit success)" "grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
21 "" ""
22# Test various data sources and destinations
23
24testing "grep (default to stdin)" "two" "two\n" "" \
25 "one\ntwo\nthree\nthree\nthree\n"
26testing "grep - (specify stdin)" "two -" "two\n" "" \
27 "one\ntwo\nthree\nthree\nthree\n"
28testing "grep input (specify file)" "two input" "two\n" \
29 "one\ntwo\nthree\nthree\nthree\n" ""
30
31# Note that this assumes actual is empty.
32testing "grep input actual (two files)" "two input actual 2> /dev/null" \
33 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
34
35testing "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.
41testing "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"
45testing "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.
50testing "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
56testing "grep -s nofile (nonexisting file, no match)" \
57 "-s nomatch nonexistent ; echo \$?" "2\n" "" ""
58testing "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
64testing "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
67testing "grep handles binary stdin" "foo" "foo\n" "" "\0foo\n\n"
68
69testing "grep matches NUL" ". input > /dev/null 2>&1 ; echo \$?" "0\n" "\0\n" ""
70
71# -e regex
72testing "grep handles multiple regexps" "-e one -e two input ; echo \$?" \
73 "one\ntwo\n0\n" "one\ntwo\n" ""
74
Rob Landley48c61572005-11-07 08:50:53 +000075optional FEATURE_GREP_EGREP_ALIAS
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000076testing "grep -E supports extended regexps" "-E fo+" "foo\n" "" "b\ar\nfoo\nbaz"
77
78exit $FAILCOUNT