This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
require die msg: only mention @INC if used
[perl5.git] / t / op / require_errors.t
CommitLineData
686c4ca0 1#!perl
686c4ca0
NC
2
3BEGIN {
a817e89d 4 chdir 't' if -d 't';
686c4ca0 5 require './test.pl';
624c42e2 6 set_up_inc( qw(../lib) );
686c4ca0
NC
7}
8
0b1b7115
JH
9use strict;
10use warnings;
11
4b62894a 12plan(tests => 28);
686c4ca0
NC
13
14my $nonfile = tempfile();
15
2fc7dfcb
NC
16# The tests for ' ' and '.h' never did fail, but previously the error reporting
17# code would read memory before the start of the SV's buffer
18
19for my $file ($nonfile, ' ') {
20 eval {
21 require $file;
22 };
23
24 like $@, qr/^Can't locate $file in \@INC \(\@INC contains: @INC\) at/,
25 "correct error message for require '$file'";
26}
686c4ca0 27
5bad2b39 28eval "require $nonfile";
2fc7dfcb 29
5bad2b39 30like $@, qr/^Can't locate $nonfile\.pm in \@INC \(you may need to install the $nonfile module\) \(\@INC contains: @INC\) at/,
614273ad 31 "correct error message for require $nonfile";
5bad2b39
DM
32
33eval "require ::$nonfile";
34
35like $@, qr/^Bareword in require must not start with a double-colon:/,
36 "correct error message for require ::$nonfile";
686c4ca0
NC
37
38eval {
39 require "$nonfile.ph";
40};
41
42like $@, qr/^Can't locate $nonfile\.ph in \@INC \(did you run h2ph\?\) \(\@INC contains: @INC\) at/;
43
2fc7dfcb
NC
44for my $file ("$nonfile.h", ".h") {
45 eval {
46 require $file
47 };
686c4ca0 48
2fc7dfcb
NC
49 like $@, qr/^Can't locate \Q$file\E in \@INC \(change \.h to \.ph maybe\?\) \(did you run h2ph\?\) \(\@INC contains: @INC\) at/,
50 "correct error message for require '$file'";
51}
686c4ca0 52
e9ce9c73
PJ
53for my $file ("$nonfile.ph", ".ph") {
54 eval {
55 require $file
56 };
57
58 like $@, qr/^Can't locate \Q$file\E in \@INC \(did you run h2ph\?\) \(\@INC contains: @INC\) at/,
59 "correct error message for require '$file'";
60}
61
32437794 62eval 'require <foom>';
808cb9e9 63like $@, qr/^<> at require-statement should be quotes at /, 'require <> error';
32437794 64
2433d39e
BF
65my $module = tempfile();
66my $mod_file = "$module.pm";
67
68open my $module_fh, ">", $mod_file or die $!;
69print { $module_fh } "print 1; 1;\n";
70close $module_fh;
71
72chmod 0333, $mod_file;
73
74SKIP: {
75 skip_if_miniperl("these modules may not be available to miniperl", 2);
76
77 push @INC, '../lib';
78 require Cwd;
79 require File::Spec::Functions;
80 if ($^O eq 'cygwin') {
81 require Win32;
82 }
83
84 # Going to try to switch away from root. Might not work.
85 # (stolen from t/op/stat.t)
86 my $olduid = $>;
87 eval { $> = 1; };
88 skip "Can't test permissions meaningfully if you're superuser", 2
89 if ($^O eq 'cygwin' ? Win32::IsAdminUser() : $> == 0);
90
91 local @INC = ".";
92 eval "use $module";
93 like $@,
94 qr<^\QCan't locate $mod_file:>,
95 "special error message if the file exists but can't be opened";
96
6e0a4ea0
JL
97 SKIP: {
98 skip "Can't make the path absolute", 1
99 if !defined(Cwd::getcwd());
100
101 my $file = File::Spec::Functions::catfile(Cwd::getcwd(), $mod_file);
102 eval {
103 require($file);
104 };
105 like $@,
106 qr<^\QCan't locate $file:>,
107 "...even if we use a full path";
108 }
2433d39e
BF
109
110 # switch uid back (may not be implemented)
111 eval { $> = $olduid; };
112}
113
1141 while unlink $mod_file;
115
686c4ca0
NC
116# I can't see how to test the EMFILE case
117# I can't see how to test the case of not displaying @INC in the message.
118# (and does that only happen on VMS?)
c8028aa6
TC
119
120# fail and print the full filename
121eval { no warnings 'syscalls'; require "strict.pm\0invalid"; };
122like $@, qr/^Can't locate strict\.pm\\0invalid: /, 'require nul check [perl #117265]';
c8028aa6
TC
123{
124 my $WARN;
125 local $SIG{__WARN__} = sub { $WARN = shift };
a1b60c8d
LM
126 {
127 my $ret = do "strict.pm\0invalid";
128 my $exc = $@;
129 my $err = $!;
130 is $ret, undef, 'do nulstring returns undef';
131 is $exc, '', 'do nulstring clears $@';
132 $! = $err;
133 ok $!{ENOENT}, 'do nulstring fails with ENOENT';
33fe1955 134 like $WARN, qr{^Invalid \\0 character in pathname for do: strict\.pm\\0invalid at }, 'do nulstring warning';
a1b60c8d
LM
135 }
136
137 $WARN = '';
c8028aa6
TC
138 eval { require "strict.pm\0invalid"; };
139 like $WARN, qr{^Invalid \\0 character in pathname for require: strict\.pm\\0invalid at }, 'nul warning';
140 like $@, qr{^Can't locate strict\.pm\\0invalid: }, 'nul error';
141
142 $WARN = '';
143 local @INC = @INC;
624c42e2 144 set_up_inc( "lib\0invalid" );
c8028aa6 145 eval { require "unknown.pm" };
ddc65b67 146 like $WARN, qr{^Invalid \\0 character in \@INC entry for require: lib\\0invalid at }, 'nul warning';
c8028aa6
TC
147}
148eval "require strict\0::invalid;";
149like $@, qr/^syntax error at \(eval \d+\) line 1/, 'parse error with \0 in barewords module names';
150
08f800f8
FC
151# Refs and globs that stringify with embedded nulls
152# These crashed from 5.20 to 5.24 [perl #128182].
153eval { no warnings 'syscalls'; require eval "qr/\0/" };
154like $@, qr/^Can't locate \(\?\^:\\0\):/,
155 'require ref that stringifies with embedded null';
156eval { no strict; no warnings 'syscalls'; require *{"\0a"} };
157like $@, qr/^Can't locate \*main::\\0a:/,
158 'require ref that stringifies with embedded null';
33fe1955
LM
159
160eval { require undef };
161like $@, qr/^Missing or undefined argument to require /;
162
163eval { do undef };
164like $@, qr/^Missing or undefined argument to do /;
165
166eval { require "" };
167like $@, qr/^Missing or undefined argument to require /;
168
169eval { do "" };
170like $@, qr/^Missing or undefined argument to do /;
4b62894a
DM
171
172# non-searchable pathnames shouldn't mention @INC in the error
173
174my $nonsearch = "./no_such_file.pm";
175
176eval "require \"$nonsearch\"";
177
178like $@, qr/^Can't locate \Q$nonsearch\E at/,
179 "correct error message for require $nonsearch";