From 518dc17b320ebb37db58e849011ebb1090249e57 Mon Sep 17 00:00:00 2001 From: rillig Date: Sat, 1 Jun 2024 18:44:05 +0000 Subject: [PATCH] tests/make: test more modifiers and special variables --- .../unit-tests/directive-export-literal.exp | 3 +++ .../unit-tests/directive-export-literal.mk | 25 ++++++++++++++++++- usr.bin/make/unit-tests/varmod-head.mk | 8 +++++- usr.bin/make/unit-tests/varmod-sysv.exp | 2 ++ usr.bin/make/unit-tests/varmod-sysv.mk | 10 +++++++- usr.bin/make/unit-tests/varmod-tail.mk | 10 +++++++- .../make/unit-tests/varmod-to-separator.exp | 24 ++++++++++-------- .../make/unit-tests/varmod-to-separator.mk | 15 ++++++++++- usr.bin/make/unit-tests/varmod.exp | 6 +++++ usr.bin/make/unit-tests/varmod.mk | 18 +++++++++++-- .../unit-tests/varname-dot-make-level.exp | 3 +++ .../make/unit-tests/varname-dot-make-level.mk | 24 ++++++++++++++---- 12 files changed, 126 insertions(+), 22 deletions(-) diff --git a/usr.bin/make/unit-tests/directive-export-literal.exp b/usr.bin/make/unit-tests/directive-export-literal.exp index c5557e363666..baece9e32217 100644 --- a/usr.bin/make/unit-tests/directive-export-literal.exp +++ b/usr.bin/make/unit-tests/directive-export-literal.exp @@ -1,2 +1,5 @@ value with ${UNEXPANDED} expression +value literal +value indirect +value ${indirect:L} exit status 0 diff --git a/usr.bin/make/unit-tests/directive-export-literal.mk b/usr.bin/make/unit-tests/directive-export-literal.mk index 5fafa4a7282d..105af0102b83 100644 --- a/usr.bin/make/unit-tests/directive-export-literal.mk +++ b/usr.bin/make/unit-tests/directive-export-literal.mk @@ -1,4 +1,4 @@ -# $NetBSD: directive-export-literal.mk,v 1.7 2020/12/13 01:07:54 rillig Exp $ +# $NetBSD: directive-export-literal.mk,v 1.8 2024/06/01 18:44:05 rillig Exp $ # # Tests for the .export-literal directive, which exports a variable value # without expanding it. @@ -9,5 +9,28 @@ UT_VAR= value with ${UNEXPANDED} expression .export-literal # oops: missing argument +# After a variable whose value does not contain a '$' is exported, a following +# .export-literal can be skipped, to avoid a setenv call, which may leak +# memory on some platforms. +UT_TWICE_LITERAL= value literal +.export UT_TWICE_LITERAL +.export-literal UT_TWICE_LITERAL + +# XXX: After an .export, an .export-literal has no effect, even when the +# variable value contains a '$'. +UT_TWICE_EXPR= value ${indirect:L} +.export UT_TWICE_EXPR +.export-literal UT_TWICE_EXPR + +# After an .export, an .unexport resets the variable's exported state, +# re-enabling a later .export-literal. +UT_TWICE_EXPR_UNEXPORT= value ${indirect:L} +.export UT_TWICE_EXPR_UNEXPORT +.unexport UT_TWICE_EXPR_UNEXPORT +.export-literal UT_TWICE_EXPR_UNEXPORT + all: @echo "$$UT_VAR" + @echo "$$UT_TWICE_LITERAL" + @echo "$$UT_TWICE_EXPR" + @echo "$$UT_TWICE_EXPR_UNEXPORT" diff --git a/usr.bin/make/unit-tests/varmod-head.mk b/usr.bin/make/unit-tests/varmod-head.mk index f1a135cb328d..03d338d42742 100644 --- a/usr.bin/make/unit-tests/varmod-head.mk +++ b/usr.bin/make/unit-tests/varmod-head.mk @@ -1,4 +1,4 @@ -# $NetBSD: varmod-head.mk,v 1.5 2022/07/10 21:11:49 rillig Exp $ +# $NetBSD: varmod-head.mk,v 1.6 2024/06/01 18:44:05 rillig Exp $ # # Tests for the :H variable modifier, which returns the dirname of # each of the words in the variable value. @@ -61,4 +61,10 @@ _!= echo "The modifier ':H' generates an empty word." 1>&2; echo . error .endif +# If the ':H' is not directly followed by a delimiting ':' or '}', the +# ':from=to' modifier is tried as a fallback. +.if ${:U Head :Head=replaced} != "replaced" +. error +.endif + all: .PHONY diff --git a/usr.bin/make/unit-tests/varmod-sysv.exp b/usr.bin/make/unit-tests/varmod-sysv.exp index 6a3728c3c0fc..fe88ebd0a306 100644 --- a/usr.bin/make/unit-tests/varmod-sysv.exp +++ b/usr.bin/make/unit-tests/varmod-sysv.exp @@ -145,6 +145,8 @@ pre-middle-suffix pre%ffix=NPre% "NPre-middle-su" suffix pre%ffix=NPre%NS "suffix" prefix pre%ffix=NPre%NS "prefix" pre-middle-suffix pre%ffix=NPre%NS "NPre-middle-suNS" +make: Unfinished modifier for "error" ('}' missing) +make: "varmod-sysv.mk" line 259: Malformed conditional (${error:L:from=$(})) make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/varmod-sysv.mk b/usr.bin/make/unit-tests/varmod-sysv.mk index 0f92e1df7032..d37c33e47229 100644 --- a/usr.bin/make/unit-tests/varmod-sysv.mk +++ b/usr.bin/make/unit-tests/varmod-sysv.mk @@ -1,4 +1,4 @@ -# $NetBSD: varmod-sysv.mk,v 1.16 2023/11/19 21:47:52 rillig Exp $ +# $NetBSD: varmod-sysv.mk,v 1.17 2024/06/01 18:44:05 rillig Exp $ # # Tests for the variable modifier ':from=to', which replaces the suffix # "from" with "to". It can also use '%' as a wildcard. @@ -252,4 +252,12 @@ INDIRECT= 1:${VALUE} 2:$${VALUE} 4:$$$${VALUE} . endfor .endfor + +# The error case of an unfinished ':from=to' modifier after the '=' requires +# an expression that is missing the closing '}'. +# expect+1: Malformed conditional (${error:L:from=$(})) +.if ${error:L:from=$(}) +.endif + + all: diff --git a/usr.bin/make/unit-tests/varmod-tail.mk b/usr.bin/make/unit-tests/varmod-tail.mk index 05eae481fe3e..614c1af2a0b4 100644 --- a/usr.bin/make/unit-tests/varmod-tail.mk +++ b/usr.bin/make/unit-tests/varmod-tail.mk @@ -1,8 +1,16 @@ -# $NetBSD: varmod-tail.mk,v 1.4 2020/12/20 22:57:40 rillig Exp $ +# $NetBSD: varmod-tail.mk,v 1.5 2024/06/01 18:44:05 rillig Exp $ # # Tests for the :T variable modifier, which returns the basename of each of # the words in the variable value. + +# If the ':T' is not directly followed by a delimiting ':' or '}', the +# ':from=to' modifier is tried as a fallback. +.if ${:U Tail :Tail=replaced} != "replaced" +. error +.endif + + all: .for path in a/b/c def a.b.c a.b/c a a.a .gitignore a a.a trailing/ @echo "tail (basename) of '"${path:Q}"' is '"${path:T:Q}"'" diff --git a/usr.bin/make/unit-tests/varmod-to-separator.exp b/usr.bin/make/unit-tests/varmod-to-separator.exp index 6bdd6f0c2d43..586b19f123b5 100644 --- a/usr.bin/make/unit-tests/varmod-to-separator.exp +++ b/usr.bin/make/unit-tests/varmod-to-separator.exp @@ -2,24 +2,28 @@ make: "varmod-to-separator.mk" line 155: while evaluating variable "WORDS": Inva make: "varmod-to-separator.mk" line 155: Malformed conditional (${WORDS:[1..3]:ts\400:tu}) make: "varmod-to-separator.mk" line 171: while evaluating variable "WORDS": Invalid character number at "100:tu}" make: "varmod-to-separator.mk" line 171: Malformed conditional (${WORDS:[1..3]:ts\x100:tu}) +make: "varmod-to-separator.mk" line 180: while evaluating variable "word": Invalid character number at ",}" +make: "varmod-to-separator.mk" line 180: Malformed conditional (${word:L:ts\x,}) +make: "varmod-to-separator.mk" line 187: while evaluating variable "word": Invalid character number at "112233445566778899}" +make: "varmod-to-separator.mk" line 187: Malformed conditional (${word:L:ts\x112233445566778899}) make: Bad modifier ":ts\-300" for variable "WORDS" -make: "varmod-to-separator.mk" line 179: Malformed conditional (${WORDS:[1..3]:ts\-300:tu}) +make: "varmod-to-separator.mk" line 192: Malformed conditional (${WORDS:[1..3]:ts\-300:tu}) make: Bad modifier ":ts\8" for variable "1 2 3" -make: "varmod-to-separator.mk" line 188: Malformed conditional (${1 2 3:L:ts\8:tu}) +make: "varmod-to-separator.mk" line 201: Malformed conditional (${1 2 3:L:ts\8:tu}) make: Bad modifier ":ts\100L" for variable "1 2 3" -make: "varmod-to-separator.mk" line 196: Malformed conditional (${1 2 3:L:ts\100L}) +make: "varmod-to-separator.mk" line 209: Malformed conditional (${1 2 3:L:ts\100L}) make: Bad modifier ":ts\x40g" for variable "1 2 3" -make: "varmod-to-separator.mk" line 204: Malformed conditional (${1 2 3:L:ts\x40g}) +make: "varmod-to-separator.mk" line 217: Malformed conditional (${1 2 3:L:ts\x40g}) make: Bad modifier ":tx" for variable "WORDS" -make: "varmod-to-separator.mk" line 214: Malformed conditional (${WORDS:tx}) +make: "varmod-to-separator.mk" line 227: Malformed conditional (${WORDS:tx}) make: Bad modifier ":ts\X" for variable "WORDS" -make: "varmod-to-separator.mk" line 223: Malformed conditional (${WORDS:ts\X}) +make: "varmod-to-separator.mk" line 236: Malformed conditional (${WORDS:ts\X}) make: Bad modifier ":t\X" for variable "WORDS" -make: "varmod-to-separator.mk" line 232: Malformed conditional (${WORDS:t\X} != "anything") +make: "varmod-to-separator.mk" line 245: Malformed conditional (${WORDS:t\X} != "anything") make: Bad modifier ":ts\69" for variable "" -make: "varmod-to-separator.mk" line 249: Malformed conditional (${:Ua b:ts\69}) -make: "varmod-to-separator.mk" line 258: while evaluating "${:Ua b:ts\x1F60E}": Invalid character number at "1F60E}" -make: "varmod-to-separator.mk" line 258: Malformed conditional (${:Ua b:ts\x1F60E}) +make: "varmod-to-separator.mk" line 262: Malformed conditional (${:Ua b:ts\69}) +make: "varmod-to-separator.mk" line 271: while evaluating "${:Ua b:ts\x1F60E}": Invalid character number at "1F60E}" +make: "varmod-to-separator.mk" line 271: Malformed conditional (${:Ua b:ts\x1F60E}) make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/varmod-to-separator.mk b/usr.bin/make/unit-tests/varmod-to-separator.mk index 8b8aeb4d3c33..ec48dacbb60c 100644 --- a/usr.bin/make/unit-tests/varmod-to-separator.mk +++ b/usr.bin/make/unit-tests/varmod-to-separator.mk @@ -1,4 +1,4 @@ -# $NetBSD: varmod-to-separator.mk,v 1.14 2024/04/20 10:18:55 rillig Exp $ +# $NetBSD: varmod-to-separator.mk,v 1.15 2024/06/01 18:44:05 rillig Exp $ # # Tests for the :ts variable modifier, which joins the words of the variable # using an arbitrary character as word separator. @@ -174,6 +174,19 @@ WORDS= one two three four five six . warning The separator \x100 is accepted even though it is out of bounds. .endif +# The number after ':ts\x' must be hexadecimal. +# expect+2: while evaluating variable "word": Invalid character number at ",}" +# expect+1: Malformed conditional (${word:L:ts\x,}) +.if ${word:L:ts\x,} +.endif + +# The hexadecimal number must be in the range of 'unsigned long' on all +# supported platforms. +# expect+2: while evaluating variable "word": Invalid character number at "112233445566778899}" +# expect+1: Malformed conditional (${word:L:ts\x112233445566778899}) +.if ${word:L:ts\x112233445566778899} +.endif + # Negative numbers are not allowed for the separator character. # expect+1: Malformed conditional (${WORDS:[1..3]:ts\-300:tu}) .if ${WORDS:[1..3]:ts\-300:tu} diff --git a/usr.bin/make/unit-tests/varmod.exp b/usr.bin/make/unit-tests/varmod.exp index 22a7019dfc1a..7afbe4b721db 100644 --- a/usr.bin/make/unit-tests/varmod.exp +++ b/usr.bin/make/unit-tests/varmod.exp @@ -3,6 +3,12 @@ make: "varmod.mk" line 101: Invalid variable name ':', at "$:L} != """ make: "varmod.mk" line 107: while evaluating "${:Uword:@word@${word}$@} != "word"": Dollar followed by nothing make: "varmod.mk" line 117: while evaluating variable "VAR": Missing delimiter ':' after modifier "P" make: "varmod.mk" line 119: Missing argument for ".error" +make: Bad modifier ":[99333000222000111000]" for variable "word" +make: "varmod.mk" line 125: Malformed conditional (${word:L:[99333000222000111000]}) +make: Bad modifier ":[2147483648]" for variable "word" +make: "varmod.mk" line 128: Malformed conditional (${word:L:[2147483648]}) +make: "varmod.mk" line 135: while evaluating variable "word": Invalid number "99333000222000111000}" for ':range' modifier +make: "varmod.mk" line 135: Malformed conditional (${word:L:range=99333000222000111000}) make: Fatal errors encountered -- cannot continue make: stopped in unit-tests exit status 1 diff --git a/usr.bin/make/unit-tests/varmod.mk b/usr.bin/make/unit-tests/varmod.mk index c749dfb9659d..b357f0cba90b 100644 --- a/usr.bin/make/unit-tests/varmod.mk +++ b/usr.bin/make/unit-tests/varmod.mk @@ -1,4 +1,4 @@ -# $NetBSD: varmod.mk,v 1.11 2024/04/20 10:18:56 rillig Exp $ +# $NetBSD: varmod.mk,v 1.12 2024/06/01 18:44:05 rillig Exp $ # # Tests for variable modifiers, such as :Q, :S,from,to or :Ufallback. # @@ -119,4 +119,18 @@ VAR= STOP . error .endif -all: # nothing +# Test the word selection modifier ':[n]' with a very large number that is +# larger than ULONG_MAX for any supported platform. +# expect+1: Malformed conditional (${word:L:[99333000222000111000]}) +.if ${word:L:[99333000222000111000]} +.endif +# expect+1: Malformed conditional (${word:L:[2147483648]}) +.if ${word:L:[2147483648]} +.endif + +# Test the range generation modifier ':range=n' with a very large number that +# is larger than SIZE_MAX for any supported platform. +# expect+2: Malformed conditional (${word:L:range=99333000222000111000}) +# expect+1: while evaluating variable "word": Invalid number "99333000222000111000}" for ':range' modifier +.if ${word:L:range=99333000222000111000} +.endif diff --git a/usr.bin/make/unit-tests/varname-dot-make-level.exp b/usr.bin/make/unit-tests/varname-dot-make-level.exp index 39a9383953dd..963b6a3a4f96 100644 --- a/usr.bin/make/unit-tests/varname-dot-make-level.exp +++ b/usr.bin/make/unit-tests/varname-dot-make-level.exp @@ -1 +1,4 @@ +level 1: variable 0, env 1 +level 2: variable 1, env 2 +level 3: variable 2, env 3 exit status 0 diff --git a/usr.bin/make/unit-tests/varname-dot-make-level.mk b/usr.bin/make/unit-tests/varname-dot-make-level.mk index c4f2c0db7da6..246890b23171 100644 --- a/usr.bin/make/unit-tests/varname-dot-make-level.mk +++ b/usr.bin/make/unit-tests/varname-dot-make-level.mk @@ -1,8 +1,22 @@ -# $NetBSD: varname-dot-make-level.mk,v 1.2 2020/08/16 14:25:16 rillig Exp $ +# $NetBSD: varname-dot-make-level.mk,v 1.3 2024/06/01 18:44:05 rillig Exp $ # -# Tests for the special .MAKE.LEVEL variable. +# Tests for the special .MAKE.LEVEL variable, which informs about the +# recursion level. It is related to the environment variable MAKELEVEL, +# even though they don't have the same value. -# TODO: Implementation +level_1: .PHONY + @printf 'level 1: variable %s, env %s\n' ${.MAKE.LEVEL} "$$${.MAKE.LEVEL.ENV}" + @${MAKE} -f ${MAKEFILE} level_2 -all: - @:; +level_2: .PHONY + @printf 'level 2: variable %s, env %s\n' ${.MAKE.LEVEL} "$$${.MAKE.LEVEL.ENV}" + @${MAKE} -f ${MAKEFILE} level_3 + +level_3: .PHONY + @printf 'level 3: variable %s, env %s\n' ${.MAKE.LEVEL} "$$${.MAKE.LEVEL.ENV}" + +# The .unexport-env directive clears the environment, except for the +# MAKE_LEVEL variable. +.if make(level_2) +.unexport-env +.endif