Skip to content

Commit 153ee16

Browse files
committed
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.
1 parent 5a425b6 commit 153ee16

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

t/test.pl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,11 +1327,15 @@ sub setup_multiple_progs {
13271327

13281328
open my $fh, '<', $file or die "Cannot open $file: $!\n" ;
13291329
my $found;
1330+
my $preamble = "";
13301331
while (<$fh>) {
13311332
if (/^__END__/) {
13321333
$found = $found + 1; # don't use ++
13331334
last;
13341335
}
1336+
if (/^#\s+PREAMBLE\s+(.*)$/) {
1337+
$preamble .= "$1\n";
1338+
}
13351339
}
13361340
# This is an internal error, and should never happen. All bar one of
13371341
# the files had an __END__ marker to signal the end of their preamble,
@@ -1346,6 +1350,12 @@ sub setup_multiple_progs {
13461350
unless $found;
13471351

13481352
my ($t, @p) = _setup_one_file($fh, $file);
1353+
if (length $preamble) {
1354+
# @p consists of ($linenumber, $source) pairs, so we only want
1355+
# to prepend the preamble to the odd numbered elements.
1356+
# Additionally, the first two elements are (0, $filename).
1357+
$_ = $preamble . $_ for @p[ grep { $_ % 2 } 2 .. $#p ];
1358+
}
13491359
$tests += $t;
13501360
push @prgs, @p;
13511361

0 commit comments

Comments
 (0)