make/tests/scripts/features/export
Paul Smith 2dc0280d82 Support "unexport" in target-specific variables.
Rewrite the environment variable algorithm to correctly inherit
export settings from parent variable sets.  The new algorithm
for computing the table of environment variables is:

- Start with the most local variable set and proceed to global.
- If the variable already exists in the table and we don't know
  its export status, update it with the current variable's status.
- If the variable is not in the table and it's not global, add it
  regardless of its status so if it's unexported we remember that.
- If the variable is not in the table and is global, check its
  export status and don't add it if we won't export it.

Then when generating the environment variables, check the export
status of each variable in case it was a target-specific variable
and we have determined it should not be exported.

Rework SHELL handling to check at the end whether we added it or
not and if we didn't, add the value from the environment.

* NEWS: Announce support for target-specific "unexport"."
* doc/make.texi (Target-specific): Document the support.
* src/variable.h (enum variable_export): Make into a global type.
* src/read.c (struct vmodifiers): Use enum variable_export rather
than individual booleans.
(parse_var_assignment): Parse the "unexport" keyword.
(eval): Remember the vmodifier value in the variable.
(record_target_var): Ditto.
* src/variable.c (should_export): Check if the variable should be
exported.
(target_environment): Implement the above algorithm.
* tests/scripts/features/export: Test export/unexport with variable
assignments on the same line.
* tests/scripts/features/targetvars: Add a comprehensive suite of
tests for different types of target-specific export / unexport.
* tests/scripts/variables/SHELL: Update the comment.
2020-11-29 17:57:33 -05:00

210 lines
4 KiB
Perl

# -*-perl-*-
$description = "Check GNU make export/unexport commands.";
$details = "";
# The test driver cleans out our environment for us so we don't have to worry
# about that here.
&run_make_test('
FOO = foo
BAR = bar
BOZ = boz
export BAZ = baz
export BOZ
BITZ = bitz
BOTZ = botz
export BITZ BOTZ
unexport BOTZ
ifdef EXPORT_ALL
export
endif
ifdef UNEXPORT_ALL
unexport
endif
ifdef EXPORT_ALL_PSEUDO
.EXPORT_ALL_VARIABLES:
endif
.RECIPEPREFIX := >
all:
> @echo "FOO=$(FOO) BAR=$(BAR) BAZ=$(BAZ) BOZ=$(BOZ) BITZ=$(BITZ) BOTZ=$(BOTZ)"
> @echo "FOO=$$FOO BAR=$$BAR BAZ=$$BAZ BOZ=$$BOZ BITZ=$$BITZ BOTZ=$$BOTZ"
',
'', "FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=botz
FOO= BAR= BAZ=baz BOZ=boz BITZ=bitz BOTZ=\n");
# TEST 1: make sure vars inherited from the parent are exported
$ENV{FOO} = 1;
&run_make_test(undef, '', "FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=botz
FOO=foo BAR= BAZ=baz BOZ=boz BITZ=bitz BOTZ=\n");
# TEST 2: global export. Explicit unexport takes precedence.
run_make_test(undef, "EXPORT_ALL=1" ,
"FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=botz
FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=\n");
# TEST 3: global unexport. Explicit export takes precedence.
&run_make_test(undef, "UNEXPORT_ALL=1",
"FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=botz
FOO= BAR= BAZ=baz BOZ=boz BITZ=bitz BOTZ=\n");
# TEST 4: both: in the above makefile the unexport comes last so that rules.
&run_make_test(undef, "EXPORT_ALL=1 UNEXPORT_ALL=1",
"FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=botz
FOO= BAR= BAZ=baz BOZ=boz BITZ=bitz BOTZ=\n");
# TEST 5: test the pseudo target.
&run_make_test(undef, "EXPORT_ALL_PSEUDO=1",
"FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=botz
FOO=foo BAR=bar BAZ=baz BOZ=boz BITZ=bitz BOTZ=\n");
# TEST 6: Test the expansion of variables inside export
&run_make_test('
foo = f-ok
bar = b-ok
FOO = foo
F = f
BAR = bar
B = b
export $(FOO)
export $(B)ar
.RECIPEPREFIX := >
all:
> @echo foo=$(foo) bar=$(bar)
> @echo foo=$$foo bar=$$bar
',
"", "foo=f-ok bar=b-ok\nfoo=f-ok bar=b-ok\n");
# TEST 7: Test the expansion of variables inside unexport
&run_make_test('
foo = f-ok
bar = b-ok
FOO = foo
F = f
BAR = bar
B = b
export foo bar
unexport $(FOO)
unexport $(B)ar
.RECIPEPREFIX := >
all:
> @echo foo=$(foo) bar=$(bar)
> @echo foo=$$foo bar=$$bar
',
'', "foo=f-ok bar=b-ok\nfoo= bar=\n");
# TEST 7: Test exporting multiple variables on the same line
&run_make_test('
A = a
B = b
C = c
D = d
E = e
F = f
G = g
H = h
I = i
J = j
SOME = A B C
export F G H I J
export D E $(SOME)
all: ; @echo A=$$A B=$$B C=$$C D=$$D E=$$E F=$$F G=$$G H=$$H I=$$I J=$$J
',
'', "A=a B=b C=c D=d E=e F=f G=g H=h I=i J=j\n");
# TEST 8: Test unexporting multiple variables on the same line
@args{qw(A B C D E F G H I J)} = qw(1 2 3 4 5 6 7 8 9 10);
%ENV = (%ENV, %args);
&run_make_test('
A = a
B = b
C = c
D = d
E = e
F = f
G = g
H = h
I = i
J = j
SOME = A B C
unexport F G H I J
unexport D E $(SOME)
all: ; @echo A=$$A B=$$B C=$$C D=$$D E=$$E F=$$F G=$$G H=$$H I=$$I J=$$J
',
'', "A= B= C= D= E= F= G= H= I= J=\n");
# TEST 9: Check setting a variable named "export"
&run_make_test('
export = 123
export export
export export = 456
a: ; @echo "\$$(export)=$(export) / \$$export=$$export"
',
'', "\$(export)=456 / \$export=456\n");
# TEST 10: Check "export" as a target
&run_make_test('
a: export
export: ; @echo "$@"
',
'', "export\n");
# Check export and assignment of a variable on the same line
$ENV{hello} = 'moon';
run_make_test(q!
all: ; @echo hello=$(hello) hello=$$hello
export hello=sun
!,
'', "hello=sun hello=sun\n");
# Check unexport and assignment of a variable on the same line
$ENV{hello} = 'moon';
run_make_test(q!
all: ; @echo hello=$(hello) hello=$$hello
unexport hello=sun
!,
'', "hello=sun hello=\n");
# This tells the test driver that the perl test script executed properly.
1;