1# $NetBSD: directive-undef.mk,v 1.10 2021/02/16 18:02:19 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 variable "1 2 3"
47.if !defined(${:U1 2 3})
48.  error
49.endif
50.if ${1:U_}${2:U_}${3:U_} != "___"	# these are still defined
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
107all:
108