xref: /freebsd/contrib/bmake/unit-tests/directive-export-gmake.mk (revision a2f733abcff64628b7771a47089628b7327a88bd)
1# $NetBSD: directive-export-gmake.mk,v 1.7 2023/08/20 20:48:32 rillig Exp $
2#
3# Tests for the export directive (without leading dot), as in GNU make.
4
5# The "export" directive only affects the environment of the make process
6# and its child processes.  It does not affect the global variables or any
7# other variables.
8VAR=	before
9export VAR=exported
10.if ${VAR} != "before"
11.  error
12.endif
13
14# Ensure that the name-value pair is actually exported.
15.if ${:!echo "\$VAR"!} != "exported"
16.  error
17.endif
18
19# This line looks like it would export 2 variables, but it doesn't.
20# It only exports VAR and appends everything else as the variable value.
21export VAR=exported VAR2=exported-as-well
22.if ${:!echo "\$VAR"!} != "exported VAR2=exported-as-well"
23.  error ${:!echo "\$VAR"!}
24.endif
25
26# Contrary to the usual variable assignments, spaces are significant
27# after the '=' sign and are prepended to the value of the environment
28# variable.
29export VAR=  leading spaces
30.if ${:!echo "\$VAR"!} != "  leading spaces"
31.  error
32.endif
33
34# Contrary to the usual variable assignments, spaces are significant
35# before the '=' sign and are appended to the name of the environment
36# variable.
37#
38# Depending on the shell, environment variables with such exotic names
39# may be silently discarded.  One such shell is dash, which is the default
40# shell on Ubuntu and Debian.
41export VAR =trailing space in varname
42.if ${:!env | grep trailing || true!} != "VAR =trailing space in varname"
43.  if ${:!env | grep trailing || true!} != "" # for dash
44.    error
45.  endif
46.endif
47
48# The right-hand side of the exported variable is expanded exactly once.
49TWICE=	expanded twice
50ONCE=	expanded once, leaving $${TWICE} as-is
51export VAR=${ONCE}
52.if ${:!echo "\$VAR"!} != "expanded once, leaving \${TWICE} as-is"
53.  error
54.endif
55
56# Undefined variables are allowed on the right-hand side, they expand
57# to an empty string, as usual.
58export VAR=an ${UNDEF} variable
59.if ${:!echo "\$VAR"!} != "an  variable"
60.  error
61.endif
62
63
64# The body of the .for loop expands to 'export VAR=${:U1}', and the 'export'
65# directive is only recognized if the line does not contain a ':', to allow
66# 'export' to be a regular target.
67.for value in 1
68# XXX: The ':' in this line is inside an expression and should thus not be
69# interpreted as a dependency operator.
70# expect+1: Invalid line 'export VAR=${:U1}'
71export VAR=${value}
72.endfor
73
74
75# The 'export' directive expands expressions, but the expressions must not
76# contain a ':', due to the overly strict parser.  The indirect expressions
77# may contain a ':', though.
78#
79# As a side effect, this test demonstrates that the 'export' directive exports
80# the environment variable immediately, other than the '.export' directive,
81# which defers that action if the variable value contains a '$'.
82INDIRECT_TZ=	${:UAmerica/Los_Angeles}
83export TZ=${INDIRECT_TZ}
84# expect+1: 16:00:00
85.info ${%T:L:localtime=86400}
86