From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 3CF561FFAF for ; Thu, 31 Dec 2020 13:51:57 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 24/36] spawn: move run_die here from PublicInbox::Import Date: Thu, 31 Dec 2020 13:51:42 +0000 Message-Id: <20201231135154.6070-25-e@80x24.org> In-Reply-To: <20201231135154.6070-1-e@80x24.org> References: <20201231135154.6070-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: It seems like a more logical place for it, but we'll favor the newly-added xsys_e() in tests for BAIL_OUT use. --- lib/PublicInbox/Import.pm | 9 +-------- lib/PublicInbox/LEI.pm | 5 ++--- lib/PublicInbox/Spawn.pm | 9 ++++++++- lib/PublicInbox/TestCommon.pm | 25 ++++++++++++++++++------- lib/PublicInbox/V2Writable.pm | 10 ++++------ script/public-inbox-init | 13 ++++++++----- t/convert-compact.t | 4 ++-- t/index-git-times.t | 3 +-- t/plack.t | 4 ++-- 9 files changed, 46 insertions(+), 36 deletions(-) diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm index 60cff9c2..6f387b77 100644 --- a/lib/PublicInbox/Import.pm +++ b/lib/PublicInbox/Import.pm @@ -9,7 +9,7 @@ package PublicInbox::Import; use strict; use parent qw(PublicInbox::Lock); use v5.10.1; -use PublicInbox::Spawn qw(spawn popen_rd); +use PublicInbox::Spawn qw(run_die popen_rd); use PublicInbox::MID qw(mids mid2path); use PublicInbox::Address; use PublicInbox::Smsg; @@ -442,13 +442,6 @@ sub add { $self->{tip} = ":$commit"; } -sub run_die ($;$$) { - my ($cmd, $env, $rdr) = @_; - my $pid = spawn($cmd, $env, $rdr); - waitpid($pid, 0) == $pid or die join(' ', @$cmd) .' did not finish'; - $? == 0 or die join(' ', @$cmd) . " failed: $?\n"; -} - my @INIT_FILES = ('HEAD' => undef, # filled in at runtime 'description' => <{'-f'}, @argv ]; my %rdr = map { $_ => $self->{$_} } (0..2); - require PublicInbox::Import; - PublicInbox::Import::run_die($cmd, $env, \%rdr); + run_die($cmd, $env, \%rdr); } sub lei_init { diff --git a/lib/PublicInbox/Spawn.pm b/lib/PublicInbox/Spawn.pm index cb16fcf6..5e2495d2 100644 --- a/lib/PublicInbox/Spawn.pm +++ b/lib/PublicInbox/Spawn.pm @@ -19,7 +19,7 @@ use strict; use parent qw(Exporter); use Symbol qw(gensym); use PublicInbox::ProcessPipe; -our @EXPORT_OK = qw/which spawn popen_rd nodatacow_dir/; +our @EXPORT_OK = qw(which spawn popen_rd run_die nodatacow_dir); our @RLIMITS = qw(RLIMIT_CPU RLIMIT_CORE RLIMIT_DATA); my $vfork_spawn = <<'VFORK_SPAWN'; @@ -306,4 +306,11 @@ sub popen_rd { $ret; } +sub run_die ($;$$) { + my ($cmd, $env, $rdr) = @_; + my $pid = spawn($cmd, $env, $rdr); + waitpid($pid, 0) == $pid or die "@$cmd did not finish"; + $? == 0 or die "@$cmd failed: \$?=$?\n"; +} + 1; diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm index 338e760c..b833984d 100644 --- a/lib/PublicInbox/TestCommon.pm +++ b/lib/PublicInbox/TestCommon.pm @@ -10,8 +10,14 @@ use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek); use POSIX qw(dup2); use IO::Socket::INET; our @EXPORT = qw(tmpdir tcp_server tcp_connect require_git require_mods - run_script start_script key2sub xsys xqx eml_load tick + run_script start_script key2sub xsys xsys_e xqx eml_load tick have_xapian_compact); +BEGIN { + require Test::More; + *BAIL_OUT = \&Test::More::BAIL_OUT; + *plan = \&Test::More::plan; + *skip = \&Test::More::skip; +} sub eml_load ($) { my ($path, $cb) = @_; @@ -38,7 +44,7 @@ sub tcp_server () { Type => Socket::SOCK_STREAM(), Listen => 1024, Blocking => 0, - ) or Test::More::BAIL_OUT("failed to create TCP server: $!"); + ) or BAIL_OUT "failed to create TCP server: $!"; } sub tcp_connect { @@ -49,7 +55,7 @@ sub tcp_connect { Type => Socket::SOCK_STREAM(), PeerAddr => $addr, %opt, - ) or Test::More::BAIL_OUT("failed to connect to $addr: $!"); + ) or BAIL_OUT "failed to connect to $addr: $!"; $s->autoflush(1); $s; } @@ -64,8 +70,8 @@ sub require_git ($;$) { my $cur_int = ($cur_maj << 24) | ($cur_min << 16) | ($cur_sub // 0); if ($cur_int < $req_int) { return 0 if $maybe; - Test::More::plan(skip_all => - "git $req+ required, have $cur_maj.$cur_min.$cur_sub"); + plan skip_all => + "git $req+ required, have $cur_maj.$cur_min.$cur_sub"; } 1; } @@ -113,8 +119,8 @@ sub require_mods { } return unless @need; my $m = join(', ', @need)." missing for $0"; - Test::More::skip($m, $maybe) if $maybe; - Test::More::plan(skip_all => $m) + skip($m, $maybe) if $maybe; + plan(skip_all => $m) } sub key2script ($) { @@ -323,6 +329,11 @@ sub xsys { $? >> 8 } +sub xsys_e { # like "/bin/sh -e" + xsys(@_) == 0 or + BAIL_OUT (ref $_[0] ? "@{$_[0]}" : "@_"). " failed \$?=$?" +} + # like `backtick` or qx{} op, but uses spawn() for env/rdr + vfork sub xqx { my ($cmd, $env, $rdr) = @_; diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm index edb8ba57..5a8caa08 100644 --- a/lib/PublicInbox/V2Writable.pm +++ b/lib/PublicInbox/V2Writable.pm @@ -16,7 +16,7 @@ use PublicInbox::ContentHash qw(content_hash content_digest); use PublicInbox::InboxWritable; use PublicInbox::OverIdx; use PublicInbox::Msgmap; -use PublicInbox::Spawn qw(spawn popen_rd); +use PublicInbox::Spawn qw(spawn popen_rd run_die); use PublicInbox::SearchIdx qw(log2stack crlf_adjust is_ancestor check_size is_bad_blob); use IO::Handle; # ->autoflush @@ -745,9 +745,8 @@ sub git_init { my ($self, $epoch) = @_; my $git_dir = "$self->{ibx}->{inboxdir}/git/$epoch.git"; PublicInbox::Import::init_bare($git_dir); - my @cmd = (qw/git config/, "--file=$git_dir/config", - 'include.path', '../../all.git/config'); - PublicInbox::Import::run_die(\@cmd); + run_die([qw(git config), "--file=$git_dir/config", + qw(include.path ../../all.git/config)]); fill_alternates($self, $epoch); $git_dir } @@ -1222,8 +1221,7 @@ sub unindex_todo ($$$) { return if $before == $after; # ensure any blob can not longer be accessed via dumb HTTP - PublicInbox::Import::run_die(['git', - "--git-dir=$unit->{git}->{git_dir}", + run_die(['git', "--git-dir=$unit->{git}->{git_dir}", qw(-c gc.reflogExpire=now gc --prune=all --quiet)]); } diff --git a/script/public-inbox-init b/script/public-inbox-init index 7ac77830..85d14377 100755 --- a/script/public-inbox-init +++ b/script/public-inbox-init @@ -185,17 +185,20 @@ $ibx->init_inbox(0, $skip_epoch, $skip_artnum); # needed for git prior to v2.1.0 umask(0077) if defined $perm; +require PublicInbox::Spawn; +PublicInbox::Spawn->import(qw(run_die)); + foreach my $addr (@address) { next if $seen{lc($addr)}; - PublicInbox::Import::run_die([@x, "--add", "$pfx.address", $addr]); + run_die([@x, "--add", "$pfx.address", $addr]); } -PublicInbox::Import::run_die([@x, "$pfx.url", $http_url]); -PublicInbox::Import::run_die([@x, "$pfx.inboxdir", $inboxdir]); +run_die([@x, "$pfx.url", $http_url]); +run_die([@x, "$pfx.inboxdir", $inboxdir]); if (defined($indexlevel)) { - PublicInbox::Import::run_die([@x, "$pfx.indexlevel", $indexlevel]); + run_die([@x, "$pfx.indexlevel", $indexlevel]); } -PublicInbox::Import::run_die([@x, "$pfx.newsgroup", $ng]) if $ng ne ''; +run_die([@x, "$pfx.newsgroup", $ng]) if $ng ne ''; # needed for git prior to v2.1.0 if (defined $perm) { diff --git a/t/convert-compact.t b/t/convert-compact.t index e479476d..fe8a7ec2 100644 --- a/t/convert-compact.t +++ b/t/convert-compact.t @@ -21,8 +21,8 @@ my $ibx = { PublicInbox::Import::init_bare($ibx->{inboxdir}); ok(umask(077), 'set restrictive umask'); -ok(PublicInbox::Import::run_die([qw(git) , "--git-dir=$ibx->{inboxdir}", - qw(config core.sharedRepository 0644)]), 'set sharedRepository'); +xsys_e(qw(git) , "--git-dir=$ibx->{inboxdir}", + qw(config core.sharedRepository 0644)); $ibx = PublicInbox::Inbox->new($ibx); my $im = PublicInbox::Import->new($ibx->git, undef, undef, $ibx); my $mime = PublicInbox::Eml->new(<<'EOF'); diff --git a/t/index-git-times.t b/t/index-git-times.t index f9869cfa..9b869f94 100644 --- a/t/index-git-times.t +++ b/t/index-git-times.t @@ -5,7 +5,6 @@ use strict; use v5.10.1; use Test::More; use PublicInbox::TestCommon; -use PublicInbox::Import; use PublicInbox::Config; use PublicInbox::Admin; use File::Path qw(remove_tree); @@ -48,7 +47,7 @@ EOF print $w $data or die; close $w or die; my $cmd = ['git', "--git-dir=$v1dir", 'fast-import', '--quiet']; - PublicInbox::Import::run_die($cmd, undef, { 0 => $r }); + xsys_e($cmd, undef, { 0 => $r }); } run_script(['-index', '--skip-docdata', $v1dir]) or die 'v1 index failed'; diff --git a/t/plack.t b/t/plack.t index 1fedf426..5d65cd05 100644 --- a/t/plack.t +++ b/t/plack.t @@ -21,8 +21,8 @@ ok(-f $psgi, "psgi example file found"); my $pfx = 'http://example.com/test'; ok(run_script(['-init', 'test', $inboxdir, "$pfx/", $addr]), 'initialized repo'); -PublicInbox::Import::run_die([qw(git config -f), $pi_config, - 'publicinbox.test.newsgroup', 'inbox.test']); +xsys_e(qw(git config -f), $pi_config, + qw(publicinbox.test.newsgroup inbox.test)); open my $fh, '>', "$inboxdir/description" or die "open: $!\n"; print $fh "test for public-inbox\n"; close $fh or die "close: $!\n";