This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Adapt properly More.t to run in the core
[perl5.git] / lib / File / CheckTree.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use Test;
9
10 BEGIN { plan tests => 8 }
11
12 use strict;
13
14 BEGIN {
15 # Cwd::cwd does an implicit "require Win32", but
16 # the ../lib directory in @INC will no longer work once
17 # we chdir() out of the "t" directory.
18     if ($^O eq 'MSWin32') {
19         require Win32;
20         Win32->import();
21     }
22 }
23
24 use File::CheckTree;
25 use File::Spec;          # used to get absolute paths
26
27 # We assume that we start from the perl "t" directory.
28 # Will move up one level to make it easier to generate
29 # reliable pathnames for testing File::CheckTree
30
31 chdir(File::Spec->updir) or die "cannot change to parent of t/ directory: $!";
32
33
34 #### TEST 1 -- No warnings ####
35 # usings both relative and full paths, indented comments
36
37 {
38     my ($num_warnings, $path_to_README);
39     $path_to_README = File::Spec->rel2abs('README');
40
41     my @warnings;
42     local $SIG{__WARN__} = sub { push @warnings, "@_" };
43
44     eval {
45         $num_warnings = validate qq{
46             lib  -d
47 # comment, followed "blank" line (w/ whitespace):
48            
49             # indented comment, followed blank line (w/o whitespace):
50
51             README -f
52             '$path_to_README' -e || warn
53         };
54     };
55
56     print STDERR $_ for @warnings;
57     if ( !$@ && !@warnings && defined($num_warnings) && $num_warnings == 0 ) {
58         ok(1);
59     }
60     else {
61         ok(0);
62     }
63 }
64
65
66 #### TEST 2 -- One warning ####
67
68 {
69     my ($num_warnings, @warnings);
70
71     local $SIG{__WARN__} = sub { push @warnings, "@_" };
72
73     eval {
74         $num_warnings = validate qq{
75             lib    -f
76             README -f
77         };
78     };
79
80     if ( !$@ && @warnings == 1
81              && $warnings[0] =~ /lib is not a plain file/
82              && defined($num_warnings)
83              && $num_warnings == 1 )
84     {
85         ok(1);
86     }
87     else {
88         ok(0);
89     }
90 }
91
92
93 #### TEST 3 -- Multiple warnings ####
94 # including first warning only from a bundle of tests,
95 # generic "|| warn", default "|| warn" and "|| warn '...' "
96
97 {
98     my ($num_warnings, @warnings);
99
100     local $SIG{__WARN__} = sub { push @warnings, "@_" };
101
102     eval {
103         $num_warnings = validate q{
104             lib     -effd
105             README -f || die
106             README -d || warn
107             lib    -f || warn "my warning: $file\n"
108         };
109     };
110
111     if ( !$@ && @warnings == 3
112              && $warnings[0] =~ /lib is not a plain file/
113              && $warnings[1] =~ /README is not a directory/
114              && $warnings[2] =~ /my warning: lib/
115              && defined($num_warnings)
116              && $num_warnings == 3 )
117     {
118         ok(1);
119     }
120     else {
121         ok(0);
122     }
123 }
124
125
126 #### TEST 4 -- cd directive ####
127 # cd directive followed by relative paths, followed by full paths
128 {
129     my ($num_warnings, @warnings, $path_to_libFile, $path_to_dist);
130     $path_to_libFile = File::Spec->rel2abs(File::Spec->catdir('lib','File'));
131     $path_to_dist    = File::Spec->rel2abs(File::Spec->curdir);
132
133     local $SIG{__WARN__} = sub { push @warnings, "@_" };
134
135     eval {
136         $num_warnings = validate qq{
137             lib                -d || die
138             '$path_to_libFile' cd
139             Spec               -e
140             Spec               -f
141             '$path_to_dist'    cd
142             README             -ef
143             INSTALL            -d || warn
144             '$path_to_libFile' -d || die
145         };
146     };
147
148     if ( !$@ && @warnings == 2
149              && $warnings[0] =~ /Spec is not a plain file/
150              && $warnings[1] =~ /INSTALL is not a directory/
151              && defined($num_warnings)
152              && $num_warnings == 2 )
153     {
154         ok(1);
155     }
156     else {
157         ok(0);
158     }
159 }
160
161
162 #### TEST 5 -- Exception ####
163 # test with generic "|| die"
164 {
165     my $num_warnings;
166
167     eval {
168         $num_warnings = validate q{
169             lib       -ef || die
170             README    -d
171         };
172     };
173
174     if ( $@ && $@ =~ /lib is not a plain file/
175             && not defined $num_warnings )
176     {
177         ok(1);
178     }
179     else {
180         ok(0);
181     }
182 }
183
184
185 #### TEST 6 -- Exception ####
186 # test with "|| die 'my error message'"
187 {
188     my $num_warnings;
189
190     eval {
191         $num_warnings = validate q{
192             lib       -ef || die "yadda $file yadda...\n"
193             README    -d
194         };
195     };
196
197     if ( $@ && $@ =~ /yadda lib yadda/
198             && not defined $num_warnings )
199     {
200         ok(1);
201     }
202     else {
203         ok(0);
204     }
205 }
206
207 #### TEST 7 -- Quoted file names ####
208 {
209     my $num_warnings;
210     eval {
211         $num_warnings = validate q{
212             "a file with whitespace" !-ef
213             'a file with whitespace' !-ef
214         };
215     };
216
217     if ( !$@ ) {
218         # No errors mean we compile correctly
219         ok(1);
220     } else {
221         ok(0);
222         print STDERR $@;
223     };
224 }
225
226 #### TEST 8 -- Malformed query ####
227 {
228     my $num_warnings;
229     eval {
230         $num_warnings = validate q{
231             a file with whitespace !-ef
232         };
233     };
234
235     if ( $@ =~ /syntax error/) {
236         # We got a syntax error for a malformed file query
237         ok(1);
238     } else {
239         ok(0);
240     };
241 }