xref: /freebsd/contrib/netbsd-tests/usr.bin/grep/t_grep.sh (revision 49b49cda41feabe3439f7318e8bf40e3896c7bf4)
1# $NetBSD: t_grep.sh,v 1.2 2013/05/17 15:39:17 christos Exp $
2#
3# Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28atf_test_case basic
29basic_head()
30{
31	atf_set "descr" "Checks basic functionality"
32}
33basic_body()
34{
35	atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
36	    'jot 10000 | grep 123'
37}
38
39atf_test_case binary
40binary_head()
41{
42	atf_set "descr" "Checks handling of binary files"
43}
44binary_body()
45{
46	# Begin FreeBSD
47	#
48	# Generate stable output instead of depending on uname to match the
49	# branded OS name of /bin/sh
50	if true; then
51	dd if=/dev/zero count=1 of=test.file
52	echo -n "foobar" >> test.file
53	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
54	else
55	# End FreeBSD
56	atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep $(uname) /bin/sh
57	# Begin FreeBSD
58	fi
59	# End FreeBSD
60}
61
62atf_test_case recurse
63recurse_head()
64{
65	atf_set "descr" "Checks recursive searching"
66}
67recurse_body()
68{
69	mkdir -p recurse/a/f recurse/d
70	echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
71	echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
72
73	atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" grep -r haddock recurse
74}
75
76atf_test_case recurse_symlink
77recurse_symlink_head()
78{
79	atf_set "descr" "Checks symbolic link recursion"
80}
81recurse_symlink_body()
82{
83	mkdir -p test/c/d
84	(cd test/c/d && ln -s ../d .)
85	echo "Test string" > test/c/match
86
87	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
88	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
89	    grep -r string test
90}
91
92atf_test_case word_regexps
93word_regexps_head()
94{
95	atf_set "descr" "Checks word-regexps"
96}
97word_regexps_body()
98{
99	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
100	    grep -w separated $(atf_get_srcdir)/d_input
101}
102
103atf_test_case begin_end
104begin_end_head()
105{
106	atf_set "descr" "Checks handling of line beginnings and ends"
107}
108begin_end_body()
109{
110	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
111	    grep ^Front "$(atf_get_srcdir)/d_input"
112
113	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
114	    grep ending$ "$(atf_get_srcdir)/d_input"
115}
116
117atf_test_case ignore_case
118ignore_case_head()
119{
120	atf_set "descr" "Checks ignore-case option"
121}
122ignore_case_body()
123{
124	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
125	    grep -i Upper "$(atf_get_srcdir)/d_input"
126}
127
128atf_test_case invert
129invert_head()
130{
131	atf_set "descr" "Checks selecting non-matching lines with -v option"
132}
133invert_body()
134{
135	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
136	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
137}
138
139atf_test_case whole_line
140whole_line_head()
141{
142	atf_set "descr" "Checks whole-line matching with -x flag"
143}
144whole_line_body()
145{
146	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
147	    grep -x matchme "$(atf_get_srcdir)/d_input"
148}
149
150atf_test_case negative
151negative_head()
152{
153	atf_set "descr" "Checks handling of files with no matches"
154}
155negative_body()
156{
157	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
158}
159
160atf_test_case context
161context_head()
162{
163	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
164}
165context_body()
166{
167	cp $(atf_get_srcdir)/d_context_*.* .
168
169	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
170	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
171	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
172	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
173}
174
175atf_test_case file_exp
176file_exp_head()
177{
178	atf_set "descr" "Checks reading expressions from file"
179}
180file_exp_body()
181{
182	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
183	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
184}
185
186atf_test_case egrep
187egrep_head()
188{
189	atf_set "descr" "Checks matching special characters with egrep"
190}
191egrep_body()
192{
193	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
194		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
195}
196
197atf_test_case zgrep
198zgrep_head()
199{
200	atf_set "descr" "Checks handling of gzipped files with zgrep"
201}
202zgrep_body()
203{
204	cp "$(atf_get_srcdir)/d_input" .
205	gzip d_input || atf_fail "gzip failed"
206
207	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
208}
209
210atf_test_case nonexistent
211nonexistent_head()
212{
213	atf_set "descr" "Checks that -s flag suppresses error" \
214	                "messages about nonexistent files"
215}
216nonexistent_body()
217{
218	atf_check -s ne:0 grep -s foobar nonexistent
219}
220
221atf_test_case context2
222context2_head()
223{
224	atf_set "descr" "Checks displaying context with -z flag"
225}
226context2_body()
227{
228	printf "haddock\000cod\000plaice\000" > test1
229	printf "mackeral\000cod\000crab\000" > test2
230
231	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
232	    grep -z -A1 cod test1 test2
233
234	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
235	    grep -z -B1 cod test1 test2
236
237	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
238	    grep -z -C1 cod test1 test2
239}
240
241atf_init_test_cases()
242{
243	atf_add_test_case basic
244	atf_add_test_case binary
245	atf_add_test_case recurse
246	atf_add_test_case recurse_symlink
247	atf_add_test_case word_regexps
248	atf_add_test_case begin_end
249	atf_add_test_case ignore_case
250	atf_add_test_case invert
251	atf_add_test_case whole_line
252	atf_add_test_case negative
253	atf_add_test_case context
254	atf_add_test_case file_exp
255	atf_add_test_case egrep
256	atf_add_test_case zgrep
257	atf_add_test_case nonexistent
258	atf_add_test_case context2
259}
260