From: Eric Wong <e@80x24.org>
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 [thread overview]
Message-ID: <20231102093539.2067470-15-e@80x24.org> (raw)
In-Reply-To: <20231102093539.2067470-1-e@80x24.org>
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 <<EOM;
+ PublicInbox::IO::write_file '>', $env->{PI_CONFIG}, <<EOM;
[publicinbox "basictest"]
inboxdir = $basic->{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 <<EOF or BAIL_OUT $!;
+PublicInbox::IO::write_file '>', $cfg_path, <<EOF;
[publicinboxMda]
spamcheck = none
EOF
-close $fh or BAIL_OUT $!;
my $v2addr = 'v2test@example.com';
my $v1addr = 'v1test@example.com';
ok(run_script([qw(-init -Lbasic -V2 v2test --newsgroup v2.example),
@@ -30,24 +28,18 @@ my $env = { ORIGINAL_RECIPIENT => $v2addr };
my $eml = eml_load('t/utf8.eml');
$eml->header_set('List-Id', '<v2.example.com>');
-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', '<v1.example.com>');
-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 <<EOF or BAIL_OUT $!;
+ PublicInbox::IO::write_file '>>', $cfg_path, <<EOF;
; for ->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 <<EOM or xbail "print $!";
+ my $in = \<<EOM;
From: b\@z
To: b\@r
Message-Id: <$mid>
@@ -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');
prev parent reply other threads:[~2023-11-02 9:35 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-02 9:35 [PATCH 00/14] IO/IPC-related cleanups Eric Wong
2023-11-02 9:35 ` [PATCH 01/14] xap_helper.pm: use do_fork to Reset and reseed Eric Wong
2023-11-02 9:35 ` [PATCH 02/14] ds: replace FD map hash table with array Eric Wong
2023-11-02 9:35 ` [PATCH 03/14] treewide: use ->close method rather than CORE::close Eric Wong
2023-11-02 21:35 ` [PATCH 15/14] ds: don't try ->close after ->accept_SSL failure Eric Wong
2023-11-02 9:35 ` [PATCH 04/14] cindex: drop redundant close on regular FH Eric Wong
2023-11-02 9:35 ` [PATCH 05/14] treewide: use ->close to call ProcessIO->CLOSE Eric Wong
2023-11-02 9:35 ` [PATCH 06/14] multi_git: use autodie Eric Wong
2023-11-02 9:35 ` [PATCH 07/14] git_credential: use autodie where appropriate Eric Wong
2023-11-02 9:35 ` [PATCH 08/14] replace ProcessIO with untied PublicInbox::IO Eric Wong
2023-11-02 9:35 ` [PATCH 09/14] io: introduce write_file helper sub Eric Wong
2023-11-02 9:35 ` [PATCH 10/14] spawn: support PerlIO layer in scalar redirects Eric Wong
2023-11-02 9:35 ` [PATCH 11/14] treewide: check alternates writes with eof + autodie Eric Wong
2023-11-02 9:35 ` [PATCH 12/14] treewide: use eof and close to detect readline errors Eric Wong
2023-11-02 9:35 ` [PATCH 13/14] move read_all, try_cat, and poll_in to PublicInbox::IO Eric Wong
2023-11-02 20:59 ` www: squash read_all usage fix Eric Wong
2023-11-02 9:35 ` Eric Wong [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231102093539.2067470-15-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/public-inbox.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).