This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More updates to Module-CoreList for Perl 5.20.2
[perl5.git] / lib / diagnostics.t
1 #!./perl -w
2
3 BEGIN {
4     chdir '..' if -d '../pod' && -d '../t';
5     @INC = 'lib';
6     require './t/test.pl';
7     plan(29);
8 }
9
10 BEGIN {
11     my $w;
12     $SIG{__WARN__} = sub { $w = shift };
13     use_ok('diagnostics');
14     is $w, undef, 'no warnings when loading diagnostics.pm';
15 }
16
17 require base;
18
19 eval {
20     'base'->import(qw(I::do::not::exist));
21 };
22
23 like( $@, qr/^Base class package "I::do::not::exist" is empty/,
24          'diagnostics not tripped up by "use base qw(Dont::Exist)"');
25
26 open *whatever, ">", \my $warning
27     or die "Couldn't redirect STDERR to var: $!";
28 my $old_stderr = *STDERR{IO};
29 *STDERR = *whatever{IO};
30
31 # Test for %.0f patterns in perldiag, added in 5.11.0
32 warn('gmtime(nan) too large');
33 like $warning, qr/\(W overflow\) You called/, '%0.f patterns';
34
35 # L<foo/bar> links
36 seek STDERR, 0,0;
37 $warning = '';
38 warn("accept() on closed socket spanner");
39 like $warning, qr/"accept" in perlfunc/, 'L<foo/bar> links';
40
41 # L<foo|bar/baz> links
42 seek STDERR, 0,0;
43 $warning = '';
44 warn
45  'Lexing code attempted to stuff non-Latin-1 character into Latin-1 input';
46 like $warning, qr/lex_stuff_pvn or similar/, 'L<foo|bar/baz>';
47
48 # Multiple messages with the same description
49 seek STDERR, 0,0;
50 $warning = '';
51 warn 'Deep recursion on anonymous subroutine';
52 like $warning, qr/W recursion/,
53    'Message sharing its description with the following message';
54 seek STDERR, 0,0;
55 $warning = '';
56 warn 'Deep recursion on subroutine "foo"';
57 like $warning, qr/W recursion/,
58    'Message sharing its description with the preceding message';
59
60 # Periods at end of entries in perldiag.pod get matched correctly
61 seek STDERR, 0,0;
62 $warning = '';
63 warn "Execution of -e aborted due to compilation errors.\n";
64 like $warning, qr/The final summary message/, 'Periods at end of line';
65
66 # Test for %d/%u
67 seek STDERR, 0,0;
68 $warning = '';
69 warn "Bad arg length for us, is 4, should be 42";
70 like $warning, qr/In C parlance/, '%u works';
71
72 # Test for %X
73 seek STDERR, 0,0;
74 $warning = '';
75 warn "Unicode surrogate U+C0FFEE is illegal in UTF-8";
76 like $warning, qr/You had a UTF-16 surrogate/, '%X';
77
78 # Test for %p
79 seek STDERR, 0,0;
80 $warning = '';
81 warn "Slab leaked from cv fadedc0ffee";
82 like $warning, qr/bookkeeping of op trees/, '%p';
83
84 # Strip S<>
85 seek STDERR, 0,0;
86 $warning = '';
87 warn "syntax error";
88 like $warning, qr/cybernetic version of 20 questions/s, 'strip S<>';
89
90 # Errors ending with dots
91 seek STDERR, 0,0;
92 $warning = '';
93 warn "I had compilation errors.\n";
94 like $warning, qr/final summary message/, 'dotty errors';
95
96 # Multiline errors
97 seek STDERR, 0,0;
98 $warning = '';
99 warn "Attempt to reload weapon aborted.\nCompilation failed in require";
100 like $warning,
101      qr/You tried to load a file.*Perl could not compile/s,
102     'multiline errors';
103
104 # Multiline entry in perldiag.pod
105 seek STDERR, 0,0;
106 $warning = '';
107 warn "Using just the first character returned by \\N{} in character class in regex; marked by <-- HERE in m/%s/";
108 like $warning,
109     qr/Named Unicode character escapes/s,
110     'multi-line entries in perldiag.pod match';
111
112 # ; at end of entry in perldiag.pod
113 seek STDERR, 0,0;
114 $warning = '';
115 warn "Perl folding rules are not up-to-date for 0x0A; please use the perlbug utility to report; in regex; marked by <-- HERE in m/\ <-- HERE q/";
116 like $warning,
117     qr/You used a regular expression with case-insensitive matching/s,
118     '; works at the end of entries in perldiag.pod';
119
120 # Differences in spaces in warnings (Why not be nice and accept them?)
121 seek STDERR, 0,0;
122 $warning = '';
123 warn "Assignment     to both a list and a scalar\n";
124 like $warning,
125     qr/2nd and 3rd/s,
126     'spaces in warnings are matched lightly';
127
128 # Differences in spaces in warnings with a period at the end
129 seek STDERR, 0,0;
130 $warning = '';
131 warn "perl: warning: Setting locale failed.\n";
132 like $warning,
133     qr/The whole warning/s,
134     'spaces in warnings with periods at the end are matched lightly';
135
136 # Wrapped links
137 seek STDERR, 0,0;
138 $warning = '';
139 warn "Argument \"%s\" treated as 0 in increment (++)";
140 like $warning,
141     qr/Auto-increment.*Auto-decrement/s,
142     'multiline links are not truncated';
143
144 {
145 # Find last warning in perldiag.pod, and last items if any
146     my $lw;
147     my $inlast;
148     my $item;
149
150     open(my $f, '<', "pod/perldiag.pod")
151         or die "failed to open pod/perldiag.pod for reading: $!";
152
153     while (<$f>) {
154         if ( /^=item\s+(.*)/) {
155             $lw = $1;
156         } elsif (/^=back/) {
157             $inlast = 1;
158         } elsif ($inlast) {
159             # Skip headings
160             next if /^=/;
161
162             # Strip specials
163             $_ =~ s/\w<(.*?)>/$1/g;
164
165             # And whitespace
166             $_ =~ s/(^\s+|\s+$)//g;
167
168             if ($_) {
169                 $item = $_;
170
171                 last;
172             }
173         }
174     }
175     close($f);
176
177     ok($item, "(sanity...) found an item to check with ($item)");
178     seek STDERR, 0,0;
179     $warning = '';
180     warn $lw;
181     ok($warning, '(sanity...) got a warning');
182     unlike $warning,
183         qr/\Q$item\E/,
184         "Junk after =back doesn't show up in last warning";
185 }
186
187 *STDERR = $old_stderr;
188
189 # These tests use a panic under the hope that the description is not likely
190 # to change.
191 @runperl_args = (
192         switches => [ '-Ilib', '-Mdiagnostics' ],
193         stderr => 1,
194         nolib => 1, # -I../lib would go outside the build dir
195 );
196 $subs =
197  "sub foo{bar()}sub bar{baz()}sub baz{die q _panic: gremlins_}foo()";
198 is runperl(@runperl_args, prog => $subs),
199    << 'EOT', 'internal error with backtrace';
200 panic: gremlins at -e line 1 (#1)
201     (P) An internal error.
202     
203 Uncaught exception from user code:
204         panic: gremlins at -e line 1.
205         main::baz() called at -e line 1
206         main::bar() called at -e line 1
207         main::foo() called at -e line 1
208 EOT
209 is runperl(@runperl_args, prog => $subs =~ s/panic\K/k/r),
210    << 'EOU', 'user error with backtrace';
211 Uncaught exception from user code:
212         panick: gremlins at -e line 1.
213         main::baz() called at -e line 1
214         main::bar() called at -e line 1
215         main::foo() called at -e line 1
216 EOU
217 is runperl(@runperl_args, prog => 'die q _panic: gremlins_'),
218    << 'EOV', 'no backtrace from top-level internal error';
219 panic: gremlins at -e line 1 (#1)
220     (P) An internal error.
221     
222 Uncaught exception from user code:
223         panic: gremlins at -e line 1.
224 EOV
225 is runperl(@runperl_args, prog => 'die q _panick: gremlins_'),
226    << 'EOW', 'no backtrace from top-level user error';
227 Uncaught exception from user code:
228         panick: gremlins at -e line 1.
229 EOW
230 like runperl(
231       @runperl_args,
232       prog => $subs =~
233          s[q _panic: gremlins_]
234           [qq _Attempt to reload foo aborted.\\nCompilation failed in require_]r,
235      ),
236      qr/Uncaught exception from user code:
237         Attempt to reload foo aborted\.
238         Compilation failed in require at -e line \d+\.
239         main::baz\(\) called at -e line \d+
240         main::bar\(\) called at -e line \d+
241         main::foo\(\) called at -e line \d+
242 /,  'backtrace from multiline error';
243 is runperl(@runperl_args, prog => 'BEGIN { die q _panic: gremlins_ }'),
244    << 'EOX', 'BEGIN{die} does not suppress diagnostics';
245 panic: gremlins at -e line 1.
246 BEGIN failed--compilation aborted at -e line 1 (#1)
247     (P) An internal error.
248     
249 Uncaught exception from user code:
250         panic: gremlins at -e line 1.
251         BEGIN failed--compilation aborted at -e line 1.
252 EOX