1a461896aSEd Maste# 24d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 3f0fb94abSKyle Evans# 426e1c38fSKyle Evans# Copyright (c) 2017 Kyle Evans <kevans@FreeBSD.org> 5a461896aSEd Maste# 6a461896aSEd Maste# Redistribution and use in source and binary forms, with or without 7a461896aSEd Maste# modification, are permitted provided that the following conditions 8a461896aSEd Maste# are met: 9a461896aSEd Maste# 1. Redistributions of source code must retain the above copyright 10a461896aSEd Maste# notice, this list of conditions and the following disclaimer. 11a461896aSEd Maste# 2. Redistributions in binary form must reproduce the above copyright 12a461896aSEd Maste# notice, this list of conditions and the following disclaimer in the 13a461896aSEd Maste# documentation and/or other materials provided with the distribution. 14a461896aSEd Maste# 158433795dSKyle Evans# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 168433795dSKyle Evans# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 178433795dSKyle Evans# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 188433795dSKyle Evans# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 198433795dSKyle Evans# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 208433795dSKyle Evans# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 218433795dSKyle Evans# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 228433795dSKyle Evans# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 238433795dSKyle Evans# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 248433795dSKyle Evans# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 258433795dSKyle Evans# SUCH DAMAGE. 26a461896aSEd Maste# 27a461896aSEd Maste 2822c00e3bSEnji Cooper# What grep(1) are we working with? 2922c00e3bSEnji Cooper# - 0 : bsdgrep 30d1c965f1SKyle Evans# - 1 : gnu grep (ports) 3122c00e3bSEnji CooperGREP_TYPE_BSD=0 32d1c965f1SKyle EvansGREP_TYPE_GNU=1 3322c00e3bSEnji Cooper 3422c00e3bSEnji Coopergrep_type() 3522c00e3bSEnji Cooper{ 3622c00e3bSEnji Cooper local grep_version=$(grep --version) 3722c00e3bSEnji Cooper 3822c00e3bSEnji Cooper case "$grep_version" in 3922c00e3bSEnji Cooper *"BSD grep"*) 4022c00e3bSEnji Cooper return $GREP_TYPE_BSD 4122c00e3bSEnji Cooper ;; 4222c00e3bSEnji Cooper *"GNU grep"*) 4322c00e3bSEnji Cooper return $GREP_TYPE_GNU 4422c00e3bSEnji Cooper ;; 4522c00e3bSEnji Cooper esac 4622c00e3bSEnji Cooper atf_fail "unknown grep type: $grep_version" 4722c00e3bSEnji Cooper} 4822c00e3bSEnji Cooper 49a461896aSEd Masteatf_test_case grep_r_implied 50a461896aSEd Mastegrep_r_implied_body() 51a461896aSEd Maste{ 5222c00e3bSEnji Cooper grep_type 5322c00e3bSEnji Cooper if [ $? -ne $GREP_TYPE_BSD ]; then 5422c00e3bSEnji Cooper atf_skip "this test only works with bsdgrep(1)" 5522c00e3bSEnji Cooper fi 5622c00e3bSEnji Cooper 57a461896aSEd Maste (cd "$(atf_get_srcdir)" && grep -r --exclude="*.out" -e "test" .) > d_grep_r_implied.out 58a461896aSEd Maste 59a461896aSEd Maste atf_check -s exit:0 -x \ 60a461896aSEd Maste "(cd $(atf_get_srcdir) && grep -r --exclude=\"*.out\" -e \"test\") | diff d_grep_r_implied.out -" 61a461896aSEd Maste} 62a461896aSEd Maste 63a461896aSEd Masteatf_test_case rgrep 64a461896aSEd Mastergrep_head() 65a461896aSEd Maste{ 66a461896aSEd Maste atf_set "require.progs" "rgrep" 67a461896aSEd Maste} 68a461896aSEd Mastergrep_body() 69a461896aSEd Maste{ 7080c5ef10SEnji Cooper atf_check -o save:d_grep_r_implied.out grep -r --exclude="*.out" -e "test" "$(atf_get_srcdir)" 71a461896aSEd Maste atf_check -o file:d_grep_r_implied.out rgrep --exclude="*.out" -e "test" "$(atf_get_srcdir)" 72a461896aSEd Maste} 73a461896aSEd Maste 74aef64c62SKyle Evansatf_test_case gnuext 75aef64c62SKyle Evansgnuext_body() 76aef64c62SKyle Evans{ 77aef64c62SKyle Evans grep_type 78aef64c62SKyle Evans _type=$? 79aef64c62SKyle Evans 80aef64c62SKyle Evans atf_check -o save:grep_alnum.out grep -o '[[:alnum:]]' /COPYRIGHT 81aef64c62SKyle Evans atf_check -o file:grep_alnum.out grep -o '\w' /COPYRIGHT 82aef64c62SKyle Evans 83aef64c62SKyle Evans atf_check -o save:grep_nalnum.out grep -o '[^[:alnum:]]' /COPYRIGHT 84aef64c62SKyle Evans atf_check -o file:grep_nalnum.out grep -o '\W' /COPYRIGHT 85aef64c62SKyle Evans 86aef64c62SKyle Evans atf_check -o save:grep_space.out grep -o '[[:space:]]' /COPYRIGHT 87aef64c62SKyle Evans atf_check -o file:grep_space.out grep -o '\s' /COPYRIGHT 88aef64c62SKyle Evans 89aef64c62SKyle Evans atf_check -o save:grep_nspace.out grep -o '[^[:space:]]' /COPYRIGHT 90aef64c62SKyle Evans atf_check -o file:grep_nspace.out grep -o '\S' /COPYRIGHT 91aef64c62SKyle Evans 92aef64c62SKyle Evans} 93aef64c62SKyle Evans 94e898a3afSKyle Evansatf_test_case zflag 95e898a3afSKyle Evanszflag_body() 96e898a3afSKyle Evans{ 97e898a3afSKyle Evans 98e898a3afSKyle Evans # The -z flag should pick up 'foo' and 'bar' as on the same line with 99e898a3afSKyle Evans # 'some kind of junk' in between; a bug was present that instead made 100e898a3afSKyle Evans # it process this incorrectly. 101e898a3afSKyle Evans printf "foo\nbar\0" > in 102e898a3afSKyle Evans 103e898a3afSKyle Evans atf_check grep -qz "foo.*bar" in 104e898a3afSKyle Evans} 105e898a3afSKyle Evans 106*4c9ffb13SKyle Evansatf_test_case color_dupe 107*4c9ffb13SKyle Evanscolor_dupe_body() 108*4c9ffb13SKyle Evans{ 109*4c9ffb13SKyle Evans 110*4c9ffb13SKyle Evans # This assumes a MAX_MATCHES of exactly 32. Previously buggy procline() 111*4c9ffb13SKyle Evans # calls would terminate the line premature every MAX_MATCHES matches, 112*4c9ffb13SKyle Evans # meaning we'd see the line be output again for the next MAX_MATCHES 113*4c9ffb13SKyle Evans # number of matches. 114*4c9ffb13SKyle Evans jot -nb 'A' -s '' 33 > in 115*4c9ffb13SKyle Evans 116*4c9ffb13SKyle Evans atf_check -o save:color.out grep --color=always . in 117*4c9ffb13SKyle Evans atf_check -o match:"^ +1 color.out" wc -l color.out 118*4c9ffb13SKyle Evans} 119*4c9ffb13SKyle Evans 120a461896aSEd Masteatf_init_test_cases() 121a461896aSEd Maste{ 122a461896aSEd Maste atf_add_test_case grep_r_implied 123a461896aSEd Maste atf_add_test_case rgrep 124aef64c62SKyle Evans atf_add_test_case gnuext 125e898a3afSKyle Evans atf_add_test_case zflag 126*4c9ffb13SKyle Evans atf_add_test_case color_dupe 127a461896aSEd Maste} 128