From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 96A821F87C for ; Thu, 2 Nov 2023 09:35:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1698917744; bh=SfaPhSs1DpS7hd/bfQodoHGs71+HdI3ikxg6EKWrhhg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=JTy36Eex2X7VTrMHtdYVN0u3AP6NSGPXfN2b3KIKgNLif6N1shDrRVdfOmLe+y2OD w0gb90UGVp4v7Ml19jJuYoi4xVNUymOb+A/4RPGNyXEsjT75qzIW8ixwqdAZFD4Iif VLeo5yV4w4hrVKb8pvza6AGeU7FdTWhJIYs+AISg= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 14/14] t/cindex+extsearch: use write_file, autodie, etc. Date: Thu, 2 Nov 2023 09:35:39 +0000 Message-Id: <20231102093539.2067470-15-e@80x24.org> In-Reply-To: <20231102093539.2067470-1-e@80x24.org> References: <20231102093539.2067470-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: write_file is a new API which makes setting up config files more pleasant, while autodie and scalarref redirects (in tests) have been available for a while, now. So do what we can to reduce the code burden we have. --- t/cindex.t | 15 ++++----------- t/extsearch.t | 48 ++++++++++++++---------------------------------- 2 files changed, 18 insertions(+), 45 deletions(-) diff --git a/t/cindex.t b/t/cindex.t index 2033945e..60711492 100644 --- a/t/cindex.t +++ b/t/cindex.t @@ -8,7 +8,7 @@ use List::Util qw(sum); use autodie qw(close open rename); require_mods(qw(json Xapian)); use_ok 'PublicInbox::CodeSearchIdx'; -require PublicInbox::Import; +use PublicInbox::Import; my ($tmp, $for_destroy) = tmpdir(); my $pwd = getcwd(); my @unused_keys = qw(last_commit has_threadid skip_docdata); @@ -45,7 +45,6 @@ ok(run_script([qw(-cindex --dangerous -q), "$tmp/wt0"]), 'cindex internal'); # it's possible for git to emit NUL characters in diffs # (see c4201214cbf10636e2c1ab9131573f735b42c8d4 in linux.git) my $zp = create_coderepo 'NUL in patch', sub { - require PublicInbox::Git; my $src = PublicInbox::IO::try_cat("$pwd/COPYING"); xsys_e([qw(git init -q)]); @@ -53,17 +52,13 @@ my $zp = create_coderepo 'NUL in patch', sub { $src =~ s/\b(Limitation of Liability\.)\n\n/$1\n\0\n/s or xbail "BUG: no `\\n\\n' in $pwd/COPYING"; - open my $fh, '>', 'f'; - print $fh $src or xbail "print: $!"; - close $fh; + PublicInbox::IO::write_file '>', 'f', $src; xsys_e([qw(/bin/sh -c), <<'EOM']); git add f && git commit -q -m 'initial with NUL character' EOM $src =~ s/\n\0\n/\n\n/ or xbail "BUG: no `\\n\\0\\n'"; - open $fh, '>', 'f'; - print $fh $src or xbail "print: $!"; - close $fh; + PublicInbox::IO::write_file '>', 'f', $src; xsys_e([qw(/bin/sh -c), <<'EOM']); git add f && git commit -q -m 'remove NUL character' && @@ -207,13 +202,11 @@ my $basic = create_inbox 'basic', indexlevel => 'basic', sub { }; { my $env = { PI_CONFIG => "$tmp/pi_config" }; - open my $fh, '>', $env->{PI_CONFIG}; - print $fh <', $env->{PI_CONFIG}, <{inboxdir} address = basic\@example.com EOM - close $fh; my $cmd = [ qw(-cindex -u --all --associate -d), "$tmp/ext", '-I', $basic->{inboxdir} ]; $cidx_out = $cidx_err = ''; diff --git a/t/extsearch.t b/t/extsearch.t index 2995cf95..1a1eb350 100644 --- a/t/extsearch.t +++ b/t/extsearch.t @@ -5,9 +5,9 @@ use v5.12; use PublicInbox::TestCommon; use PublicInbox::Config; use PublicInbox::InboxWritable; -use Fcntl qw(:seek); require_git(2.6); require_mods(qw(json DBD::SQLite Xapian)); +use autodie qw(open rename truncate); require PublicInbox::Search; use_ok 'PublicInbox::ExtSearch'; use_ok 'PublicInbox::ExtSearchIdx'; @@ -16,12 +16,10 @@ my ($home, $for_destroy) = tmpdir(); local $ENV{HOME} = $home; mkdir "$home/.public-inbox" or BAIL_OUT $!; my $cfg_path = "$home/.public-inbox/config"; -open my $fh, '>', $cfg_path or BAIL_OUT $!; -print $fh <', $cfg_path, < $v2addr }; my $eml = eml_load('t/utf8.eml'); $eml->header_set('List-Id', ''); -open($fh, '+>', undef) or BAIL_OUT $!; -$fh->autoflush(1); -print $fh $eml->as_string or BAIL_OUT $!; -seek($fh, 0, SEEK_SET) or BAIL_OUT $!; -run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda'; +my $in = \($eml->as_string); +run_script(['-mda', '--no-precheck'], $env, { 0 => $in }) or BAIL_OUT '-mda'; ok(run_script([qw(-init -V1 v1test --newsgroup v1.example), "$home/v1test", 'http://example.com/v1test', $v1addr ]), 'v1test init'); $eml->header_set('List-Id', ''); -seek($fh, 0, SEEK_SET) or BAIL_OUT $!; -truncate($fh, 0) or BAIL_OUT $!; -print $fh $eml->as_string or BAIL_OUT $!; -seek($fh, 0, SEEK_SET) or BAIL_OUT $!; +$in = \$eml->as_string; $env = { ORIGINAL_RECIPIENT => $v1addr }; -run_script(['-mda', '--no-precheck'], $env, { 0 => $fh }) or BAIL_OUT '-mda'; +run_script(['-mda', '--no-precheck'], $env, { 0 => $in }) or BAIL_OUT '-mda'; run_script([qw(-index -Lbasic), "$home/v1test"]) or BAIL_OUT "index $?"; @@ -103,14 +95,11 @@ if ('with boost') { } { # TODO: -extindex should write this to config - open $fh, '>>', $cfg_path or BAIL_OUT $!; - print $fh <>', $cfg_path, <ALL [extindex "all"] topdir = $home/extindex EOF - close $fh or BAIL_OUT $!; - my $pi_cfg = PublicInbox::Config->new; $pi_cfg->fill_all; ok($pi_cfg->ALL, '->ALL'); @@ -202,11 +191,7 @@ if ('inbox edited') { is_deeply($res, $exp, 'isearch limited results'); $pi_cfg = $res = $exp = undef; - open my $rmfh, '+>', undef or BAIL_OUT $!; - $rmfh->autoflush(1); - print $rmfh $eml2->as_string or BAIL_OUT $!; - seek($rmfh, 0, SEEK_SET) or BAIL_OUT $!; - $opt->{0} = $rmfh; + $opt->{0} = \($eml2->as_string); ok(run_script([qw(-learn rm --all)], undef, $opt), '-learn rm'); ok(run_script([qw(-extindex --all), "$home/extindex"], undef, undef), @@ -245,13 +230,11 @@ if ('inject w/o indexing') { isnt($tip, $cmt, '0.git v2 updated'); # inject a message w/o updating index - rename("$home/v1test/public-inbox", "$home/v1test/skip-index") or - BAIL_OUT $!; - open(my $eh, '<', 't/iso-2202-jp.eml') or BAIL_OUT $!; + rename("$home/v1test/public-inbox", "$home/v1test/skip-index"); + open(my $eh, '<', 't/iso-2202-jp.eml'); run_script(['-mda', '--no-precheck'], $env, { 0 => $eh}) or BAIL_OUT '-mda'; - rename("$home/v1test/skip-index", "$home/v1test/public-inbox") or - BAIL_OUT $!; + rename("$home/v1test/skip-index", "$home/v1test/public-inbox"); my ($in, $out, $err); $in = $out = $err = ''; @@ -500,10 +483,8 @@ SKIP: { "$home/v2tmp", 'http://example.com/v2tmp', $tmp_addr ]) or xbail '-init'; $env = { ORIGINAL_RECIPIENT => $tmp_addr }; - open $fh, '+>', undef or xbail "open $!"; - $fh->autoflush(1); my $mid = 'tmpmsg@example.com'; - print $fh < @@ -511,8 +492,7 @@ Subject: tmpmsg Date: Tue, 19 Jan 2038 03:14:07 +0000 EOM - seek $fh, 0, SEEK_SET or xbail "seek $!"; - run_script([qw(-mda --no-precheck)], $env, {0 => $fh}) or xbail '-mda'; + run_script([qw(-mda --no-precheck)], $env, {0 => $in}) or xbail '-mda'; ok(run_script([qw(-extindex --all), "$home/extindex"]), 'update'); my $nr; { @@ -525,7 +505,7 @@ EOM $mset = $es->search->mset('z:0..'); $nr = $mset->size; } - truncate($cfg_path, $old_size) or xbail "truncate $!"; + truncate($cfg_path, $old_size); my $rdr = { 2 => \(my $err) }; ok(run_script([qw(-extindex --gc), "$home/extindex"], undef, $rdr), 'gc to get rid of removed inbox');