Commit | Line | Data |
d1b75174 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
5 | @INC = '../lib'; |
6 | } |
7 | |
8 | use strict; |
9 | use Config; |
10 | |
3a33ea64 |
11 | use Test::More tests => 15; |
d1b75174 |
12 | |
13 | use_ok( 'sigtrap' ); |
14 | |
15 | package main; |
16 | local %SIG; |
17 | |
18 | # use a version of sigtrap.pm somewhat too high |
19 | eval{ sigtrap->import(99999) }; |
20 | like( $@, qr/version 99999 required,/, 'import excessive version number' ); |
21 | |
22 | # use an invalid signal name |
23 | eval{ sigtrap->import('abadsignal') }; |
24 | like( $@, qr/^Unrecognized argument abadsignal/, 'send bad signame to import' ); |
25 | |
26 | eval{ sigtrap->import('handler') }; |
27 | like( $@, qr/^No argument specified/, 'send handler without subref' ); |
28 | |
29 | sigtrap->import('AFAKE'); |
30 | is( $SIG{AFAKE}, \&sigtrap::handler_traceback, 'install normal handler' ); |
31 | |
32 | sigtrap->import('die', 'AFAKE', 'stack-trace', 'FAKE2'); |
33 | is( $SIG{AFAKE}, \&sigtrap::handler_die, 'install the die handler' ); |
34 | is( $SIG{FAKE2}, \&sigtrap::handler_traceback, 'install traceback handler' ); |
35 | |
36 | my @normal = qw( HUP INT PIPE TERM ); |
37 | @SIG{@normal} = 1 x @normal; |
38 | sigtrap->import('normal-signals'); |
39 | is( (grep { ref $_ } @SIG{@normal}), @normal, 'check normal-signals set' ); |
40 | |
41 | my @error = qw( ABRT BUS EMT FPE ILL QUIT SEGV SYS TRAP ); |
42 | @SIG{@error} = 1 x @error; |
43 | sigtrap->import('error-signals'); |
44 | is( (grep { ref $_ } @SIG{@error}), @error, 'check error-signals set' ); |
45 | |
46 | my @old = qw( ABRT BUS EMT FPE ILL PIPE QUIT SEGV SYS TERM TRAP ); |
47 | @SIG{@old} = 1 x @old; |
48 | sigtrap->import('old-interface-signals'); |
49 | is( (grep { ref $_ } @SIG{@old}), @old, 'check old-interface-signals set' ); |
50 | |
51 | my $handler = sub {}; |
52 | sigtrap->import(handler => $handler, 'FAKE3'); |
53 | is( $SIG{FAKE3}, $handler, 'install custom handler' ); |
54 | |
55 | $SIG{FAKE} = 'IGNORE'; |
56 | sigtrap->import('untrapped', 'FAKE'); |
57 | is( $SIG{FAKE}, 'IGNORE', 'respect existing handler set to IGNORE' ); |
58 | |
59 | my $out = tie *STDOUT, 'TieOut'; |
60 | $SIG{FAKE} = 'DEFAULT'; |
61 | $sigtrap::Verbose = 1; |
62 | sigtrap->import('any', 'FAKE'); |
63 | is( $SIG{FAKE}, \&sigtrap::handler_traceback, 'should set default handler' ); |
64 | like( $out->read, qr/^Installing handler/, 'does it talk with $Verbose set?' ); |
65 | |
66 | # handler_die croaks with first argument |
67 | eval { sigtrap::handler_die('FAKE') }; |
68 | like( $@, qr/^Caught a SIGFAKE/, 'does handler_die() croak?' ); |
69 | |
d1b75174 |
70 | package TieOut; |
71 | |
72 | sub TIEHANDLE { |
73 | bless(\(my $scalar), $_[0]); |
74 | } |
75 | |
76 | sub PRINT { |
77 | my $self = shift; |
78 | $$self .= join '', @_; |
79 | } |
80 | |
81 | sub WRITE { |
82 | my ($self, $msg, $length) = @_; |
83 | $$self .= $msg; |
84 | } |
85 | |
86 | sub read { |
87 | my $self = shift; |
88 | substr($$self, 0, length($$self), ''); |
89 | } |