]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
t/cindex+extsearch: use write_file, autodie, etc.
authorEric Wong <e@80x24.org>
Thu, 2 Nov 2023 09:35:39 +0000 (09:35 +0000)
committerEric Wong <e@80x24.org>
Fri, 3 Nov 2023 06:39:53 +0000 (06:39 +0000)
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
t/extsearch.t

index 2033945e3fa84360c068ac92c0a9097824e9f78b..60711492cac37fe0609e93116cd939a6dea5cd6c 100644 (file)
@@ -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 = '';
index 2995cf95581ced7865fe582db112a363fec7433e..1a1eb3500cd0cf5984977bdac7fad07079028769 100644 (file)
@@ -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');