xref: /freebsd/contrib/bmake/unit-tests/directive-undef.mk (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1# $NetBSD: directive-undef.mk,v 1.15 2024/07/04 17:47:54 rillig Exp $
2#
3# Tests for the .undef directive.
4#
5# See also:
6#	directive-misspellings.mk
7
8# Before var.c 1.737 from 2020-12-19, .undef only undefined the first
9# variable, silently skipping all further variable names.
10#
11# Before var.c 1.761 from 2020-12-22, .undef complained about too many
12# arguments.
13#
14# Since var.c 1.761 from 2020-12-22, .undef handles multiple variable names
15# just like the .export directive.
161=		1
172=		2
183=		3
19.undef 1 2 3
20.if ${1:U_}${2:U_}${3:U_} != ___
21.  warning $1$2$3
22.endif
23
24
25# Without any arguments, until var.c 1.736 from 2020-12-19, .undef tried
26# to delete the variable with the empty name, which never exists; see
27# varname-empty.mk.  Since var.c 1.737 from 2020-12-19, .undef complains
28# about a missing argument.
29# expect+1: The .undef directive requires an argument
30.undef
31
32
33# Trying to delete the variable with the empty name is ok, it just won't
34# ever do anything since that variable is never defined.
35.undef ${:U}
36
37
38# The argument of .undef is first expanded exactly once and then split into
39# words, just like everywhere else.  This prevents variables whose names
40# contain spaces or unbalanced 'single' or "double" quotes from being
41# undefined, but these characters do not appear in variables names anyway.
421=		1
432=		2
443=		3
45${:U1 2 3}=	one two three
46VARNAMES=	1 2 3
47.undef ${VARNAMES}		# undefines the variables "1", "2" and "3"
48.if ${${:U1 2 3}} != "one two three"	# still there
49.  error
50.endif
51.if ${1:U_}${2:U_}${3:U_} != "___"	# these have been undefined
52.  error
53.endif
54
55
56# A variable named " " cannot be undefined.  There's no practical use case
57# for such variables anyway.
58SPACE=		${:U }
59${SPACE}=	space
60.if !defined(${SPACE})
61.  error
62.endif
63.undef ${SPACE}
64.if !defined(${SPACE})
65.  error
66.endif
67
68
69# A variable named "$" can be undefined since the argument to .undef is
70# expanded exactly once, before being split into words.
71DOLLAR=		$$
72${DOLLAR}=	dollar
73.if !defined(${DOLLAR})
74.  error
75.endif
76.undef ${DOLLAR}
77.if defined(${DOLLAR})
78.  error
79.endif
80
81
82# Since var.c 1.762 from 2020-12-22, parse errors in the argument should be
83# properly detected and should stop the .undef directive from doing any work.
84#
85# As of var.c 1.762, this doesn't happen though because the error handling
86# in Var_Parse and Var_Subst is not done properly.
87# expect+1: while evaluating variable "VARNAMES" with value "VARNAMES": Unknown modifier "Z"
88.undef ${VARNAMES:L:Z}
89
90
91UT_EXPORTED=	exported-value
92.export UT_EXPORTED
93.if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "exported-value"
94.  error
95.endif
96.if !${.MAKE.EXPORTED:MUT_EXPORTED}
97.  error
98.endif
99.undef UT_EXPORTED		# XXX: does not update .MAKE.EXPORTED
100.if ${:!echo "\${UT_EXPORTED:-not-exported}"!} != "not-exported"
101.  error
102.endif
103.if ${.MAKE.EXPORTED:MUT_EXPORTED}
104# expect+1: warning: UT_EXPORTED is still listed in .MAKE.EXPORTED even though spaceit is not exported anymore.
105.  warning UT_EXPORTED is still listed in .MAKE.EXPORTED even though $\
106	   it is not exported anymore.
107.endif
108
109
110# When an exported variable is undefined, the variable is removed both from
111# the global scope as well as from the environment.
112DIRECT=		direct
113INDIRECT=	in-${DIRECT}
114.export DIRECT INDIRECT
115.if ${DIRECT} != "direct"
116.  error
117.endif
118.if ${INDIRECT} != "in-direct"
119.  error
120.endif
121
122# Deletes the variables from the global scope and also from the environment.
123# This applies to both variables, even though 'INDIRECT' is not actually
124# exported yet since it refers to another variable.
125.undef DIRECT			# Separate '.undef' directives,
126.undef INDIRECT			# for backwards compatibility.
127
128.if ${DIRECT:Uundefined} != "undefined"
129.  error
130.endif
131.if ${INDIRECT:Uundefined} != "undefined"
132.  error
133.endif
134
135
136# Since var.c 1.570 from 2020-10-06 and before var.c 1.1014 from 2022-03-26,
137# make ran into an assertion failure when trying to undefine a variable that
138# was based on an environment variable.
139.if ${ENV_VAR} != "env-value"	# see ./Makefile, ENV.directive-undef
140.  error
141.endif
142ENV_VAR+=	appended	# moves the short-lived variable to the
143				# global scope
144.undef ENV_VAR			# removes the variable from both the global
145				# scope and from the environment
146
147
148all:
149