blob: c4f534d1ac7966a91612b084e7466cef9a32dd89 [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
12# Depends on grep
13_BB_CONFIG_DEP=grep
14
15# testing "test name" "options" "expected result" "file input" "stdin"
16# file input will be file called "input"
17# test can create a file "actual" instead of writing to stdout
18
19# Test exit status
20
21testing "grep (exit with error)" "nonexistent 2> /dev/null ; echo \$?" \
22 "1\n" "" ""
23testing "grep (exit success)" "grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
24 "" ""
25# Test various data sources and destinations
26
27testing "grep (default to stdin)" "two" "two\n" "" \
28 "one\ntwo\nthree\nthree\nthree\n"
29testing "grep - (specify stdin)" "two -" "two\n" "" \
30 "one\ntwo\nthree\nthree\nthree\n"
31testing "grep input (specify file)" "two input" "two\n" \
32 "one\ntwo\nthree\nthree\nthree\n" ""
33
34# Note that this assumes actual is empty.
35testing "grep input actual (two files)" "two input actual 2> /dev/null" \
36 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
37
38testing "grep - infile (specify stdin and file)" "two - input" \
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.
44testing "grep - nofile (specify stdin and nonexisting file)" \
45 "two - nonexistent 2> /dev/null ; echo \$?" \
46 "(standard input):two\n(standard input):two\n2\n" \
47 "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
48testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
49 "-q nomatch - nonexistent 2> /dev/null ; echo \$?" \
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.
53testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
54 "-q two - nonexistent ; echo \$?" \
55 "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
56
57# Test various command line options
58# -s no error messages
59testing "grep -s nofile (nonexisting file, no match)" \
60 "-s nomatch nonexistent ; echo \$?" "2\n" "" ""
61testing "grep -s nofile - (stdin and nonexisting file, match)" \
62 "-s domatch nonexistent - ; echo \$?" "(standard input):domatch\n2\n" \
63 "" "nomatch\ndomatch\nend\n"
64
65# This doesn't match GNU behaviour (Binary file input matches)
66# acts like GNU grep -a
67testing "grep handles binary files" "foo input" "foo\n" "\0foo\n\n" ""
68# This doesn't match GNU behaviour (Binary file (standard input) matches)
69# acts like GNU grep -a
70testing "grep handles binary stdin" "foo" "foo\n" "" "\0foo\n\n"
71
72testing "grep matches NUL" ". input > /dev/null 2>&1 ; echo \$?" "0\n" "\0\n" ""
73
74# -e regex
75testing "grep handles multiple regexps" "-e one -e two input ; echo \$?" \
76 "one\ntwo\n0\n" "one\ntwo\n" ""
77
78# Depends on FEATURE_GREP_EGREP_ALIAS
79_BB_CONFIG_DEP=FEATURE_GREP_EGREP_ALIAS
80testing "grep -E supports extended regexps" "-E fo+" "foo\n" "" "b\ar\nfoo\nbaz"
81
82exit $FAILCOUNT