Home | History | Annotate | Line # | Download | only in unit-tests
modmisc.mk revision 1.42
      1 # $Id: modmisc.mk,v 1.42 2020/08/16 12:30:45 rillig Exp $
      2 #
      3 # miscellaneous modifier tests
      4 
      5 # do not put any dirs in this list which exist on some
      6 # but not all target systems - an exists() check is below.
      7 path=:/bin:/tmp::/:.:/no/such/dir:.
      8 # strip cwd from path.
      9 MOD_NODOT=S/:/ /g:N.:ts:
     10 # and decorate, note that $'s need to be doubled. Also note that 
     11 # the modifier_variable can be used with other modifiers.
     12 MOD_NODOTX=S/:/ /g:N.:@d@'$$d'@
     13 # another mod - pretend it is more interesting
     14 MOD_HOMES=S,/home/,/homes/,
     15 MOD_OPT=@d@$${exists($$d):?$$d:$${d:S,/usr,/opt,}}@
     16 MOD_SEP=S,:, ,g
     17 
     18 all:	modvar modvarloop modsysv mod-HTE emptyvar undefvar
     19 all:	mod-assign
     20 all:	mod-assign-nested
     21 all:	mod-tu-space
     22 all:	mod-quote
     23 all:	mod-break-many-words
     24 all:	mod-remember
     25 all:	mod-gmtime
     26 all:	mod-gmtime-indirect
     27 all:	mod-localtime
     28 all:	mod-hash
     29 all:	mod-range
     30 
     31 # See also sysv.mk.
     32 modsysv:
     33 	@echo "The answer is ${libfoo.a:L:libfoo.a=42}"
     34 
     35 # Demonstrates modifiers that are given indirectly from a variable.
     36 modvar:
     37 	@echo "path='${path}'"
     38 	@echo "path='${path:${MOD_NODOT}}'"
     39 	@echo "path='${path:S,home,homes,:${MOD_NODOT}}'"
     40 	@echo "path=${path:${MOD_NODOTX}:ts:}"
     41 	@echo "path=${path:${MOD_HOMES}:${MOD_NODOTX}:ts:}"
     42 
     43 .for d in ${path:${MOD_SEP}:N.} /usr/xbin
     44 path_$d?= ${d:${MOD_OPT}:${MOD_HOMES}}/
     45 paths+= ${d:${MOD_OPT}:${MOD_HOMES}}
     46 .endfor
     47 
     48 modvarloop:
     49 	@echo "path_/usr/xbin=${path_/usr/xbin}"
     50 	@echo "paths=${paths}"
     51 	@echo "PATHS=${paths:tu}"
     52 
     53 PATHNAMES=	a/b/c def a.b.c a.b/c a a.a .gitignore a a.a
     54 mod-HTE:
     55 	@echo "dirname of '"${PATHNAMES:Q}"' is '"${PATHNAMES:H:Q}"'"
     56 	@echo "basename of '"${PATHNAMES:Q}"' is '"${PATHNAMES:T:Q}"'"
     57 	@echo "suffix of '"${PATHNAMES:Q}"' is '"${PATHNAMES:E:Q}"'"
     58 	@echo "root of '"${PATHNAMES:Q}"' is '"${PATHNAMES:R:Q}"'"
     59 
     60 # When a modifier is applied to the "" variable, the result is discarded.
     61 emptyvar:
     62 	@echo S:${:S,^$,empty,}
     63 	@echo C:${:C,^$,empty,}
     64 	@echo @:${:@var@${var}@}
     65 
     66 # The :U modifier turns even the "" variable into something that has a value.
     67 # The resulting variable is empty, but is still considered to contain a
     68 # single empty word. This word can be accessed by the :S and :C modifiers,
     69 # but not by the :@ modifier since it explicitly skips empty words.
     70 undefvar:
     71 	@echo S:${:U:S,^$,empty,}
     72 	@echo C:${:U:C,^$,empty,}
     73 	@echo @:${:U:@var@empty@}
     74 
     75 # Just a bit of basic code coverage for the obscure ::= assignment modifiers.
     76 mod-assign:
     77 	@echo $@: ${1 2 3:L:@i@${FIRST::?=$i}@} first=${FIRST}.
     78 	@echo $@: ${1 2 3:L:@i@${LAST::=$i}@} last=${LAST}.
     79 	@echo $@: ${1 2 3:L:@i@${APPENDED::+=$i}@} appended=${APPENDED}.
     80 	@echo $@: ${echo.1 echo.2 echo.3:L:@i@${RAN::!=${i:C,.*,&; & 1>\&2,:S,., ,g}}@} ran:${RAN}.
     81 	# The assignments happen in the global scope and thus are
     82 	# preserved even after the shell command has been run.
     83 	@echo $@: global: ${FIRST:Q}, ${LAST:Q}, ${APPENDED:Q}, ${RAN:Q}.
     84 
     85 mod-assign-nested:
     86 	@echo $@: ${1:?${THEN1::=then1${IT1::=t1}}:${ELSE1::=else1${IE1::=e1}}}${THEN1}${ELSE1}${IT1}${IE1}
     87 	@echo $@: ${0:?${THEN2::=then2${IT2::=t2}}:${ELSE2::=else2${IE2::=e2}}}${THEN2}${ELSE2}${IT2}${IE2}
     88 	@echo $@: ${SINK3:Q}
     89 	@echo $@: ${SINK4:Q}
     90 SINK3:=	${1:?${THEN3::=then3${IT3::=t3}}:${ELSE3::=else3${IE3::=e3}}}${THEN3}${ELSE3}${IT3}${IE3}
     91 SINK4:=	${0:?${THEN4::=then4${IT4::=t4}}:${ELSE4::=else4${IE4::=e4}}}${THEN4}${ELSE4}${IT4}${IE4}
     92 
     93 mod-tu-space:
     94 	# The :tu and :tl modifiers operate on the variable value
     95 	# as a single string, not as a list of words. Therefore,
     96 	# the adjacent spaces are preserved.
     97 	@echo $@: ${a   b:L:tu:Q}
     98 
     99 mod-quote:
    100 	@echo $@: new${.newline:Q}${.newline:Q}line
    101 
    102 # Cover the bmake_realloc in brk_string.
    103 mod-break-many-words:
    104 	@echo $@: ${UNDEF:U:range=500:[#]}
    105 
    106 # Demonstrate the :_ modifier.
    107 # In the parameterized form, having the variable name on the right side
    108 # of the = assignment operator is confusing. Luckily this modifier is
    109 # only rarely needed.
    110 mod-remember:
    111 	@echo $@: ${1 2 3:L:_:@var@${_}@}
    112 	@echo $@: ${1 2 3:L:@var@${var:_=SAVED:}@}, SAVED=${SAVED}
    113 
    114 mod-gmtime:
    115 	@echo $@:
    116 	@echo ${%Y:L:gmtim=1593536400}		# modifier name too short
    117 	@echo ${%Y:L:gmtime=1593536400}		# 2020-07-01T00:00:00Z
    118 	@echo ${%Y:L:gmtimer=1593536400}	# modifier name too long
    119 	@echo ${%Y:L:gm=gm:M*}
    120 
    121 mod-gmtime-indirect:
    122 	@echo $@:
    123 	# It's not possible to pass the seconds via a variable expression.
    124 	# Parsing of the :gmtime modifier stops at the '$' and returns to
    125 	# ApplyModifiers.  There, a colon would be skipped but not a dollar.
    126 	# Parsing continues by looking at the next modifier.  Now the ${:U}
    127 	# is expanded and interpreted as a variable modifier, which results
    128 	# in the error message "Unknown modifier '1'".
    129 	@echo ${%Y:L:gmtime=${:U1593536400}}
    130 
    131 mod-localtime:
    132 	@echo $@:
    133 	@echo ${%Y:L:localtim=1593536400}	# modifier name too short
    134 	@echo ${%Y:L:localtime=1593536400}	# 2020-07-01T00:00:00Z
    135 	@echo ${%Y:L:localtimer=1593536400}	# modifier name too long
    136 
    137 mod-hash:
    138 	@echo $@:
    139 	@echo ${12345:L:has}			# modifier name too short
    140 	@echo ${12345:L:hash}			# ok
    141 	@echo ${12345:L:hash=SHA-256}		# :hash does not accept '='
    142 	@echo ${12345:L:hasX}			# misspelled
    143 	@echo ${12345:L:hashed}			# modifier name too long
    144 
    145 mod-range:
    146 	@echo $@:
    147 	@echo ${a b c:L:rang}			# modifier name too short
    148 	@echo ${a b c:L:range}			# ok
    149 	@echo ${a b c:L:rango}			# misspelled
    150 	@echo ${a b c:L:ranger}			# modifier name too long
    151 
    152 # To apply a modifier indirectly via another variable, the whole
    153 # modifier must be put into a single variable.
    154 .if ${value:L:${:US}${:U,value,replacement,}} != "S,value,replacement,}"
    155 .warning unexpected
    156 .endif
    157 
    158 # Adding another level of indirection (the 2 nested :U expressions) helps.
    159 .if ${value:L:${:U${:US}${:U,value,replacement,}}} != "replacement"
    160 .warning unexpected
    161 .endif
    162 
    163 # Multiple indirect modifiers can be applied one after another as long as
    164 # they are separated with colons.
    165 .if ${value:L:${:US,a,A,}:${:US,e,E,}} != "vAluE"
    166 .warning unexpected
    167 .endif
    168 
    169 # An indirect variable that evaluates to the empty string is allowed though.
    170 # This makes it possible to define conditional modifiers, like this:
    171 #
    172 # M.little-endian=	S,1234,4321,
    173 # M.big-endian=		# none
    174 .if ${value:L:${:Dempty}S,a,A,} != "vAlue"
    175 .warning unexpected
    176 .endif
    177 
    178 # begin mod-shell
    179 
    180 .if ${:!echo hello | tr 'l' 'l'!} != "hello"
    181 .warning unexpected
    182 .endif
    183 
    184 # The output is truncated at the first null byte.
    185 # Cmd_Exec returns only a string pointer without length information.
    186 .if ${:!echo hello | tr 'l' '\0'!} != "he"
    187 .warning unexpected
    188 .endif
    189 
    190 .if ${:!echo!} != ""
    191 .warning A newline at the end of the output must be stripped.
    192 .endif
    193 
    194 .if ${:!echo;echo!} != " "
    195 .warning Only a single newline at the end of the output is stripped.
    196 .endif
    197 
    198 .if ${:!echo;echo;echo;echo!} != "   "
    199 .warning Other newlines in the output are converted to spaces.
    200 .endif
    201 
    202 # end mod-shell
    203