| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use File::Temp qw(tempfile); |
| 6 |
use IO::Handle; |
| 7 |
use File::Spec; |
| 8 |
use FindBin qw($Bin); |
| 9 |
|
| 10 |
my ($truncate_status, $tmpfh, $tmpfile); |
| 11 |
|
| 12 |
# Some systems have a screwy tempfile. We don't run our tests there. |
| 13 |
eval { |
| 14 |
($tmpfh, $tmpfile) = tempfile(UNLINK => 1); |
| 15 |
}; |
| 16 |
|
| 17 |
if ($@ or !defined $tmpfh) { |
| 18 |
plan skip_all => 'tempfile() not happy on this system.'; |
| 19 |
} |
| 20 |
|
| 21 |
eval { |
| 22 |
$truncate_status = truncate($tmpfh, 0); |
| 23 |
}; |
| 24 |
|
| 25 |
if ($@ || !defined($truncate_status)) { |
| 26 |
plan skip_all => 'Truncate not implemented or not working on this system'; |
| 27 |
} |
| 28 |
|
| 29 |
plan tests => 12; |
| 30 |
|
| 31 |
SKIP: { |
| 32 |
my $can_truncate_stdout = truncate(\*STDOUT,0); |
| 33 |
|
| 34 |
if ($can_truncate_stdout) { |
| 35 |
skip("This system thinks we can truncate STDOUT. Suuure!", 1); |
| 36 |
} |
| 37 |
|
| 38 |
eval { |
| 39 |
use autodie; |
| 40 |
truncate(\*STDOUT,0); |
| 41 |
}; |
| 42 |
|
| 43 |
isa_ok($@, 'autodie::exception', "Truncating STDOUT should throw an exception"); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
eval { |
| 48 |
use autodie; |
| 49 |
no warnings 'once'; |
| 50 |
truncate(\*FOO, 0); |
| 51 |
}; |
| 52 |
|
| 53 |
isa_ok($@, 'autodie::exception', "Truncating an unopened file is wrong."); |
| 54 |
|
| 55 |
$tmpfh->print("Hello World"); |
| 56 |
$tmpfh->flush; |
| 57 |
|
| 58 |
eval { |
| 59 |
use autodie; |
| 60 |
truncate($tmpfh, 0); |
| 61 |
}; |
| 62 |
|
| 63 |
is($@, "", "Truncating a normal file should be fine"); |
| 64 |
|
| 65 |
$tmpfh->close; |
| 66 |
|
| 67 |
# Time to test truncating via globs. |
| 68 |
|
| 69 |
# Firstly, truncating a closed filehandle should fail. |
| 70 |
# I know we tested this above, but we'll do a full dance of |
| 71 |
# opening and closing TRUNCATE_FH here. |
| 72 |
|
| 73 |
eval { |
| 74 |
use autodie qw(truncate); |
| 75 |
truncate(\*TRUNCATE_FH, 0); |
| 76 |
}; |
| 77 |
|
| 78 |
isa_ok($@, 'autodie::exception', "Truncating unopened file (TRUNCATE_FH)"); |
| 79 |
|
| 80 |
# Now open the file. If this throws an exception, there's something |
| 81 |
# wrong with our tests, or autodie... |
| 82 |
{ |
| 83 |
use autodie qw(open); |
| 84 |
open(TRUNCATE_FH, '+<', $tmpfile); |
| 85 |
} |
| 86 |
|
| 87 |
# Now try truncating the filehandle. This should succeed. |
| 88 |
|
| 89 |
eval { |
| 90 |
use autodie qw(truncate); |
| 91 |
truncate(\*TRUNCATE_FH,0); |
| 92 |
}; |
| 93 |
|
| 94 |
is($@, "", 'Truncating an opened glob (\*TRUNCATE_FH)'); |
| 95 |
|
| 96 |
eval { |
| 97 |
use autodie qw(truncate); |
| 98 |
truncate(*TRUNCATE_FH,0); |
| 99 |
}; |
| 100 |
|
| 101 |
is($@, "", 'Truncating an opened glob (*TRUNCATE_FH)'); |
| 102 |
|
| 103 |
# Now let's change packages, since globs are package dependent |
| 104 |
|
| 105 |
eval { |
| 106 |
package Fatal::Test; |
| 107 |
no warnings 'once'; |
| 108 |
use autodie qw(truncate); |
| 109 |
truncate(\*TRUNCATE_FH,0); # Should die, as now unopened |
| 110 |
}; |
| 111 |
|
| 112 |
isa_ok($@, 'autodie::exception', 'Truncating unopened file in different package (\*TRUNCATE_FH)'); |
| 113 |
|
| 114 |
eval { |
| 115 |
package Fatal::Test; |
| 116 |
no warnings 'once'; |
| 117 |
use autodie qw(truncate); |
| 118 |
truncate(*TRUNCATE_FH,0); # Should die, as now unopened |
| 119 |
}; |
| 120 |
|
| 121 |
isa_ok($@, 'autodie::exception', 'Truncating unopened file in different package (*TRUNCATE_FH)'); |
| 122 |
|
| 123 |
# Now back to our previous test, just to make sure it hasn't changed |
| 124 |
# the original file. |
| 125 |
|
| 126 |
eval { |
| 127 |
use autodie qw(truncate); |
| 128 |
truncate(\*TRUNCATE_FH,0); |
| 129 |
}; |
| 130 |
|
| 131 |
is($@, "", 'Truncating an opened glob #2 (\*TRUNCATE_FH)'); |
| 132 |
|
| 133 |
eval { |
| 134 |
use autodie qw(truncate); |
| 135 |
truncate(*TRUNCATE_FH,0); |
| 136 |
}; |
| 137 |
|
| 138 |
is($@, "", 'Truncating an opened glob #2 (*TRUNCATE_FH)'); |
| 139 |
|
| 140 |
# Now to close the file and retry. |
| 141 |
{ |
| 142 |
use autodie qw(close); |
| 143 |
close(TRUNCATE_FH); |
| 144 |
} |
| 145 |
|
| 146 |
eval { |
| 147 |
use autodie qw(truncate); |
| 148 |
truncate(\*TRUNCATE_FH,0); |
| 149 |
}; |
| 150 |
|
| 151 |
isa_ok($@, 'autodie::exception', 'Truncating freshly closed glob (\*TRUNCATE_FH)'); |
| 152 |
|
| 153 |
eval { |
| 154 |
use autodie qw(truncate); |
| 155 |
truncate(*TRUNCATE_FH,0); |
| 156 |
}; |
| 157 |
|
| 158 |
isa_ok($@, 'autodie::exception', 'Truncating freshly closed glob (*TRUNCATE_FH)'); |