blob: 2a7a7b9017a24a514a8334fbcb1221ed287ed124 [file] [log] [blame]
#!/bin/sh
# grep tests.
# Copyright 2005 by Rob Landley <rob@landley.net>
# Licensed under GPL v2, see file LICENSE for details.
# AUDIT:
[ ${#COMMAND} -eq 0 ] && COMMAND=grep
. testing.sh
# testing "test name" "options" "expected result" "file input" "stdin"
# file input will be file called "input"
# test can create a file "actual" instead of writing to stdout
# Test exit status
testing "grep (exit with error)" "nonexistent 2> /dev/null ; echo \$?" \
"1\n" "" ""
testing "grep (exit success)" "grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
"" ""
# Test various data sources and destinations
testing "grep (default to stdin)" "two" "two\n" "" \
"one\ntwo\nthree\nthree\nthree\n"
testing "grep - (specify stdin)" "two -" "two\n" "" \
"one\ntwo\nthree\nthree\nthree\n"
testing "grep input (specify file)" "two input" "two\n" \
"one\ntwo\nthree\nthree\nthree\n" ""
# Note that this assumes actual is empty.
testing "grep input actual (two files)" "two input actual 2> /dev/null" \
"input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
testing "grep - infile (specify stdin and file)" "two - input" \
"(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
"one\ntwo\ntoo\nthree\nthree\n"
# Check if we see the correct return value if both stdin and non-existing file
# are given.
testing "grep - nofile (specify stdin and nonexisting file)" \
"two - nonexistent 2> /dev/null ; echo \$?" \
"(standard input):two\n(standard input):two\n2\n" \
"" "one\ntwo\ntwo\nthree\nthree\nthree\n"
testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
"-q nomatch - nonexistent 2> /dev/null ; echo \$?" \
"2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
# SUSv3: If the -q option is specified, the exit status shall be zero
# if an input line is selected, even if an error was detected.
testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
"-q two - nonexistent ; echo \$?" \
"0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
# Test various command line options
# -s no error messages
testing "grep -s nofile (nonexisting file, no match)" \
"-s nomatch nonexistent ; echo \$?" "2\n" "" ""
testing "grep -s nofile - (stdin and nonexisting file, match)" \
"-s domatch nonexistent - ; echo \$?" "(standard input):domatch\n2\n" \
"" "nomatch\ndomatch\nend\n"
# This doesn't match GNU behaviour (Binary file input matches)
# acts like GNU grep -a
testing "grep handles binary files" "foo input" "foo\n" "\0foo\n\n" ""
# This doesn't match GNU behaviour (Binary file (standard input) matches)
# acts like GNU grep -a
testing "grep handles binary stdin" "foo" "foo\n" "" "\0foo\n\n"
testing "grep matches NUL" ". input > /dev/null 2>&1 ; echo \$?" "0\n" "\0\n" ""
# -e regex
testing "grep handles multiple regexps" "-e one -e two input ; echo \$?" \
"one\ntwo\n0\n" "one\ntwo\n" ""
optional FEATURE_GREP_EGREP_ALIAS
testing "grep -E supports extended regexps" "-E fo+" "foo\n" "" "b\ar\nfoo\nbaz"
exit $FAILCOUNT