Skip to content

Commit 3bc0c2a

Browse files
committed
add Test::Utils::write_file
1 parent e1c4fc9 commit 3bc0c2a

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

t/basic.t

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,9 @@ is( $?, 0, ' exited normally' ) ||
178178
diag $test_out;
179179

180180
# now simulate what Module::Install does, and edit $(PERL) to add flags
181-
open my $fh, '<', $makefile;
182-
my $mtext = join '', <$fh>;
183-
close $fh;
181+
my $mtext = slurp($makefile);
184182
$mtext =~ s/^(\s*PERL\s*=.*)$/$1 -Iinc/m;
185-
open $fh, '>', $makefile;
186-
print $fh $mtext;
187-
close $fh;
183+
write_file($makefile, $mtext);
188184

189185
sub check_dummy_inst {
190186
my ($loc, $install_args, $label, $skipsubdir) = @_;
@@ -424,10 +420,10 @@ note "META file validity"; SKIP: {
424420

425421
# Make sure init_dirscan doesn't go into the distdir
426422
# also with a "messup.PL" that will make a build fail
427-
open $fh, '>', 'messup.PL' or die "messup.PL: $!";
428-
print $fh 'print "Extracting messup (with variable substitutions)\n";' . "\n";
429-
print $fh 'die';
430-
close $fh;
423+
write_file('messup.PL', <<'TEXT');
424+
print "Extracting messup (with variable substitutions)\n";
425+
die;
426+
TEXT
431427
@mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
432428

433429
cmp_ok( $?, '==', 0, 'Makefile.PL exited with zero' ) || diag(@mpl_out);
@@ -457,13 +453,10 @@ open(STDERR, ">&SAVERR") or die $!;
457453
close SAVERR;
458454

459455
# test linkext=>{LINKTYPE=>''} still installs a pure-perl installation
460-
# warning, edits the Makefile.PL so either rewrite after this or do this last
461456
my $file = 'Makefile.PL';
462-
my $text = slurp $file;
457+
my $text = my $preserve_MPL = slurp $file;
463458
ok(($text =~ s#\);# linkext=>{LINKTYPE=>''},\n$&#), 'successful M.PL edit');
464-
open $fh, '>', $file or die "$file: $!";
465-
print $fh $text;
466-
close $fh;
459+
write_file($file, $text);
467460
# now do with "Liar" subdir still there
468461
rmtree $DUMMYINST; # so no false positive from before
469462
@mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
@@ -474,6 +467,10 @@ rmtree 'Liar';
474467
rmtree $DUMMYINST; # so no false positive from before
475468
@mpl_out = run(qq{$perl Makefile.PL "PREFIX=$DUMMYINST"});
476469
check_dummy_inst($DUMMYINST, '', "with PREFIX=$DUMMYINST minus subdir", 1);
470+
write_file($file, $preserve_MPL); # restore Makefile.PL
471+
$realclean_out = run("$make realclean");
472+
rmtree 'Liar';
473+
rmtree $DUMMYINST;
477474

478475
sub _normalize {
479476
my $hash = shift;

t/fixin.t

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ BEGIN {
1212

1313
use File::Spec;
1414

15-
use Test::More tests => 30;
15+
use Test::More tests => 25;
1616

1717
use Config;
1818
use TieOut;
@@ -52,13 +52,11 @@ sub test_fixin {
5252
my($code, $test) = @_;
5353

5454
my $file = "fixin_test";
55-
ok(open(my $fh, ">", $file), "write $file") or diag "Can't write $file: $!";
56-
print $fh $code;
57-
close $fh;
55+
write_file($file, $code);
5856

5957
MY->fixin($file);
6058

61-
ok(open($fh, "<", $file), "read $file") or diag "Can't read $file: $!";
59+
ok(open(my $fh, "<", $file), "read $file") or diag "Can't read $file: $!";
6260
my @lines = <$fh>;
6361
close $fh;
6462

@@ -110,7 +108,7 @@ END
110108

111109
# fixin shouldn't pick this up.
112110
SKIP: {
113-
skip "Not relevant on VMS", 4 if $^O eq 'VMS';
111+
skip "Not relevant on VMS", 3 if $^O eq 'VMS';
114112
test_fixin(<<END,
115113
#!/foo/bar/perly -w
116114
@@ -129,8 +127,8 @@ END
129127

130128
SKIP: {
131129
eval { chmod(0755, "usrbin/interp") }
132-
or skip "no chmod", 8;
133-
skip "Not relevant on VMS or MSWin32", 8 if $^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'cygwin';
130+
or skip "no chmod", 6;
131+
skip "Not relevant on VMS or MSWin32", 6 if $^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'cygwin';
134132

135133
my $dir = getcwd();
136134
local $ENV{PATH} = join $Config{path_sep}, map "$dir/$_", qw(usrbin bin);

t/lib/MakeMaker/Test/Utils.pm

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ our $Is_FreeBSD = $^O eq 'freebsd';
1818

1919
our @EXPORT = qw(which_perl perl_lib makefile_name makefile_backup
2020
make make_run run make_macro calibrate_mtime
21-
have_compiler slurp
21+
have_compiler slurp write_file
2222
$Is_VMS $Is_MacOS
2323
run_ok
2424
hash2files
@@ -395,6 +395,22 @@ sub slurp {
395395
return $text;
396396
}
397397

398+
=item write_file
399+
400+
write_file('filename', @contents);
401+
402+
Writes the content to the given file. Will die if errors occur.
403+
404+
=cut
405+
406+
sub write_file {
407+
my ($file, @contents) = @_;
408+
my $utf8 = ("$]" < 5.008 or !$Config{useperlio}) ? "" : ":utf8";
409+
open my $fh, ">$utf8", $file or die "Can't create $file: $!";
410+
print $fh @contents or die "Can't write to $file: $!";
411+
close $fh or die "Can't close $file: $!";
412+
}
413+
398414
=item hash2files
399415
400416
hash2files('dirname', { 'filename' => 'some content' });
@@ -414,10 +430,7 @@ sub hash2files {
414430
$file = File::Spec->catfile(File::Spec->curdir, $prefix, split m{\/}, $file);
415431
my $dir = dirname($file);
416432
mkpath $dir;
417-
my $utf8 = ("$]" < 5.008 or !$Config{useperlio}) ? "" : ":utf8";
418-
open(FILE, ">$utf8", $file) || die "Can't create $file: $!";
419-
print FILE $text;
420-
close FILE;
433+
write_file($file, $text);
421434
# ensure file at least 1 second old for makes that assume
422435
# files with the same time are out of date.
423436
my $time = calibrate_mtime();

0 commit comments

Comments
 (0)