summaryrefslogtreecommitdiff
path: root/cpan/autodie/t/truncate.t
blob: e69ee32d2eec78d4dbaba18c7b58fe9b000c3bf6 (plain)
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
#!/usr/bin/perl -w
use strict;

use Test::More;
use File::Temp qw(tempfile);
use IO::Handle;

my $tmpfh = tempfile();
my $truncate_status;

eval {
    $truncate_status = truncate($tmpfh, 0);
};

if ($@ || !defined($truncate_status)) {
    plan skip_all => 'Truncate not implemented or not working on this system';
}

plan tests => 3;

SKIP: {
    my $can_truncate_stdout = truncate(\*STDOUT,0);

    if ($can_truncate_stdout) {
        skip("This system thinks we can truncate STDOUT. Suuure!", 1);
    }

    eval {
        use autodie;
        truncate(\*STDOUT,0);
    };

    isa_ok($@, 'autodie::exception', "Truncating STDOUT should throw an exception");

}

eval {
    use autodie;
    no warnings 'once';
    truncate(\*FOO, 0);
};

isa_ok($@, 'autodie::exception', "Truncating an unopened file is wrong.");

$tmpfh->print("Hello World");
$tmpfh->flush;

eval {
    use autodie;
    truncate($tmpfh, 0);
};

is($@, "", "Truncating a normal file should be fine");