blob: bb682dba4cc001fb0fd3aa4c5136e286b020f8f7 [file] [log] [blame]
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00001#!/bin/sh
2
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00003# Copyright 2005 by Rob Landley <rob@landley.net>
4# Licensed under GPL v2, see file LICENSE for details.
5
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006# AUDIT:
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00007
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00008. testing.sh
9
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000010# testing "test name" "options" "expected result" "file input" "stdin"
11# file input will be file called "input"
12# test can create a file "actual" instead of writing to stdout
13
14# Test exit status
15
Rob Landley4bb1b042006-03-16 15:20:45 +000016testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000017 "1\n" "" ""
Rob Landley4bb1b042006-03-16 15:20:45 +000018testing "grep (exit success)" "grep grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000019 "" ""
20# Test various data sources and destinations
21
Rob Landley4bb1b042006-03-16 15:20:45 +000022testing "grep (default to stdin)" "grep two" "two\n" "" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000023 "one\ntwo\nthree\nthree\nthree\n"
Rob Landley4bb1b042006-03-16 15:20:45 +000024testing "grep - (specify stdin)" "grep two -" "two\n" "" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000025 "one\ntwo\nthree\nthree\nthree\n"
Rob Landley4bb1b042006-03-16 15:20:45 +000026testing "grep input (specify file)" "grep two input" "two\n" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000027 "one\ntwo\nthree\nthree\nthree\n" ""
28
Bernhard Reutner-Fischer1e347312006-06-04 18:40:48 +000029testing "grep (no newline at EOL)" "grep bug" "bug" "bug" ""
30
Denis Vlasenko4e1e7202007-11-26 05:38:20 +000031>empty
32testing "grep two files" "grep two input empty 2>/dev/null" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000033 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
Denis Vlasenko4e1e7202007-11-26 05:38:20 +000034rm empty
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000035
Rob Landley4bb1b042006-03-16 15:20:45 +000036testing "grep - infile (specify stdin and file)" "grep two - input" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000037 "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
38 "one\ntwo\ntoo\nthree\nthree\n"
39
40# Check if we see the correct return value if both stdin and non-existing file
41# are given.
42testing "grep - nofile (specify stdin and nonexisting file)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000043 "grep two - nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000044 "(standard input):two\n(standard input):two\n2\n" \
45 "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
46testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000047 "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000048 "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
49# SUSv3: If the -q option is specified, the exit status shall be zero
50# if an input line is selected, even if an error was detected.
51testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000052 "grep -q two - nonexistent ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000053 "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
54
55# Test various command line options
56# -s no error messages
57testing "grep -s nofile (nonexisting file, no match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000058 "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000059testing "grep -s nofile - (stdin and nonexisting file, match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000060 "grep -s domatch nonexistent - ; echo \$?" \
61 "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000062
63# This doesn't match GNU behaviour (Binary file input matches)
64# acts like GNU grep -a
Rob Landley4bb1b042006-03-16 15:20:45 +000065testing "grep handles binary files" "grep foo input" "foo\n" "\0foo\n\n" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000066# This doesn't match GNU behaviour (Binary file (standard input) matches)
67# acts like GNU grep -a
Rob Landley4bb1b042006-03-16 15:20:45 +000068testing "grep handles binary stdin" "grep foo" "foo\n" "" "\0foo\n\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000069
Rob Landley4bb1b042006-03-16 15:20:45 +000070testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
71 "0\n" "\0\n" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000072
73# -e regex
Rob Landley4bb1b042006-03-16 15:20:45 +000074testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000075 "one\ntwo\n0\n" "one\ntwo\n" ""
Denis Vlasenko4e1e7202007-11-26 05:38:20 +000076testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \
77 "one\ntwo\n0\n" "one\ntwo\n" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000078
Rob Landley48c61572005-11-07 08:50:53 +000079optional FEATURE_GREP_EGREP_ALIAS
Rob Landley4bb1b042006-03-16 15:20:45 +000080testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
81 "b\ar\nfoo\nbaz"
82testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
83testing "egrep is not case insensitive" \
84 "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000085
86exit $FAILCOUNT