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