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