This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Setting $_ to multiline glob in @INC filter
[perl5.git] / lib / diagnostics.t
CommitLineData
00680da2 1#!./perl -w
f5ad5489
GS
2
3BEGIN {
7b903762
RGS
4 chdir '..' if -d '../pod' && -d '../t';
5 @INC = 'lib';
f63ff350 6 require './t/test.pl';
6fbc9859 7 plan(24);
f5ad5489
GS
8}
9
00680da2
FC
10BEGIN {
11 my $w;
12 $SIG{__WARN__} = sub { $w = shift };
13 use_ok('diagnostics');
14 is $w, undef, 'no warnings when loading diagnostics.pm';
15}
f5ad5489 16
d23f0205 17require base;
f5ad5489 18
d23f0205
MS
19eval {
20 'base'->import(qw(I::do::not::exist));
21};
f5ad5489 22
d142a076
FC
23like( $@, qr/^Base class package "I::do::not::exist" is empty/,
24 'diagnostics not tripped up by "use base qw(Dont::Exist)"');
8b56d6ff 25
2dde0467 26open *whatever, ">", \my $warning
8b56d6ff 27 or die "Couldn't redirect STDERR to var: $!";
2dde0467
FC
28my $old_stderr = *STDERR{IO};
29*STDERR = *whatever{IO};
30
31# Test for %.0f patterns in perldiag, added in 5.11.0
8b56d6ff
FC
32warn('gmtime(nan) too large');
33like $warning, qr/\(W overflow\) You called/, '%0.f patterns';
4b056c06
FC
34
35# L<foo/bar> links
36seek STDERR, 0,0;
37$warning = '';
38warn("accept() on closed socket spanner");
39like $warning, qr/"accept" in perlfunc/, 'L<foo/bar> links';
40
41# L<foo|bar/baz> links
42seek STDERR, 0,0;
43$warning = '';
44warn
45 'Lexing code attempted to stuff non-Latin-1 character into Latin-1 input';
3d6c5fec 46like $warning, qr/lex_stuff_pvn or similar/, 'L<foo|bar/baz>';
f4739a71
FC
47
48# Multiple messages with the same description
49seek STDERR, 0,0;
50$warning = '';
b6a06234 51warn 'Code point 0xBEE5 is not Unicode, may not be portable';
989843c8 52like $warning, qr/S utf8/,
f4739a71
FC
53 'Message sharing its description with the following message';
54
c0d3a21f
MH
55# Periods at end of entries in perldiag.pod get matched correctly
56seek STDERR, 0,0;
57$warning = '';
58warn "Execution of -e aborted due to compilation errors.\n";
59like $warning, qr/The final summary message/, 'Periods at end of line';
e958e573 60
e958e573
MH
61# Test for %d/%u
62seek STDERR, 0,0;
63$warning = '';
64warn "Bad arg length for us, is 4, should be 42";
65like $warning, qr/In C parlance/, '%u works';
524e9188 66
b6a06234
FC
67# Test for %X
68seek STDERR, 0,0;
69$warning = '';
70warn "Unicode surrogate U+C0FFEE is illegal in UTF-8";
71like $warning, qr/You had a UTF-16 surrogate/, '%X';
72
01bfea8b
FC
73# Test for %p
74seek STDERR, 0,0;
75$warning = '';
76warn "Slab leaked from cv fadedc0ffee";
77like $warning, qr/bookkeeping of op trees/, '%p';
78
524e9188
MH
79# Strip S<>
80seek STDERR, 0,0;
81$warning = '';
82warn "syntax error";
83like $warning, qr/cybernetic version of 20 questions/s, 'strip S<>';
2dde0467 84
33327ed8
FC
85# Errors ending with dots
86seek STDERR, 0,0;
87$warning = '';
0a437bc9
FC
88warn "I had compilation errors.\n";
89like $warning, qr/final summary message/, 'dotty errors';
90
91# Multiline errors
92seek STDERR, 0,0;
93$warning = '';
94warn "Attempt to reload weapon aborted.\nCompilation failed in require";
95like $warning,
96 qr/You tried to load a file.*Perl could not compile/s,
97 'multiline errors';
33327ed8 98
6fbc9859
MH
99# Multiline entry in perldiag.pod
100seek STDERR, 0,0;
101$warning = '';
102warn "Using just the first character returned by \\N{} in character class in regex; marked by <-- HERE in m/%s/";
103like $warning,
104 qr/A charnames handler may return a sequence/s,
105 'multi-line entries in perldiag.pod match';
106
107# ; at end of entry in perldiag.pod
108seek STDERR, 0,0;
109$warning = '';
b33c0c71 110warn "Perl folding rules are not up-to-date for 0xA; please use the perlbug utility to report; in regex; marked by <-- HERE in m/\ <-- HERE q/";
6fbc9859 111like $warning,
80f78131 112 qr/You used a regular expression with case-insensitive matching/s,
6fbc9859
MH
113 '; works at the end of entries in perldiag.pod';
114
115# Differences in spaces in warnings (Why not be nice and accept them?)
116seek STDERR, 0,0;
117$warning = '';
118warn "Assignment to both a list and a scalar\n";
119like $warning,
120 qr/2nd and 3rd/s,
121 'spaces in warnings are matched lightly';
122
123# Differences in spaces in warnings with a period at the end
124seek STDERR, 0,0;
125$warning = '';
126warn "perl: warning: Setting locale failed.\n";
127like $warning,
128 qr/The whole warning/s,
129 'spaces in warnings with periods at the end are matched lightly';
130
131
2dde0467
FC
132*STDERR = $old_stderr;
133
134# These tests use a panic under the hope that the description is not likely
135# to change.
136@runperl_args = (
137 switches => [ '-Ilib', '-Mdiagnostics' ],
138 stderr => 1,
139 nolib => 1, # -I../lib would go outside the build dir
140);
141$subs =
142 "sub foo{bar()}sub bar{baz()}sub baz{die q _panic: gremlins_}foo()";
143is runperl(@runperl_args, prog => $subs),
144 << 'EOT', 'internal error with backtrace';
145panic: gremlins at -e line 1 (#1)
146 (P) An internal error.
147
148Uncaught exception from user code:
149 panic: gremlins at -e line 1.
150 main::baz() called at -e line 1
151 main::bar() called at -e line 1
152 main::foo() called at -e line 1
153EOT
154is runperl(@runperl_args, prog => $subs =~ s/panic\K/k/r),
155 << 'EOU', 'user error with backtrace';
156Uncaught exception from user code:
157 panick: gremlins at -e line 1.
158 main::baz() called at -e line 1
159 main::bar() called at -e line 1
160 main::foo() called at -e line 1
161EOU
162is runperl(@runperl_args, prog => 'die q _panic: gremlins_'),
163 << 'EOV', 'no backtrace from top-level internal error';
164panic: gremlins at -e line 1 (#1)
165 (P) An internal error.
166
167Uncaught exception from user code:
168 panic: gremlins at -e line 1.
169EOV
170is runperl(@runperl_args, prog => 'die q _panick: gremlins_'),
171 << 'EOW', 'no backtrace from top-level user error';
172Uncaught exception from user code:
173 panick: gremlins at -e line 1.
174EOW
245e6c67
FC
175like runperl(
176 @runperl_args,
177 prog => $subs =~
f5aea3e0
NC
178 s[q _panic: gremlins_]
179 [qq _Attempt to reload foo aborted.\\nCompilation failed in require_]r,
245e6c67
FC
180 ),
181 qr/Uncaught exception from user code:
182 Attempt to reload foo aborted\.
183 Compilation failed in require at -e line \d+\.
184 main::baz\(\) called at -e line \d+
185 main::bar\(\) called at -e line \d+
186 main::foo\(\) called at -e line \d+
187/, 'backtrace from multiline error';
d271dac9
FC
188is runperl(@runperl_args, prog => 'BEGIN { die q _panic: gremlins_ }'),
189 << 'EOX', 'BEGIN{die} does not suppress diagnostics';
190panic: gremlins at -e line 1.
191BEGIN failed--compilation aborted at -e line 1 (#1)
192 (P) An internal error.
193
194Uncaught exception from user code:
195 panic: gremlins at -e line 1.
196 BEGIN failed--compilation aborted at -e line 1.
197EOX