1#
2# Copyright (c) 2017 Kyle Evans <kevans@FreeBSD.org>
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: stable/11/usr.bin/grep/tests/grep_freebsd_test.sh 354628 2019-11-11 19:54:08Z kevans $
27
28# What grep(1) are we working with?
29# - 0 : bsdgrep
30# - 1 : gnu grep 2.51 (base)
31# - 2 : gnu grep (ports)
32GREP_TYPE_BSD=0
33GREP_TYPE_GNU_FREEBSD=1
34GREP_TYPE_GNU=2
35GREP_TYPE_UNKNOWN=3
36
37grep_type()
38{
39	local grep_version=$(grep --version)
40
41	case "$grep_version" in
42	*"BSD grep"*)
43		return $GREP_TYPE_BSD
44		;;
45	*"GNU grep"*)
46		case "$grep_version" in
47		*2.5.1-FreeBSD*)
48			return $GREP_TYPE_GNU_FREEBSD
49			;;
50		*)
51			return $GREP_TYPE_GNU
52			;;
53		esac
54		;;
55	esac
56	atf_fail "unknown grep type: $grep_version"
57}
58
59atf_test_case grep_r_implied
60grep_r_implied_body()
61{
62	grep_type
63	if [ $? -ne $GREP_TYPE_BSD ]; then
64		atf_skip "this test only works with bsdgrep(1)"
65	fi
66
67	(cd "$(atf_get_srcdir)" && grep -r --exclude="*.out" -e "test" .) > d_grep_r_implied.out
68
69	atf_check -s exit:0 -x \
70	    "(cd $(atf_get_srcdir) && grep -r --exclude=\"*.out\" -e \"test\") | diff d_grep_r_implied.out -"
71}
72
73atf_test_case rgrep
74rgrep_head()
75{
76	atf_set "require.progs" "rgrep"
77}
78rgrep_body()
79{
80	atf_check -o save:d_grep_r_implied.out grep -r --exclude="*.out" -e "test" "$(atf_get_srcdir)"
81	atf_check -o file:d_grep_r_implied.out rgrep --exclude="*.out" -e "test" "$(atf_get_srcdir)"
82}
83
84atf_test_case gnuext
85gnuext_body()
86{
87	grep_type
88	_type=$?
89	if [ $_type -eq $GREP_TYPE_BSD ]; then
90		atf_expect_fail "this test requires GNU extensions in regex(3)"
91	elif [ $_type -eq $GREP_TYPE_GNU_FREEBSD ]; then
92		atf_expect_fail "\\s and \\S are known to be buggy in base gnugrep"
93	fi
94
95	atf_check -o save:grep_alnum.out grep -o '[[:alnum:]]' /COPYRIGHT
96	atf_check -o file:grep_alnum.out grep -o '\w' /COPYRIGHT
97
98	atf_check -o save:grep_nalnum.out grep -o '[^[:alnum:]]' /COPYRIGHT
99	atf_check -o file:grep_nalnum.out grep -o '\W' /COPYRIGHT
100
101	atf_check -o save:grep_space.out grep -o '[[:space:]]' /COPYRIGHT
102	atf_check -o file:grep_space.out grep -o '\s' /COPYRIGHT
103
104	atf_check -o save:grep_nspace.out grep -o '[^[:space:]]' /COPYRIGHT
105	atf_check -o file:grep_nspace.out grep -o '\S' /COPYRIGHT
106
107}
108
109atf_init_test_cases()
110{
111	atf_add_test_case grep_r_implied
112	atf_add_test_case rgrep
113	atf_add_test_case gnuext
114}
115