This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don't load the Win32 module on non-win32 platforms
[perl5.git] / lib / File / CheckTree.t
CommitLineData
849d5e34 1#!./perl -w
1a3850a5
GA
2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
1a3850a5
GA
6}
7
849d5e34
JH
8use Test;
9
10BEGIN { plan tests => 6 }
11
12use strict;
1a3850a5 13
7cda84c2 14BEGIN {
cf2f24a4
JD
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.
7cda84c2
RGS
18 if ($^O eq 'MSWin32') {
19 require Win32;
20 Win32->import();
21 }
22}
cf2f24a4 23
1a3850a5 24use File::CheckTree;
849d5e34
JH
25use 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
86dc4f03 31chdir(File::Spec->updir) or die "cannot change to parent of t/ directory: $!";
849d5e34
JH
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 if ( !$@ && !@warnings && defined($num_warnings) && $num_warnings == 0 ) {
57 ok(1);
58 }
59 else {
60 ok(0);
61 }
62}
63
64
65#### TEST 2 -- One warning ####
66
67{
68 my ($num_warnings, @warnings);
69
70 local $SIG{__WARN__} = sub { push @warnings, "@_" };
71
72 eval {
73 $num_warnings = validate qq{
74 lib -f
75 README -f
76 };
77 };
78
79 if ( !$@ && @warnings == 1
80 && $warnings[0] =~ /lib is not a plain file/
81 && defined($num_warnings)
82 && $num_warnings == 1 )
83 {
84 ok(1);
85 }
86 else {
87 ok(0);
88 }
89}
90
1a3850a5 91
849d5e34
JH
92#### TEST 3 -- Multiple warnings ####
93# including first warning only from a bundle of tests,
94# generic "|| warn", default "|| warn" and "|| warn '...' "
1a3850a5 95
849d5e34
JH
96{
97 my ($num_warnings, @warnings);
1a3850a5 98
849d5e34
JH
99 local $SIG{__WARN__} = sub { push @warnings, "@_" };
100
101 eval {
102 $num_warnings = validate q{
103 lib -effd
104 README -f || die
105 README -d || warn
106 lib -f || warn "my warning: $file\n"
107 };
108 };
109
110 if ( !$@ && @warnings == 3
111 && $warnings[0] =~ /lib is not a plain file/
112 && $warnings[1] =~ /README is not a directory/
113 && $warnings[2] =~ /my warning: lib/
114 && defined($num_warnings)
115 && $num_warnings == 3 )
116 {
117 ok(1);
118 }
119 else {
120 ok(0);
121 }
122}
123
124
125#### TEST 4 -- cd directive ####
126# cd directive followed by relative paths, followed by full paths
127{
128 my ($num_warnings, @warnings, $path_to_libFile, $path_to_dist);
026a9d8a 129 $path_to_libFile = File::Spec->rel2abs(File::Spec->catdir('lib','File'));
849d5e34
JH
130 $path_to_dist = File::Spec->rel2abs(File::Spec->curdir);
131
132 local $SIG{__WARN__} = sub { push @warnings, "@_" };
133
134 eval {
135 $num_warnings = validate qq{
136 lib -d || die
137 $path_to_libFile cd
138 Spec -e
139 Spec -f
140 $path_to_dist cd
141 README -ef
142 INSTALL -d || warn
143 $path_to_libFile -d || die
144 };
145 };
146
147 if ( !$@ && @warnings == 2
148 && $warnings[0] =~ /Spec is not a plain file/
149 && $warnings[1] =~ /INSTALL is not a directory/
150 && defined($num_warnings)
151 && $num_warnings == 2 )
152 {
153 ok(1);
154 }
155 else {
156 ok(0);
157 }
158}
159
160
161#### TEST 5 -- Exception ####
162# test with generic "|| die"
163{
164 my $num_warnings;
165
166 eval {
167 $num_warnings = validate q{
168 lib -ef || die
169 README -d
170 };
171 };
172
173 if ( $@ && $@ =~ /lib is not a plain file/
174 && not defined $num_warnings )
175 {
176 ok(1);
177 }
178 else {
179 ok(0);
180 }
181}
182
183
184#### TEST 6 -- Exception ####
185# test with "|| die 'my error message'"
186{
187 my $num_warnings;
188
189 eval {
190 $num_warnings = validate q{
191 lib -ef || die "yadda $file yadda...\n"
192 README -d
193 };
194 };
195
196 if ( $@ && $@ =~ /yadda lib yadda/
197 && not defined $num_warnings )
198 {
199 ok(1);
200 }
201 else {
202 ok(0);
203 }
204}