xref: /freebsd/contrib/bmake/unit-tests/directive-export.mk (revision 988758838533b24f5893b36514fe2c68a3f911a2)
1*98875883SSimon J. Gerraty# $NetBSD: directive-export.mk,v 1.9 2023/08/20 20:48:32 rillig Exp $
22c3632d1SSimon J. Gerraty#
32c3632d1SSimon J. Gerraty# Tests for the .export directive.
406b9b3e0SSimon J. Gerraty#
506b9b3e0SSimon J. Gerraty# See also:
606b9b3e0SSimon J. Gerraty#	directive-misspellings.mk
72c3632d1SSimon J. Gerraty
82c3632d1SSimon J. Gerraty# TODO: Implementation
92c3632d1SSimon J. Gerraty
10956e45f6SSimon J. GerratyINDIRECT=	indirect
11956e45f6SSimon J. GerratyVAR=		value $$ ${INDIRECT}
12956e45f6SSimon J. Gerraty
1306b9b3e0SSimon J. Gerraty# Before 2020-12-13, this unusual expression invoked undefined behavior since
1406b9b3e0SSimon J. Gerraty# it accessed out-of-bounds memory via Var_Export -> ExportVar -> MayExport.
1506b9b3e0SSimon J. Gerraty.export ${:U }
1606b9b3e0SSimon J. Gerraty
17956e45f6SSimon J. Gerraty# A variable is exported using the .export directive.
18956e45f6SSimon J. Gerraty# During that, its value is expanded, just like almost everywhere else.
19956e45f6SSimon J. Gerraty.export VAR
20956e45f6SSimon J. Gerraty.if ${:!env | grep '^VAR'!} != "VAR=value \$ indirect"
21956e45f6SSimon J. Gerraty.  error
22956e45f6SSimon J. Gerraty.endif
23956e45f6SSimon J. Gerraty
24956e45f6SSimon J. Gerraty# Undefining a variable that has been exported implicitly removes it from
25956e45f6SSimon J. Gerraty# the environment of all child processes.
26956e45f6SSimon J. Gerraty.undef VAR
27956e45f6SSimon J. Gerraty.if ${:!env | grep '^VAR' || true!} != ""
28956e45f6SSimon J. Gerraty.  error
29956e45f6SSimon J. Gerraty.endif
30956e45f6SSimon J. Gerraty
31b0c40a00SSimon J. Gerraty# No syntactical argument means to export all variables.
3206b9b3e0SSimon J. Gerraty.export
33e2eeea75SSimon J. Gerraty
34b0c40a00SSimon J. Gerraty# An empty argument means no additional variables to export.
35b0c40a00SSimon J. Gerraty.export ${:U}
36b0c40a00SSimon J. Gerraty
37b0c40a00SSimon J. Gerraty
38b0c40a00SSimon J. Gerraty# Trigger the "This isn't going to end well" in ExportVarEnv.
39b0c40a00SSimon J. GerratyEMPTY_SHELL=	${:sh}
40b0c40a00SSimon J. Gerraty.export EMPTY_SHELL	# only marked for export at this point
41b0c40a00SSimon J. Gerraty_!=		:;:	# Force the variable to be actually exported.
42b0c40a00SSimon J. Gerraty
43b0c40a00SSimon J. Gerraty
44*98875883SSimon J. Gerraty# If the '.export' directive exports a variable whose value contains a '$',
45*98875883SSimon J. Gerraty# the actual export action is deferred until a subprocess is started, assuming
46*98875883SSimon J. Gerraty# that only subprocesses access the environment variables.  The ':localtime'
47*98875883SSimon J. Gerraty# modifier depends on the 'TZ' environment variable, without any subprocess.
48*98875883SSimon J. Gerratyexport TZ=${UTC}
49*98875883SSimon J. Gerraty# expect+1: 00:00:00
50*98875883SSimon J. Gerraty.info ${%T:L:localtime=86400}
51*98875883SSimon J. GerratyINDIRECT_TZ=	${:UAmerica/Los_Angeles}
52*98875883SSimon J. GerratyTZ=		${INDIRECT_TZ}
53*98875883SSimon J. Gerraty.export TZ
54*98875883SSimon J. Gerraty# expect+1: 00:00:00
55*98875883SSimon J. Gerraty.info ${%T:L:localtime=86400}
56*98875883SSimon J. Gerraty_!=	echo 'force exporting the environment variables'
57*98875883SSimon J. Gerraty# expect+1: 16:00:00
58*98875883SSimon J. Gerraty.info ${%T:L:localtime=86400}
59*98875883SSimon J. Gerraty
60*98875883SSimon J. Gerraty
612c3632d1SSimon J. Gerratyall:
62