xref: /freebsd/usr.bin/grep/tests/grep_freebsd_test.sh (revision d1c965f1436aae22127485370332555ec01b3a23)
1a461896aSEd Maste#
2f0fb94abSKyle Evans# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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# $FreeBSD$
28a461896aSEd Maste
2922c00e3bSEnji Cooper# What grep(1) are we working with?
3022c00e3bSEnji Cooper# - 0 : bsdgrep
31*d1c965f1SKyle Evans# - 1 : gnu grep (ports)
3222c00e3bSEnji CooperGREP_TYPE_BSD=0
33*d1c965f1SKyle EvansGREP_TYPE_GNU=1
3422c00e3bSEnji Cooper
3522c00e3bSEnji Coopergrep_type()
3622c00e3bSEnji Cooper{
3722c00e3bSEnji Cooper	local grep_version=$(grep --version)
3822c00e3bSEnji Cooper
3922c00e3bSEnji Cooper	case "$grep_version" in
4022c00e3bSEnji Cooper	*"BSD grep"*)
4122c00e3bSEnji Cooper		return $GREP_TYPE_BSD
4222c00e3bSEnji Cooper		;;
4322c00e3bSEnji Cooper	*"GNU grep"*)
4422c00e3bSEnji Cooper		return $GREP_TYPE_GNU
4522c00e3bSEnji Cooper		;;
4622c00e3bSEnji Cooper	esac
4722c00e3bSEnji Cooper	atf_fail "unknown grep type: $grep_version"
4822c00e3bSEnji Cooper}
4922c00e3bSEnji Cooper
50a461896aSEd Masteatf_test_case grep_r_implied
51a461896aSEd Mastegrep_r_implied_body()
52a461896aSEd Maste{
5322c00e3bSEnji Cooper	grep_type
5422c00e3bSEnji Cooper	if [ $? -ne $GREP_TYPE_BSD ]; then
5522c00e3bSEnji Cooper		atf_skip "this test only works with bsdgrep(1)"
5622c00e3bSEnji Cooper	fi
5722c00e3bSEnji Cooper
58a461896aSEd Maste	(cd "$(atf_get_srcdir)" && grep -r --exclude="*.out" -e "test" .) > d_grep_r_implied.out
59a461896aSEd Maste
60a461896aSEd Maste	atf_check -s exit:0 -x \
61a461896aSEd Maste	    "(cd $(atf_get_srcdir) && grep -r --exclude=\"*.out\" -e \"test\") | diff d_grep_r_implied.out -"
62a461896aSEd Maste}
63a461896aSEd Maste
64a461896aSEd Masteatf_test_case rgrep
65a461896aSEd Mastergrep_head()
66a461896aSEd Maste{
67a461896aSEd Maste	atf_set "require.progs" "rgrep"
68a461896aSEd Maste}
69a461896aSEd Mastergrep_body()
70a461896aSEd Maste{
7180c5ef10SEnji Cooper	atf_check -o save:d_grep_r_implied.out grep -r --exclude="*.out" -e "test" "$(atf_get_srcdir)"
72a461896aSEd Maste	atf_check -o file:d_grep_r_implied.out rgrep --exclude="*.out" -e "test" "$(atf_get_srcdir)"
73a461896aSEd Maste}
74a461896aSEd Maste
75aef64c62SKyle Evansatf_test_case gnuext
76aef64c62SKyle Evansgnuext_body()
77aef64c62SKyle Evans{
78aef64c62SKyle Evans	grep_type
79aef64c62SKyle Evans	_type=$?
80aef64c62SKyle Evans
81aef64c62SKyle Evans	atf_check -o save:grep_alnum.out grep -o '[[:alnum:]]' /COPYRIGHT
82aef64c62SKyle Evans	atf_check -o file:grep_alnum.out grep -o '\w' /COPYRIGHT
83aef64c62SKyle Evans
84aef64c62SKyle Evans	atf_check -o save:grep_nalnum.out grep -o '[^[:alnum:]]' /COPYRIGHT
85aef64c62SKyle Evans	atf_check -o file:grep_nalnum.out grep -o '\W' /COPYRIGHT
86aef64c62SKyle Evans
87aef64c62SKyle Evans	atf_check -o save:grep_space.out grep -o '[[:space:]]' /COPYRIGHT
88aef64c62SKyle Evans	atf_check -o file:grep_space.out grep -o '\s' /COPYRIGHT
89aef64c62SKyle Evans
90aef64c62SKyle Evans	atf_check -o save:grep_nspace.out grep -o '[^[:space:]]' /COPYRIGHT
91aef64c62SKyle Evans	atf_check -o file:grep_nspace.out grep -o '\S' /COPYRIGHT
92aef64c62SKyle Evans
93aef64c62SKyle Evans}
94aef64c62SKyle Evans
95a461896aSEd Masteatf_init_test_cases()
96a461896aSEd Maste{
97a461896aSEd Maste	atf_add_test_case grep_r_implied
98a461896aSEd Maste	atf_add_test_case rgrep
99aef64c62SKyle Evans	atf_add_test_case gnuext
100a461896aSEd Maste}
101