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