1 # $NetBSD: t_grep.sh,v 1.3 2017/01/14 20:43:52 christos Exp $
3 # Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
31 atf_set "descr" "Checks basic functionality"
35 atf_check -o file:"$(atf_get_srcdir)/d_basic.out" -x \
36 'jot 10000 | grep 123'
42 atf_set "descr" "Checks handling of binary files"
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
54 atf_set "descr" "Checks recursive searching"
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
62 atf_check -o file:"$(atf_get_srcdir)/d_recurse.out" -x "grep -r haddock recurse | sort"
65 atf_test_case recurse_symlink
66 recurse_symlink_head()
68 atf_set "descr" "Checks symbolic link recursion"
70 recurse_symlink_body()
73 (cd test/c/d && ln -s ../d .)
74 echo "Test string" > test/c/match
76 atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
77 -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
81 atf_test_case word_regexps
84 atf_set "descr" "Checks word-regexps"
88 atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
89 grep -w separated $(atf_get_srcdir)/d_input
92 atf_test_case begin_end
95 atf_set "descr" "Checks handling of line beginnings and ends"
99 atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
100 grep ^Front "$(atf_get_srcdir)/d_input"
102 atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
103 grep ending$ "$(atf_get_srcdir)/d_input"
106 atf_test_case ignore_case
109 atf_set "descr" "Checks ignore-case option"
113 atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
114 grep -i Upper "$(atf_get_srcdir)/d_input"
120 atf_set "descr" "Checks selecting non-matching lines with -v option"
124 atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
125 grep -v fish "$(atf_get_srcdir)/d_invert.in"
128 atf_test_case whole_line
131 atf_set "descr" "Checks whole-line matching with -x flag"
135 atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
136 grep -x matchme "$(atf_get_srcdir)/d_input"
139 atf_test_case negative
142 atf_set "descr" "Checks handling of files with no matches"
146 atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
149 atf_test_case context
152 atf_set "descr" "Checks displaying context with -A, -B and -C flags"
156 cp $(atf_get_srcdir)/d_context_*.* .
158 atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
159 atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
160 atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
161 atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
164 atf_test_case file_exp
167 atf_set "descr" "Checks reading expressions from file"
171 atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
172 'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
178 atf_set "descr" "Checks matching special characters with egrep"
182 atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
183 egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
189 atf_set "descr" "Checks handling of gzipped files with zgrep"
193 cp "$(atf_get_srcdir)/d_input" .
194 gzip d_input || atf_fail "gzip failed"
196 atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
199 atf_test_case nonexistent
202 atf_set "descr" "Checks that -s flag suppresses error" \
203 "messages about nonexistent files"
207 atf_check -s ne:0 grep -s foobar nonexistent
210 atf_test_case context2
213 atf_set "descr" "Checks displaying context with -z flag"
217 printf "haddock\000cod\000plaice\000" > test1
218 printf "mackeral\000cod\000crab\000" > test2
220 atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
221 grep -z -A1 cod test1 test2
223 atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
224 grep -z -B1 cod test1 test2
226 atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
227 grep -z -C1 cod test1 test2
230 atf_init_test_cases()
232 atf_add_test_case basic
233 atf_add_test_case binary
234 atf_add_test_case recurse
235 atf_add_test_case recurse_symlink
236 atf_add_test_case word_regexps
237 atf_add_test_case begin_end
238 atf_add_test_case ignore_case
239 atf_add_test_case invert
240 atf_add_test_case whole_line
241 atf_add_test_case negative
242 atf_add_test_case context
243 atf_add_test_case file_exp
244 atf_add_test_case egrep
245 atf_add_test_case zgrep
246 atf_add_test_case nonexistent
247 atf_add_test_case context2