Blame t/truncate.t

Packit 6427f8
#!/usr/bin/perl -w
Packit 6427f8
use strict;
Packit 6427f8
Packit 6427f8
use Test::More;
Packit 6427f8
use File::Temp qw(tempfile);
Packit 6427f8
use IO::Handle;
Packit 6427f8
use File::Spec;
Packit 6427f8
use FindBin qw($Bin);
Packit 6427f8
Packit 6427f8
my ($truncate_status, $tmpfh, $tmpfile);
Packit 6427f8
Packit 6427f8
# Some systems have a screwy tempfile. We don't run our tests there.
Packit 6427f8
eval {
Packit 6427f8
    ($tmpfh, $tmpfile) = tempfile(UNLINK => 1);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
if ($@ or !defined $tmpfh) {
Packit 6427f8
    plan skip_all => 'tempfile() not happy on this system.';
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    $truncate_status = truncate($tmpfh, 0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
if ($@ || !defined($truncate_status)) {
Packit 6427f8
    plan skip_all => 'Truncate not implemented or not working on this system';
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
plan tests => 12;
Packit 6427f8
Packit 6427f8
SKIP: {
Packit 6427f8
    my $can_truncate_stdout = truncate(\*STDOUT,0);
Packit 6427f8
Packit 6427f8
    if ($can_truncate_stdout) {
Packit 6427f8
        skip("This system thinks we can truncate STDOUT. Suuure!", 1);
Packit 6427f8
    }
Packit 6427f8
Packit 6427f8
    eval {
Packit 6427f8
        use autodie;
Packit 6427f8
        truncate(\*STDOUT,0);
Packit 6427f8
    };
Packit 6427f8
Packit 6427f8
    isa_ok($@, 'autodie::exception', "Truncating STDOUT should throw an exception");
Packit 6427f8
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie;
Packit 6427f8
    no warnings 'once';
Packit 6427f8
    truncate(\*FOO, 0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
isa_ok($@, 'autodie::exception', "Truncating an unopened file is wrong.");
Packit 6427f8
Packit 6427f8
$tmpfh->print("Hello World");
Packit 6427f8
$tmpfh->flush;
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie;
Packit 6427f8
    truncate($tmpfh, 0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
is($@, "", "Truncating a normal file should be fine");
Packit 6427f8
Packit 6427f8
$tmpfh->close;
Packit 6427f8
Packit 6427f8
# Time to test truncating via globs.
Packit 6427f8
Packit 6427f8
# Firstly, truncating a closed filehandle should fail.
Packit 6427f8
# I know we tested this above, but we'll do a full dance of
Packit 6427f8
# opening and closing TRUNCATE_FH here.
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(\*TRUNCATE_FH, 0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
isa_ok($@, 'autodie::exception', "Truncating unopened file (TRUNCATE_FH)");
Packit 6427f8
Packit 6427f8
# Now open the file. If this throws an exception, there's something
Packit 6427f8
# wrong with our tests, or autodie...
Packit 6427f8
{
Packit 6427f8
    use autodie qw(open);
Packit 6427f8
    open(TRUNCATE_FH, '+<', $tmpfile);
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
# Now try truncating the filehandle. This should succeed.
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(\*TRUNCATE_FH,0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
is($@, "", 'Truncating an opened glob (\*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(*TRUNCATE_FH,0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
is($@, "", 'Truncating an opened glob (*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
# Now let's change packages, since globs are package dependent
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    package Fatal::Test;
Packit 6427f8
    no warnings 'once';
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(\*TRUNCATE_FH,0);  # Should die, as now unopened
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
isa_ok($@, 'autodie::exception', 'Truncating unopened file in different package (\*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    package Fatal::Test;
Packit 6427f8
    no warnings 'once';
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(*TRUNCATE_FH,0);  # Should die, as now unopened
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
isa_ok($@, 'autodie::exception', 'Truncating unopened file in different package (*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
# Now back to our previous test, just to make sure it hasn't changed
Packit 6427f8
# the original file.
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(\*TRUNCATE_FH,0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
is($@, "", 'Truncating an opened glob #2 (\*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(*TRUNCATE_FH,0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
is($@, "", 'Truncating an opened glob #2 (*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
# Now to close the file and retry.
Packit 6427f8
{
Packit 6427f8
    use autodie qw(close);
Packit 6427f8
    close(TRUNCATE_FH);
Packit 6427f8
}
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(\*TRUNCATE_FH,0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
isa_ok($@, 'autodie::exception', 'Truncating freshly closed glob (\*TRUNCATE_FH)');
Packit 6427f8
Packit 6427f8
eval {
Packit 6427f8
    use autodie qw(truncate);
Packit 6427f8
    truncate(*TRUNCATE_FH,0);
Packit 6427f8
};
Packit 6427f8
Packit 6427f8
isa_ok($@, 'autodie::exception', 'Truncating freshly closed glob (*TRUNCATE_FH)');