blob: cc9520e90b9dd9be93ea9c294e91325e8c76b825 [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
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00009. testing.sh
10
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000011# 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
14
15# Test exit status
16
Rob Landley4bb1b042006-03-16 15:20:45 +000017testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000018 "1\n" "" ""
Rob Landley4bb1b042006-03-16 15:20:45 +000019testing "grep (exit success)" "grep grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000020 "" ""
21# Test various data sources and destinations
22
Rob Landley4bb1b042006-03-16 15:20:45 +000023testing "grep (default to stdin)" "grep two" "two\n" "" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000024 "one\ntwo\nthree\nthree\nthree\n"
Rob Landley4bb1b042006-03-16 15:20:45 +000025testing "grep - (specify stdin)" "grep two -" "two\n" "" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000026 "one\ntwo\nthree\nthree\nthree\n"
Rob Landley4bb1b042006-03-16 15:20:45 +000027testing "grep input (specify file)" "grep two input" "two\n" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000028 "one\ntwo\nthree\nthree\nthree\n" ""
29
30# Note that this assumes actual is empty.
Rob Landley4bb1b042006-03-16 15:20:45 +000031testing "grep input actual (two files)" "grep two input actual 2> /dev/null" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000032 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
33
Rob Landley4bb1b042006-03-16 15:20:45 +000034testing "grep - infile (specify stdin and file)" "grep two - input" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000035 "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
36 "one\ntwo\ntoo\nthree\nthree\n"
37
38# Check if we see the correct return value if both stdin and non-existing file
39# are given.
40testing "grep - nofile (specify stdin and nonexisting file)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000041 "grep two - nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000042 "(standard input):two\n(standard input):two\n2\n" \
43 "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
44testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000045 "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000046 "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
47# SUSv3: If the -q option is specified, the exit status shall be zero
48# if an input line is selected, even if an error was detected.
49testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000050 "grep -q two - nonexistent ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000051 "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
52
53# Test various command line options
54# -s no error messages
55testing "grep -s nofile (nonexisting file, no match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000056 "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000057testing "grep -s nofile - (stdin and nonexisting file, match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000058 "grep -s domatch nonexistent - ; echo \$?" \
59 "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000060
61# This doesn't match GNU behaviour (Binary file input matches)
62# acts like GNU grep -a
Rob Landley4bb1b042006-03-16 15:20:45 +000063testing "grep handles binary files" "grep foo input" "foo\n" "\0foo\n\n" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000064# This doesn't match GNU behaviour (Binary file (standard input) matches)
65# acts like GNU grep -a
Rob Landley4bb1b042006-03-16 15:20:45 +000066testing "grep handles binary stdin" "grep foo" "foo\n" "" "\0foo\n\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000067
Rob Landley4bb1b042006-03-16 15:20:45 +000068testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
69 "0\n" "\0\n" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000070
71# -e regex
Rob Landley4bb1b042006-03-16 15:20:45 +000072testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000073 "one\ntwo\n0\n" "one\ntwo\n" ""
74
Rob Landley48c61572005-11-07 08:50:53 +000075optional FEATURE_GREP_EGREP_ALIAS
Rob Landley4bb1b042006-03-16 15:20:45 +000076testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
77 "b\ar\nfoo\nbaz"
78testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
79testing "egrep is not case insensitive" \
80 "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000081
82exit $FAILCOUNT