1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
use strict;
use warnings;
use File::Basename ();
use File::Spec ();
use lib File::Spec->catdir(File::Spec->rel2abs(File::Basename::dirname(__FILE__)), 'lib');
use FileSlurpTest qw(temp_file_path trap_function);
use File::Slurp qw(write_file prepend_file read_file);
use Test::More ;
plan tests => 32;
my $existing_data = <<PRE ;
line 1
line 2
more
PRE
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, $prepend_data);
ok($res, 'prepend_file: empty string: got response!');
ok(!$warn, 'prepend_file: empty string: no warnings!');
ok(!$err, 'prepend_file: empty string: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: empty string: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "line 0\n";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, $prepend_data);
ok($res, 'prepend_file: add line: got response!');
ok(!$warn, 'prepend_file: add line: no warnings!');
ok(!$err, 'prepend_file: add line: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: add line: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "partial line";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, $prepend_data);
ok($res, 'prepend_file: partial line: got response!');
ok(!$warn, 'prepend_file: partial line: no warnings!');
ok(!$err, 'prepend_file: partial line: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: partial line: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "line 0\n";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, {}, $prepend_data);
ok($res, 'prepend_file: add line, empty opts: got response!');
ok(!$warn, 'prepend_file: add line, empty opts: no warnings!');
ok(!$err, 'prepend_file: add line, empty opts: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: add line, empty opts: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "line 0\n";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, {foo=>1,bar=>2}, $prepend_data);
ok($res, 'prepend_file: add line, invalid opts: got response!');
ok(!$warn, 'prepend_file: add line, invalid opts: no warnings!');
ok(!$err, 'prepend_file: add line, invalid opts: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: add line, invalid opts: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "line 0\n";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, {foo=>1,bar=>2,binmode=>':raw'}, $prepend_data);
ok($res, 'prepend_file: add line, invalid opts, binmode: got response!');
# this should get fixed
SKIP: {
skip "Binmode is bad news bears with sysread on Perl 5.30+", 1;
ok(!$warn, 'prepend_file: add line, invalid opts, binmode: no warnings!');
}
ok(!$err, 'prepend_file: add line, invalid opts, binmode: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: add line, invalid opts, binmode: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "line 0\n";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, {foo=>1,bar=>2,err_mode=>'quiet'}, $prepend_data);
ok($res, 'prepend_file: add line, invalid opts, err_mode quiet: got response!');
ok(!$warn, 'prepend_file: add line, invalid opts, err_mode quiet: no warnings!');
ok(!$err, 'prepend_file: add line, invalid opts, err_mode quiet: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: add line, invalid opts, err_mode quiet: contents match');
unlink $file;
}
{
my $file = temp_file_path();
write_file($file, $existing_data);
my $prepend_data = "line 0\n";
my ($res, $warn, $err) = trap_function(\&prepend_file, $file, \$prepend_data);
ok($res, 'prepend_file: add line, scalar ref: got response!');
ok(!$warn, 'prepend_file: add line, scalar ref: no warnings!');
ok(!$err, 'prepend_file: add line, scalar ref: no exceptions!');
my $data = read_file($file);
is($data, $prepend_data.$existing_data, 'prepend_file: add line, scalar ref: contents match');
unlink $file;
}
|