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