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