First patch from:
[p5sagit/p5-mst-13.2.git] / lib / File / CheckTree.t
CommitLineData
849d5e34 1#!./perl -w
1a3850a5 2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
1a3850a5 6}
7
849d5e34 8use Test;
9
10BEGIN { plan tests => 6 }
11
12use strict;
1a3850a5 13
cf2f24a4 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.
17use Win32;
18
1a3850a5 19use File::CheckTree;
849d5e34 20use 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
86dc4f03 26chdir(File::Spec->updir) or die "cannot change to parent of t/ directory: $!";
849d5e34 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
1a3850a5 86
849d5e34 87#### TEST 3 -- Multiple warnings ####
88# including first warning only from a bundle of tests,
89# generic "|| warn", default "|| warn" and "|| warn '...' "
1a3850a5 90
849d5e34 91{
92 my ($num_warnings, @warnings);
1a3850a5 93
849d5e34 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);
026a9d8a 124 $path_to_libFile = File::Spec->rel2abs(File::Spec->catdir('lib','File'));
849d5e34 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}