blob: 006a215e1b723efd59629b2f13f4cfff45edde29 [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>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004# Licensed under GPLv2, see file LICENSE in this source tree.
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00005
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006# AUDIT:
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00007
Mike Frysingercaa79402009-11-04 18:41:22 -05008. ./testing.sh
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +00009
Lauri Kasanen7b462202011-08-28 12:39:04 +020010# testing "test name" "commands" "expected result" "file input" "stdin"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000011# 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
Denis Vlasenko4e790492008-05-02 13:26:18 +000029# GNU grep 2.5.3 outputs a new line character after the located string
Denis Vlasenko6da9b002008-05-02 12:34:59 +000030# even if there is no new line character in the input
31testing "grep (no newline at EOL)" "grep bug input" "bug\n" "bug" ""
Bernhard Reutner-Fischer1e347312006-06-04 18:40:48 +000032
Denis Vlasenko4e1e7202007-11-26 05:38:20 +000033>empty
34testing "grep two files" "grep two input empty 2>/dev/null" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000035 "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
Denis Vlasenko4e1e7202007-11-26 05:38:20 +000036rm empty
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000037
Rob Landley4bb1b042006-03-16 15:20:45 +000038testing "grep - infile (specify stdin and file)" "grep two - input" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000039 "(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)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000045 "grep two - nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000046 "(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)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000049 "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000050 "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)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000054 "grep -q two - nonexistent ; echo \$?" \
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000055 "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)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000060 "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000061testing "grep -s nofile - (stdin and nonexisting file, match)" \
Rob Landley4bb1b042006-03-16 15:20:45 +000062 "grep -s domatch nonexistent - ; echo \$?" \
63 "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000064
Denys Vlasenko29f354e2010-03-14 00:51:56 +010065optional EXTRA_COMPAT
Denis Vlasenko3fd15e12008-08-09 16:15:14 +000066testing "grep handles NUL in files" "grep -a foo input" "\0foo\n" "\0foo\n\n" ""
67testing "grep handles NUL on stdin" "grep -a foo" "\0foo\n" "" "\0foo\n\n"
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000068
Rob Landley4bb1b042006-03-16 15:20:45 +000069testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
70 "0\n" "\0\n" ""
Denys Vlasenko29f354e2010-03-14 00:51:56 +010071SKIP=
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" ""
Ian Wienand0a2c7932010-04-30 09:32:10 +020078testing "grep -F handles -i" "grep -F -i foo input ; echo \$?" \
79 "FOO\n0\n" "FOO\n" ""
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +000080
Denis Vlasenkoa05c0712008-06-07 05:19:31 +000081# -f file/-
82testing "grep can read regexps from stdin" "grep -f - input ; echo \$?" \
83 "two\nthree\n0\n" "tw\ntwo\nthree\n" "tw.\nthr\n"
84
Rob Landley48c61572005-11-07 08:50:53 +000085optional FEATURE_GREP_EGREP_ALIAS
Rob Landley4bb1b042006-03-16 15:20:45 +000086testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
87 "b\ar\nfoo\nbaz"
88testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
89testing "egrep is not case insensitive" \
90 "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
Denys Vlasenko09449632009-07-29 01:20:09 +020091testing "grep -E -o prints all matches" \
92 "grep -E -o '([[:xdigit:]]{2}[:-]){5}[[:xdigit:]]{2}'" \
93 "00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \
94 "" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n"
Denys Vlasenko29f354e2010-03-14 00:51:56 +010095SKIP=
Denys Vlasenko09449632009-07-29 01:20:09 +020096
Denys Vlasenko6dc0ace2009-12-04 02:48:14 +010097testing "grep -o does not loop forever" \
98 'grep -o "[^/]*$"' \
99 "test\n" \
100 "" "/var/test\n"
Denys Vlasenko3d8b96d2010-08-23 02:39:47 +0200101testing "grep -o does not loop forever on zero-length match" \
102 'grep -o "" | head -n1' \
103 "" \
104 "" "test\n"
Denys Vlasenko6dc0ace2009-12-04 02:48:14 +0100105
Lauri Kasanen7b462202011-08-28 12:39:04 +0200106testing "grep -f EMPTY_FILE" \
107 "grep -f input" \
108 "" \
109 "" \
110 "test\n"
111
112testing "grep -v -f EMPTY_FILE" \
113 "grep -v -f input" \
114 "test\n" \
115 "" \
116 "test\n"
117
118# testing "test name" "commands" "expected result" "file input" "stdin"
119# file input will be file called "input"
120# test can create a file "actual" instead of writing to stdout
121
Bernhard Reutner-Fischerb47a74f2005-09-23 15:44:46 +0000122exit $FAILCOUNT