Home | History | Annotate | Line # | Download | only in unit-tests
varmod-assign.mk revision 1.22
      1 # $NetBSD: varmod-assign.mk,v 1.22 2024/06/30 15:21: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 
      6 .if !make(target)
      7 
      8 all:	mod-assign-empty
      9 all:	mod-assign-parse
     10 all:	mod-assign-shell-error
     11 
     12 # In the following loop expression,
     13 # the '::?=' modifier applies the assignment operator '?=' 3 times. The
     14 # operator '?=' only has an effect for the first time, therefore the variable
     15 # FIRST ends up with the value 1.
     16 .if "${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}" != " first=1"
     17 .  error
     18 .endif
     19 
     20 # In the following loop expression,
     21 # the modifier '::=' applies the assignment operator '=' 3 times. The
     22 # operator '=' overwrites the previous value, therefore the variable LAST ends
     23 # up with the value 3.
     24 .if "${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}" != " last=3"
     25 .  error
     26 .endif
     27 
     28 # In the following loop expression,
     29 # the modifier '::+=' applies the assignment operator '+=' 3 times. The
     30 # operator '+=' appends 3 times to the variable, therefore the variable
     31 # APPENDED ends up with the value "1 2 3".
     32 .if "${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}" != " appended=1 2 3"
     33 .  error
     34 .endif
     35 
     36 # In the following loop expression,
     37 # the modifier '::!=' applies the assignment operator '!=' 3 times. Just as
     38 # with the modifier '::=', the last value is stored in the RAN variable.
     39 .if "${1 2 3:L:@i@${RAN::!=${i:%=echo '<%>';}}@} ran=${RAN}" != " ran=<3>"
     40 .  error
     41 .endif
     42 
     43 # When a '::=' modifier is evaluated as part of an .if condition, it happens
     44 # in the command line scope.
     45 .if "${FIRST}, ${LAST}, ${APPENDED}, ${RAN}" != "1, 3, 1 2 3, <3>"
     46 .  error
     47 .endif
     48 
     49 # Tests for nested assignments, which are hard to read and therefore seldom
     50 # used in practice.
     51 
     52 # The condition "1" is true, therefore THEN1 gets assigned a value,
     53 # and the inner IT1 as well.  Nothing surprising here.
     54 .if "${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}} ${THEN1}${ELSE1}${IT1}${IE1}" != " then1t1"
     55 .  error
     56 .endif
     57 
     58 # The condition "0" is false, therefore ELSE2 gets assigned a value,
     59 # and the inner IE2 as well.  Nothing surprising here as well.
     60 .if "${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}} ${THEN2}${ELSE2}${IT2}${IE2}" != " else2e2"
     61 .  error
     62 .endif
     63 
     64 # The same effects happen when the variables are defined elsewhere.
     65 SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}} ${THEN3}${ELSE3}${IT3}${IE3}
     66 SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}} ${THEN4}${ELSE4}${IT4}${IE4}
     67 .if ${SINK3} != " then3t3"
     68 .  error
     69 .endif
     70 .if ${SINK4} != " else4e4"
     71 .  error
     72 .endif
     73 
     74 mod-assign-empty:
     75 	# Assigning to the empty variable would obviously not work since that
     76 	# variable is write-protected.  Therefore it is rejected early with a
     77 	# "Bad modifier" message.
     78 	@echo $@: ${::=value}
     79 
     80 	# In this variant, it is not as obvious that the name of the
     81 	# expression is empty.  Assigning to it is rejected as well, with the
     82 	# same "Bad modifier" message.
     83 	@echo $@: ${:Uvalue::=overwritten}
     84 
     85 	# The :L modifier sets the value of the expression to its variable
     86 	# name.  The name of the expression is "VAR", therefore assigning to
     87 	# that variable works.
     88 	@echo $@: ${VAR:L::=overwritten} VAR=${VAR}
     89 
     90 mod-assign-parse:
     91 	# The modifier for assignment operators starts with a ':'.
     92 	# An 'x' after that is an invalid modifier.
     93 	# expect: make: in target "mod-assign-parse": while evaluating variable "ASSIGN": Unknown modifier ":x"
     94 	@echo ${ASSIGN::x}
     95 
     96 	# When parsing an assignment operator fails because the operator is
     97 	# incomplete, make falls back to the SysV modifier.
     98 # expect: make: in target "mod-assign-parse": while evaluating variable "ASSIGN": Unfinished modifier ('}' missing)
     99 	@echo ${SYSV::=sysv\:x}${SYSV::x=:y}
    100 
    101 	@echo ${ASSIGN::=value	# missing closing brace
    102 
    103 mod-assign-shell-error:
    104 	# If the command succeeds, the variable is assigned.
    105 	@${SH_OK::!= echo word; true } echo ok=${SH_OK}
    106 
    107 	# If the command fails, the variable keeps its previous value.
    108 	@${SH_ERR::=previous}
    109 	@${SH_ERR::!= echo word; (exit 13) } echo err=${SH_ERR}
    110 
    111 # XXX: The ::= modifier expands its right-hand side exactly once.
    112 # This differs subtly from normal assignments such as '+=' or '=', which copy
    113 # their right-hand side literally.
    114 APPEND.prev=		previous
    115 APPEND.var=		${APPEND.prev}
    116 APPEND.indirect=	indirect $${:Unot expanded}
    117 APPEND.dollar=		$${APPEND.indirect}
    118 .if ${APPEND.var::+=${APPEND.dollar}} != ""
    119 .  error
    120 .endif
    121 .if ${APPEND.var} != "previous indirect \${:Unot expanded}"
    122 .  error
    123 .endif
    124 
    125 
    126 # The assignment modifier can be used in an expression that is
    127 # enclosed in parentheses.  In such a case, parsing stops at the first ')',
    128 # not at the first '}'.
    129 VAR=	previous
    130 _:=	$(VAR::=current})
    131 .if ${VAR} != "current}"
    132 .  error
    133 .endif
    134 
    135 
    136 # Before var.c 1.888 from 2021-03-15, an expression using the modifier '::='
    137 # expanded its variable name once too often during evaluation.  This was only
    138 # relevant for variable names containing a '$' sign in their actual name, not
    139 # the usual VAR.${param}.
    140 .MAKEFLAGS: -dv
    141 param=		twice
    142 VARNAME=	VAR.$${param}	# Indirect variable name because of the '$',
    143 				# to avoid difficult escaping rules.
    144 
    145 ${VARNAME}=	initial-value	# Sets 'VAR.${param}' to 'expanded'.
    146 .if defined(VAR.twice)		# At this point, the '$$' is not expanded.
    147 .  error
    148 .endif
    149 .if ${${VARNAME}::=assigned-value} # Here the variable name gets expanded once
    150 .  error			# too often.
    151 .endif
    152 .if defined(VAR.twice)
    153 .  error The variable name in the '::=' modifier is expanded once too often.
    154 .endif
    155 .if ${${VARNAME}} != "assigned-value"
    156 .  error
    157 .endif
    158 .MAKEFLAGS: -d0
    159 
    160 
    161 # Conditional directives are evaluated in command line scope.  An assignment
    162 # modifier that creates a new variable creates it in the command line scope.
    163 # Existing variables are updated in their previous scope, and environment
    164 # variables are created in the global scope, as in other situations.
    165 .MAKEFLAGS: CMD_CMD_VAR=cmd-value
    166 CMD_GLOBAL_VAR=global-value
    167 export CMD_ENV_VAR=env-value
    168 .MAKEFLAGS: -dv
    169 # expect-reset
    170 # expect: Command: CMD_CMD_VAR = new-value
    171 # expect: Global: CMD_GLOBAL_VAR = new-value
    172 # expect: Global: CMD_ENV_VAR = new-value
    173 # expect: Global: ignoring delete 'CMD_NEW_VAR' as it is not found
    174 # expect: Command: CMD_NEW_VAR = new-value
    175 .if ${CMD_CMD_VAR::=new-value} \
    176   || ${CMD_GLOBAL_VAR::=new-value} \
    177   || ${CMD_ENV_VAR::=new-value} \
    178   || "${CMD_NEW_VAR::=new-value}"
    179 .  error
    180 .endif
    181 .MAKEFLAGS: -d0
    182 
    183 # Run the 'target' test in a separate sub-make, with reduced debug logging.
    184 all: run-target
    185 run-target: .PHONY
    186 	@${MAKE} -r -f ${MAKEFILE} -dv target 2>&1 | grep ': TARGET_'
    187 
    188 .else # make(target)
    189 
    190 # The commands of a target are evaluated in target scope.  An assignment
    191 # modifier that creates a new variable creates it in the target scope.
    192 # Existing variables are updated in their previous scope, and environment
    193 # variables are created in the global scope, as in other situations.
    194 #
    195 # expect: target: TARGET_TARGET_VAR = new-value
    196 # expect: Global: TARGET_GLOBAL_VAR = new-value
    197 # expect: Global: TARGET_ENV_VAR = new-value
    198 # expect: target: TARGET_NEW_VAR = new-value
    199 .MAKEFLAGS: TARGET_CMD_VAR=cmd-value
    200 TARGET_GLOBAL_VAR=global-value
    201 export TARGET_ENV_VAR=env-value
    202 target: .PHONY TARGET_TARGET_VAR=target-value
    203 	: ${TARGET_TARGET_VAR::=new-value}
    204 	: ${TARGET_CMD_VAR::=new-value}
    205 	: ${TARGET_GLOBAL_VAR::=new-value}
    206 	: ${TARGET_ENV_VAR::=new-value}
    207 	: ${TARGET_NEW_VAR::=new-value}
    208 
    209 .endif
    210