Home | History | Annotate | Line # | Download | only in unit-tests
directive-export.mk revision 1.11
      1 # $NetBSD: directive-export.mk,v 1.11 2024/06/01 06:26:36 sjg Exp $
      2 #
      3 # Tests for the .export directive.
      4 #
      5 # See also:
      6 #	directive-misspellings.mk
      7 
      8 # TODO: Implementation
      9 
     10 INDIRECT=	indirect
     11 VAR=		value $$ ${INDIRECT}
     12 
     13 # Before 2020-12-13, this unusual expression invoked undefined behavior since
     14 # it accessed out-of-bounds memory via Var_Export -> ExportVar -> MayExport.
     15 .export ${:U }
     16 
     17 # A variable is exported using the .export directive.
     18 # During that, its value is expanded, just like almost everywhere else.
     19 .export VAR
     20 .if ${:!env | grep '^VAR'!} != "VAR=value \$ indirect"
     21 .  error
     22 .endif
     23 
     24 # Undefining a variable that has been exported implicitly removes it from
     25 # the environment of all child processes.
     26 .undef VAR
     27 .if ${:!env | grep '^VAR' || true!} != ""
     28 .  error
     29 .endif
     30 
     31 # No syntactical argument used to mean export all variables.
     32 # Since var.c 1.1117 2024/06/01 it causes a warning.
     33 .export
     34 
     35 # An empty argument means no additional variables to export.
     36 .export ${:U}
     37 
     38 
     39 # Before a child process is started, whether for the '!=' assignment operator
     40 # or for the ':sh' modifier, all variables that were marked for being exported
     41 # are expanded and then exported.  If expanding such a variable requires
     42 # running a child command, the marked-as-exported variables would need to be
     43 # exported first, ending in an endless loop.  To avoid this endless loop,
     44 # don't export the variables while preparing a child process, see
     45 # ExportVarEnv.
     46 EMPTY_SHELL=	${:sh}
     47 .export EMPTY_SHELL	# only marked for export at this point
     48 _!=		:;:	# Force the variable to be actually exported.
     49 
     50 
     51 # If the '.export' directive exports a variable whose value contains a '$',
     52 # the actual export action is deferred until a subprocess is started, assuming
     53 # that only subprocesses access the environment variables.  The ':localtime'
     54 # modifier depends on the 'TZ' environment variable, without any subprocess.
     55 export TZ=${UTC}
     56 # expect+1: 00:00:00
     57 .info ${%T:L:localtime=86400}
     58 INDIRECT_TZ=	${:UAmerica/Los_Angeles}
     59 TZ=		${INDIRECT_TZ}
     60 .export TZ
     61 # expect+1: 00:00:00
     62 .info ${%T:L:localtime=86400}
     63 _!=	echo 'force exporting the environment variables'
     64 # expect+1: 16:00:00
     65 .info ${%T:L:localtime=86400}
     66 
     67 
     68 all:
     69