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