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