Home | History | Annotate | Line # | Download | only in grep
      1  1.8    rillig # $NetBSD: t_grep.sh,v 1.8 2024/11/23 09:38:02 rillig Exp $
      2  1.1    jruoho #
      3  1.8    rillig # Copyright (c) 2008, 2009, 2021, 2024 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.7       wiz {
     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.7       wiz atf_test_case recurse_noarg
     66  1.7       wiz recurse_noarg_head()
     67  1.7       wiz {
     68  1.7       wiz 	atf_set "descr" "Checks recursive searching without file argument"
     69  1.7       wiz }
     70  1.7       wiz recurse_noarg_body()
     71  1.7       wiz {
     72  1.7       wiz 	mkdir -p recurse/a/f recurse/d
     73  1.7       wiz 	echo -e "cod\ndover sole\nhaddock\nhalibut\npilchard" > recurse/d/fish
     74  1.7       wiz 	echo -e "cod\nhaddock\nplaice" > recurse/a/f/favourite-fish
     75  1.7       wiz 
     76  1.7       wiz 	atf_check -o file:"$(atf_get_srcdir)/d_recurse_noarg.out" -x "cd recurse && grep -r haddock | sort"
     77  1.7       wiz }
     78  1.7       wiz 
     79  1.1    jruoho atf_test_case recurse_symlink
     80  1.1    jruoho recurse_symlink_head()
     81  1.1    jruoho {
     82  1.1    jruoho 	atf_set "descr" "Checks symbolic link recursion"
     83  1.1    jruoho }
     84  1.1    jruoho recurse_symlink_body()
     85  1.1    jruoho {
     86  1.1    jruoho 	mkdir -p test/c/d
     87  1.1    jruoho 	(cd test/c/d && ln -s ../d .)
     88  1.1    jruoho 	echo "Test string" > test/c/match
     89  1.1    jruoho 
     90  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_recurse_symlink.out" \
     91  1.1    jruoho 	    -e file:"$(atf_get_srcdir)/d_recurse_symlink.err" \
     92  1.1    jruoho 	    grep -r string test
     93  1.1    jruoho }
     94  1.1    jruoho 
     95  1.1    jruoho atf_test_case word_regexps
     96  1.1    jruoho word_regexps_head()
     97  1.1    jruoho {
     98  1.1    jruoho 	atf_set "descr" "Checks word-regexps"
     99  1.1    jruoho }
    100  1.1    jruoho word_regexps_body()
    101  1.1    jruoho {
    102  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_word_regexps.out" \
    103  1.1    jruoho 	    grep -w separated $(atf_get_srcdir)/d_input
    104  1.1    jruoho }
    105  1.1    jruoho 
    106  1.4    rillig atf_test_case word_locale
    107  1.4    rillig word_locale_head()
    108  1.4    rillig {
    109  1.4    rillig 	atf_set "descr" "Checks word search with locale"
    110  1.4    rillig }
    111  1.4    rillig word_locale_body()
    112  1.4    rillig {
    113  1.4    rillig 	echo "array[]" > "input"
    114  1.4    rillig 
    115  1.4    rillig 	# In the default locale, word search works.
    116  1.4    rillig 	atf_check -o file:"input" \
    117  1.4    rillig 	    env LC_ALL=C grep "array" "input"
    118  1.4    rillig 	atf_check -o file:"input" \
    119  1.4    rillig 	    env LC_ALL=C grep -w "array" "input"
    120  1.4    rillig 
    121  1.5    rillig 	# XXX: In an UTF-8 locale, GNU Grep treats '[' as a word character.
    122  1.4    rillig 	atf_check -s exit:1 -o empty \
    123  1.4    rillig 	    env LC_ALL="C.UTF-8" grep -w "array" "input"
    124  1.4    rillig }
    125  1.4    rillig 
    126  1.5    rillig atf_test_case word_in_line
    127  1.5    rillig word_in_line_head()
    128  1.5    rillig {
    129  1.6    rillig 	atf_set "descr" "Checks word search in different locations of a line"
    130  1.5    rillig }
    131  1.5    rillig word_in_line_body()
    132  1.5    rillig {
    133  1.5    rillig 	# See usr.bin/grep/util.c, "Check for whole word match", which
    134  1.5    rillig 	# looks suspiciously wrong.  And indeed, NetBSD grep does not
    135  1.5    rillig 	# survive this test.  GNU Grep does.
    136  1.5    rillig 
    137  1.5    rillig 	echo "begin middle end" > "input"
    138  1.5    rillig 
    139  1.5    rillig 	# A word at the beginning of a line is found.
    140  1.5    rillig 	atf_check -o file:"input" \
    141  1.5    rillig 	    env LC_ALL=C grep -w "begin" "input"
    142  1.5    rillig 
    143  1.5    rillig 	# A word in the middle of a line is found.
    144  1.5    rillig 	atf_check -o file:"input" \
    145  1.5    rillig 	    env LC_ALL=C grep -w "middle" "input"
    146  1.5    rillig 
    147  1.5    rillig 	# A word at the end of a line is found.
    148  1.5    rillig 	atf_check -o file:"input" \
    149  1.5    rillig 	    env LC_ALL=C grep -w "end" "input"
    150  1.5    rillig 
    151  1.5    rillig 	# A subword at the beginning of a line is not found.
    152  1.5    rillig 	atf_check -s exit:1 -o empty \
    153  1.5    rillig 	    env LC_ALL=C grep -w "be" "input"
    154  1.5    rillig 
    155  1.5    rillig 	# A subword in the middle of a line is not found.
    156  1.5    rillig 	atf_check -s exit:1 -o empty \
    157  1.5    rillig 	    env LC_ALL=C grep -w "mid" "input"
    158  1.5    rillig 	atf_check -s exit:1 -o empty \
    159  1.5    rillig 	    env LC_ALL=C grep -w "dle" "input"
    160  1.5    rillig 
    161  1.5    rillig 	# A subword at the end of a line is not found.
    162  1.5    rillig 	atf_check -s exit:1 -o empty \
    163  1.5    rillig 	    env LC_ALL=C grep -w "nd" "input"
    164  1.5    rillig }
    165  1.5    rillig 
    166  1.5    rillig atf_test_case word_in_line_utf8
    167  1.5    rillig word_in_line_utf8_head()
    168  1.5    rillig {
    169  1.5    rillig 	atf_set "descr" "Checks word search at the beginning of a line"
    170  1.5    rillig }
    171  1.5    rillig word_in_line_utf8_body()
    172  1.5    rillig {
    173  1.5    rillig 	# See usr.bin/grep/util.c, "Check for whole word match", which
    174  1.5    rillig 	# looks suspiciously wrong.  And indeed, NetBSD grep does not
    175  1.5    rillig 	# survive this test.  GNU Grep does.
    176  1.5    rillig 
    177  1.5    rillig 	echo "begin middle end" > "input"
    178  1.5    rillig 
    179  1.5    rillig 	# A word at the beginning of a line is found.
    180  1.5    rillig 	atf_check -o file:"input" \
    181  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "begin" "input"
    182  1.5    rillig 
    183  1.5    rillig 	# A word in the middle of a line is found.
    184  1.5    rillig 	atf_check -o file:"input" \
    185  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "middle" "input"
    186  1.5    rillig 
    187  1.5    rillig 	# A word at the end of a line is found.
    188  1.5    rillig 	atf_check -o file:"input" \
    189  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "end" "input"
    190  1.5    rillig 
    191  1.5    rillig 	# A subword at the beginning of a line is not found.
    192  1.5    rillig 	atf_check -s exit:1 -o empty \
    193  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "be" "input"
    194  1.5    rillig 
    195  1.5    rillig 	# A subword in the middle of a line is not found.
    196  1.5    rillig 	atf_check -s exit:1 -o empty \
    197  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "mid" "input"
    198  1.5    rillig 	atf_check -s exit:1 -o empty \
    199  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "dle" "input"
    200  1.5    rillig 
    201  1.5    rillig 	# A subword at the end of a line is not found.
    202  1.5    rillig 	atf_check -s exit:1 -o empty \
    203  1.5    rillig 	    env LC_ALL="C.UTF-8" grep -w "nd" "input"
    204  1.5    rillig }
    205  1.5    rillig 
    206  1.1    jruoho atf_test_case begin_end
    207  1.1    jruoho begin_end_head()
    208  1.1    jruoho {
    209  1.1    jruoho 	atf_set "descr" "Checks handling of line beginnings and ends"
    210  1.1    jruoho }
    211  1.1    jruoho begin_end_body()
    212  1.1    jruoho {
    213  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_a.out" \
    214  1.1    jruoho 	    grep ^Front "$(atf_get_srcdir)/d_input"
    215  1.1    jruoho 
    216  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_begin_end_b.out" \
    217  1.1    jruoho 	    grep ending$ "$(atf_get_srcdir)/d_input"
    218  1.1    jruoho }
    219  1.1    jruoho 
    220  1.1    jruoho atf_test_case ignore_case
    221  1.1    jruoho ignore_case_head()
    222  1.1    jruoho {
    223  1.1    jruoho 	atf_set "descr" "Checks ignore-case option"
    224  1.1    jruoho }
    225  1.1    jruoho ignore_case_body()
    226  1.1    jruoho {
    227  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_ignore_case.out" \
    228  1.1    jruoho 	    grep -i Upper "$(atf_get_srcdir)/d_input"
    229  1.1    jruoho }
    230  1.1    jruoho 
    231  1.1    jruoho atf_test_case invert
    232  1.1    jruoho invert_head()
    233  1.1    jruoho {
    234  1.1    jruoho 	atf_set "descr" "Checks selecting non-matching lines with -v option"
    235  1.1    jruoho }
    236  1.1    jruoho invert_body()
    237  1.1    jruoho {
    238  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_invert.out" \
    239  1.1    jruoho 	    grep -v fish "$(atf_get_srcdir)/d_invert.in"
    240  1.1    jruoho }
    241  1.1    jruoho 
    242  1.1    jruoho atf_test_case whole_line
    243  1.1    jruoho whole_line_head()
    244  1.1    jruoho {
    245  1.1    jruoho 	atf_set "descr" "Checks whole-line matching with -x flag"
    246  1.1    jruoho }
    247  1.1    jruoho whole_line_body()
    248  1.1    jruoho {
    249  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_whole_line.out" \
    250  1.1    jruoho 	    grep -x matchme "$(atf_get_srcdir)/d_input"
    251  1.1    jruoho }
    252  1.1    jruoho 
    253  1.1    jruoho atf_test_case negative
    254  1.1    jruoho negative_head()
    255  1.1    jruoho {
    256  1.1    jruoho 	atf_set "descr" "Checks handling of files with no matches"
    257  1.1    jruoho }
    258  1.1    jruoho negative_body()
    259  1.1    jruoho {
    260  1.1    jruoho 	atf_check -s ne:0 grep "not a hope in hell" "$(atf_get_srcdir)/d_input"
    261  1.1    jruoho }
    262  1.1    jruoho 
    263  1.1    jruoho atf_test_case context
    264  1.1    jruoho context_head()
    265  1.1    jruoho {
    266  1.1    jruoho 	atf_set "descr" "Checks displaying context with -A, -B and -C flags"
    267  1.1    jruoho }
    268  1.1    jruoho context_body()
    269  1.1    jruoho {
    270  1.1    jruoho 	cp $(atf_get_srcdir)/d_context_*.* .
    271  1.1    jruoho 
    272  1.1    jruoho 	atf_check -o file:d_context_a.out grep -C2 bamboo d_context_a.in
    273  1.1    jruoho 	atf_check -o file:d_context_b.out grep -A3 tilt d_context_a.in
    274  1.1    jruoho 	atf_check -o file:d_context_c.out grep -B4 Whig d_context_a.in
    275  1.1    jruoho 	atf_check -o file:d_context_d.out grep -C1 pig d_context_a.in d_context_b.in
    276  1.1    jruoho }
    277  1.1    jruoho 
    278  1.1    jruoho atf_test_case file_exp
    279  1.1    jruoho file_exp_head()
    280  1.1    jruoho {
    281  1.1    jruoho 	atf_set "descr" "Checks reading expressions from file"
    282  1.1    jruoho }
    283  1.1    jruoho file_exp_body()
    284  1.1    jruoho {
    285  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_file_exp.out" -x \
    286  1.1    jruoho 	    'jot 21 -1 1.00 | grep -f '"$(atf_get_srcdir)"'/d_file_exp.in'
    287  1.1    jruoho }
    288  1.1    jruoho 
    289  1.1    jruoho atf_test_case egrep
    290  1.1    jruoho egrep_head()
    291  1.1    jruoho {
    292  1.1    jruoho 	atf_set "descr" "Checks matching special characters with egrep"
    293  1.1    jruoho }
    294  1.1    jruoho egrep_body()
    295  1.1    jruoho {
    296  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_egrep.out" \
    297  1.1    jruoho 		egrep '\?|\*$$' "$(atf_get_srcdir)/d_input"
    298  1.1    jruoho }
    299  1.1    jruoho 
    300  1.1    jruoho atf_test_case zgrep
    301  1.1    jruoho zgrep_head()
    302  1.1    jruoho {
    303  1.1    jruoho 	atf_set "descr" "Checks handling of gzipped files with zgrep"
    304  1.1    jruoho }
    305  1.1    jruoho zgrep_body()
    306  1.1    jruoho {
    307  1.1    jruoho 	cp "$(atf_get_srcdir)/d_input" .
    308  1.1    jruoho 	gzip d_input || atf_fail "gzip failed"
    309  1.1    jruoho 
    310  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_zgrep.out" zgrep -h line d_input.gz
    311  1.1    jruoho }
    312  1.1    jruoho 
    313  1.1    jruoho atf_test_case nonexistent
    314  1.1    jruoho nonexistent_head()
    315  1.1    jruoho {
    316  1.1    jruoho 	atf_set "descr" "Checks that -s flag suppresses error" \
    317  1.1    jruoho 	                "messages about nonexistent files"
    318  1.1    jruoho }
    319  1.1    jruoho nonexistent_body()
    320  1.1    jruoho {
    321  1.1    jruoho 	atf_check -s ne:0 grep -s foobar nonexistent
    322  1.1    jruoho }
    323  1.1    jruoho 
    324  1.1    jruoho atf_test_case context2
    325  1.1    jruoho context2_head()
    326  1.1    jruoho {
    327  1.1    jruoho 	atf_set "descr" "Checks displaying context with -z flag"
    328  1.1    jruoho }
    329  1.1    jruoho context2_body()
    330  1.1    jruoho {
    331  1.1    jruoho 	printf "haddock\000cod\000plaice\000" > test1
    332  1.1    jruoho 	printf "mackeral\000cod\000crab\000" > test2
    333  1.1    jruoho 
    334  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_context2_a.out" \
    335  1.1    jruoho 	    grep -z -A1 cod test1 test2
    336  1.1    jruoho 
    337  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_context2_b.out" \
    338  1.1    jruoho 	    grep -z -B1 cod test1 test2
    339  1.1    jruoho 
    340  1.1    jruoho 	atf_check -o file:"$(atf_get_srcdir)/d_context2_c.out" \
    341  1.1    jruoho 	    grep -z -C1 cod test1 test2
    342  1.1    jruoho }
    343  1.1    jruoho 
    344  1.8    rillig atf_test_case pr_58849
    345  1.8    rillig pr_58849_head()
    346  1.8    rillig {
    347  1.8    rillig 	atf_set "descr" "Checks overlapping patterns in whole-line search"
    348  1.8    rillig }
    349  1.8    rillig pr_58849_body()
    350  1.8    rillig {
    351  1.8    rillig 	printf '%s\n' __bss_start__ __bss_end__ hello > input
    352  1.8    rillig 
    353  1.8    rillig 	# The line '__bss_end__' must not occur in the output.
    354  1.8    rillig 	atf_check -o inline:'__bss_start__\nhello\n' \
    355  1.8    rillig 	    grep -Fvx -e _end -e __bss_end__ input
    356  1.8    rillig 
    357  1.8    rillig 	# Listing the most specific pattern first works around PR bin/58849.
    358  1.8    rillig 	atf_check -o inline:'__bss_start__\nhello\n' \
    359  1.8    rillig 	    grep -Fvx -e __bss_end__ -e _end input
    360  1.8    rillig }
    361  1.8    rillig 
    362  1.1    jruoho atf_init_test_cases()
    363  1.1    jruoho {
    364  1.7       wiz 	atf_add_test_case basic
    365  1.1    jruoho 	atf_add_test_case binary
    366  1.1    jruoho 	atf_add_test_case recurse
    367  1.7       wiz 	atf_add_test_case recurse_noarg
    368  1.1    jruoho 	atf_add_test_case recurse_symlink
    369  1.1    jruoho 	atf_add_test_case word_regexps
    370  1.4    rillig 	atf_add_test_case word_locale
    371  1.5    rillig 	atf_add_test_case word_in_line
    372  1.5    rillig 	atf_add_test_case word_in_line_utf8
    373  1.1    jruoho 	atf_add_test_case begin_end
    374  1.1    jruoho 	atf_add_test_case ignore_case
    375  1.1    jruoho 	atf_add_test_case invert
    376  1.1    jruoho 	atf_add_test_case whole_line
    377  1.1    jruoho 	atf_add_test_case negative
    378  1.1    jruoho 	atf_add_test_case context
    379  1.1    jruoho 	atf_add_test_case file_exp
    380  1.1    jruoho 	atf_add_test_case egrep
    381  1.1    jruoho 	atf_add_test_case zgrep
    382  1.1    jruoho 	atf_add_test_case nonexistent
    383  1.1    jruoho 	atf_add_test_case context2
    384  1.8    rillig 	atf_add_test_case pr_58849
    385  1.1    jruoho }
    386