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