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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#!/usr/bin/perl -w
use strict;
use Test::More;
use File::Temp qw(tempfile);
use IO::Handle;
use File::Spec;
use FindBin qw($Bin);
my ($truncate_status, $tmpfh, $tmpfile);
# Some systems have a screwy tempfile. We don't run our tests there.
eval {
($tmpfh, $tmpfile) = tempfile();
};
if ($@ or !defined $tmpfh) {
plan skip_all => 'tempfile() not happy on this system.';
}
eval {
$truncate_status = truncate($tmpfh, 0);
};
if ($@ || !defined($truncate_status)) {
plan skip_all => 'Truncate not implemented or not working on this system';
}
plan tests => 12;
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");
# Time to test truncating via globs.
# Firstly, truncating a closed filehandle should fail.
# I know we tested this above, but we'll do a full dance of
# opening and closing TRUNCATE_FH here.
eval {
use autodie qw(truncate);
truncate(\*TRUNCATE_FH, 0);
};
isa_ok($@, 'autodie::exception', "Truncating unopened file (TRUNCATE_FH)");
# Now open the file. If this throws an exception, there's something
# wrong with our tests, or autodie...
{
use autodie qw(open);
open(TRUNCATE_FH, '+<', $tmpfile);
}
# Now try truncating the filehandle. This should succeed.
eval {
use autodie qw(truncate);
truncate(\*TRUNCATE_FH,0);
};
is($@, "", 'Truncating an opened glob (\*TRUNCATE_FH)');
eval {
use autodie qw(truncate);
truncate(*TRUNCATE_FH,0);
};
is($@, "", 'Truncating an opened glob (*TRUNCATE_FH)');
# Now let's change packages, since globs are package dependent
eval {
package Fatal::Test;
no warnings 'once';
use autodie qw(truncate);
truncate(\*TRUNCATE_FH,0); # Should die, as now unopened
};
isa_ok($@, 'autodie::exception', 'Truncating unopened file in different package (\*TRUNCATE_FH)');
eval {
package Fatal::Test;
no warnings 'once';
use autodie qw(truncate);
truncate(*TRUNCATE_FH,0); # Should die, as now unopened
};
isa_ok($@, 'autodie::exception', 'Truncating unopened file in different package (*TRUNCATE_FH)');
# Now back to our previous test, just to make sure it hasn't changed
# the original file.
eval {
use autodie qw(truncate);
truncate(\*TRUNCATE_FH,0);
};
is($@, "", 'Truncating an opened glob #2 (\*TRUNCATE_FH)');
eval {
use autodie qw(truncate);
truncate(*TRUNCATE_FH,0);
};
is($@, "", 'Truncating an opened glob #2 (*TRUNCATE_FH)');
# Now to close the file and retry.
{
use autodie qw(close);
close(TRUNCATE_FH);
}
eval {
use autodie qw(truncate);
truncate(\*TRUNCATE_FH,0);
};
isa_ok($@, 'autodie::exception', 'Truncating freshly closed glob (\*TRUNCATE_FH)');
eval {
use autodie qw(truncate);
truncate(*TRUNCATE_FH,0);
};
isa_ok($@, 'autodie::exception', 'Truncating freshly closed glob (*TRUNCATE_FH)');
|