]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: test some likely errors due to misuse
authorEric Wong <e@80x24.org>
Tue, 19 Jan 2021 09:34:34 +0000 (09:34 +0000)
committerEric Wong <e@80x24.org>
Thu, 21 Jan 2021 03:29:13 +0000 (03:29 +0000)
Because user errors happen...

lib/PublicInbox/LeiOverview.pm
lib/PublicInbox/LeiToMail.pm
lib/PublicInbox/LeiXSearch.pm
t/lei.t

index 8781259addfb8bb33440dc7c337f4f1f15a59a7a..a7021b03d9246da2c4b584a0353ec73f58f17b40 100644 (file)
@@ -82,7 +82,8 @@ sub new {
        if (!$json) {
                # default to the cheapest sort since MUA usually resorts
                $lei->{opt}->{'sort'} //= 'docid' if $dst ne '/dev/stdout';
-               $lei->{l2m} = PublicInbox::LeiToMail->new($lei);
+               $lei->{l2m} = eval { PublicInbox::LeiToMail->new($lei) };
+               return $lei->fail($@) if $@;
        }
        $lei->{dedupe} //= PublicInbox::LeiDedupe->new($lei);
        $self;
index a6e517ea50ee3da25b6aa30c7c20a15a37bf6a02..49b5c8ab4cbdb3cb04480725dee6e7473c38cbfd 100644 (file)
@@ -339,8 +339,12 @@ sub new {
        my $self = bless {}, $cls;
        if ($fmt eq 'maildir') {
                $self->{base_type} = 'maildir';
+               -e $dst && !-d _ and die
+                               "$dst exists and is not a directory\n";
                $lei->{ovv}->{dst} = $dst .= '/' if substr($dst, -1) ne '/';
        } elsif (substr($fmt, 0, 4) eq 'mbox') {
+               -e $dst && !-f _ && !-p _ and die
+                               "$dst exists and is not a regular file\n";
                $self->can("eml2$fmt") or die "bad mbox --format=$fmt\n";
                $self->{base_type} = 'mbox';
        } else {
@@ -374,7 +378,7 @@ sub _post_augment_maildir {
                my $d = $dst.$x;
                next if -d $d;
                require File::Path;
-               File::Path::mkpath($d) or die "mkpath($d): $!";
+               File::Path::mkpath($d);
                -d $d or die "$d is not a directory";
        }
 }
index 002791c21b73075448c3fed56a428d1126ebd0f4..fa37543f32625d21fa25f4410a026f7467e30363 100644 (file)
@@ -201,7 +201,14 @@ sub query_done { # EOF callback
 sub do_post_augment {
        my ($lei, $zpipe, $au_done) = @_;
        my $l2m = $lei->{l2m} or die 'BUG: no {l2m}';
-       $l2m->post_augment($lei, $zpipe);
+       eval { $l2m->post_augment($lei, $zpipe) };
+       if (my $err = $@) {
+               if (my $lxs = delete $lei->{lxs}) {
+                       $lxs->wq_kill;
+                       $lxs->wq_close;
+               }
+               $lei->fail("$err");
+       }
        close $au_done; # triggers wait_startq
 }
 
diff --git a/t/lei.t b/t/lei.t
index c804ff5916c5378e0795b3f8db4bf9571899e178..8bb4e439bfadbbc98171155e10a5317ab2528c20 100644 (file)
--- a/t/lei.t
+++ b/t/lei.t
@@ -181,6 +181,20 @@ my $test_external = sub {
        $lei->('ls-external');
        like($out, qr/boost=0\n/s, 'ls-external has output');
 
+       ok(!$lei->(qw(q s:prefix -o /dev/null -f maildir)), 'bad maildir');
+       like($err, qr!/dev/null exists and is not a directory!,
+               'error shown');
+       is($? >> 8, 1, 'errored out with exit 1');
+
+       ok(!$lei->(qw(q s:prefix -f mboxcl2 -o), $home), 'bad mbox');
+       like($err, qr!\Q$home\E exists and is not a regular file!,
+               'error shown');
+       is($? >> 8, 1, 'errored out with exit 1');
+
+       ok(!$lei->(qw(q s:prefix -o /dev/stdout -f Mbox2)), 'bad format');
+       like($err, qr/bad mbox --format=mbox2/, 'error shown');
+       is($? >> 8, 1, 'errored out with exit 1');
+
        # note, on a Bourne shell users should be able to use either:
        #       s:"use boolean prefix"
        #       "s:use boolean prefix"