t_grep.sh revision 302408
1139825Simp# $NetBSD: t_grep.sh,v 1.2 2013/05/17 15:39:17 christos Exp $
2148078Srwatson#
3148078Srwatson# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
4148078Srwatson# All rights reserved.
592654Sjeff#
692654Sjeff# Redistribution and use in source and binary forms, with or without
792654Sjeff# modification, are permitted provided that the following conditions
892654Sjeff# are met:
992654Sjeff# 1. Redistributions of source code must retain the above copyright
1092654Sjeff#    notice, this list of conditions and the following disclaimer.
1192654Sjeff# 2. Redistributions in binary form must reproduce the above copyright
1292654Sjeff#    notice, this list of conditions and the following disclaimer in the
1392654Sjeff#    documentation and/or other materials provided with the distribution.
1492654Sjeff#
1592654Sjeff# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1692654Sjeff# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1792654Sjeff# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1892654Sjeff# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1992654Sjeff# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2092654Sjeff# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2192654Sjeff# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2292654Sjeff# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2392654Sjeff# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2492654Sjeff# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2592654Sjeff# POSSIBILITY OF SUCH DAMAGE.
2692654Sjeff#
2792654Sjeff
2892654Sjeffatf_test_case basic
2992654Sjeffbasic_head()
3092654Sjeff{
3192654Sjeff	atf_set "descr" "Checks basic functionality"
3292654Sjeff}
3392654Sjeffbasic_body()
3492654Sjeff{ 
3592654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
3692654Sjeff	    'jot 10000 | grep 123'
3792654Sjeff}
3892654Sjeff
3992654Sjeffatf_test_case binary
4092654Sjeffbinary_head()
4192654Sjeff{
42184546Skeramida	atf_set "descr" "Checks handling of binary files"
4392654Sjeff}
4492654Sjeffbinary_body()
4592654Sjeff{
4692654Sjeff	# Begin FreeBSD
47129906Sbmilekic	#
4892654Sjeff	# Generate stable output instead of depending on uname to match the
4992654Sjeff	# branded OS name of /bin/sh
5092654Sjeff	if true; then
51166213Smohans	dd if=/dev/zero count=1 of=test.file
52166213Smohans	echo -n "foobar" >> test.file
5392654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
5492654Sjeff	else
5592654Sjeff	# End FreeBSD
5692654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep $(uname) /bin/sh
5792654Sjeff	# Begin FreeBSD
5892654Sjeff	fi
5992654Sjeff	# End FreeBSD
60132987Sgreen}
6192654Sjeff
6292654Sjeffatf_test_case recurse
63132987Sgreenrecurse_head()
64132987Sgreen{
6592654Sjeff	atf_set "descr" "Checks recursive searching"
6692654Sjeff}
6792654Sjeffrecurse_body()
68105689Ssheldonh{
6992654Sjeff	mkdir -p recurse/a/f recurse/d
70132987Sgreen	echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
7192654Sjeff	echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
7292654Sjeff
7392654Sjeff	# Begin FreeBSD
7492654Sjeff	if true; then
7592654Sjeff		atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
7692654Sjeff	else
7792654Sjeff	# End FreeBSD
7892654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" grep -r haddock recurse
7992654Sjeff	# Begin FreeBSD
8092654Sjeff	fi
8192654Sjeff	# End FreeBSD
8292654Sjeff}
8392654Sjeff
8492654Sjeffatf_test_case recurse_symlink
8592654Sjeffrecurse_symlink_head()
8692654Sjeff{
8792654Sjeff	atf_set "descr" "Checks symbolic link recursion"
8892654Sjeff}
8992654Sjeffrecurse_symlink_body()
9092654Sjeff{
9192654Sjeff	mkdir -p test/c/d
9292654Sjeff	(cd test/c/d && ln -s ../d .)
9392654Sjeff	echo "Test string" > test/c/match
9492654Sjeff
9592654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
96132987Sgreen	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
9792654Sjeff	    grep -r string test
9892654Sjeff}
99132987Sgreen
100132987Sgreenatf_test_case word_regexps
10192654Sjeffword_regexps_head()
10292654Sjeff{
10392654Sjeff	atf_set "descr" "Checks word-regexps"
104184546Skeramida}
105184546Skeramidaword_regexps_body()
10692654Sjeff{
107132987Sgreen	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
10892654Sjeff	    grep -w separated $(atf_get_srcdir)/d_input
10992654Sjeff}
11092654Sjeff
11192654Sjeffatf_test_case begin_end
11292654Sjeffbegin_end_head()
11392654Sjeff{
11492654Sjeff	atf_set "descr" "Checks handling of line beginnings and ends"
11592654Sjeff}
11692654Sjeffbegin_end_body()
11792654Sjeff{
11892654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
11992654Sjeff	    grep ^Front "$(atf_get_srcdir)/d_input"
12092654Sjeff
12192654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
122184546Skeramida	    grep ending$ "$(atf_get_srcdir)/d_input"
12392654Sjeff}
12492654Sjeff
12592654Sjeffatf_test_case ignore_case
12692654Sjeffignore_case_head()
12792654Sjeff{
12892654Sjeff	atf_set "descr" "Checks ignore-case option"
12992654Sjeff}
13092654Sjeffignore_case_body()
13192654Sjeff{
13292654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
13392654Sjeff	    grep -i Upper "$(atf_get_srcdir)/d_input"
13492654Sjeff}
135184546Skeramida
13692654Sjeffatf_test_case invert
137105689Ssheldonhinvert_head()
13892654Sjeff{
13992654Sjeff	atf_set "descr" "Checks selecting non-matching lines with -v option"
14092654Sjeff}
14192654Sjeffinvert_body()
14292654Sjeff{
14392654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
14492654Sjeff	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
14592654Sjeff}
14692654Sjeff
14792654Sjeffatf_test_case whole_line
14892654Sjeffwhole_line_head()
14992654Sjeff{
15092654Sjeff	atf_set "descr" "Checks whole-line matching with -x flag"
15192654Sjeff}
152184546Skeramidawhole_line_body()
15392654Sjeff{
15492654Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
155184546Skeramida	    grep -x matchme "$(atf_get_srcdir)/d_input"
15692654Sjeff}
15792654Sjeff
15892654Sjeffatf_test_case negative
15992654Sjeffnegative_head()
160184546Skeramida{
16192654Sjeff	atf_set "descr" "Checks handling of files with no matches"
162184546Skeramida}
16392654Sjeffnegative_body()
16492654Sjeff{
16592654Sjeff	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
16692654Sjeff}
16792654Sjeff
16895925Sarratf_test_case context
16992654Sjeffcontext_head()
170148072Ssilby{
17192654Sjeff	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
172120223Sjeff}
173129906Sbmilekiccontext_body()
174129906Sbmilekic{
175129906Sbmilekic	cp $(atf_get_srcdir)/d_context_*.* .
176184546Skeramida
177129906Sbmilekic	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
178184546Skeramida	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
179129906Sbmilekic	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
180129906Sbmilekic	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
181129906Sbmilekic}
182129906Sbmilekic
183129906Sbmilekicatf_test_case file_exp
184129906Sbmilekicfile_exp_head()
185129906Sbmilekic{
186129906Sbmilekic	atf_set "descr" "Checks reading expressions from file"
187129906Sbmilekic}
188129906Sbmilekicfile_exp_body()
189129906Sbmilekic{
190129906Sbmilekic	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
191129906Sbmilekic	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
192129906Sbmilekic}
193129906Sbmilekic
194129906Sbmilekicatf_test_case egrep
195129906Sbmilekicegrep_head()
196129913Sbmilekic{
197129913Sbmilekic	atf_set "descr" "Checks matching special characters with egrep"
198129913Sbmilekic}
199129906Sbmilekicegrep_body()
200129906Sbmilekic{
201129906Sbmilekic	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
202129906Sbmilekic		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
203129906Sbmilekic}
204129906Sbmilekic
205129906Sbmilekicatf_test_case zgrep
206129906Sbmilekiczgrep_head()
207129906Sbmilekic{
208187681Sjeff	atf_set "descr" "Checks handling of gzipped files with zgrep"
209187681Sjeff}
210187681Sjeffzgrep_body()
211187681Sjeff{
212187681Sjeff	cp "$(atf_get_srcdir)/d_input" .
213187681Sjeff	gzip d_input || atf_fail "gzip failed"
214187681Sjeff
215187681Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
216187681Sjeff}
217187681Sjeff
218187681Sjeffatf_test_case nonexistent
219120223Sjeffnonexistent_head()
220120223Sjeff{
221120223Sjeff	atf_set "descr" "Checks that -s flag suppresses error" \
222148072Ssilby	                "messages about nonexistent files"
223120223Sjeff}
22492654Sjeffnonexistent_body()
22592654Sjeff{
22692654Sjeff	atf_check -s ne:0 grep -s foobar nonexistent
227184546Skeramida}
22892654Sjeff
22992654Sjeffatf_test_case context2
23092654Sjeffcontext2_head()
23192654Sjeff{
23295758Sjeff	atf_set "descr" "Checks displaying context with -z flag"
233103531Sjeff}
234103531Sjeffcontext2_body()
235103531Sjeff{
236103531Sjeff	printf "haddock\000cod\000plaice\000" > test1
237103531Sjeff	printf "mackeral\000cod\000crab\000" > test2
238103531Sjeff
239103531Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
240103531Sjeff	    grep -z -A1 cod test1 test2
241129906Sbmilekic
242129906Sbmilekic	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
243129906Sbmilekic	    grep -z -B1 cod test1 test2
244187681Sjeff
245187681Sjeff	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
246187681Sjeff	    grep -z -C1 cod test1 test2
247187681Sjeff}
248187681Sjeff
249187681Sjeffatf_init_test_cases()
250187681Sjeff{
25192654Sjeff	atf_add_test_case basic 
252187681Sjeff	atf_add_test_case binary
253187681Sjeff	atf_add_test_case recurse
254187681Sjeff	atf_add_test_case recurse_symlink
255187681Sjeff	atf_add_test_case word_regexps
256187681Sjeff	atf_add_test_case begin_end
257187681Sjeff	atf_add_test_case ignore_case
258187681Sjeff	atf_add_test_case invert
259187681Sjeff	atf_add_test_case whole_line
260187681Sjeff	atf_add_test_case negative
26192654Sjeff	atf_add_test_case context
26292654Sjeff	atf_add_test_case file_exp
26392654Sjeff	atf_add_test_case egrep
26492654Sjeff	atf_add_test_case zgrep
26592654Sjeff	atf_add_test_case nonexistent
26692654Sjeff	atf_add_test_case context2
267166654Srwatson}
26892654Sjeff