This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: New harness is failing things
[perl5.git] / lib / File / CheckTree.pm
CommitLineData
a0d0e21e
LW
1package File::CheckTree;
2require 5.000;
3require Exporter;
4
f06db76b
AD
5=head1 NAME
6
7validate - run many filetest checks on a tree
8
9=head1 SYNOPSIS
10
11 use File::CheckTree;
12
13 $warnings += validate( q{
14 /vmunix -e || die
15 /boot -e || die
16 /bin cd
17 csh -ex
18 csh !-ug
19 sh -ex
20 sh !-ug
21 /usr -d || warn "What happened to $file?\n"
22 });
23
24=head1 DESCRIPTION
25
26The validate() routine takes a single multiline string consisting of
27lines containing a filename plus a file test to try on it. (The
28file test may also be a "cd", causing subsequent relative filenames
29to be interpreted relative to that directory.) After the file test
30you may put C<|| die> to make it a fatal error if the file test fails.
31The default is C<|| warn>. The file test may optionally have a "!' prepended
32to test for the opposite condition. If you do a cd and then list some
33relative filenames, you may want to indent them slightly for readability.
34If you supply your own die() or warn() message, you can use $file to
35interpolate the filename.
36
37Filetests may be bunched: "-rwx" tests for all of C<-r>, C<-w>, and C<-x>.
38Only the first failed test of the bunch will produce a warning.
39
40The routine returns the number of warnings issued.
41
42=cut
43
a0d0e21e
LW
44@ISA = qw(Exporter);
45@EXPORT = qw(validate);
46
47# $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
48
49# The validate routine takes a single multiline string consisting of
50# lines containing a filename plus a file test to try on it. (The
51# file test may also be a 'cd', causing subsequent relative filenames
52# to be interpreted relative to that directory.) After the file test
53# you may put '|| die' to make it a fatal error if the file test fails.
54# The default is '|| warn'. The file test may optionally have a ! prepended
55# to test for the opposite condition. If you do a cd and then list some
56# relative filenames, you may want to indent them slightly for readability.
57# If you supply your own "die" or "warn" message, you can use $file to
58# interpolate the filename.
59
60# Filetests may be bunched: -rwx tests for all of -r, -w and -x.
61# Only the first failed test of the bunch will produce a warning.
62
63# The routine returns the number of warnings issued.
64
65# Usage:
66# use File::CheckTree;
67# $warnings += validate('
68# /vmunix -e || die
69# /boot -e || die
70# /bin cd
71# csh -ex
72# csh !-ug
73# sh -ex
74# sh !-ug
75# /usr -d || warn "What happened to $file?\n"
76# ');
77
78sub validate {
79 local($file,$test,$warnings,$oldwarnings);
80 foreach $check (split(/\n/,$_[0])) {
81 next if $check =~ /^#/;
82 next if $check =~ /^$/;
83 ($file,$test) = split(' ',$check,2);
84 if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
85 $testlist = $2;
86 @testlist = split(//,$testlist);
87 }
88 else {
89 @testlist = ('Z');
90 }
91 $oldwarnings = $warnings;
92 foreach $one (@testlist) {
93 $this = $test;
94 $this =~ s/(-\w\b)/$1 \$file/g;
95 $this =~ s/-Z/-$one/;
96 $this .= ' || warn' unless $this =~ /\|\|/;
97 $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || valmess('$2','$1')/;
98 $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
99 eval $this;
100 last if $warnings > $oldwarnings;
101 }
102 }
103 $warnings;
104}
105
106sub valmess {
107 local($disposition,$this) = @_;
1b1e14d3 108 $file = $cwd . '/' . $file unless $file =~ m|^/|s;
a0d0e21e
LW
109 if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
110 $neg = $1;
111 $tmp = $2;
112 $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
113 $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
114 $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
115 $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
116 $tmp eq 'R' && ($mess = "$file is not readable by you.");
117 $tmp eq 'W' && ($mess = "$file is not writable by you.");
118 $tmp eq 'X' && ($mess = "$file is not executable by you.");
119 $tmp eq 'O' && ($mess = "$file is not owned by you.");
120 $tmp eq 'e' && ($mess = "$file does not exist.");
121 $tmp eq 'z' && ($mess = "$file does not have zero size.");
122 $tmp eq 's' && ($mess = "$file does not have non-zero size.");
123 $tmp eq 'f' && ($mess = "$file is not a plain file.");
124 $tmp eq 'd' && ($mess = "$file is not a directory.");
125 $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
126 $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
127 $tmp eq 'S' && ($mess = "$file is not a socket.");
128 $tmp eq 'b' && ($mess = "$file is not a block special file.");
129 $tmp eq 'c' && ($mess = "$file is not a character special file.");
130 $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
131 $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
132 $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
133 $tmp eq 'T' && ($mess = "$file is not a text file.");
134 $tmp eq 'B' && ($mess = "$file is not a binary file.");
135 if ($neg eq '!') {
136 $mess =~ s/ is not / should not be / ||
137 $mess =~ s/ does not / should not / ||
138 $mess =~ s/ not / /;
139 }
a0d0e21e
LW
140 }
141 else {
142 $this =~ s/\$file/'$file'/g;
9b599b2a 143 $mess = "Can't do $this.\n";
a0d0e21e 144 }
9b599b2a
GS
145 die "$mess\n" if $disposition eq 'die';
146 warn "$mess\n";
a0d0e21e
LW
147 ++$warnings;
148}
149
1501;
151