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 (ports)
32GREP_TYPE_BSD=0
33GREP_TYPE_GNU=1
34
35grep_type()
36{
37	local grep_version=$(grep --version)
38
39	case "$grep_version" in
40	*"BSD grep"*)
41		return $GREP_TYPE_BSD
42		;;
43	*"GNU grep"*)
44		return $GREP_TYPE_GNU
45		;;
46	esac
47	atf_fail "unknown grep type: $grep_version"
48}
49
50atf_test_case grep_r_implied
51grep_r_implied_body()
52{
53	grep_type
54	if [ $? -ne $GREP_TYPE_BSD ]; then
55		atf_skip "this test only works with bsdgrep(1)"
56	fi
57
58	(cd "$(atf_get_srcdir)" && grep -r --exclude="*.out" -e "test" .) > d_grep_r_implied.out
59
60	atf_check -s exit:0 -x \
61	    "(cd $(atf_get_srcdir) && grep -r --exclude=\"*.out\" -e \"test\") | diff d_grep_r_implied.out -"
62}
63
64atf_test_case rgrep
65rgrep_head()
66{
67	atf_set "require.progs" "rgrep"
68}
69rgrep_body()
70{
71	atf_check -o save:d_grep_r_implied.out grep -r --exclude="*.out" -e "test" "$(atf_get_srcdir)"
72	atf_check -o file:d_grep_r_implied.out rgrep --exclude="*.out" -e "test" "$(atf_get_srcdir)"
73}
74
75atf_test_case gnuext
76gnuext_body()
77{
78	grep_type
79	_type=$?
80
81	atf_check -o save:grep_alnum.out grep -o '[[:alnum:]]' /COPYRIGHT
82	atf_check -o file:grep_alnum.out grep -o '\w' /COPYRIGHT
83
84	atf_check -o save:grep_nalnum.out grep -o '[^[:alnum:]]' /COPYRIGHT
85	atf_check -o file:grep_nalnum.out grep -o '\W' /COPYRIGHT
86
87	atf_check -o save:grep_space.out grep -o '[[:space:]]' /COPYRIGHT
88	atf_check -o file:grep_space.out grep -o '\s' /COPYRIGHT
89
90	atf_check -o save:grep_nspace.out grep -o '[^[:space:]]' /COPYRIGHT
91	atf_check -o file:grep_nspace.out grep -o '\S' /COPYRIGHT
92
93}
94
95atf_test_case zflag
96zflag_body()
97{
98
99	# The -z flag should pick up 'foo' and 'bar' as on the same line with
100	# 'some kind of junk' in between; a bug was present that instead made
101	# it process this incorrectly.
102	printf "foo\nbar\0" > in
103
104	atf_check grep -qz "foo.*bar" in
105}
106
107atf_init_test_cases()
108{
109	atf_add_test_case grep_r_implied
110	atf_add_test_case rgrep
111	atf_add_test_case gnuext
112	atf_add_test_case zflag
113}
114