xref: /freebsd/contrib/bmake/unit-tests/var-eval-short.mk (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1# $NetBSD: var-eval-short.mk,v 1.8 2021/12/27 18:54:19 rillig Exp $
2#
3# Tests for each variable modifier to ensure that they only do the minimum
4# necessary computations.  If the result of the expression is irrelevant,
5# the modifier should only be parsed.  The modifier should not be evaluated,
6# but if it is evaluated for simplicity of the code (such as ':ts'), it must
7# not have any observable side effects.
8#
9# See also:
10#	var.c, the comment starting with 'The ApplyModifier functions'
11#	ParseModifierPart, for evaluating nested expressions
12#	cond-short.mk
13
14FAIL=	${:!echo unexpected 1>&2!}
15
16# The following tests only ensure that nested expressions are not evaluated.
17# They cannot ensure that any unexpanded text returned from ParseModifierPart
18# is ignored as well.  To do that, it is necessary to step through the code of
19# each modifier.
20
21# TODO: Test the modifiers in the same order as they appear in ApplyModifier.
22
23.if 0 && ${FAIL}
24.endif
25
26.if 0 && ${VAR::=${FAIL}}
27.elif defined(VAR)
28.  error
29.endif
30
31.if 0 && ${${FAIL}:?then:else}
32.endif
33
34.if 0 && ${1:?${FAIL}:${FAIL}}
35.endif
36
37.if 0 && ${0:?${FAIL}:${FAIL}}
38.endif
39
40# Before var.c 1.870 from 2021-03-14, the expression ${FAIL} was evaluated
41# after the loop, when undefining the temporary global loop variable.
42# Since var.c 1.907 from 2021-04-04, a '$' is no longer allowed in the
43# variable name.
44.if 0 && ${:Uword:@${FAIL}@expr@}
45.endif
46
47.if 0 && ${:Uword:@var@${FAIL}@}
48.endif
49
50# Before var.c 1.877 from 2021-03-14, the modifier ':[...]' did not expand
51# the nested expression ${FAIL} and then tried to parse the unexpanded text,
52# which failed since '$' is not a valid range character.
53.if 0 && ${:Uword:[${FAIL}]}
54.endif
55
56# Before var.c 1.867 from 2021-03-14, the modifier ':_' defined the variable
57# even though the whole expression should have only been parsed, not
58# evaluated.
59.if 0 && ${:Uword:_=VAR}
60.elif defined(VAR)
61.  error
62.endif
63
64# Before var.c 1.856 from 2021-03-14, the modifier ':C' did not expand the
65# nested expression ${FAIL}, which is correct, and then tried to compile the
66# unexpanded text as a regular expression, which is unnecessary since the
67# right-hand side of the '&&' cannot influence the outcome of the condition.
68# Compiling the regular expression then failed both because of the '{FAIL}',
69# which is not a valid repetition of the form '{1,5}', and because of the
70# '****', which are repeated repetitions as well.
71# '${FAIL}'
72.if 0 && ${:Uword:C,${FAIL}****,,}
73.endif
74
75DEFINED=	# defined
76.if 0 && ${DEFINED:D${FAIL}}
77.endif
78
79.if 0 && ${:Uword:E}
80.endif
81
82# As of 2021-03-14, the error 'Invalid time value: ${FAIL}}' is ok since
83# ':gmtime' does not expand its argument.
84.if 0 && ${:Uword:gmtime=${FAIL}}
85.endif
86
87.if 0 && ${:Uword:H}
88.endif
89
90.if 0 && ${:Uword:hash}
91.endif
92
93.if 0 && ${value:L}
94.endif
95
96# As of 2021-03-14, the error 'Invalid time value: ${FAIL}}' is ok since
97# ':localtime' does not expand its argument.
98.if 0 && ${:Uword:localtime=${FAIL}}
99.endif
100
101.if 0 && ${:Uword:M${FAIL}}
102.endif
103
104.if 0 && ${:Uword:N${FAIL}}
105.endif
106
107.if 0 && ${:Uword:O}
108.endif
109
110.if 0 && ${:Uword:Ox}
111.endif
112
113.if 0 && ${:Uword:P}
114.endif
115
116.if 0 && ${:Uword:Q}
117.endif
118
119.if 0 && ${:Uword:q}
120.endif
121
122.if 0 && ${:Uword:R}
123.endif
124
125.if 0 && ${:Uword:range}
126.endif
127
128.if 0 && ${:Uword:S,${FAIL},${FAIL},}
129.endif
130
131.if 0 && ${:Uword:sh}
132.endif
133
134.if 0 && ${:Uword:T}
135.endif
136
137.if 0 && ${:Uword:ts/}
138.endif
139
140.if 0 && ${:U${FAIL}}
141.endif
142
143.if 0 && ${:Uword:u}
144.endif
145
146.if 0 && ${:Uword:word=replacement}
147.endif
148
149# Before var.c 1.875 from 2021-03-14, Var_Parse returned "${FAIL}else" for the
150# irrelevant right-hand side of the condition, even though this was not
151# necessary.  Since the return value from Var_Parse is supposed to be ignored
152# anyway, and since it is actually ignored in an overly complicated way,
153# an empty string suffices.
154.MAKEFLAGS: -dcpv
155.if 0 && ${0:?${FAIL}then:${FAIL}else}
156.endif
157
158# The ':L' is applied before the ':?' modifier, giving the expression a name
159# and a value, just to see whether this value gets passed through or whether
160# the parse-only mode results in an empty string (only visible in the debug
161# log).  As of var.c 1.875 from 2021-03-14, the value of the variable gets
162# through, even though an empty string would suffice.
163DEFINED=	defined
164.if 0 && ${DEFINED:L:?${FAIL}then:${FAIL}else}
165.endif
166.MAKEFLAGS: -d0
167
168all:
169