Debian Codegraph @

cpan/autodie/t/open.t

raw
1 #!/usr/bin/perl -w
2 use strict;
3
4 use Test::More 'no_plan';
5
6 use constant NO_SUCH_FILE => "this_file_had_better_not_exist";
7
8 use autodie;
9
10 eval { open(my $fh, '<', NO_SUCH_FILE); };
11 ok($@, "3-arg opening non-existent file fails");
12 like($@, qr/for reading/, "Well-formatted 3-arg open failure");
13
14 eval { open(my $fh, "< ".NO_SUCH_FILE) };
15 ok($@, "2-arg opening non-existent file fails");
16
17 like($@, qr/for reading/, "Well-formatted 2-arg open failure");
18 unlike($@, qr/GLOB\(0x/, "No ugly globs in 2-arg open messsage");
19
20 # RT 47520. 2-argument open without mode would repeat the file
21 # and line number.
22
23 eval {
24 use autodie;
25
26 open(my $fh, NO_SUCH_FILE);
27 };
28
29 isa_ok($@, 'autodie::exception');
30 like( $@, qr/at \S+ line \d+/, "At least one mention");
31 unlike($@, qr/at \S+ line \d+\s+at \S+ line \d+/, "...but not too mentions");
32
33 # RT 47520-ish. 2-argument open without a mode should be marked
34 # as 'for reading'.
35 like($@, qr/for reading/, "Well formatted 2-arg open without mode");
36
37 # We also shouldn't get repeated messages, even if the default mode
38 # was used. Single-arg open always falls through to the default
39 # formatter.
40
41 eval {
42 use autodie;
43
44 open( NO_SUCH_FILE . "" );
45 };
46
47 isa_ok($@, 'autodie::exception');
48 like( $@, qr/at \S+ line \d+/, "At least one mention");
49 unlike($@, qr/at \S+ line \d+\s+at \S+ line \d+/, "...but not too mentions");
50
51 # RT 52427. Piped open can have any many args.
52
53 # Sniff to see if we can run 'true' on this system. Changes we can't
54 # on non-Unix systems.
55
56 use Config;
57 my @true = ($^O =~ /android/
58 || ($Config{usecrosscompile} && $^O eq 'nto' ))
59 ? ('sh', '-c', 'true $@', '--')
60 : 'true';
61
62 eval {
63 use autodie;
64
65 die "Windows and VMS do not support multi-arg pipe" if $^O eq "MSWin32" or $^O eq 'VMS';
66
67 open(my $fh, '-|', @true);
68 };
69
70 SKIP: {
71 skip('true command or list pipe not available on this system', 1) if $@;
72
73 eval {
74 use autodie;
75
76 my $fh;
77 open $fh, "-|", @true;
78 open $fh, "-|", @true, "foo";
79 open $fh, "-|", @true, "foo", "bar";
80 open $fh, "-|", @true, "foo", "bar", "baz";
81 };
82
83 is $@, '', "multi arg piped open does not fail";
84 }
85
86 # Github 6
87 # Non-vanilla modes (such as <:utf8) would cause the formatter in
88 # autodie::exception to fail.
89
90 eval {
91 use autodie;
92 open(my $fh, '<:utf8', NO_SUCH_FILE);
93 };
94
95 ok( $@, "Error thrown.");
96 unlike($@, qr/Don't know how to format mode/, "No error on exotic open.");
97 like( $@, qr/Can't open .*? with mode '<:utf8'/, "Nicer looking error.");