]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/netbsd-tests/usr.bin/grep/t_grep.sh
Merge ^/vendor/lld/dist up to its last change, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / netbsd-tests / usr.bin / grep / t_grep.sh
1 # $NetBSD: t_grep.sh,v 1.3 2017/01/14 20:43:52 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
28 atf_test_case basic
29 basic_head()
30 {
31         atf_set "descr" "Checks basic functionality"
32 }
33 basic_body()
34
35         atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
36             'jot 10000 | grep 123'
37 }
38
39 atf_test_case binary
40 binary_head()
41 {
42         atf_set "descr" "Checks handling of binary files"
43 }
44 binary_body()
45 {
46         dd if=/dev/zero count=1 of=test.file
47         echo -n "foobar" >> test.file
48         atf_check -o file:"$(atf_get_srcdir)/d_binary.out" grep foobar test.file
49 }
50
51 atf_test_case recurse
52 recurse_head()
53 {
54         atf_set "descr" "Checks recursive searching"
55 }
56 recurse_body()
57 {
58         mkdir -p recurse/a/f recurse/d
59         echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
60         echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
61
62         atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
63 }
64
65 atf_test_case recurse_symlink
66 recurse_symlink_head()
67 {
68         atf_set "descr" "Checks symbolic link recursion"
69 }
70 recurse_symlink_body()
71 {
72         # Begin FreeBSD
73         grep_type
74         if [ $? -eq $GREP_TYPE_GNU ]; then
75                 atf_expect_fail "this test doesn't pass with gnu grep from ports"
76         fi
77         # End FreeBSD
78         mkdir -p test/c/d
79         (cd test/c/d && ln -s ../d .)
80         echo "Test string" > test/c/match
81
82         atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
83             -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
84             grep -r string test
85 }
86
87 atf_test_case word_regexps
88 word_regexps_head()
89 {
90         atf_set "descr" "Checks word-regexps"
91 }
92 word_regexps_body()
93 {
94         atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
95             grep -w separated $(atf_get_srcdir)/d_input
96
97         # Begin FreeBSD
98         printf "xmatch pmatch\n" > test1
99
100         atf_check -o inline:"pmatch\n" grep -Eow "(match )?pmatch" test1
101         # End FreeBSD
102 }
103
104 atf_test_case begin_end
105 begin_end_head()
106 {
107         atf_set "descr" "Checks handling of line beginnings and ends"
108 }
109 begin_end_body()
110 {
111         atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
112             grep ^Front "$(atf_get_srcdir)/d_input"
113
114         atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
115             grep ending$ "$(atf_get_srcdir)/d_input"
116 }
117
118 atf_test_case ignore_case
119 ignore_case_head()
120 {
121         atf_set "descr" "Checks ignore-case option"
122 }
123 ignore_case_body()
124 {
125         atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
126             grep -i Upper "$(atf_get_srcdir)/d_input"
127 }
128
129 atf_test_case invert
130 invert_head()
131 {
132         atf_set "descr" "Checks selecting non-matching lines with -v option"
133 }
134 invert_body()
135 {
136         atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
137             grep -v fish "$(atf_get_srcdir)/d_invert.in"
138 }
139
140 atf_test_case whole_line
141 whole_line_head()
142 {
143         atf_set "descr" "Checks whole-line matching with -x flag"
144 }
145 whole_line_body()
146 {
147         atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
148             grep -x matchme "$(atf_get_srcdir)/d_input"
149 }
150
151 atf_test_case negative
152 negative_head()
153 {
154         atf_set "descr" "Checks handling of files with no matches"
155 }
156 negative_body()
157 {
158         atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
159 }
160
161 atf_test_case context
162 context_head()
163 {
164         atf_set "descr" "Checks displaying context with -A, -B and -C flags"
165 }
166 context_body()
167 {
168         cp $(atf_get_srcdir)/d_context_*.* .
169
170         atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
171         atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
172         atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
173         atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
174         atf_check -o file:d_context_e.out \
175             grep -E -C1 '(banana|monkey)' d_context_e.in
176         atf_check -o file:d_context_f.out \
177             grep -Ev -B2 '(banana|monkey|fruit)' d_context_e.in
178         atf_check -o file:d_context_g.out \
179             grep -Ev -A1 '(banana|monkey|fruit)' d_context_e.in
180 }
181
182 atf_test_case file_exp
183 file_exp_head()
184 {
185         atf_set "descr" "Checks reading expressions from file"
186 }
187 file_exp_body()
188 {
189         atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
190             'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
191 }
192
193 atf_test_case egrep
194 egrep_head()
195 {
196         atf_set "descr" "Checks matching special characters with egrep"
197 }
198 egrep_body()
199 {
200         atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
201                 egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
202 }
203
204 atf_test_case zgrep
205 zgrep_head()
206 {
207         atf_set "descr" "Checks handling of gzipped files with zgrep"
208 }
209 zgrep_body()
210 {
211         cp "$(atf_get_srcdir)/d_input" .
212         gzip d_input || atf_fail "gzip failed"
213
214         atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
215 }
216
217 atf_test_case nonexistent
218 nonexistent_head()
219 {
220         atf_set "descr" "Checks that -s flag suppresses error" \
221                         "messages about nonexistent files"
222 }
223 nonexistent_body()
224 {
225         atf_check -s ne:0 grep -s foobar nonexistent
226 }
227
228 atf_test_case context2
229 context2_head()
230 {
231         atf_set "descr" "Checks displaying context with -z flag"
232 }
233 context2_body()
234 {
235         printf "haddock\000cod\000plaice\000" > test1
236         printf "mackeral\000cod\000crab\000" > test2
237
238         atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
239             grep -z -A1 cod test1 test2
240
241         atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
242             grep -z -B1 cod test1 test2
243
244         atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
245             grep -z -C1 cod test1 test2
246 }
247 # Begin FreeBSD
248
249 # What grep(1) are we working with?
250 # - 0 : bsdgrep
251 # - 1 : gnu grep 2.51 (base)
252 # - 2 : gnu grep (ports)
253 GREP_TYPE_BSD=0
254 GREP_TYPE_GNU_FREEBSD=1
255 GREP_TYPE_GNU=2
256 GREP_TYPE_UNKNOWN=3
257
258 grep_type()
259 {
260         local grep_version=$(grep --version)
261
262         case "$grep_version" in
263         *"BSD grep"*)
264                 return $GREP_TYPE_BSD
265                 ;;
266         *"GNU grep"*)
267                 case "$grep_version" in
268                 *2.5.1-FreeBSD*)
269                         return $GREP_TYPE_GNU_FREEBSD
270                         ;;
271                 *)
272                         return $GREP_TYPE_GNU
273                         ;;
274                 esac
275                 ;;
276         esac
277         atf_fail "unknown grep type: $grep_version"
278 }
279
280 atf_test_case oflag_zerolen
281 oflag_zerolen_head()
282 {
283         atf_set "descr" "Check behavior of zero-length matches with -o flag (PR 195763)"
284 }
285 oflag_zerolen_body()
286 {
287         grep_type
288         if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
289                 atf_expect_fail "this test doesn't pass with gnu grep in base"
290         fi
291
292         atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_a.out" \
293             grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_a.in"
294
295         atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_b.out" \
296             grep -Eo '(^|:)0*' "$(atf_get_srcdir)/d_oflag_zerolen_b.in"
297
298         atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_c.out" \
299             grep -Eo '[[:alnum:]]*' "$(atf_get_srcdir)/d_oflag_zerolen_c.in"
300
301         atf_check -o empty grep -Eo '' "$(atf_get_srcdir)/d_oflag_zerolen_d.in"
302
303         atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
304             grep -o -e 'ab' -e 'bc' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
305
306         atf_check -o file:"$(atf_get_srcdir)/d_oflag_zerolen_e.out" \
307             grep -o -e 'bc' -e 'ab' "$(atf_get_srcdir)/d_oflag_zerolen_e.in"
308 }
309
310 atf_test_case xflag
311 xflag_head()
312 {
313         atf_set "descr" "Check that we actually get a match with -x flag (PR 180990)"
314 }
315 xflag_body()
316 {
317         echo 128 > match_file
318         seq 1 128 > pattern_file
319         grep -xf pattern_file match_file
320 }
321
322 atf_test_case color
323 color_head()
324 {
325         atf_set "descr" "Check --color support"
326 }
327 color_body()
328 {
329         grep_type
330         if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
331                 atf_expect_fail "this test doesn't pass with gnu grep in base"
332         fi
333
334         echo 'abcd*' > grepfile
335         echo 'abc$' >> grepfile
336         echo '^abc' >> grepfile
337
338         atf_check -o file:"$(atf_get_srcdir)/d_color_a.out" \
339             grep --color=auto -e '.*' -e 'a' "$(atf_get_srcdir)/d_color_a.in"
340
341         atf_check -o file:"$(atf_get_srcdir)/d_color_b.out" \
342             grep --color=auto -f grepfile "$(atf_get_srcdir)/d_color_b.in"
343
344         atf_check -o file:"$(atf_get_srcdir)/d_color_c.out" \
345             grep --color=always -f grepfile "$(atf_get_srcdir)/d_color_b.in"
346 }
347
348 atf_test_case f_file_empty
349 f_file_empty_head()
350 {
351         atf_set "descr" "Check for handling of a null byte in empty file, specified by -f (PR 202022)"
352 }
353 f_file_empty_body()
354 {
355         printf "\0\n" > nulpat
356
357         atf_check -s exit:1 grep -f nulpat "$(atf_get_srcdir)/d_f_file_empty.in"
358 }
359
360 atf_test_case escmap
361 escmap_head()
362 {
363         atf_set "descr" "Check proper handling of escaped vs. unescaped dot expressions (PR 175314)"
364 }
365 escmap_body()
366 {
367         atf_check -s exit:1 grep -o 'f.o\.' "$(atf_get_srcdir)/d_escmap.in"
368         atf_check -o not-empty grep -o 'f.o.' "$(atf_get_srcdir)/d_escmap.in"
369 }
370
371 atf_test_case egrep_empty_invalid
372 egrep_empty_invalid_head()
373 {
374         atf_set "descr" "Check for handling of an invalid empty pattern (PR 194823)"
375 }
376 egrep_empty_invalid_body()
377 {
378         atf_check -e ignore -s not-exit:0 egrep '{' /dev/null
379 }
380
381 atf_test_case zerolen
382 zerolen_head()
383 {
384         atf_set "descr" "Check for successful zero-length matches with ^$"
385 }
386 zerolen_body()
387 {
388         printf "Eggs\n\nCheese" > test1
389
390         atf_check -o inline:"\n" grep -e "^$" test1
391
392         atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1
393 }
394
395 atf_test_case wflag_emptypat
396 wflag_emptypat_head()
397 {
398         atf_set "descr" "Check for proper handling of -w with an empty pattern (PR 105221)"
399 }
400 wflag_emptypat_body()
401 {
402         printf "" > test1
403         printf "\n" > test2
404         printf "qaz" > test3
405         printf " qaz\n" > test4
406
407         atf_check -s exit:1 -o empty grep -w -e "" test1
408
409         atf_check -o file:test2 grep -w -e "" test2
410
411         atf_check -s exit:1 -o empty grep -w -e "" test3
412
413         atf_check -o file:test4 grep -w -e "" test4
414 }
415
416 atf_test_case xflag_emptypat
417 xflag_emptypat_body()
418 {
419         printf "" > test1
420         printf "\n" > test2
421         printf "qaz" > test3
422         printf " qaz\n" > test4
423
424         # -x is whole-line, more strict than -w.
425         atf_check -s exit:1 -o empty grep -x -e "" test1
426
427         atf_check -o file:test2 grep -x -e "" test2
428
429         atf_check -s exit:1 -o empty grep -x -e "" test3
430
431         atf_check -s exit:1 -o empty grep -x -e "" test4
432
433         total=$(wc -l /COPYRIGHT | sed 's/[^0-9]//g')
434
435         # Simple checks that grep -x with an empty pattern isn't matching every
436         # line.  The exact counts aren't important, as long as they don't
437         # match the total line count and as long as they don't match each other.
438         atf_check -o save:xpositive.count grep -Fxc '' /COPYRIGHT
439         atf_check -o save:xnegative.count grep -Fvxc '' /COPYRIGHT
440
441         atf_check -o not-inline:"${total}" cat xpositive.count
442         atf_check -o not-inline:"${total}" cat xnegative.count
443
444         atf_check -o not-file:xnegative.count cat xpositive.count
445 }
446
447 atf_test_case xflag_emptypat_plus
448 xflag_emptypat_plus_body()
449 {
450         printf "foo\n\nbar\n\nbaz\n" > target
451         printf "foo\n \nbar\n \nbaz\n" > target_spacelines
452         printf "foo\nbar\nbaz\n" > matches
453         printf " \n \n" > spacelines
454
455         printf "foo\n\nbar\n\nbaz\n" > patlist1
456         printf "foo\n\nba\n\nbaz\n" > patlist2
457
458         sed -e '/bar/d' target > matches_not2
459
460         # Normal handling first
461         atf_check -o file:target grep -Fxf patlist1 target
462         atf_check -o file:matches grep -Fxf patlist1 target_spacelines
463         atf_check -o file:matches_not2 grep -Fxf patlist2 target
464
465         # -v handling
466         atf_check -s exit:1 -o empty grep -Fvxf patlist1 target
467         atf_check -o file:spacelines grep -Fxvf patlist1 target_spacelines
468 }
469
470 atf_test_case excessive_matches
471 excessive_matches_head()
472 {
473         atf_set "descr" "Check for proper handling of lines with excessive matches (PR 218811)"
474 }
475 excessive_matches_body()
476 {
477         grep_type
478         if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
479                 atf_expect_fail "this test does not pass with GNU grep in base"
480         fi
481
482         for i in $(jot 4096); do
483                 printf "x" >> test.in
484         done
485
486         atf_check -s exit:0 -x '[ $(grep -o x test.in | wc -l) -eq 4096 ]'
487         atf_check -s exit:1 -x 'grep -on x test.in | grep -v "1:x"'
488 }
489
490 atf_test_case fgrep_sanity
491 fgrep_sanity_head()
492 {
493         atf_set "descr" "Check for fgrep sanity, literal expressions only"
494 }
495 fgrep_sanity_body()
496 {
497         printf "Foo" > test1
498
499         atf_check -o inline:"Foo\n" fgrep -e "Foo" test1
500
501         atf_check -s exit:1 -o empty fgrep -e "Fo." test1
502 }
503
504 atf_test_case egrep_sanity
505 egrep_sanity_head()
506 {
507         atf_set "descr" "Check for egrep sanity, EREs only"
508 }
509 egrep_sanity_body()
510 {
511         printf "Foobar(ed)" > test1
512         printf "M{1}" > test2
513
514         atf_check -o inline:"Foo\n" egrep -o -e "F.." test1
515
516         atf_check -o inline:"Foobar\n" egrep -o -e "F[a-z]*" test1
517
518         atf_check -o inline:"Fo\n" egrep -o -e "F(o|p)" test1
519
520         atf_check -o inline:"(ed)\n" egrep -o -e "\(ed\)" test1
521
522         atf_check -o inline:"M\n" egrep -o -e "M{1}" test2
523
524         atf_check -o inline:"M{1}\n" egrep -o -e "M\{1\}" test2
525 }
526
527 atf_test_case grep_sanity
528 grep_sanity_head()
529 {
530         atf_set "descr" "Check for basic grep sanity, BREs only"
531 }
532 grep_sanity_body()
533 {
534         printf "Foobar(ed)" > test1
535         printf "M{1}" > test2
536
537         atf_check -o inline:"Foo\n" grep -o -e "F.." test1
538
539         atf_check -o inline:"Foobar\n" grep -o -e "F[a-z]*" test1
540
541         atf_check -o inline:"Fo\n" grep -o -e "F\(o\)" test1
542
543         atf_check -o inline:"(ed)\n" grep -o -e "(ed)" test1
544
545         atf_check -o inline:"M{1}\n" grep -o -e "M{1}" test2
546
547         atf_check -o inline:"M\n" grep -o -e "M\{1\}" test2
548 }
549
550 atf_test_case wv_combo_break
551 wv_combo_break_head()
552 {
553         atf_set "descr" "Check for incorrectly matching lines with both -w and -v flags (PR 218467)"
554 }
555 wv_combo_break_body()
556 {
557         printf "x xx\n" > test1
558         printf "xx x\n" > test2
559
560         atf_check -o file:test1 grep -w "x" test1
561         atf_check -o file:test2 grep -w "x" test2
562
563         atf_check -s exit:1 grep -v -w "x" test1
564         atf_check -s exit:1 grep -v -w "x" test2
565 }
566
567 atf_test_case ocolor_metadata
568 ocolor_metadata_head()
569 {
570         atf_set "descr" "Check for -n/-b producing per-line metadata output"
571 }
572 ocolor_metadata_body()
573 {
574         grep_type
575         if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
576                 atf_expect_fail "this test does not pass with GNU grep in base"
577         fi
578
579         printf "xxx\nyyyy\nzzz\nfoobarbaz\n" > test1
580         check_expr="^[^:]*[0-9][^:]*:[^:]+$"
581
582         atf_check -o inline:"1:1:xx\n" grep -bon "xx$" test1
583
584         atf_check -o inline:"2:4:yyyy\n" grep -bn "yy" test1
585
586         atf_check -o inline:"2:6:yy\n" grep -bon "yy$" test1
587
588         # These checks ensure that grep isn't producing bogus line numbering
589         # in the middle of a line.
590         atf_check -s exit:1 -x \
591             "grep -Eon 'x|y|z|f' test1 | grep -Ev '${check_expr}'"
592
593         atf_check -s exit:1 -x \
594             "grep -En 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
595
596         atf_check -s exit:1 -x \
597             "grep -Eon 'x|y|z|f' --color=always test1 | grep -Ev '${check_expr}'"
598 }
599
600 atf_test_case grep_nomatch_flags
601 grep_nomatch_flags_head()
602 {
603         atf_set "descr" "Check for no match (-c, -l, -L, -q) flags not producing line matches or context (PR 219077)"
604 }
605
606 grep_nomatch_flags_body()
607 {
608         grep_type
609
610         if [ $? -eq $GREP_TYPE_GNU_FREEBSD ]; then
611                 atf_expect_fail "this test does not pass with GNU grep in base"
612         fi
613
614         printf "A\nB\nC\n" > test1
615
616         atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
617         atf_check -o inline:"1\n" grep -c -B 1 -e "B" test1
618         atf_check -o inline:"1\n" grep -c -A 1 -e "B" test1
619         atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1
620
621         atf_check -o inline:"test1\n" grep -l -e "B" test1
622         atf_check -o inline:"test1\n" grep -l -B 1 -e "B" test1
623         atf_check -o inline:"test1\n" grep -l -A 1 -e "B" test1
624         atf_check -o inline:"test1\n" grep -l -C 1 -e "B" test1
625
626         atf_check -o inline:"test1\n" grep -L -e "D" test1
627
628         atf_check -o empty grep -q -e "B" test1
629         atf_check -o empty grep -q -B 1 -e "B" test1
630         atf_check -o empty grep -q -A 1 -e "B" test1
631         atf_check -o empty grep -q -C 1 -e "B" test1
632 }
633
634 atf_test_case badcontext
635 badcontext_head()
636 {
637         atf_set "descr" "Check for handling of invalid context arguments"
638 }
639 badcontext_body()
640 {
641         printf "A\nB\nC\n" > test1
642
643         atf_check -s not-exit:0 -e ignore grep -A "-1" "B" test1
644
645         atf_check -s not-exit:0 -e ignore grep -B "-1" "B" test1
646
647         atf_check -s not-exit:0 -e ignore grep -C "-1" "B" test1
648
649         atf_check -s not-exit:0 -e ignore grep -A "B" "B" test1
650
651         atf_check -s not-exit:0 -e ignore grep -B "B" "B" test1
652
653         atf_check -s not-exit:0 -e ignore grep -C "B" "B" test1
654 }
655
656 atf_test_case binary_flags
657 binary_flags_head()
658 {
659         atf_set "descr" "Check output for binary flags (-a, -I, -U, --binary-files)"
660 }
661 binary_flags_body()
662 {
663         printf "A\000B\000C" > test1
664         printf "A\n\000B\n\000C" > test2
665         binmatchtext="Binary file test1 matches\n"
666
667         # Binaries not treated as text (default, -U)
668         atf_check -o inline:"${binmatchtext}" grep 'B' test1
669         atf_check -o inline:"${binmatchtext}" grep 'B' -C 1 test1
670
671         atf_check -o inline:"${binmatchtext}" grep -U 'B' test1
672         atf_check -o inline:"${binmatchtext}" grep -U 'B' -C 1 test1
673
674         # Binary, -a, no newlines
675         atf_check -o inline:"A\000B\000C\n" grep -a 'B' test1
676         atf_check -o inline:"A\000B\000C\n" grep -a 'B' -C 1 test1
677
678         # Binary, -a, newlines
679         atf_check -o inline:"\000B\n" grep -a 'B' test2
680         atf_check -o inline:"A\n\000B\n\000C\n" grep -a 'B' -C 1 test2
681
682         # Binary files ignored
683         atf_check -s exit:1 grep -I 'B' test2
684
685         # --binary-files equivalence
686         atf_check -o inline:"${binmatchtext}" grep --binary-files=binary 'B' test1
687         atf_check -o inline:"A\000B\000C\n" grep --binary-files=text 'B' test1
688         atf_check -s exit:1 grep --binary-files=without-match 'B' test2
689 }
690
691 atf_test_case mmap
692 mmap_head()
693 {
694         atf_set "descr" "Check basic matching with --mmap flag"
695 }
696 mmap_body()
697 {
698         grep_type
699         if [ $? -eq $GREP_TYPE_GNU ]; then
700                 atf_expect_fail "gnu grep from ports has no --mmap option"
701         fi
702
703         printf "A\nB\nC\n" > test1
704
705         atf_check -s exit:0 -o inline:"B\n" grep --mmap -oe "B" test1
706         atf_check -s exit:1 grep --mmap -e "Z" test1
707 }
708
709 atf_test_case matchall
710 matchall_head()
711 {
712         atf_set "descr" "Check proper behavior of matching all with an empty string"
713 }
714 matchall_body()
715 {
716         printf "" > test1
717         printf "A" > test2
718         printf "A\nB" > test3
719
720         atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test1 test2 test3
721         atf_check -o inline:"test3:A\ntest3:B\ntest2:A\n" grep "" test3 test1 test2
722         atf_check -o inline:"test2:A\ntest3:A\ntest3:B\n" grep "" test2 test3 test1
723
724         atf_check -s exit:1 grep "" test1
725 }
726
727 atf_test_case fgrep_multipattern
728 fgrep_multipattern_head()
729 {
730         atf_set "descr" "Check proper behavior with multiple patterns supplied to fgrep"
731 }
732 fgrep_multipattern_body()
733 {
734         printf "Foo\nBar\nBaz" > test1
735
736         atf_check -o inline:"Foo\nBaz\n" grep -F -e "Foo" -e "Baz" test1
737         atf_check -o inline:"Foo\nBaz\n" grep -F -e "Baz" -e "Foo" test1
738         atf_check -o inline:"Bar\nBaz\n" grep -F -e "Bar" -e "Baz" test1
739 }
740
741 atf_test_case fgrep_icase
742 fgrep_icase_head()
743 {
744         atf_set "descr" "Check proper handling of -i supplied to fgrep"
745 }
746 fgrep_icase_body()
747 {
748         printf "Foo\nBar\nBaz" > test1
749
750         atf_check -o inline:"Foo\nBaz\n" grep -Fi -e "foo" -e "baz" test1
751         atf_check -o inline:"Foo\nBaz\n" grep -Fi -e "baz" -e "foo" test1
752         atf_check -o inline:"Bar\nBaz\n" grep -Fi -e "bar" -e "baz" test1
753         atf_check -o inline:"Bar\nBaz\n" grep -Fi -e "BAR" -e "bAz" test1
754 }
755
756 atf_test_case fgrep_oflag
757 fgrep_oflag_head()
758 {
759         atf_set "descr" "Check proper handling of -o supplied to fgrep"
760 }
761 fgrep_oflag_body()
762 {
763         printf "abcdefghi\n" > test1
764
765         atf_check -o inline:"a\n" grep -Fo "a" test1
766         atf_check -o inline:"i\n" grep -Fo "i" test1
767         atf_check -o inline:"abc\n" grep -Fo "abc" test1
768         atf_check -o inline:"fgh\n" grep -Fo "fgh" test1
769         atf_check -o inline:"cde\n" grep -Fo "cde" test1
770         atf_check -o inline:"bcd\n" grep -Fo -e "bcd" -e "cde" test1
771         atf_check -o inline:"bcd\nefg\n" grep -Fo -e "bcd" -e "efg" test1
772
773         atf_check -s exit:1 grep -Fo "xabc" test1
774         atf_check -s exit:1 grep -Fo "abcx" test1
775         atf_check -s exit:1 grep -Fo "xghi" test1
776         atf_check -s exit:1 grep -Fo "ghix" test1
777         atf_check -s exit:1 grep -Fo "abcdefghiklmnopqrstuvwxyz" test1
778 }
779
780 atf_test_case cflag
781 cflag_head()
782 {
783         atf_set "descr" "Check proper handling of -c"
784 }
785 cflag_body()
786 {
787         printf "a\nb\nc\n" > test1
788
789         atf_check -o inline:"1\n" grep -Ec "a" test1
790         atf_check -o inline:"2\n" grep -Ec "a|b" test1
791         atf_check -o inline:"3\n" grep -Ec "a|b|c" test1
792
793         atf_check -o inline:"test1:2\n" grep -EHc "a|b" test1
794 }
795
796 atf_test_case mflag
797 mflag_head()
798 {
799         atf_set "descr" "Check proper handling of -m"
800 }
801 mflag_body()
802 {
803         printf "a\nb\nc\nd\ne\nf\n" > test1
804
805         atf_check -o inline:"1\n" grep -m 1 -Ec "a" test1
806         atf_check -o inline:"2\n" grep -m 2 -Ec "a|b" test1
807         atf_check -o inline:"3\n" grep -m 3 -Ec "a|b|c|f" test1
808
809         atf_check -o inline:"test1:2\n" grep -m 2 -EHc "a|b|e|f" test1
810 }
811 # End FreeBSD
812
813 atf_init_test_cases()
814 {
815         atf_add_test_case basic
816         atf_add_test_case binary
817         atf_add_test_case recurse
818         atf_add_test_case recurse_symlink
819         atf_add_test_case word_regexps
820         atf_add_test_case begin_end
821         atf_add_test_case ignore_case
822         atf_add_test_case invert
823         atf_add_test_case whole_line
824         atf_add_test_case negative
825         atf_add_test_case context
826         atf_add_test_case file_exp
827         atf_add_test_case egrep
828         atf_add_test_case zgrep
829         atf_add_test_case nonexistent
830         atf_add_test_case context2
831 # Begin FreeBSD
832         atf_add_test_case oflag_zerolen
833         atf_add_test_case xflag
834         atf_add_test_case color
835         atf_add_test_case f_file_empty
836         atf_add_test_case escmap
837         atf_add_test_case egrep_empty_invalid
838         atf_add_test_case zerolen
839         atf_add_test_case wflag_emptypat
840         atf_add_test_case xflag_emptypat
841         atf_add_test_case xflag_emptypat_plus
842         atf_add_test_case excessive_matches
843         atf_add_test_case wv_combo_break
844         atf_add_test_case fgrep_sanity
845         atf_add_test_case egrep_sanity
846         atf_add_test_case grep_sanity
847         atf_add_test_case ocolor_metadata
848         atf_add_test_case grep_nomatch_flags
849         atf_add_test_case binary_flags
850         atf_add_test_case badcontext
851         atf_add_test_case mmap
852         atf_add_test_case matchall
853         atf_add_test_case fgrep_multipattern
854         atf_add_test_case fgrep_icase
855         atf_add_test_case fgrep_oflag
856         atf_add_test_case cflag
857         atf_add_test_case mflag
858 # End FreeBSD
859 }