+ $lei_opt = { 1 => \$lei_out, 2 => \$lei_err };
+ my ($daemon_pid, $for_destroy, $daemon_xrd);
+ my $tmpdir = $test_opt->{tmpdir};
+ File::Path::mkpath($tmpdir) if (defined $tmpdir && !-d $tmpdir);
+ ($tmpdir, $for_destroy) = tmpdir unless $tmpdir;
+ state $persist_xrd = $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
+ SKIP: {
+ $ENV{TEST_LEI_ONESHOT} and
+ xbail 'TEST_LEI_ONESHOT no longer supported';
+ my $home = "$tmpdir/lei-daemon";
+ mkdir($home, 0700) or BAIL_OUT "mkdir: $!";
+ local $ENV{HOME} = $home;
+ my $persist;
+ if ($persist_xrd && !$test_opt->{daemon_only}) {
+ $persist = $daemon_xrd = $persist_xrd;
+ } else {
+ $daemon_xrd = "$home/xdg_run";
+ mkdir($daemon_xrd, 0700) or BAIL_OUT "mkdir: $!";
+ }
+ local $ENV{XDG_RUNTIME_DIR} = $daemon_xrd;
+ $cb->();
+ if ($persist) { # remove before ~/.local gets removed
+ File::Path::rmtree([glob("$home/*")]);
+ File::Path::rmtree("$home/.config");
+ } else {
+ lei_ok(qw(daemon-pid), \"daemon-pid after $t");
+ chomp($daemon_pid = $lei_out);
+ if (!$daemon_pid) {
+ fail("daemon not running after $t");
+ skip 'daemon died unexpectedly', 2;
+ }
+ ok(kill(0, $daemon_pid), "daemon running after $t");
+ lei_ok(qw(daemon-kill), \"daemon-kill after $t");
+ }
+ }; # SKIP for lei_daemon
+ if ($daemon_pid) {
+ for (0..10) {
+ kill(0, $daemon_pid) or last;
+ tick;
+ }
+ ok(!kill(0, $daemon_pid), "$t daemon stopped");
+ my $f = "$daemon_xrd/lei/errors.log";
+ open my $fh, '<', $f or BAIL_OUT "$f: $!";
+ my @l = <$fh>;
+ is_xdeeply(\@l, [],
+ "$t daemon XDG_RUNTIME_DIR/lei/errors.log empty");
+ }
+}; # SKIP if missing git 2.6+ || Xapian || SQLite || json
+} # /test_lei
+
+# returns the pathname to a ~/.public-inbox/config in scalar context,
+# ($test_home, $pi_config_pathname) in list context
+sub setup_public_inboxes () {
+ my $test_home = "t/home2";
+ my $pi_config = "$test_home/.public-inbox/config";
+ my $stamp = "$test_home/setup-stamp";
+ my @ret = ($test_home, $pi_config);
+ return @ret if -f $stamp;
+
+ require PublicInbox::Lock;
+ my $lk = bless { lock_path => "$test_home/setup.lock" },
+ 'PublicInbox::Lock';
+ my $end = $lk->lock_for_scope;
+ return @ret if -f $stamp;
+
+ local $ENV{PI_CONFIG} = $pi_config;
+ for my $V (1, 2) {
+ run_script([qw(-init --skip-docdata), "-V$V",
+ '--newsgroup', "t.v$V", "t$V",
+ "$test_home/t$V", "http://example.com/t$V",
+ "t$V\@example.com" ]) or xbail "init v$V";
+ unlink "$test_home/t$V/description" or xbail "unlink $!";
+ }
+ require PublicInbox::Config;
+ require PublicInbox::InboxWritable;
+ my $cfg = PublicInbox::Config->new;
+ my $seen = 0;
+ $cfg->each_inbox(sub {
+ my ($ibx) = @_;
+ $ibx->{-no_fsync} = 1;
+ my $im = PublicInbox::InboxWritable->new($ibx)->importer(0);
+ my $V = $ibx->version;
+ my @eml = (glob('t/*.eml'), 't/data/0001.patch');
+ for (@eml) {
+ next if $_ eq 't/psgi_v2-old.eml'; # dup mid
+ $im->add(eml_load($_)) or BAIL_OUT "v$V add $_";
+ $seen++;
+ }
+ $im->done;
+ });
+ $seen or BAIL_OUT 'no imports';
+ open my $fh, '>', $stamp or BAIL_OUT "open $stamp: $!";
+ @ret;