]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/lei.t
lei: drop $SIG{__DIE__}, add oneshot fallbacks
[public-inbox.git] / t / lei.t
diff --git a/t/lei.t b/t/lei.t
index b09439620b6cf52c854c457af735335b2fed4a9b..cce90fff89c62c5bbaf5d30d03db24a65844de21 100644 (file)
--- a/t/lei.t
+++ b/t/lei.t
@@ -9,13 +9,18 @@ use PublicInbox::Config;
 use File::Path qw(rmtree);
 require_mods(qw(json DBD::SQLite Search::Xapian));
 my $LEI = 'lei';
+my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
 my $lei = sub {
        my ($cmd, $env, $opt) = @_;
+       $out = $err = '';
+       if (!ref($cmd)) {
+               ($env, $opt) = grep { (!defined) || ref } @_;
+               $cmd = [ grep { defined } @_ ];
+       }
        run_script([$LEI, @$cmd], $env, $opt);
 };
 
 my ($home, $for_destroy) = tmpdir();
-my $opt = { 1 => \(my $out = ''), 2 => \(my $err = '') };
 delete local $ENV{XDG_DATA_HOME};
 delete local $ENV{XDG_CONFIG_HOME};
 local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
@@ -23,10 +28,7 @@ local $ENV{HOME} = $home;
 local $ENV{FOO} = 'BAR';
 mkdir "$home/xdg_run", 0700 or BAIL_OUT "mkdir: $!";
 my $home_trash = [ "$home/.local", "$home/.config" ];
-my $cleanup = sub {
-       rmtree([@$home_trash, @_]);
-       $out = $err = '';
-};
+my $cleanup = sub { rmtree([@$home_trash, @_]) };
 
 my $test_help = sub {
        ok(!$lei->([], undef, $opt), 'no args fails');
@@ -48,6 +50,21 @@ my $test_help = sub {
                isnt($err, '', 'something in stderr');
                is($out, '', 'nothing in stdout');
        }
+       ok($lei->(qw(init -h), undef, $opt), 'init -h');
+       like($out, qr! \Q$home\E/\.local/share/lei/store\b!,
+               'actual path shown in init -h');
+       ok($lei->(qw(init -h), { XDG_DATA_HOME => '/XDH' }, $opt),
+               'init with XDG_DATA_HOME');
+       like($out, qr! /XDH/lei/store\b!, 'XDG_DATA_HOME in init -h');
+       is($err, '', 'no errors from init -h');
+
+       ok($lei->(qw(config -h), undef, $opt), 'config-h');
+       like($out, qr! \Q$home\E/\.config/lei/config\b!,
+               'actual path shown in config -h');
+       ok($lei->(qw(config -h), { XDG_CONFIG_HOME => '/XDC' }, $opt),
+               'config with XDG_CONFIG_HOME');
+       like($out, qr! /XDC/lei/config\b!, 'XDG_CONFIG_HOME in config -h');
+       is($err, '', 'no errors from config -h');
 };
 
 my $ok_err_info = sub {
@@ -110,7 +127,7 @@ my $test_lei_common = sub {
 my $test_lei_oneshot = $ENV{TEST_LEI_ONESHOT};
 SKIP: {
        last SKIP if $test_lei_oneshot;
-       require_mods('IO::FDPass', 16);
+       require_mods(qw(IO::FDPass Cwd), 41);
        my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/sock";
 
        ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid');
@@ -171,7 +188,34 @@ SKIP: {
        chomp(my $new_pid = $out);
        ok(kill(0, $new_pid), 'new pid is running');
        ok(-S $sock, 'sock exists again');
-       unlink $sock or BAIL_OUT "unlink $!";
+
+       if ('socket inaccessible') {
+               chmod 0000, $sock or BAIL_OUT "chmod 0000: $!";
+               $out = $err = '';
+               ok(run_script([qw(lei help)], undef, $opt),
+                       'connect fail, one-shot fallback works');
+               like($err, qr/\bconnect\(/, 'connect error noted');
+               like($out, qr/^usage: /, 'help output works');
+               chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
+       }
+       if ('oneshot on cwd gone') {
+               my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
+               my $d = "$home/to-be-removed";
+               mkdir $d or BAIL_OUT "mkdir($d) $!";
+               chdir $d or BAIL_OUT "chdir($d) $!";
+               if (rmdir($d)) {
+                       $out = $err = '';
+                       ok(run_script([qw(lei help)], undef, $opt),
+                               'cwd fail, one-shot fallback works');
+               } else {
+                       $err = "rmdir=$!";
+               }
+               chdir $cwd or BAIL_OUT "chdir($cwd) $!";
+               like($err, qr/cwd\(/, 'cwd error noted');
+               like($out, qr/^usage: /, 'help output still works');
+       }
+
+       unlink $sock or BAIL_OUT "unlink($sock) $!";
        for (0..100) {
                kill('CHLD', $new_pid) or last;
                tick();