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
CommitLineData
849d5e34 1#!./perl -w
1a3850a5
GA
2
3BEGIN {
4 chdir 't' if -d 't';
69113548
KW
5
6 # We will shortly chdir .., so '../lib' will be wrong at that time, and
7 # 'lib' will be correct
8 @INC = ('../lib', 'lib');
1a3850a5
GA
9}
10
3d47529a 11use Test::More tests => 23;
849d5e34
JH
12
13use strict;
1a3850a5 14
69113548 15require overload;
cf2f24a4 16
1a3850a5 17use File::CheckTree;
849d5e34
JH
18use 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
86dc4f03 24chdir(File::Spec->updir) or die "cannot change to parent of t/ directory: $!";
849d5e34
JH
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
96dbb1e1 45 '$path_to_README' -e || warn
849d5e34
JH
46 };
47 };
48
3d47529a
NC
49 diag($_) for @warnings;
50 is( $@, '' );
51 is( scalar @warnings, 0 );
52 is( $num_warnings, 0 );
849d5e34
JH
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
3d47529a
NC
70 is( $@, '' );
71 is( scalar @warnings, 1 );
72 like( $warnings[0], qr/lib is not a plain file/);
73 is( $num_warnings, 1 );
849d5e34
JH
74}
75
1a3850a5 76
849d5e34
JH
77#### TEST 3 -- Multiple warnings ####
78# including first warning only from a bundle of tests,
79# generic "|| warn", default "|| warn" and "|| warn '...' "
1a3850a5 80
849d5e34
JH
81{
82 my ($num_warnings, @warnings);
1a3850a5 83
849d5e34
JH
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
3d47529a
NC
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 );
849d5e34
JH
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);
026a9d8a 108 $path_to_libFile = File::Spec->rel2abs(File::Spec->catdir('lib','File'));
849d5e34
JH
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
1f0bbb56 116 '$path_to_libFile' cd
849d5e34
JH
117 Spec -e
118 Spec -f
1f0bbb56 119 '$path_to_dist' cd
849d5e34
JH
120 README -ef
121 INSTALL -d || warn
1f0bbb56 122 '$path_to_libFile' -d || die
849d5e34
JH
123 };
124 };
125
3d47529a
NC
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 );
849d5e34
JH
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
3d47529a 146 like($@, qr/lib is not a plain file/);
849d5e34
JH
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
3d47529a
NC
162 like($@, qr/yadda lib yadda/);
163 is( $num_warnings, undef );
849d5e34 164}
96dbb1e1
MM
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
3d47529a 176 is ( $@, '', 'No errors mean we compile correctly');
96dbb1e1
MM
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
3d47529a
NC
188 like( $@, qr/syntax error/,
189 'We got a syntax error for a malformed file query' );
96dbb1e1 190}