This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
re-implement OPpASSIGN_COMMON mechanism
[perl5.git] / t / op / require_errors.t
1 #!perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require './test.pl';
6 }
7
8 use strict;
9 use warnings;
10
11 plan(tests => 17);
12
13 my $nonfile = tempfile();
14
15 @INC = qw(Perl Rules);
16
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
20 for 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 }
28
29 eval "require $nonfile";
30
31 like $@, qr/^Can't locate $nonfile\.pm in \@INC \(you may need to install the $nonfile module\) \(\@INC contains: @INC\) at/,
32     "correct error message for require $nonfile";
33
34 eval {
35     require "$nonfile.ph";
36 };
37
38 like $@, qr/^Can't locate $nonfile\.ph in \@INC \(did you run h2ph\?\) \(\@INC contains: @INC\) at/;
39
40 for my $file ("$nonfile.h", ".h") {
41     eval {
42         require $file
43     };
44
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 }
48
49 for 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
58 eval 'require <foom>';
59 like $@, qr/^<> at require-statement should be quotes at /, 'require <> error';
60
61 my $module   = tempfile();
62 my $mod_file = "$module.pm";
63
64 open my $module_fh, ">", $mod_file or die $!;
65 print { $module_fh } "print 1; 1;\n";
66 close $module_fh;
67
68 chmod 0333, $mod_file;
69
70 SKIP: {
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
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     }
105
106     # switch uid back (may not be implemented)
107     eval { $> = $olduid; };
108 }
109
110 1 while unlink $mod_file;
111
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?)
115
116 # fail and print the full filename
117 eval { no warnings 'syscalls'; require "strict.pm\0invalid"; };
118 like $@, qr/^Can't locate strict\.pm\\0invalid: /, 'require nul check [perl #117265]';
119 eval { no warnings 'syscalls'; do "strict.pm\0invalid"; };
120 like $@, 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" };
132   like $WARN, qr{^Invalid \\0 character in \@INC entry for require: lib\\0invalid at }, 'nul warning';
133 }
134 eval "require strict\0::invalid;";
135 like $@, qr/^syntax error at \(eval \d+\) line 1/, 'parse error with \0 in barewords module names';
136