File: edit_file.t

package info (click to toggle)
libfile-slurp-perl 9999.32-2.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 412 kB
  • sloc: perl: 650; makefile: 39
file content (79 lines) | stat: -rw-r--r-- 3,456 bytes parent folder | download | duplicates (4)
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
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(read_file write_file edit_file);
use Test::More;

plan tests => 20;

my $file = 'edit_file';
my $existing_data = <<PRE;
line 1
line 2
more
PRE

{
    my $file = temp_file_path();
    write_file($file, $existing_data);
    my ($res, $warn, $err) = trap_function(\&edit_file, sub {s/([0-9])/${1}000/g}, $file);
    ok($res, 'edit_file: edit line: got response!');
    ok(!$warn, 'edit_file: edit line: no warnings!');
    ok(!$err, 'edit_file: edit line: no exceptions!');
    my $expected = join("\n", ('line 1000', 'line 2000', 'more', ''));
    is(read_file($file), $expected, 'edit_file: edit line: contents are right');
    unlink $file;
}
{
    my $file = temp_file_path();
    write_file($file, $existing_data);
    my ($res, $warn, $err) = trap_function(\&edit_file, sub {s/([0-9])/${1}000/g}, $file, {});
    ok($res, 'edit_file: edit line, empty options hashref: got response!');
    ok(!$warn, 'edit_file: edit line, empty options hashref: no warnings!');
    ok(!$err, 'edit_file: edit line, empty options hashref: no exceptions!');
    my $expected = join("\n", ('line 1000', 'line 2000', 'more', ''));
    is(read_file($file), $expected, 'edit_file: edit line, empty options hashref: contents are right');
    unlink $file;
}
{
    my $file = temp_file_path();
    write_file($file, $existing_data);
    my ($res, $warn, $err) = trap_function(\&edit_file, sub {s/([0-9])/${1}000/g}, $file, {foo=>1,bar=>2});
    ok($res, 'edit_file: edit line, invalid options: got response!');
    ok(!$warn, 'edit_file: edit line, invalid options: no warnings!');
    ok(!$err, 'edit_file: edit line, invalid options: no exceptions!');
    my $expected = join("\n", ('line 1000', 'line 2000', 'more', ''));
    is(read_file($file), $expected, 'edit_file: edit line, invalid options: contents are right');
    unlink $file;
}
{
    my $file = temp_file_path();
    write_file($file, $existing_data);
    my ($res, $warn, $err) = trap_function(\&edit_file, sub {s/([0-9])/${1}000/g}, $file, {foo=>1,bar=>2, binmode=>':raw'});
    ok($res, 'edit_file: edit line, invalid options, binmode: got response!');
    # this should get fixed
    SKIP: {
        skip "Binmode is bad news bears with sysread on Perl 5.30+", 1;
        ok(!$warn, 'edit_file: edit line, invalid options, binmode: no warnings!');
    }
    ok(!$err, 'edit_file: edit line, invalid options, binmode: no exceptions!');
    my $expected = join("\n", ('line 1000', 'line 2000', 'more', ''));
    is(read_file($file), $expected, 'edit_file: edit line, invalid options, binmode: contents are right');
    unlink $file;
}
{
    my $file = temp_file_path();
    write_file($file, $existing_data);
    my ($res, $warn, $err) = trap_function(\&edit_file, sub {s/([0-9])/${1}000/g}, $file, {foo=>1,bar=>2, err_mode=>'quiet'});
    ok($res, 'edit_file: edit line, invalid options, err_mode: got response!');
    ok(!$warn, 'edit_file: edit line, invalid options, err_mode: no warnings!');
    ok(!$err, 'edit_file: edit line, invalid options, err_mode: no exceptions!');
    my $expected = join("\n", ('line 1000', 'line 2000', 'more', ''));
    is(read_file($file), $expected, 'edit_file: edit line, invalid options, err_mode: contents are right');
    unlink $file;
}