1# $NetBSD: t_grep.sh,v 1.6 2021/08/30 23:14:14 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 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 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 51atf_test_case recurse 52recurse_head() 53{ 54 atf_set "descr" "Checks recursive searching" 55} 56recurse_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 65atf_test_case recurse_symlink 66recurse_symlink_head() 67{ 68 atf_set "descr" "Checks symbolic link recursion" 69} 70recurse_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 81atf_test_case word_regexps 82word_regexps_head() 83{ 84 atf_set "descr" "Checks word-regexps" 85} 86word_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 92atf_test_case word_locale 93word_locale_head() 94{ 95 atf_set "descr" "Checks word search with locale" 96} 97word_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, GNU Grep treats '[' as a word character. 108 atf_check -s exit:1 -o empty \ 109 env LC_ALL="C.UTF-8" grep -w "array" "input" 110} 111 112atf_test_case word_in_line 113word_in_line_head() 114{ 115 atf_set "descr" "Checks word search in different locations of a line" 116} 117word_in_line_body() 118{ 119 # See usr.bin/grep/util.c, "Check for whole word match", which 120 # looks suspiciously wrong. And indeed, NetBSD grep does not 121 # survive this test. GNU Grep does. 122 123 echo "begin middle end" > "input" 124 125 # A word at the beginning of a line is found. 126 atf_check -o file:"input" \ 127 env LC_ALL=C grep -w "begin" "input" 128 129 # A word in the middle of a line is found. 130 atf_check -o file:"input" \ 131 env LC_ALL=C grep -w "middle" "input" 132 133 # A word at the end of a line is found. 134 atf_check -o file:"input" \ 135 env LC_ALL=C grep -w "end" "input" 136 137 # A subword at the beginning of a line is not found. 138 atf_check -s exit:1 -o empty \ 139 env LC_ALL=C grep -w "be" "input" 140 141 # A subword in the middle of a line is not found. 142 atf_check -s exit:1 -o empty \ 143 env LC_ALL=C grep -w "mid" "input" 144 atf_check -s exit:1 -o empty \ 145 env LC_ALL=C grep -w "dle" "input" 146 147 # A subword at the end of a line is not found. 148 atf_check -s exit:1 -o empty \ 149 env LC_ALL=C grep -w "nd" "input" 150} 151 152atf_test_case word_in_line_utf8 153word_in_line_utf8_head() 154{ 155 atf_set "descr" "Checks word search at the beginning of a line" 156} 157word_in_line_utf8_body() 158{ 159 # See usr.bin/grep/util.c, "Check for whole word match", which 160 # looks suspiciously wrong. And indeed, NetBSD grep does not 161 # survive this test. GNU Grep does. 162 163 echo "begin middle end" > "input" 164 165 # A word at the beginning of a line is found. 166 atf_check -o file:"input" \ 167 env LC_ALL="C.UTF-8" grep -w "begin" "input" 168 169 # A word in the middle of a line is found. 170 atf_check -o file:"input" \ 171 env LC_ALL="C.UTF-8" grep -w "middle" "input" 172 173 # A word at the end of a line is found. 174 atf_check -o file:"input" \ 175 env LC_ALL="C.UTF-8" grep -w "end" "input" 176 177 # A subword at the beginning of a line is not found. 178 atf_check -s exit:1 -o empty \ 179 env LC_ALL="C.UTF-8" grep -w "be" "input" 180 181 # A subword in the middle of a line is not found. 182 atf_check -s exit:1 -o empty \ 183 env LC_ALL="C.UTF-8" grep -w "mid" "input" 184 atf_check -s exit:1 -o empty \ 185 env LC_ALL="C.UTF-8" grep -w "dle" "input" 186 187 # A subword at the end of a line is not found. 188 atf_check -s exit:1 -o empty \ 189 env LC_ALL="C.UTF-8" grep -w "nd" "input" 190} 191 192atf_test_case begin_end 193begin_end_head() 194{ 195 atf_set "descr" "Checks handling of line beginnings and ends" 196} 197begin_end_body() 198{ 199 atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \ 200 grep ^Front "$(atf_get_srcdir)/d_input" 201 202 atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \ 203 grep ending$ "$(atf_get_srcdir)/d_input" 204} 205 206atf_test_case ignore_case 207ignore_case_head() 208{ 209 atf_set "descr" "Checks ignore-case option" 210} 211ignore_case_body() 212{ 213 atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \ 214 grep -i Upper "$(atf_get_srcdir)/d_input" 215} 216 217atf_test_case invert 218invert_head() 219{ 220 atf_set "descr" "Checks selecting non-matching lines with -v option" 221} 222invert_body() 223{ 224 atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \ 225 grep -v fish "$(atf_get_srcdir)/d_invert.in" 226} 227 228atf_test_case whole_line 229whole_line_head() 230{ 231 atf_set "descr" "Checks whole-line matching with -x flag" 232} 233whole_line_body() 234{ 235 atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \ 236 grep -x matchme "$(atf_get_srcdir)/d_input" 237} 238 239atf_test_case negative 240negative_head() 241{ 242 atf_set "descr" "Checks handling of files with no matches" 243} 244negative_body() 245{ 246 atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input" 247} 248 249atf_test_case context 250context_head() 251{ 252 atf_set "descr" "Checks displaying context with -A, -B and -C flags" 253} 254context_body() 255{ 256 cp $(atf_get_srcdir)/d_context_*.* . 257 258 atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in 259 atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in 260 atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in 261 atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in 262} 263 264atf_test_case file_exp 265file_exp_head() 266{ 267 atf_set "descr" "Checks reading expressions from file" 268} 269file_exp_body() 270{ 271 atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \ 272 'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in' 273} 274 275atf_test_case egrep 276egrep_head() 277{ 278 atf_set "descr" "Checks matching special characters with egrep" 279} 280egrep_body() 281{ 282 atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \ 283 egrep '\?|\*$$' "$(atf_get_srcdir)/d_input" 284} 285 286atf_test_case zgrep 287zgrep_head() 288{ 289 atf_set "descr" "Checks handling of gzipped files with zgrep" 290} 291zgrep_body() 292{ 293 cp "$(atf_get_srcdir)/d_input" . 294 gzip d_input || atf_fail "gzip failed" 295 296 atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz 297} 298 299atf_test_case nonexistent 300nonexistent_head() 301{ 302 atf_set "descr" "Checks that -s flag suppresses error" \ 303 "messages about nonexistent files" 304} 305nonexistent_body() 306{ 307 atf_check -s ne:0 grep -s foobar nonexistent 308} 309 310atf_test_case context2 311context2_head() 312{ 313 atf_set "descr" "Checks displaying context with -z flag" 314} 315context2_body() 316{ 317 printf "haddock\000cod\000plaice\000" > test1 318 printf "mackeral\000cod\000crab\000" > test2 319 320 atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \ 321 grep -z -A1 cod test1 test2 322 323 atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \ 324 grep -z -B1 cod test1 test2 325 326 atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \ 327 grep -z -C1 cod test1 test2 328} 329 330atf_init_test_cases() 331{ 332 atf_add_test_case basic 333 atf_add_test_case binary 334 atf_add_test_case recurse 335 atf_add_test_case recurse_symlink 336 atf_add_test_case word_regexps 337 atf_add_test_case word_locale 338 atf_add_test_case word_in_line 339 atf_add_test_case word_in_line_utf8 340 atf_add_test_case begin_end 341 atf_add_test_case ignore_case 342 atf_add_test_case invert 343 atf_add_test_case whole_line 344 atf_add_test_case negative 345 atf_add_test_case context 346 atf_add_test_case file_exp 347 atf_add_test_case egrep 348 atf_add_test_case zgrep 349 atf_add_test_case nonexistent 350 atf_add_test_case context2 351} 352