xref: /freebsd/contrib/bmake/unit-tests/varmod-assign.mk (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1# $NetBSD: varmod-assign.mk,v 1.15 2022/02/09 21:09:24 rillig Exp $
2#
3# Tests for the obscure ::= variable modifiers, which perform variable
4# assignments during evaluation, just like the = operator in C.
5
6all:	mod-assign-empty
7all:	mod-assign-parse
8all:	mod-assign-shell-error
9
10# The modifier '::?=' applies the assignment operator '?=' 3 times. The
11# operator '?=' only has an effect for the first time, therefore the variable
12# FIRST ends up with the value 1.
13.if "${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}" != " first=1"
14.  error
15.endif
16
17# The modifier '::=' applies the assignment operator '=' 3 times. The
18# operator '=' overwrites the previous value, therefore the variable LAST ends
19# up with the value 3.
20.if "${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}" != " last=3"
21.  error
22.endif
23
24# The modifier '::+=' applies the assignment operator '+=' 3 times. The
25# operator '+=' appends 3 times to the variable, therefore the variable
26# APPENDED ends up with the value "1 2 3".
27.if "${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}" != " appended=1 2 3"
28.  error
29.endif
30
31# The modifier '::!=' applies the assignment operator '!=' 3 times. Just as
32# with the modifier '::=', the last value is stored in the RAN variable.
33.if "${1 2 3:L:@i@${RAN::!=${i:%=echo '<%>';}}@} ran=${RAN}" != " ran=<3>"
34.  error
35.endif
36
37# The assignments were performed as part of .if conditions and thus happened
38# in the command line scope.
39.if "${FIRST}, ${LAST}, ${APPENDED}, ${RAN}" != "1, 3, 1 2 3, <3>"
40.  error
41.endif
42
43# Tests for nested assignments, which are hard to read and therefore seldom
44# used in practice.
45
46# The condition "1" is true, therefore THEN1 gets assigned a value,
47# and the inner IT1 as well.  Nothing surprising here.
48.if "${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}} ${THEN1}${ELSE1}${IT1}${IE1}" != " then1t1"
49.  error
50.endif
51
52# The condition "0" is false, therefore ELSE2 gets assigned a value,
53# and the inner IE2 as well.  Nothing surprising here as well.
54.if "${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}} ${THEN2}${ELSE2}${IT2}${IE2}" != " else2e2"
55.  error
56.endif
57
58# The same effects happen when the variables are defined elsewhere.
59SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}} ${THEN3}${ELSE3}${IT3}${IE3}
60SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}} ${THEN4}${ELSE4}${IT4}${IE4}
61.if ${SINK3} != " then3t3"
62.  error
63.endif
64.if ${SINK4} != " else4e4"
65.  error
66.endif
67
68mod-assign-empty:
69	# Assigning to the empty variable would obviously not work since that
70	# variable is write-protected.  Therefore it is rejected early with a
71	# "Bad modifier" message.
72	@echo $@: ${::=value}
73
74	# In this variant, it is not as obvious that the name of the
75	# expression is empty.  Assigning to it is rejected as well, with the
76	# same "Bad modifier" message.
77	@echo $@: ${:Uvalue::=overwritten}
78
79	# The :L modifier sets the value of the expression to its variable
80	# name.  The name of the expression is "VAR", therefore assigning to
81	# that variable works.
82	@echo $@: ${VAR:L::=overwritten} VAR=${VAR}
83
84mod-assign-parse:
85	# The modifier for assignment operators starts with a ':'.
86	# An 'x' after that is an invalid modifier.
87	# expect: make: Unknown modifier ":x"
88	@echo ${ASSIGN::x}
89
90	# When parsing an assignment operator fails because the operator is
91	# incomplete, make falls back to the SysV modifier.
92	@echo ${SYSV::=sysv\:x}${SYSV::x=:y}
93
94	@echo ${ASSIGN::=value	# missing closing brace
95
96mod-assign-shell-error:
97	# If the command succeeds, the variable is assigned.
98	@${SH_OK::!= echo word; true } echo ok=${SH_OK}
99
100	# If the command fails, the variable keeps its previous value.
101	@${SH_ERR::=previous}
102	@${SH_ERR::!= echo word; false } echo err=${SH_ERR}
103
104# XXX: The ::= modifier expands its right-hand side exactly once.
105# This differs subtly from normal assignments such as '+=' or '=', which copy
106# their right-hand side literally.
107APPEND.prev=		previous
108APPEND.var=		${APPEND.prev}
109APPEND.indirect=	indirect $${:Unot expanded}
110APPEND.dollar=		$${APPEND.indirect}
111.if ${APPEND.var::+=${APPEND.dollar}} != ""
112.  error
113.endif
114.if ${APPEND.var} != "previous indirect \${:Unot expanded}"
115.  error
116.endif
117
118
119# The assignment modifier can be used in a variable expression that is
120# enclosed in parentheses.  In such a case, parsing stops at the first ')',
121# not at the first '}'.
122VAR=	previous
123_:=	$(VAR::=current})
124.if ${VAR} != "current}"
125.  error
126.endif
127
128
129# Before var.c 1.888 from 2021-03-15, an expression using the modifier '::='
130# expanded its variable name once too often during evaluation.  This was only
131# relevant for variable names containing a '$' sign in their actual name, not
132# the usual VAR.${param}.
133.MAKEFLAGS: -dv
134param=		twice
135VARNAME=	VAR.$${param}	# Indirect variable name because of the '$',
136				# to avoid difficult escaping rules.
137
138${VARNAME}=	initial-value	# Sets 'VAR.${param}' to 'expanded'.
139.if defined(VAR.twice)		# At this point, the '$$' is not expanded.
140.  error
141.endif
142.if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once
143.  error			# too often.
144.endif
145.if defined(VAR.twice)
146.  error The variable name in the '::=' modifier is expanded once too often.
147.endif
148.if ${${VARNAME}} != "assigned-value"
149.  error
150.endif
151.MAKEFLAGS: -d0
152