Skip to content

Commit da7edca

Browse files
committed
Use native methods to open input in TestLib::slurp_file on Windows.
This is a backport of commits 114541d and 6f59826f0 to the remaining live branches.
1 parent 133a090 commit da7edca

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/test/perl/TestLib.pm

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ use SimpleTee;
4444

4545
use Test::More;
4646

47-
our $windows_os = $Config{osname} eq 'MSWin32' || $Config{osname} eq 'msys';
47+
our $windows_os;
48+
49+
BEGIN
50+
{
51+
$windows_os = $Config{osname} eq 'MSWin32' || $Config{osname} eq 'msys';
52+
if ($windows_os)
53+
{
54+
require Win32API::File;
55+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
56+
}
57+
}
4858

4959
# Open log file. For each test, the log file name uses the name of the
5060
# file launching this module, without the .pl suffix.
@@ -277,10 +287,24 @@ sub slurp_file
277287
{
278288
my ($filename) = @_;
279289
local $/;
280-
open(my $in, '<', $filename)
281-
or die "could not read \"$filename\": $!";
282-
my $contents = <$in>;
283-
close $in;
290+
my $contents;
291+
if ($Config{osname} ne 'MSWin32')
292+
{
293+
open(my $in, '<', $filename)
294+
or die "could not read \"$filename\": $!";
295+
$contents = <$in>;
296+
close $in;
297+
}
298+
else
299+
{
300+
my $fHandle = createFile($filename, "r", "rwd")
301+
or die "could not open \"$filename\": $^E";
302+
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
303+
or die "could not read \"$filename\": $^E\n";
304+
$contents = <$fh>;
305+
CloseHandle($fHandle)
306+
or die "could not close \"$filename\": $^E\n";
307+
}
284308
$contents =~ s/\r\n/\n/g if $Config{osname} eq 'msys';
285309
return $contents;
286310
}

0 commit comments

Comments
 (0)