xref: /netbsd-src/tests/usr.bin/mkdep/t_findcc.sh (revision 1c89b83dd464d704e1d3c9e71ad026804cea137f)
1# $NetBSD: t_findcc.sh,v 1.3 2021/08/20 06:36:10 rillig Exp $
2#
3# Copyright (c) 2021 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Roland Illig.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31n='
32'
33
34# A plain program name is searched in the PATH.  Since in this case, the
35# environment is empty, nothing is found.
36#
37atf_test_case base_not_found
38base_not_found_body() {
39	atf_check -o "inline:(not found)$n" \
40	    env -i \
41		"$(atf_get_srcdir)"/h_findcc 'echo'
42}
43
44# A plain program name is searched in the PATH and, in this example, it is
45# found in '/bin'.
46#
47atf_test_case base_found
48base_found_body() {
49	atf_check -o "inline:/bin/echo$n" \
50	    env -i PATH='/bin:/nonexistent' \
51		"$(atf_get_srcdir)"/h_findcc 'echo'
52}
53
54# A plain program name is searched in the PATH and, in this example, it is
55# found in '/bin', which comes second in the PATH.
56#
57atf_test_case base_found_second
58base_found_second_body() {
59	atf_check -o "inline:/bin/echo$n" \
60	    env -i PATH='/nonexistent:/bin' \
61		"$(atf_get_srcdir)"/h_findcc 'echo'
62}
63
64# A plain program name is searched in the PATH and, in this example, it is
65# found in './bin', a relative path in the PATH, which is rather unusual in
66# practice.
67#
68atf_test_case base_found_reldir
69base_found_reldir_body() {
70	mkdir bin
71	echo '#! /bin/sh' > 'bin/reldir-echo'
72	chmod +x 'bin/reldir-echo'
73
74	atf_check -o "inline:bin/reldir-echo$n" \
75	    env -i PATH='/nonexistent:bin' \
76		"$(atf_get_srcdir)"/h_findcc 'reldir-echo'
77}
78
79# The C compiler can be specified as a program with one or more arguments.
80# If the program name is a plain name without any slash, the argument is
81# discarded.
82#
83# XXX: Discarding the arguments feels unintended.
84#
85atf_test_case base_arg_found
86base_arg_found_body() {
87	atf_check -o "inline:/bin/echo$n" \
88	    env -i PATH='/bin:/nonexistent' \
89		"$(atf_get_srcdir)"/h_findcc 'echo arg'
90}
91
92
93# If the program name contains a slash, no matter where, the program is not
94# searched in the PATH.  This is the same behavior as in /bin/sh.
95#
96atf_test_case rel_not_found
97rel_not_found_body() {
98	atf_check -o "inline:(not found)$n" \
99	    env -i PATH='/' \
100		"$(atf_get_srcdir)"/h_findcc 'bin/echo'
101}
102
103# If the program name contains a slash, no matter where, the program is not
104# searched in the PATH.  This is the same behavior as in /bin/sh.
105#
106atf_test_case rel_found
107rel_found_body() {
108	mkdir bin
109	echo '#! /bin/sh' > bin/echo
110	chmod +x bin/echo
111
112	atf_check -o "inline:bin/echo$n" \
113	    env -i PATH='/' \
114		"$(atf_get_srcdir)"/h_findcc 'bin/echo'
115}
116
117# If the program name contains a slash in the middle and has additional
118# arguments, the arguments are discarded.
119#
120# XXX: Discarding the arguments feels unintended.
121#
122atf_test_case rel_arg_found
123rel_arg_found_body() {
124	mkdir bin
125	echo '#! /bin/sh' > bin/echo
126	chmod +x bin/echo
127
128	atf_check -o "inline:bin/echo$n" \
129	    env -i PATH='/' \
130		"$(atf_get_srcdir)"/h_findcc 'bin/echo arg'
131}
132
133
134atf_test_case abs_not_found
135abs_not_found_body() {
136	atf_check -o "inline:(not found)$n" \
137	    env -i \
138		"$(atf_get_srcdir)"/h_findcc "$PWD/nonexistent/echo"
139}
140
141atf_test_case abs_found
142abs_found_body() {
143	mkdir bin
144	echo '#! /bin/sh' > bin/echo
145	chmod +x bin/echo
146
147	atf_check -o "inline:$PWD/bin/echo$n" \
148	    env -i \
149		"$(atf_get_srcdir)"/h_findcc "$PWD/bin/echo"
150}
151
152# If the program name is an absolute pathname, the arguments are discarded.
153#
154# XXX: Discarding the arguments feels unintended.
155#
156atf_test_case abs_arg_found
157abs_arg_found_body() {
158	mkdir bin
159	echo '#! /bin/sh' > bin/echo
160	chmod +x bin/echo
161
162	atf_check -o "inline:$PWD/bin/echo$n" \
163	    env -i \
164		"$(atf_get_srcdir)"/h_findcc "$PWD/bin/echo arg"
165}
166
167
168atf_init_test_cases() {
169	atf_add_test_case base_not_found
170	atf_add_test_case base_found
171	atf_add_test_case base_found_second
172	atf_add_test_case base_found_reldir
173	atf_add_test_case base_arg_found
174
175	atf_add_test_case rel_not_found
176	atf_add_test_case rel_found
177	atf_add_test_case rel_arg_found
178
179	atf_add_test_case abs_not_found
180	atf_add_test_case abs_found
181	atf_add_test_case abs_arg_found
182}
183