Home | History | Annotate | Line # | Download | only in unit-tests
varmod-assign.mk revision 1.14
      1 # $NetBSD: varmod-assign.mk,v 1.14 2021/12/05 10:13:44 rillig Exp $
      2 #
      3 # Tests for the obscure ::= variable modifiers, which perform variable
      4 # assignments during evaluation, just like the = operator in C.
      5 
      6 all:	mod-assign-empty
      7 all:	mod-assign-parse
      8 all:	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 happen in the global scope and thus are preserved even after
     38 # the shell command has been run and the condition has been evaluated.
     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.
     59 SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}} ${THEN3}${ELSE3}${IT3}${IE3}
     60 SINK4:=	${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 
     68 mod-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 
     84 mod-assign-parse:
     85 	# The modifier for assignment operators starts with a ':'.
     86 	# An 'x' after that is an invalid modifier.
     87 	@echo ${ASSIGN::x}	# 'x' is an unknown assignment operator
     88 
     89 	# When parsing an assignment operator fails because the operator is
     90 	# incomplete, make falls back to the SysV modifier.
     91 	@echo ${SYSV::=sysv\:x}${SYSV::x=:y}
     92 
     93 	@echo ${ASSIGN::=value	# missing closing brace
     94 
     95 mod-assign-shell-error:
     96 	# If the command succeeds, the variable is assigned.
     97 	@${SH_OK::!= echo word; true } echo ok=${SH_OK}
     98 
     99 	# If the command fails, the variable keeps its previous value.
    100 	@${SH_ERR::=previous}
    101 	@${SH_ERR::!= echo word; false } echo err=${SH_ERR}
    102 
    103 # XXX: The ::= modifier expands its right-hand side exactly once.
    104 # This differs subtly from normal assignments such as '+=' or '=', which copy
    105 # their right-hand side literally.
    106 APPEND.prev=		previous
    107 APPEND.var=		${APPEND.prev}
    108 APPEND.indirect=	indirect $${:Unot expanded}
    109 APPEND.dollar=		$${APPEND.indirect}
    110 .if ${APPEND.var::+=${APPEND.dollar}} != ""
    111 .  error
    112 .endif
    113 .if ${APPEND.var} != "previous indirect \${:Unot expanded}"
    114 .  error
    115 .endif
    116 
    117 
    118 # The assignment modifier can be used in a variable expression that is
    119 # enclosed in parentheses.  In such a case, parsing stops at the first ')',
    120 # not at the first '}'.
    121 VAR=	previous
    122 _:=	$(VAR::=current})
    123 .if ${VAR} != "current}"
    124 .  error
    125 .endif
    126 
    127 
    128 # Before var.c 1.888 from 2021-03-15, an expression using the modifier '::='
    129 # expanded its variable name once too often during evaluation.  This was only
    130 # relevant for variable names containing a '$' sign in their actual name, not
    131 # the usual VAR.${param}.
    132 .MAKEFLAGS: -dv
    133 param=		twice
    134 VARNAME=	VAR.$${param}	# Indirect variable name because of the '$',
    135 				# to avoid difficult escaping rules.
    136 
    137 ${VARNAME}=	initial-value	# Sets 'VAR.${param}' to 'expanded'.
    138 .if defined(VAR.twice)		# At this point, the '$$' is not expanded.
    139 .  error
    140 .endif
    141 .if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once
    142 .  error			# too often.
    143 .endif
    144 .if defined(VAR.twice)
    145 .  error The variable name in the '::=' modifier is expanded once too often.
    146 .endif
    147 .if ${${VARNAME}} != "assigned-value"
    148 .  error
    149 .endif
    150 .MAKEFLAGS: -d0
    151