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