Home | History | Annotate | Line # | Download | only in grep
t_grep.sh revision 1.4
      1 # $NetBSD: t_grep.sh,v 1.4 2021/08/30 22:17:32 rillig 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 	mkdir -p test/c/d
     73 	(cd test/c/d && ln -s ../d .)
     74 	echo "Test string" > test/c/match
     75 
     76 	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
     77 	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
     78 	    grep -r string test
     79 }
     80 
     81 atf_test_case word_regexps
     82 word_regexps_head()
     83 {
     84 	atf_set "descr" "Checks word-regexps"
     85 }
     86 word_regexps_body()
     87 {
     88 	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
     89 	    grep -w separated $(atf_get_srcdir)/d_input
     90 }
     91 
     92 atf_test_case word_locale
     93 word_locale_head()
     94 {
     95 	atf_set "descr" "Checks word search with locale"
     96 }
     97 word_locale_body()
     98 {
     99 	echo "array[]" > "input"
    100 
    101 	# In the default locale, word search works.
    102 	atf_check -o file:"input" \
    103 	    env LC_ALL=C grep "array" "input"
    104 	atf_check -o file:"input" \
    105 	    env LC_ALL=C grep -w "array" "input"
    106 
    107 	# XXX: In an UTF-8 locale, '[' seems to be a word character.
    108 	atf_check -s exit:1 -o empty \
    109 	    env LC_ALL="C.UTF-8" grep -w "array" "input"
    110 }
    111 
    112 atf_test_case begin_end
    113 begin_end_head()
    114 {
    115 	atf_set "descr" "Checks handling of line beginnings and ends"
    116 }
    117 begin_end_body()
    118 {
    119 	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
    120 	    grep ^Front "$(atf_get_srcdir)/d_input"
    121 
    122 	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
    123 	    grep ending$ "$(atf_get_srcdir)/d_input"
    124 }
    125 
    126 atf_test_case ignore_case
    127 ignore_case_head()
    128 {
    129 	atf_set "descr" "Checks ignore-case option"
    130 }
    131 ignore_case_body()
    132 {
    133 	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
    134 	    grep -i Upper "$(atf_get_srcdir)/d_input"
    135 }
    136 
    137 atf_test_case invert
    138 invert_head()
    139 {
    140 	atf_set "descr" "Checks selecting non-matching lines with -v option"
    141 }
    142 invert_body()
    143 {
    144 	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
    145 	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
    146 }
    147 
    148 atf_test_case whole_line
    149 whole_line_head()
    150 {
    151 	atf_set "descr" "Checks whole-line matching with -x flag"
    152 }
    153 whole_line_body()
    154 {
    155 	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
    156 	    grep -x matchme "$(atf_get_srcdir)/d_input"
    157 }
    158 
    159 atf_test_case negative
    160 negative_head()
    161 {
    162 	atf_set "descr" "Checks handling of files with no matches"
    163 }
    164 negative_body()
    165 {
    166 	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
    167 }
    168 
    169 atf_test_case context
    170 context_head()
    171 {
    172 	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
    173 }
    174 context_body()
    175 {
    176 	cp $(atf_get_srcdir)/d_context_*.* .
    177 
    178 	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
    179 	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
    180 	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
    181 	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
    182 }
    183 
    184 atf_test_case file_exp
    185 file_exp_head()
    186 {
    187 	atf_set "descr" "Checks reading expressions from file"
    188 }
    189 file_exp_body()
    190 {
    191 	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
    192 	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
    193 }
    194 
    195 atf_test_case egrep
    196 egrep_head()
    197 {
    198 	atf_set "descr" "Checks matching special characters with egrep"
    199 }
    200 egrep_body()
    201 {
    202 	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
    203 		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
    204 }
    205 
    206 atf_test_case zgrep
    207 zgrep_head()
    208 {
    209 	atf_set "descr" "Checks handling of gzipped files with zgrep"
    210 }
    211 zgrep_body()
    212 {
    213 	cp "$(atf_get_srcdir)/d_input" .
    214 	gzip d_input || atf_fail "gzip failed"
    215 
    216 	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
    217 }
    218 
    219 atf_test_case nonexistent
    220 nonexistent_head()
    221 {
    222 	atf_set "descr" "Checks that -s flag suppresses error" \
    223 	                "messages about nonexistent files"
    224 }
    225 nonexistent_body()
    226 {
    227 	atf_check -s ne:0 grep -s foobar nonexistent
    228 }
    229 
    230 atf_test_case context2
    231 context2_head()
    232 {
    233 	atf_set "descr" "Checks displaying context with -z flag"
    234 }
    235 context2_body()
    236 {
    237 	printf "haddock\000cod\000plaice\000" > test1
    238 	printf "mackeral\000cod\000crab\000" > test2
    239 
    240 	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
    241 	    grep -z -A1 cod test1 test2
    242 
    243 	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
    244 	    grep -z -B1 cod test1 test2
    245 
    246 	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
    247 	    grep -z -C1 cod test1 test2
    248 }
    249 
    250 atf_init_test_cases()
    251 {
    252 	atf_add_test_case basic 
    253 	atf_add_test_case binary
    254 	atf_add_test_case recurse
    255 	atf_add_test_case recurse_symlink
    256 	atf_add_test_case word_regexps
    257 	atf_add_test_case word_locale
    258 	atf_add_test_case begin_end
    259 	atf_add_test_case ignore_case
    260 	atf_add_test_case invert
    261 	atf_add_test_case whole_line
    262 	atf_add_test_case negative
    263 	atf_add_test_case context
    264 	atf_add_test_case file_exp
    265 	atf_add_test_case egrep
    266 	atf_add_test_case zgrep
    267 	atf_add_test_case nonexistent
    268 	atf_add_test_case context2
    269 }
    270