aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/unit-tests/varmod-assign.mk
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/bmake/unit-tests/varmod-assign.mk')
-rw-r--r--contrib/bmake/unit-tests/varmod-assign.mk103
1 files changed, 86 insertions, 17 deletions
diff --git a/contrib/bmake/unit-tests/varmod-assign.mk b/contrib/bmake/unit-tests/varmod-assign.mk
index a6236253068d..af2c90385315 100644
--- a/contrib/bmake/unit-tests/varmod-assign.mk
+++ b/contrib/bmake/unit-tests/varmod-assign.mk
@@ -1,40 +1,46 @@
-# $NetBSD: varmod-assign.mk,v 1.15 2022/02/09 21:09:24 rillig Exp $
+# $NetBSD: varmod-assign.mk,v 1.28 2025/03/30 01:09:41 rillig Exp $
#
# Tests for the obscure ::= variable modifiers, which perform variable
# assignments during evaluation, just like the = operator in C.
-all: mod-assign-empty
-all: mod-assign-parse
+.if !make(target)
+
+all: mod-assign-empty-{1,2,3,4}
+all: mod-assign-parse-{1,2,3}
all: mod-assign-shell-error
-# The modifier '::?=' applies the assignment operator '?=' 3 times. The
+# In the following loop expression,
+# the '::?=' modifier applies the assignment operator '?=' 3 times. The
# operator '?=' only has an effect for the first time, therefore the variable
# FIRST ends up with the value 1.
.if "${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}" != " first=1"
. error
.endif
-# The modifier '::=' applies the assignment operator '=' 3 times. The
+# In the following loop expression,
+# the modifier '::=' applies the assignment operator '=' 3 times. The
# operator '=' overwrites the previous value, therefore the variable LAST ends
# up with the value 3.
.if "${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}" != " last=3"
. error
.endif
-# The modifier '::+=' applies the assignment operator '+=' 3 times. The
+# In the following loop expression,
+# the modifier '::+=' applies the assignment operator '+=' 3 times. The
# operator '+=' appends 3 times to the variable, therefore the variable
# APPENDED ends up with the value "1 2 3".
.if "${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}" != " appended=1 2 3"
. error
.endif
-# The modifier '::!=' applies the assignment operator '!=' 3 times. Just as
+# In the following loop expression,
+# the modifier '::!=' applies the assignment operator '!=' 3 times. Just as
# with the modifier '::=', the last value is stored in the RAN variable.
.if "${1 2 3:L:@i@${RAN::!=${i:%=echo '<%>';}}@} ran=${RAN}" != " ran=<3>"
. error
.endif
-# The assignments were performed as part of .if conditions and thus happened
+# When a '::=' modifier is evaluated as part of an .if condition, it happens
# in the command line scope.
.if "${FIRST}, ${LAST}, ${APPENDED}, ${RAN}" != "1, 3, 1 2 3, <3>"
. error
@@ -65,32 +71,44 @@ SINK4:= ${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}} ${THEN4}${
. error
.endif
-mod-assign-empty:
+mod-assign-empty-1:
# Assigning to the empty variable would obviously not work since that
- # variable is write-protected. Therefore it is rejected early with a
- # "Bad modifier" message.
+ # variable is write-protected.
+# expect: make: Invalid attempt to assign "value" to variable "" via modifier "::="
@echo $@: ${::=value}
+mod-assign-empty-2:
# In this variant, it is not as obvious that the name of the
- # expression is empty. Assigning to it is rejected as well, with the
- # same "Bad modifier" message.
+ # expression is empty.
+# expect: make: Invalid attempt to assign "overwritten" to variable "" via modifier "::="
@echo $@: ${:Uvalue::=overwritten}
+mod-assign-empty-3:
+ # In this variant, it is not as obvious that the name of the
+ # expression is empty.
+# expect: make: Invalid attempt to assign "appended" to variable "" via modifier "::+="
+ @echo $@: ${:Uvalue::+=appended}
+
+mod-assign-empty-4:
# The :L modifier sets the value of the expression to its variable
# name. The name of the expression is "VAR", therefore assigning to
# that variable works.
+# expect: mod-assign-empty-4: VAR=overwritten
@echo $@: ${VAR:L::=overwritten} VAR=${VAR}
-mod-assign-parse:
+mod-assign-parse-1:
# The modifier for assignment operators starts with a ':'.
# An 'x' after that is an invalid modifier.
- # expect: make: Unknown modifier ":x"
+# expect: make: Unknown modifier "::x"
@echo ${ASSIGN::x}
+mod-assign-parse-2:
# When parsing an assignment operator fails because the operator is
# incomplete, make falls back to the SysV modifier.
@echo ${SYSV::=sysv\:x}${SYSV::x=:y}
+mod-assign-parse-3:
+# expect: make: Unfinished modifier after "value # missing closing brace", expecting "}"
@echo ${ASSIGN::=value # missing closing brace
mod-assign-shell-error:
@@ -99,7 +117,7 @@ mod-assign-shell-error:
# If the command fails, the variable keeps its previous value.
@${SH_ERR::=previous}
- @${SH_ERR::!= echo word; false } echo err=${SH_ERR}
+ @${SH_ERR::!= echo word; (exit 13) } echo err=${SH_ERR}
# XXX: The ::= modifier expands its right-hand side exactly once.
# This differs subtly from normal assignments such as '+=' or '=', which copy
@@ -116,7 +134,7 @@ APPEND.dollar= $${APPEND.indirect}
.endif
-# The assignment modifier can be used in a variable expression that is
+# The assignment modifier can be used in an expression that is
# enclosed in parentheses. In such a case, parsing stops at the first ')',
# not at the first '}'.
VAR= previous
@@ -149,3 +167,54 @@ ${VARNAME}= initial-value # Sets 'VAR.${param}' to 'expanded'.
. error
.endif
.MAKEFLAGS: -d0
+
+
+# Conditional directives are evaluated in command line scope. An assignment
+# modifier that creates a new variable creates it in the command line scope.
+# Existing variables are updated in their previous scope, and environment
+# variables are created in the global scope, as in other situations.
+.MAKEFLAGS: CMD_CMD_VAR=cmd-value
+CMD_GLOBAL_VAR=global-value
+export CMD_ENV_VAR=env-value
+.MAKEFLAGS: -dv
+# expect-reset
+# expect: Command: CMD_CMD_VAR = new-value
+# expect: Global: CMD_GLOBAL_VAR = new-value
+# expect: Global: CMD_ENV_VAR = new-value
+# expect: Global: ignoring delete 'CMD_NEW_VAR' as it is not found
+# expect: Command: CMD_NEW_VAR = new-value
+.if ${CMD_CMD_VAR::=new-value} \
+ || ${CMD_GLOBAL_VAR::=new-value} \
+ || ${CMD_ENV_VAR::=new-value} \
+ || "${CMD_NEW_VAR::=new-value}"
+. error
+.endif
+.MAKEFLAGS: -d0
+
+# Run the 'target' test in a separate sub-make, with reduced debug logging.
+all: run-target
+run-target: .PHONY
+ @${MAKE} -r -f ${MAKEFILE} -dv target 2>&1 | grep ': TARGET_'
+
+.else # make(target)
+
+# The commands of a target are evaluated in target scope. An assignment
+# modifier that creates a new variable creates it in the target scope.
+# Existing variables are updated in their previous scope, and environment
+# variables are created in the global scope, as in other situations.
+#
+# expect: target: TARGET_TARGET_VAR = new-value
+# expect: Global: TARGET_GLOBAL_VAR = new-value
+# expect: Global: TARGET_ENV_VAR = new-value
+# expect: target: TARGET_NEW_VAR = new-value
+.MAKEFLAGS: TARGET_CMD_VAR=cmd-value
+TARGET_GLOBAL_VAR=global-value
+export TARGET_ENV_VAR=env-value
+target: .PHONY TARGET_TARGET_VAR=target-value
+ : ${TARGET_TARGET_VAR::=new-value}
+ : ${TARGET_CMD_VAR::=new-value}
+ : ${TARGET_GLOBAL_VAR::=new-value}
+ : ${TARGET_ENV_VAR::=new-value}
+ : ${TARGET_NEW_VAR::=new-value}
+
+.endif