This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
File::CheckTree.t: @INC should be change for chdir
[perl5.git] / lib / File / CheckTree.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5
6     # We will shortly chdir .., so '../lib' will be wrong at that time, and
7     # 'lib' will be correct
8     @INC = ('../lib', 'lib');
9 }
10
11 use Test::More tests => 23;
12
13 use strict;
14
15 require overload;
16
17 use File::CheckTree;
18 use File::Spec;          # used to get absolute paths
19
20 # We assume that we start from the perl "t" directory.
21 # Will move up one level to make it easier to generate
22 # reliable pathnames for testing File::CheckTree
23
24 chdir(File::Spec->updir) or die "cannot change to parent of t/ directory: $!";
25
26
27 #### TEST 1 -- No warnings ####
28 # usings both relative and full paths, indented comments
29
30 {
31     my ($num_warnings, $path_to_README);
32     $path_to_README = File::Spec->rel2abs('README');
33
34     my @warnings;
35     local $SIG{__WARN__} = sub { push @warnings, "@_" };
36
37     eval {
38         $num_warnings = validate qq{
39             lib  -d
40 # comment, followed "blank" line (w/ whitespace):
41            
42             # indented comment, followed blank line (w/o whitespace):
43
44             README -f
45             '$path_to_README' -e || warn
46         };
47     };
48
49     diag($_) for @warnings;
50     is( $@, '' );
51     is( scalar @warnings, 0 );
52     is( $num_warnings, 0 );
53 }
54
55
56 #### TEST 2 -- One warning ####
57
58 {
59     my ($num_warnings, @warnings);
60
61     local $SIG{__WARN__} = sub { push @warnings, "@_" };
62
63     eval {
64         $num_warnings = validate qq{
65             lib    -f
66             README -f
67         };
68     };
69
70     is( $@, '' );
71     is( scalar @warnings, 1 );
72     like( $warnings[0], qr/lib is not a plain file/);
73     is( $num_warnings, 1 );
74 }
75
76
77 #### TEST 3 -- Multiple warnings ####
78 # including first warning only from a bundle of tests,
79 # generic "|| warn", default "|| warn" and "|| warn '...' "
80
81 {
82     my ($num_warnings, @warnings);
83
84     local $SIG{__WARN__} = sub { push @warnings, "@_" };
85
86     eval {
87         $num_warnings = validate q{
88             lib     -effd
89             README -f || die
90             README -d || warn
91             lib    -f || warn "my warning: $file\n"
92         };
93     };
94
95     is( $@, '' );
96     is( scalar @warnings, 3 );
97     like( $warnings[0], qr/lib is not a plain file/);
98     like( $warnings[1], qr/README is not a directory/);
99     like( $warnings[2], qr/my warning: lib/);
100     is( $num_warnings, 3 );
101 }
102
103
104 #### TEST 4 -- cd directive ####
105 # cd directive followed by relative paths, followed by full paths
106 {
107     my ($num_warnings, @warnings, $path_to_libFile, $path_to_dist);
108     $path_to_libFile = File::Spec->rel2abs(File::Spec->catdir('lib','File'));
109     $path_to_dist    = File::Spec->rel2abs(File::Spec->curdir);
110
111     local $SIG{__WARN__} = sub { push @warnings, "@_" };
112
113     eval {
114         $num_warnings = validate qq{
115             lib                -d || die
116             '$path_to_libFile' cd
117             Spec               -e
118             Spec               -f
119             '$path_to_dist'    cd
120             README             -ef
121             INSTALL            -d || warn
122             '$path_to_libFile' -d || die
123         };
124     };
125
126     is( $@, '' );
127     is( scalar @warnings, 2 );
128     like( $warnings[0], qr/Spec is not a plain file/);
129     like( $warnings[1], qr/INSTALL is not a directory/);
130     is( $num_warnings, 2 );
131 }
132
133
134 #### TEST 5 -- Exception ####
135 # test with generic "|| die"
136 {
137     my $num_warnings;
138
139     eval {
140         $num_warnings = validate q{
141             lib       -ef || die
142             README    -d
143         };
144     };
145
146     like($@, qr/lib is not a plain file/);
147 }
148
149
150 #### TEST 6 -- Exception ####
151 # test with "|| die 'my error message'"
152 {
153     my $num_warnings;
154
155     eval {
156         $num_warnings = validate q{
157             lib       -ef || die "yadda $file yadda...\n"
158             README    -d
159         };
160     };
161
162     like($@, qr/yadda lib yadda/);
163     is( $num_warnings, undef );
164 }
165
166 #### TEST 7 -- Quoted file names ####
167 {
168     my $num_warnings;
169     eval {
170         $num_warnings = validate q{
171             "a file with whitespace" !-ef
172             'a file with whitespace' !-ef
173         };
174     };
175
176     is ( $@, '', 'No errors mean we compile correctly');
177 }
178
179 #### TEST 8 -- Malformed query ####
180 {
181     my $num_warnings;
182     eval {
183         $num_warnings = validate q{
184             a file with whitespace !-ef
185         };
186     };
187
188     like( $@, qr/syntax error/, 
189           'We got a syntax error for a malformed file query' );
190 }