Recognise a # PREAMBLE directive in a t/lib file

This allows a test file to begin with a common pragma line or other
setup code and avoid having to repeat that setup across every individual
test block in the file.
This commit is contained in:
Paul "LeoNerd" Evans 2025-10-07 14:08:38 +01:00 committed by Paul Evans
parent 5a425b64b8
commit 153ee16033

View File

@ -1327,11 +1327,15 @@ sub setup_multiple_progs {
open my $fh, '<', $file or die "Cannot open $file: $!\n" ;
my $found;
my $preamble = "";
while (<$fh>) {
if (/^__END__/) {
$found = $found + 1; # don't use ++
last;
}
if (/^#\s+PREAMBLE\s+(.*)$/) {
$preamble .= "$1\n";
}
}
# This is an internal error, and should never happen. All bar one of
# the files had an __END__ marker to signal the end of their preamble,
@ -1346,6 +1350,12 @@ sub setup_multiple_progs {
unless $found;
my ($t, @p) = _setup_one_file($fh, $file);
if (length $preamble) {
# @p consists of ($linenumber, $source) pairs, so we only want
# to prepend the preamble to the odd numbered elements.
# Additionally, the first two elements are (0, $filename).
$_ = $preamble . $_ for @p[ grep { $_ % 2 } 2 .. $#p ];
}
$tests += $t;
push @prgs, @p;