make/tests/scripts/functions/foreach
Paul Smith 414af96a50 Refresh the test suite framework implementation.
Go through both run_make_tests.pl and test_driver.pl and slightly
modernize the Perl and clean up indentation etc.  Fix a number of
warnings in the test scripts detected by running with -w.

* tests/test_driver.pl: Move make error string detection out of the
base test driver.
(run_all_tests): Ensure that we always look for tests in the cwd.
* tests/run_make_tests.pl: Use File::Spec for path manipulations.
Correctly use setlocale() when detecting error strings.
Get configuration from the config-flags.pm file not config.status.
* tests/scripts/features/archives: Use new $cwddir variable.
* tests/scripts/features/reinvoke: Add missing semicolon.
* tests/scripts/features/vpath2: Avoid non-existent variable.
* tests/scripts/functions/foreach: Escape variables.
* tests/scripts/misc/bs-nl: Remove non-existing \v escape sequence.
* tests/scripts/misc/general4: Use handy create_file().
* tests/scripts/options/dash-C: Use Cwd/$cwddir.
* tests/scripts/options/dash-I: Use subst_make_string() and #PWD#.
* tests/scripts/options/symlinks: Use File::Spec.
* tests/scripts/targets/DEFAULT: Use create_file and run_make_test.
* tests/scripts/variables/CURDIR: Use run_make_test.
* tests/scripts/variables/automatic: Remove extraneous "\".
* tests/scripts/vms/library: Remove extra "my" and extraneous "\".
2019-09-16 08:25:33 -04:00

95 lines
2.3 KiB
Perl

# -*-perl-*-
# $Id$
$description = "Test the foreach function.";
$details = "This is a test of the foreach function in gnu make.
This function starts with a space separated list of
names and a variable. Each name in the list is subsituted
into the variable and the given text evaluated. The general
form of the command is $(foreach var,\$list,\$text). Several
types of foreach loops are tested\n";
# TEST 0
# Set an environment variable that we can test in the makefile.
$extraENV{FOOFOO} = 'foo foo';
run_make_test("space = ' '".'
null :=
auto_var = udef space CC null FOOFOO MAKE foo CFLAGS WHITE @ <
foo = bletch null @ garf
av = $(foreach var, $(auto_var), $(origin $(var)) )
override WHITE := BLACK
for_var = $(addsuffix .c,foo $(null) $(foo) $(space) $(av) )
fe = $(foreach var2, $(for_var),$(subst .c,.o, $(var2) ) )
all: auto for2
auto : ; @echo $(av)
for2: ; @echo $(fe)',
'-e WHITE=WHITE CFLAGS=',
"undefined file default file environment default file command line override automatic automatic
foo.o bletch.o null.o @.o garf.o .o .o undefined.o file.o default.o file.o environment.o default.o file.o command.o line.o override.o automatic.o automatic.o");
delete $extraENV{FOOFOO};
# TEST 1: Test that foreach variables take precedence over global
# variables in a global scope (like inside an eval). Tests bug #11913
run_make_test('
.PHONY: all target
all: target
x := BAD
define mktarget
target: x := $(x)
target: ; @echo "$(x)"
endef
x := GLOBAL
$(foreach x,FOREACH,$(eval $(value mktarget)))',
'',
'FOREACH');
# Allow variable names with trailing space
run_make_test(q!
$(foreach \
a \
, b c d \
, $(info $a))
all:;@:
!,
"", "b\nc\nd\n");
# Allow empty variable names. We still expand the body.
run_make_test('
x = $(foreach ,1 2 3,a)
y := $x
all: ; @echo $y',
'', "a a a\n");
# Check some error conditions.
run_make_test('
x = $(foreach )
y = $x
all: ; @echo $y',
'',
"#MAKEFILE#:2: *** insufficient number of arguments (1) to function 'foreach'. Stop.",
512);
run_make_test('
x = $(foreach x,y)
y := $x
all: ; @echo $y',
'',
"#MAKEFILE#:2: *** insufficient number of arguments (2) to function 'foreach'. Stop.",
512);
1;