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