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