package File::CheckTree;
-our $VERSION = '4.1';
-
use 5.006;
-require Exporter;
+use Exporter;
+use Cwd;
use warnings;
+use strict;
+
+our $VERSION = '4.2';
+our @ISA = qw(Exporter);
+our @EXPORT = qw(validate);
=head1 NAME
use File::CheckTree;
- $warnings += validate( q{
- /vmunix -e || die
- /boot -e || die
- /bin cd
- csh -ex
- csh !-ug
- sh -ex
- sh !-ug
- /usr -d || warn "What happened to $file?\n"
+ $num_warnings = validate( q{
+ /vmunix -e || die
+ /boot -e || die
+ /bin cd
+ csh -ex
+ csh !-ug
+ sh -ex
+ sh !-ug
+ /usr -d || warn "What happened to $file?\n"
});
=head1 DESCRIPTION
The validate() routine takes a single multiline string consisting of
-lines containing a filename plus a file test to try on it. (The
-file test may also be a "cd", causing subsequent relative filenames
+directives, each containing a filename plus a file test to try on it.
+(The file test may also be a "cd", causing subsequent relative filenames
to be interpreted relative to that directory.) After the file test
you may put C<|| die> to make it a fatal error if the file test fails.
The default is C<|| warn>. The file test may optionally have a "!' prepended
The routine returns the number of warnings issued.
+=head1 AUTHOR
+
+Unknown. Revised by Paul Grassie <F<grassie@perl.com>> in 2002.
+
+=head1 HISTORY
+
+File::CheckTree used to not display fatal error messages.
+It used to count only those warnings produced by a generic C<|| warn>
+(and not those in which the user supplied the message). In addition,
+the validate() routine would leave the user program in whatever
+directory was last entered through the use of "cd" directives.
+These bugs were fixed during the development of perl 5.8.
+The first fixed version of File::CheckTree was 4.2.
+
=cut
-our @ISA = qw(Exporter);
-our @EXPORT = qw(validate);
+my $Warnings;
sub validate {
- local($file,$test,$warnings,$oldwarnings);
- $warnings = 0;
- foreach $check (split(/\n/,$_[0])) {
- next if $check =~ /^#/;
- next if $check =~ /^$/;
- ($file,$test) = split(' ',$check,2);
- if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
- $testlist = $2;
- @testlist = split(//,$testlist);
- }
- else {
- @testlist = ('Z');
- }
- $oldwarnings = $warnings;
- foreach $one (@testlist) {
- $this = $test;
- $this =~ s/(-\w\b)/$1 \$file/g;
- $this =~ s/-Z/-$one/;
- $this .= ' || warn' unless $this =~ /\|\|/;
- $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 ||
- valmess('$2','$1')/;
- $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
- eval $this;
- last if $warnings > $oldwarnings;
- }
+ my ($starting_dir, $file, $test, $cwd, $oldwarnings);
+
+ $starting_dir = cwd;
+
+ $cwd = "";
+ $Warnings = 0;
+
+ foreach my $check (split /\n/, $_[0]) {
+ my ($testlist, @testlist);
+
+ # skip blanks/comments
+ next if $check =~ /^\s*#/ || $check =~ /^\s*$/;
+
+ # Todo:
+ # should probably check for invalid directives and die
+ # but earlier versions of File::CheckTree did not do this either
+
+ # split a line like "/foo -r || die"
+ # so that $file is "/foo", $test is "-rwx || die"
+ ($file, $test) = split(' ', $check, 2); # special whitespace split
+
+ # change a $test like "!-ug || die" to "!-Z || die",
+ # capturing the bundled tests (e.g. "ug") in $2
+ if ($test =~ s/ ^ (!?-) (\w{2,}) \b /$1Z/x) {
+ $testlist = $2;
+ # split bundled tests, e.g. "ug" to 'u', 'g'
+ @testlist = split(//, $testlist);
+ }
+ else {
+ # put in placeholder Z for stand-alone test
+ @testlist = ('Z');
+ }
+
+ # will compare these two later to stop on 1st warning w/in a bundle
+ $oldwarnings = $Warnings;
+
+ foreach my $one (@testlist) {
+ # examples of $test: "!-Z || die" or "-w || warn"
+ my $this = $test;
+
+ # expand relative $file to full pathname if preceded by cd directive
+ $file = $cwd . '/' . $file if $cwd && $file !~ m|^/|;
+
+ # put filename in after the test operator
+ $this =~ s/(-\w\b)/$1 "$file"/g;
+
+ # change the "-Z" representing a bundle with the $one test
+ $this =~ s/-Z/-$one/;
+
+ # if it's a "cd" directive...
+ if ($this =~ /^cd\b/) {
+ # add "|| die ..."
+ $this .= ' || die "cannot cd to $file\n"';
+ # expand "cd" directive with directory name
+ $this =~ s/\bcd\b/chdir(\$cwd = '$file')/;
+ }
+ else {
+ # add "|| warn" as a default disposition
+ $this .= ' || warn' unless $this =~ /\|\|/;
+
+ # change a generic ".. || die" or ".. || warn"
+ # to call valmess instead of die/warn directly
+ # valmess will look up the error message from %Val_Message
+ $this =~ s/ ^ ( (\S+) \s+ \S+ ) \s* \|\| \s* (die|warn) \s* $
+ /$1 || valmess('$3', '$2', '$file')/x;
+ }
+
+ {
+ # count warnings, either from valmess or '-r || warn "my msg"'
+ # also, call any pre-existing signal handler for __WARN__
+ my $orig_sigwarn = $SIG{__WARN__};
+ local $SIG{__WARN__} = sub {
+ ++$Warnings;
+ if ( $orig_sigwarn ) {
+ $orig_sigwarn->(@_);
+ }
+ else {
+ warn "@_";
+ }
+ };
+
+ # do the test
+ eval $this;
+
+ # re-raise an exception caused by a "... || die" test
+ if ($@) {
+ # in case of any cd directives, return from whence we came
+ if ($starting_dir ne cwd) {
+ chdir($starting_dir) || die "$starting_dir: $!";
+ }
+ die $@ if $@;
+ }
+ }
+
+ # stop on 1st warning within a bundle of tests
+ last if $Warnings > $oldwarnings;
+ }
}
- $warnings;
+
+ # in case of any cd directives, return from whence we came
+ if ($starting_dir ne cwd) {
+ chdir($starting_dir) || die "$starting_dir: $!";
+ }
+
+ return $Warnings;
}
-our %Val_Switch = (
- 'r' => sub { "$_[0] is not readable by uid $>." },
- 'w' => sub { "$_[0] is not writable by uid $>." },
- 'x' => sub { "$_[0] is not executable by uid $>." },
- 'o' => sub { "$_[0] is not owned by uid $>." },
- 'R' => sub { "$_[0] is not readable by you." },
- 'W' => sub { "$_[0] is not writable by you." },
- 'X' => sub { "$_[0] is not executable by you." },
- 'O' => sub { "$_[0] is not owned by you." },
- 'e' => sub { "$_[0] does not exist." },
- 'z' => sub { "$_[0] does not have zero size." },
- 's' => sub { "$_[0] does not have non-zero size." },
- 'f' => sub { "$_[0] is not a plain file." },
- 'd' => sub { "$_[0] is not a directory." },
- 'l' => sub { "$_[0] is not a symbolic link." },
- 'p' => sub { "$_[0] is not a named pipe (FIFO)." },
- 'S' => sub { "$_[0] is not a socket." },
- 'b' => sub { "$_[0] is not a block special file." },
- 'c' => sub { "$_[0] is not a character special file." },
- 'u' => sub { "$_[0] does not have the setuid bit set." },
- 'g' => sub { "$_[0] does not have the setgid bit set." },
- 'k' => sub { "$_[0] does not have the sticky bit set." },
- 'T' => sub { "$_[0] is not a text file." },
- 'B' => sub { "$_[0] is not a binary file." },
+my %Val_Message = (
+ 'r' => "is not readable by uid $>.",
+ 'w' => "is not writable by uid $>.",
+ 'x' => "is not executable by uid $>.",
+ 'o' => "is not owned by uid $>.",
+ 'R' => "is not readable by you.",
+ 'W' => "is not writable by you.",
+ 'X' => "is not executable by you.",
+ 'O' => "is not owned by you.",
+ 'e' => "does not exist.",
+ 'z' => "does not have zero size.",
+ 's' => "does not have non-zero size.",
+ 'f' => "is not a plain file.",
+ 'd' => "is not a directory.",
+ 'l' => "is not a symbolic link.",
+ 'p' => "is not a named pipe (FIFO).",
+ 'S' => "is not a socket.",
+ 'b' => "is not a block special file.",
+ 'c' => "is not a character special file.",
+ 'u' => "does not have the setuid bit set.",
+ 'g' => "does not have the setgid bit set.",
+ 'k' => "does not have the sticky bit set.",
+ 'T' => "is not a text file.",
+ 'B' => "is not a binary file."
);
sub valmess {
- my($disposition,$this) = @_;
- my $file = $cwd . '/' . $file unless $file =~ m|^/|s;
-
+ my ($disposition, $test, $file) = @_;
my $ferror;
- if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
- my($neg,$ftype) = ($1,$2);
- $ferror = $Val_Switch{$tmp}->($file);
+ if ($test =~ / ^ (!?) -(\w) \s* $ /x) {
+ my ($neg, $ftype) = ($1, $2);
- if ($neg eq '!') {
- $ferror =~ s/ is not / should not be / ||
- $ferror =~ s/ does not / should not / ||
- $ferror =~ s/ not / /;
- }
+ $ferror = "$file $Val_Message{$ftype}";
+
+ if ($neg eq '!') {
+ $ferror =~ s/ is not / should not be / ||
+ $ferror =~ s/ does not / should not / ||
+ $ferror =~ s/ not / /;
+ }
}
else {
- $this =~ s/\$file/'$file'/g;
- $ferror = "Can't do $this.\n";
+ $ferror = "Can't do $test $file.\n";
}
+
die "$ferror\n" if $disposition eq 'die';
warn "$ferror\n";
- ++$warnings;
}
1;
-
-#!./perl
+#!./perl -w
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
}
-print "1..1\n";
+use Test;
+
+BEGIN { plan tests => 6 }
+
+use strict;
use File::CheckTree;
+use File::Spec; # used to get absolute paths
+
+# We assume that we start from the perl "t" directory.
+# Will move up one level to make it easier to generate
+# reliable pathnames for testing File::CheckTree
+
+chdir('..') or die "cannot change to parent of t/ directory: $!";
+
+
+#### TEST 1 -- No warnings ####
+# usings both relative and full paths, indented comments
+
+{
+ my ($num_warnings, $path_to_README);
+ $path_to_README = File::Spec->rel2abs('README');
+
+ my @warnings;
+ local $SIG{__WARN__} = sub { push @warnings, "@_" };
+
+ eval {
+ $num_warnings = validate qq{
+ lib -d
+# comment, followed "blank" line (w/ whitespace):
+
+ # indented comment, followed blank line (w/o whitespace):
+
+ README -f
+ $path_to_README -e || warn
+ };
+ };
+
+ if ( !$@ && !@warnings && defined($num_warnings) && $num_warnings == 0 ) {
+ ok(1);
+ }
+ else {
+ ok(0);
+ }
+}
+
+
+#### TEST 2 -- One warning ####
+
+{
+ my ($num_warnings, @warnings);
+
+ local $SIG{__WARN__} = sub { push @warnings, "@_" };
+
+ eval {
+ $num_warnings = validate qq{
+ lib -f
+ README -f
+ };
+ };
+
+ if ( !$@ && @warnings == 1
+ && $warnings[0] =~ /lib is not a plain file/
+ && defined($num_warnings)
+ && $num_warnings == 1 )
+ {
+ ok(1);
+ }
+ else {
+ ok(0);
+ }
+}
+
-# We assume that we run from the perl "t" directory.
+#### TEST 3 -- Multiple warnings ####
+# including first warning only from a bundle of tests,
+# generic "|| warn", default "|| warn" and "|| warn '...' "
-validate q{
- lib -d || die
- TEST -f || die
-};
+{
+ my ($num_warnings, @warnings);
-print "ok 1\n";
+ local $SIG{__WARN__} = sub { push @warnings, "@_" };
+
+ eval {
+ $num_warnings = validate q{
+ lib -effd
+ README -f || die
+ README -d || warn
+ lib -f || warn "my warning: $file\n"
+ };
+ };
+
+ if ( !$@ && @warnings == 3
+ && $warnings[0] =~ /lib is not a plain file/
+ && $warnings[1] =~ /README is not a directory/
+ && $warnings[2] =~ /my warning: lib/
+ && defined($num_warnings)
+ && $num_warnings == 3 )
+ {
+ ok(1);
+ }
+ else {
+ ok(0);
+ }
+}
+
+
+#### TEST 4 -- cd directive ####
+# cd directive followed by relative paths, followed by full paths
+{
+ my ($num_warnings, @warnings, $path_to_libFile, $path_to_dist);
+ $path_to_libFile = File::Spec->rel2abs('lib/File');
+ $path_to_dist = File::Spec->rel2abs(File::Spec->curdir);
+
+ local $SIG{__WARN__} = sub { push @warnings, "@_" };
+
+ eval {
+ $num_warnings = validate qq{
+ lib -d || die
+ $path_to_libFile cd
+ Spec -e
+ Spec -f
+ $path_to_dist cd
+ README -ef
+ INSTALL -d || warn
+ $path_to_libFile -d || die
+ };
+ };
+
+ if ( !$@ && @warnings == 2
+ && $warnings[0] =~ /Spec is not a plain file/
+ && $warnings[1] =~ /INSTALL is not a directory/
+ && defined($num_warnings)
+ && $num_warnings == 2 )
+ {
+ ok(1);
+ }
+ else {
+ ok(0);
+ }
+}
+
+
+#### TEST 5 -- Exception ####
+# test with generic "|| die"
+{
+ my $num_warnings;
+
+ eval {
+ $num_warnings = validate q{
+ lib -ef || die
+ README -d
+ };
+ };
+
+ if ( $@ && $@ =~ /lib is not a plain file/
+ && not defined $num_warnings )
+ {
+ ok(1);
+ }
+ else {
+ ok(0);
+ }
+}
+
+
+#### TEST 6 -- Exception ####
+# test with "|| die 'my error message'"
+{
+ my $num_warnings;
+
+ eval {
+ $num_warnings = validate q{
+ lib -ef || die "yadda $file yadda...\n"
+ README -d
+ };
+ };
+
+ if ( $@ && $@ =~ /yadda lib yadda/
+ && not defined $num_warnings )
+ {
+ ok(1);
+ }
+ else {
+ ok(0);
+ }
+}