t_grep.sh revision 1.6 1 1.6 rillig # $NetBSD: t_grep.sh,v 1.6 2021/08/30 23:14:14 rillig Exp $
2 1.1 jruoho #
3 1.1 jruoho # Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
4 1.1 jruoho # All rights reserved.
5 1.1 jruoho #
6 1.1 jruoho # Redistribution and use in source and binary forms, with or without
7 1.1 jruoho # modification, are permitted provided that the following conditions
8 1.1 jruoho # are met:
9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright
10 1.1 jruoho # notice, this list of conditions and the following disclaimer.
11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright
12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the
13 1.1 jruoho # documentation and/or other materials provided with the distribution.
14 1.1 jruoho #
15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE.
26 1.1 jruoho #
27 1.1 jruoho
28 1.1 jruoho atf_test_case basic
29 1.1 jruoho basic_head()
30 1.1 jruoho {
31 1.1 jruoho atf_set "descr" "Checks basic functionality"
32 1.1 jruoho }
33 1.1 jruoho basic_body()
34 1.1 jruoho {
35 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
36 1.1 jruoho 'jot 10000 | grep 123'
37 1.1 jruoho }
38 1.1 jruoho
39 1.1 jruoho atf_test_case binary
40 1.1 jruoho binary_head()
41 1.1 jruoho {
42 1.1 jruoho atf_set "descr" "Checks handling of binary files"
43 1.1 jruoho }
44 1.1 jruoho binary_body()
45 1.1 jruoho {
46 1.3 christos dd if=/dev/zero count=1 of=test.file
47 1.3 christos echo -n "foobar" >> test.file
48 1.3 christos atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
49 1.1 jruoho }
50 1.1 jruoho
51 1.1 jruoho atf_test_case recurse
52 1.1 jruoho recurse_head()
53 1.1 jruoho {
54 1.1 jruoho atf_set "descr" "Checks recursive searching"
55 1.1 jruoho }
56 1.1 jruoho recurse_body()
57 1.1 jruoho {
58 1.1 jruoho mkdir -p recurse/a/f recurse/d
59 1.1 jruoho echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
60 1.1 jruoho echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
61 1.1 jruoho
62 1.3 christos atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
63 1.1 jruoho }
64 1.1 jruoho
65 1.1 jruoho atf_test_case recurse_symlink
66 1.1 jruoho recurse_symlink_head()
67 1.1 jruoho {
68 1.1 jruoho atf_set "descr" "Checks symbolic link recursion"
69 1.1 jruoho }
70 1.1 jruoho recurse_symlink_body()
71 1.1 jruoho {
72 1.1 jruoho mkdir -p test/c/d
73 1.1 jruoho (cd test/c/d && ln -s ../d .)
74 1.1 jruoho echo "Test string" > test/c/match
75 1.1 jruoho
76 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
77 1.1 jruoho -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
78 1.1 jruoho grep -r string test
79 1.1 jruoho }
80 1.1 jruoho
81 1.1 jruoho atf_test_case word_regexps
82 1.1 jruoho word_regexps_head()
83 1.1 jruoho {
84 1.1 jruoho atf_set "descr" "Checks word-regexps"
85 1.1 jruoho }
86 1.1 jruoho word_regexps_body()
87 1.1 jruoho {
88 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
89 1.1 jruoho grep -w separated $(atf_get_srcdir)/d_input
90 1.1 jruoho }
91 1.1 jruoho
92 1.4 rillig atf_test_case word_locale
93 1.4 rillig word_locale_head()
94 1.4 rillig {
95 1.4 rillig atf_set "descr" "Checks word search with locale"
96 1.4 rillig }
97 1.4 rillig word_locale_body()
98 1.4 rillig {
99 1.4 rillig echo "array[]" > "input"
100 1.4 rillig
101 1.4 rillig # In the default locale, word search works.
102 1.4 rillig atf_check -o file:"input" \
103 1.4 rillig env LC_ALL=C grep "array" "input"
104 1.4 rillig atf_check -o file:"input" \
105 1.4 rillig env LC_ALL=C grep -w "array" "input"
106 1.4 rillig
107 1.5 rillig # XXX: In an UTF-8 locale, GNU Grep treats '[' as a word character.
108 1.4 rillig atf_check -s exit:1 -o empty \
109 1.4 rillig env LC_ALL="C.UTF-8" grep -w "array" "input"
110 1.4 rillig }
111 1.4 rillig
112 1.5 rillig atf_test_case word_in_line
113 1.5 rillig word_in_line_head()
114 1.5 rillig {
115 1.6 rillig atf_set "descr" "Checks word search in different locations of a line"
116 1.5 rillig }
117 1.5 rillig word_in_line_body()
118 1.5 rillig {
119 1.5 rillig # See usr.bin/grep/util.c, "Check for whole word match", which
120 1.5 rillig # looks suspiciously wrong. And indeed, NetBSD grep does not
121 1.5 rillig # survive this test. GNU Grep does.
122 1.5 rillig
123 1.5 rillig echo "begin middle end" > "input"
124 1.5 rillig
125 1.5 rillig # A word at the beginning of a line is found.
126 1.5 rillig atf_check -o file:"input" \
127 1.5 rillig env LC_ALL=C grep -w "begin" "input"
128 1.5 rillig
129 1.5 rillig # A word in the middle of a line is found.
130 1.5 rillig atf_check -o file:"input" \
131 1.5 rillig env LC_ALL=C grep -w "middle" "input"
132 1.5 rillig
133 1.5 rillig # A word at the end of a line is found.
134 1.5 rillig atf_check -o file:"input" \
135 1.5 rillig env LC_ALL=C grep -w "end" "input"
136 1.5 rillig
137 1.5 rillig # A subword at the beginning of a line is not found.
138 1.5 rillig atf_check -s exit:1 -o empty \
139 1.5 rillig env LC_ALL=C grep -w "be" "input"
140 1.5 rillig
141 1.5 rillig # A subword in the middle of a line is not found.
142 1.5 rillig atf_check -s exit:1 -o empty \
143 1.5 rillig env LC_ALL=C grep -w "mid" "input"
144 1.5 rillig atf_check -s exit:1 -o empty \
145 1.5 rillig env LC_ALL=C grep -w "dle" "input"
146 1.5 rillig
147 1.5 rillig # A subword at the end of a line is not found.
148 1.5 rillig atf_check -s exit:1 -o empty \
149 1.5 rillig env LC_ALL=C grep -w "nd" "input"
150 1.5 rillig }
151 1.5 rillig
152 1.5 rillig atf_test_case word_in_line_utf8
153 1.5 rillig word_in_line_utf8_head()
154 1.5 rillig {
155 1.5 rillig atf_set "descr" "Checks word search at the beginning of a line"
156 1.5 rillig }
157 1.5 rillig word_in_line_utf8_body()
158 1.5 rillig {
159 1.5 rillig # See usr.bin/grep/util.c, "Check for whole word match", which
160 1.5 rillig # looks suspiciously wrong. And indeed, NetBSD grep does not
161 1.5 rillig # survive this test. GNU Grep does.
162 1.5 rillig
163 1.5 rillig echo "begin middle end" > "input"
164 1.5 rillig
165 1.5 rillig # A word at the beginning of a line is found.
166 1.5 rillig atf_check -o file:"input" \
167 1.5 rillig env LC_ALL="C.UTF-8" grep -w "begin" "input"
168 1.5 rillig
169 1.5 rillig # A word in the middle of a line is found.
170 1.5 rillig atf_check -o file:"input" \
171 1.5 rillig env LC_ALL="C.UTF-8" grep -w "middle" "input"
172 1.5 rillig
173 1.5 rillig # A word at the end of a line is found.
174 1.5 rillig atf_check -o file:"input" \
175 1.5 rillig env LC_ALL="C.UTF-8" grep -w "end" "input"
176 1.5 rillig
177 1.5 rillig # A subword at the beginning of a line is not found.
178 1.5 rillig atf_check -s exit:1 -o empty \
179 1.5 rillig env LC_ALL="C.UTF-8" grep -w "be" "input"
180 1.5 rillig
181 1.5 rillig # A subword in the middle of a line is not found.
182 1.5 rillig atf_check -s exit:1 -o empty \
183 1.5 rillig env LC_ALL="C.UTF-8" grep -w "mid" "input"
184 1.5 rillig atf_check -s exit:1 -o empty \
185 1.5 rillig env LC_ALL="C.UTF-8" grep -w "dle" "input"
186 1.5 rillig
187 1.5 rillig # A subword at the end of a line is not found.
188 1.5 rillig atf_check -s exit:1 -o empty \
189 1.5 rillig env LC_ALL="C.UTF-8" grep -w "nd" "input"
190 1.5 rillig }
191 1.5 rillig
192 1.1 jruoho atf_test_case begin_end
193 1.1 jruoho begin_end_head()
194 1.1 jruoho {
195 1.1 jruoho atf_set "descr" "Checks handling of line beginnings and ends"
196 1.1 jruoho }
197 1.1 jruoho begin_end_body()
198 1.1 jruoho {
199 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
200 1.1 jruoho grep ^Front "$(atf_get_srcdir)/d_input"
201 1.1 jruoho
202 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
203 1.1 jruoho grep ending$ "$(atf_get_srcdir)/d_input"
204 1.1 jruoho }
205 1.1 jruoho
206 1.1 jruoho atf_test_case ignore_case
207 1.1 jruoho ignore_case_head()
208 1.1 jruoho {
209 1.1 jruoho atf_set "descr" "Checks ignore-case option"
210 1.1 jruoho }
211 1.1 jruoho ignore_case_body()
212 1.1 jruoho {
213 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
214 1.1 jruoho grep -i Upper "$(atf_get_srcdir)/d_input"
215 1.1 jruoho }
216 1.1 jruoho
217 1.1 jruoho atf_test_case invert
218 1.1 jruoho invert_head()
219 1.1 jruoho {
220 1.1 jruoho atf_set "descr" "Checks selecting non-matching lines with -v option"
221 1.1 jruoho }
222 1.1 jruoho invert_body()
223 1.1 jruoho {
224 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
225 1.1 jruoho grep -v fish "$(atf_get_srcdir)/d_invert.in"
226 1.1 jruoho }
227 1.1 jruoho
228 1.1 jruoho atf_test_case whole_line
229 1.1 jruoho whole_line_head()
230 1.1 jruoho {
231 1.1 jruoho atf_set "descr" "Checks whole-line matching with -x flag"
232 1.1 jruoho }
233 1.1 jruoho whole_line_body()
234 1.1 jruoho {
235 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
236 1.1 jruoho grep -x matchme "$(atf_get_srcdir)/d_input"
237 1.1 jruoho }
238 1.1 jruoho
239 1.1 jruoho atf_test_case negative
240 1.1 jruoho negative_head()
241 1.1 jruoho {
242 1.1 jruoho atf_set "descr" "Checks handling of files with no matches"
243 1.1 jruoho }
244 1.1 jruoho negative_body()
245 1.1 jruoho {
246 1.1 jruoho atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
247 1.1 jruoho }
248 1.1 jruoho
249 1.1 jruoho atf_test_case context
250 1.1 jruoho context_head()
251 1.1 jruoho {
252 1.1 jruoho atf_set "descr" "Checks displaying context with -A, -B and -C flags"
253 1.1 jruoho }
254 1.1 jruoho context_body()
255 1.1 jruoho {
256 1.1 jruoho cp $(atf_get_srcdir)/d_context_*.* .
257 1.1 jruoho
258 1.1 jruoho atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
259 1.1 jruoho atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
260 1.1 jruoho atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
261 1.1 jruoho atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
262 1.1 jruoho }
263 1.1 jruoho
264 1.1 jruoho atf_test_case file_exp
265 1.1 jruoho file_exp_head()
266 1.1 jruoho {
267 1.1 jruoho atf_set "descr" "Checks reading expressions from file"
268 1.1 jruoho }
269 1.1 jruoho file_exp_body()
270 1.1 jruoho {
271 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
272 1.1 jruoho 'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
273 1.1 jruoho }
274 1.1 jruoho
275 1.1 jruoho atf_test_case egrep
276 1.1 jruoho egrep_head()
277 1.1 jruoho {
278 1.1 jruoho atf_set "descr" "Checks matching special characters with egrep"
279 1.1 jruoho }
280 1.1 jruoho egrep_body()
281 1.1 jruoho {
282 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
283 1.1 jruoho egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
284 1.1 jruoho }
285 1.1 jruoho
286 1.1 jruoho atf_test_case zgrep
287 1.1 jruoho zgrep_head()
288 1.1 jruoho {
289 1.1 jruoho atf_set "descr" "Checks handling of gzipped files with zgrep"
290 1.1 jruoho }
291 1.1 jruoho zgrep_body()
292 1.1 jruoho {
293 1.1 jruoho cp "$(atf_get_srcdir)/d_input" .
294 1.1 jruoho gzip d_input || atf_fail "gzip failed"
295 1.1 jruoho
296 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
297 1.1 jruoho }
298 1.1 jruoho
299 1.1 jruoho atf_test_case nonexistent
300 1.1 jruoho nonexistent_head()
301 1.1 jruoho {
302 1.1 jruoho atf_set "descr" "Checks that -s flag suppresses error" \
303 1.1 jruoho "messages about nonexistent files"
304 1.1 jruoho }
305 1.1 jruoho nonexistent_body()
306 1.1 jruoho {
307 1.1 jruoho atf_check -s ne:0 grep -s foobar nonexistent
308 1.1 jruoho }
309 1.1 jruoho
310 1.1 jruoho atf_test_case context2
311 1.1 jruoho context2_head()
312 1.1 jruoho {
313 1.1 jruoho atf_set "descr" "Checks displaying context with -z flag"
314 1.1 jruoho }
315 1.1 jruoho context2_body()
316 1.1 jruoho {
317 1.1 jruoho printf "haddock\000cod\000plaice\000" > test1
318 1.1 jruoho printf "mackeral\000cod\000crab\000" > test2
319 1.1 jruoho
320 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
321 1.1 jruoho grep -z -A1 cod test1 test2
322 1.1 jruoho
323 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
324 1.1 jruoho grep -z -B1 cod test1 test2
325 1.1 jruoho
326 1.1 jruoho atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
327 1.1 jruoho grep -z -C1 cod test1 test2
328 1.1 jruoho }
329 1.1 jruoho
330 1.1 jruoho atf_init_test_cases()
331 1.1 jruoho {
332 1.1 jruoho atf_add_test_case basic
333 1.1 jruoho atf_add_test_case binary
334 1.1 jruoho atf_add_test_case recurse
335 1.1 jruoho atf_add_test_case recurse_symlink
336 1.1 jruoho atf_add_test_case word_regexps
337 1.4 rillig atf_add_test_case word_locale
338 1.5 rillig atf_add_test_case word_in_line
339 1.5 rillig atf_add_test_case word_in_line_utf8
340 1.1 jruoho atf_add_test_case begin_end
341 1.1 jruoho atf_add_test_case ignore_case
342 1.1 jruoho atf_add_test_case invert
343 1.1 jruoho atf_add_test_case whole_line
344 1.1 jruoho atf_add_test_case negative
345 1.1 jruoho atf_add_test_case context
346 1.1 jruoho atf_add_test_case file_exp
347 1.1 jruoho atf_add_test_case egrep
348 1.1 jruoho atf_add_test_case zgrep
349 1.1 jruoho atf_add_test_case nonexistent
350 1.1 jruoho atf_add_test_case context2
351 1.1 jruoho }
352