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