]> Sergey Matveev's repositories - public-inbox.git/commitdiff
t/*.t: avoid sharing "my" variables in subs
authorEric Wong <e@80x24.org>
Wed, 18 Dec 2019 03:36:43 +0000 (03:36 +0000)
committerEric Wong <e@80x24.org>
Thu, 19 Dec 2019 03:59:17 +0000 (03:59 +0000)
These usages of file-local global variables make the *.t files
incompatible with run_script().  Instead, use anonymous subs,
"our", or pass the parameter as appropriate.

t/httpd-corner.t
t/indexlevels-mirror.t
t/mda.t
t/nntpd-tls.t
t/solver_git.t
t/v2mirror.t
t/view.t
t/www_listing.t

index 551af2b21526c822e7f479186d668d4ed380be85..a8cdb2e97ae8d43c40557a81eda97f4b46797d8f 100644 (file)
@@ -553,16 +553,16 @@ SKIP: {
        # filter out pipes inherited from the parent
        my @this = `lsof -p $$`;
        my $bad;
-       sub extract_inodes {
+       my $extract_inodes = sub {
                map {;
                        my @f = split(' ', $_);
                        my $inode = $f[-2];
                        $bad = $_ if $inode !~ /\A[0-9]+\z/;
                        $inode => 1;
                } grep (/\bpipe\b/, @_);
-       }
-       my %child = extract_inodes(@lsof);
-       my %parent = extract_inodes(@this);
+       };
+       my %child = $extract_inodes->(@lsof);
+       my %parent = $extract_inodes->(@this);
        skip("inode not in expected format: $bad", 1) if defined($bad);
        delete @child{(keys %parent)};
        is_deeply([], [keys %child], 'no extra pipes with -W0');
index 3d4813bec9e814227b3dbc0c8f9480a066d50ce8..aae42510491d2d9759ce2fda2c58c0e1f5cfe9de 100644 (file)
@@ -16,8 +16,6 @@ foreach my $mod (qw(DBD::SQLite)) {
        plan skip_all => "$mod missing for $0" if $@;
 }
 
-my @xcpdb = qw(-xcpdb -q);
-
 my $mime = PublicInbox::MIME->create(
        header => [
                From => 'a@example.com',
@@ -29,7 +27,7 @@ my $mime = PublicInbox::MIME->create(
 );
 
 sub import_index_incremental {
-       my ($v, $level) = @_;
+       my ($v, $level, $mime) = @_;
        my $this = "pi-$v-$level-indexlevels";
        my ($tmpdir, $for_destroy) = tmpdir();
        local $ENV{PI_CONFIG} = "$tmpdir/config";
@@ -120,7 +118,7 @@ sub import_index_incremental {
        is_deeply(\@rw_nums, [1], 'unindex NNTP article'.$v.$level);
 
        if ($level ne 'basic') {
-               ok(run_script([@xcpdb, $mirror]), "v$v xcpdb OK");
+               ok(run_script(['-xcpdb', '-q', $mirror]), "v$v xcpdb OK");
                is(PublicInbox::Admin::detect_indexlevel($ro_mirror), $level,
                   'indexlevel detectable by Admin after xcpdb v' .$v.$level);
                delete $ro_mirror->{$_} for (qw(over search));
@@ -167,13 +165,13 @@ sub import_index_incremental {
 }
 
 # we can probably cull some other tests
-import_index_incremental($PI_TEST_VERSION, 'basic');
+import_index_incremental($PI_TEST_VERSION, 'basic', $mime);
 
 SKIP: {
        require PublicInbox::Search;
        PublicInbox::Search::load_xapian() or skip 'Search::Xapian missing', 2;
        foreach my $l (qw(medium full)) {
-               import_index_incremental($PI_TEST_VERSION, $l);
+               import_index_incremental($PI_TEST_VERSION, $l, $mime);
        }
 }
 
diff --git a/t/mda.t b/t/mda.t
index 47d061322744be3c8745b4f84f9b80c8f6bab94e..3686a97bbe42977b23cbbba428ec16f830cc8fdf 100644 (file)
--- a/t/mda.t
+++ b/t/mda.t
@@ -23,6 +23,23 @@ my $faildir = "$home/faildir/";
 my $mime;
 my $git = PublicInbox::Git->new($maindir);
 
+my $fail_bad_header = sub ($$$) {
+       my ($good_rev, $msg, $in) = @_;
+       my @f = glob("$faildir/*/*");
+       unlink @f if @f;
+       my ($out, $err) = ("", "");
+       my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
+       local $ENV{PATH} = $main_path;
+       ok(run_script(['-mda'], undef, $opt),
+               "no error on undeliverable ($msg)");
+       my $rev = $git->qx(qw(rev-list HEAD));
+       chomp $rev;
+       is($rev, $good_rev, "bad revision not commited ($msg)");
+       @f = glob("$faildir/*/*");
+       is(scalar @f, 1, "faildir written to");
+       [ $in, $out, $err ];
+};
+
 {
        ok(-x "$main_bin/spamc",
                "spamc ham mock found (run in top of source tree");
@@ -110,14 +127,14 @@ EOF
                is(scalar @new, 1, "PI_EMERGENCY is written to");
        }
 
-       fail_bad_header($good_rev, "bad recipient", <<"");
+       $fail_bad_header->($good_rev, "bad recipient", <<"");
 From: Me <me\@example.com>
 To: You <you\@example.com>
 Message-Id: <bad-recipient\@example.com>
 Subject: hihi
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
-       my $fail = fail_bad_header($good_rev, "duplicate Message-ID", <<"");
+       my $fail = $fail_bad_header->($good_rev, "duplicate Message-ID", <<"");
 From: Me <me\@example.com>
 To: You <you\@example.com>
 Cc: $addr
@@ -127,26 +144,26 @@ Date: Thu, 01 Jan 1970 00:00:00 +0000
 
        like($fail->[2], qr/CONFLICT/, "duplicate Message-ID message");
 
-       fail_bad_header($good_rev, "missing From:", <<"");
+       $fail_bad_header->($good_rev, "missing From:", <<"");
 To: $addr
 Message-ID: <missing-from\@example.com>
 Subject: hihi
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
-       fail_bad_header($good_rev, "short subject:", <<"");
+       $fail_bad_header->($good_rev, "short subject:", <<"");
 To: $addr
 From: cat\@example.com
 Message-ID: <short-subject\@example.com>
 Subject: a
 Date: Thu, 01 Jan 1970 00:00:00 +0000
 
-       fail_bad_header($good_rev, "no date", <<"");
+       $fail_bad_header->($good_rev, "no date", <<"");
 To: $addr
 From: u\@example.com
 Message-ID: <no-date\@example.com>
 Subject: hihi
 
-       fail_bad_header($good_rev, "bad date", <<"");
+       $fail_bad_header->($good_rev, "bad date", <<"");
 To: $addr
 From: u\@example.com
 Message-ID: <bad-date\@example.com>
@@ -329,20 +346,3 @@ EOF
 }
 
 done_testing();
-
-sub fail_bad_header {
-       my ($good_rev, $msg, $in) = @_;
-       my @f = glob("$faildir/*/*");
-       unlink @f if @f;
-       my ($out, $err) = ("", "");
-       my $opt = { 0 => \$in, 1 => \$out, 2 => \$err };
-       local $ENV{PATH} = $main_path;
-       ok(run_script(['-mda'], undef, $opt),
-               "no error on undeliverable ($msg)");
-       my $rev = $git->qx(qw(rev-list HEAD));
-       chomp $rev;
-       is($rev, $good_rev, "bad revision not commited ($msg)");
-       @f = glob("$faildir/*/*");
-       is(scalar @f, 1, "faildir written to");
-       [ $in, $out, $err ];
-}
index bbcc04c0c0e063a35b1d7b018831b3d6ae9c0469..c6dceaaa660793b8f6ce67fa1b8f57cd081f26fc 100644 (file)
@@ -28,7 +28,7 @@ require './t/common.perl';
 require PublicInbox::InboxWritable;
 require PublicInbox::MIME;
 require PublicInbox::SearchIdx;
-my $need_zlib;
+our $need_zlib;
 eval { require Compress::Raw::Zlib } or
        $need_zlib = 'Compress::Raw::Zlib missing';
 my $version = 2; # v2 needs newer git
index 6bac17ead0cad4ddc9cc2a4d5fed802b8e155a6d..88f83bdb7ae28525b75c7e97bb3a374817930dc2 100644 (file)
@@ -32,15 +32,16 @@ my $ibx = PublicInbox::Inbox->new($opts);
 my $im = PublicInbox::V2Writable->new($ibx, 1);
 $im->{parallel} = 0;
 
-sub deliver_patch ($) {
+my $deliver_patch = sub ($) {
        open my $fh, '<', $_[0] or die "open: $!";
        my $mime = PublicInbox::MIME->new(do { local $/; <$fh> });
        $im->add($mime);
        $im->done;
-}
+};
 
-deliver_patch('t/solve/0001-simple-mod.patch');
+$deliver_patch->('t/solve/0001-simple-mod.patch');
 my $v1_0_0_tag = 'cb7c42b1e15577ed2215356a2bf925aef59cdd8d';
+
 my $git = PublicInbox::Git->new($git_dir);
 is('public-inbox 1.0.0',
        $git->commit_title($v1_0_0_tag),
@@ -96,7 +97,7 @@ $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
 is($res, undef, 'no error on a tag not in our repo');
 
-deliver_patch('t/solve/0002-rename-with-modifications.patch');
+$deliver_patch->('t/solve/0002-rename-with-modifications.patch');
 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
 $solver->solve($psgi_env, $log, '0a92431', {});
 ok($res, 'resolved without hints');
index a45a262e59dc7c16ee1b3c7564ddaae8d52e4155..213a5f1534d47e139840bf75920a93a9a7bfdb15 100644 (file)
@@ -97,15 +97,15 @@ for my $i (10..15) {
 $v2w->done;
 $ibx->cleanup;
 
-sub fetch_each_epoch {
+my $fetch_each_epoch = sub {
        foreach my $i (0..$epoch_max) {
                my $dir = "$tmpdir/m/git/$i.git";
                is(system('git', "--git-dir=$dir", 'fetch', '-q'), 0,
                        'fetch successful');
        }
-}
+};
 
-fetch_each_epoch();
+$fetch_each_epoch->();
 
 my $mset = $mibx->search->reopen->query('m:15@example.com', {mset => 1});
 is(scalar($mset->items), 0, 'new message not found in mirror, yet');
@@ -135,7 +135,7 @@ like($to_purge, qr/\A[a-f0-9]{40,}\z/, 'read blob to be purged');
 $mset = $ibx->search->reopen->query('m:10@example.com', {mset => 1});
 is(scalar($mset->items), 0, 'purged message gone from origin');
 
-fetch_each_epoch();
+$fetch_each_epoch->();
 {
        $ibx->cleanup;
        PublicInbox::InboxWritable::cleanup($mibx);
@@ -173,7 +173,7 @@ is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
        ok($v2w->remove($mime), 'removed <1@example.com> from source');
        $v2w->done;
        $ibx->cleanup;
-       fetch_each_epoch();
+       $fetch_each_epoch->();
        PublicInbox::InboxWritable::cleanup($mibx);
 
        my $cmd = [ "-index", "$tmpdir/m" ];
index 1de3a02cf2eaa717cd2c17f026aba87240a5b31a..92962b15f2007eeac9b3df438508edc5be94ea2f 100644 (file)
--- a/t/view.t
+++ b/t/view.t
@@ -24,8 +24,8 @@ my $ctx = {
 };
 $ctx->{-inbox}->{-primary_address} = 'test@example.com';
 
-sub msg_html ($) {
-       my ($mime) = @_;
+sub msg_html ($$) {
+       my ($ctx, $mime) = @_;
 
        my $s = '';
        my $r = PublicInbox::View::msg_html($ctx, $mime);
@@ -72,7 +72,7 @@ EOF
                body => $body,
        )->as_string;
        my $mime = Email::MIME->new($s);
-       my $html = msg_html($mime);
+       my $html = msg_html($ctx, $mime);
 
        # ghetto tests
        like($html, qr!<a\nhref="raw"!s, "raw link present");
@@ -102,7 +102,7 @@ EOF
                parts => $parts,
        );
 
-       my $html = msg_html($mime);
+       my $html = msg_html($ctx, $mime);
        like($html, qr/hi\n.*-- Attachment #2.*\nbye\n/s, "multipart split");
 }
 
@@ -131,7 +131,7 @@ EOF
                parts => $parts,
        );
 
-       my $html = msg_html($mime);
+       my $html = msg_html($ctx, $mime);
        like($html, qr!.*Attachment #2: foo&(?:amp|#38);\.patch --!,
                "parts split with filename");
 }
@@ -157,7 +157,7 @@ EOF
        );
 
        my $orig = $mime->body_raw;
-       my $html = msg_html($mime);
+       my $html = msg_html($ctx, $mime);
        like($orig, qr/hi =3D bye=/, "our test used QP correctly");
        like($html, qr/\bhi = bye\b/, "HTML output decoded QP");
 }
index c9201213c12482056b48984b2ac39a8c9ffeb8b1..e1263360c68f1d21d2ad325dfbaa6bc033881e9a 100644 (file)
@@ -33,7 +33,7 @@ like(PublicInbox::WwwListing::fingerprint($bare), qr/\A[a-f0-9]{40}\z/,
        'got fingerprint with non-empty repo');
 
 sub tiny_test {
-       my ($host, $port) = @_;
+       my ($json, $host, $port) = @_;
        my $http = HTTP::Tiny->new;
        my $res = $http->get("http://$host:$port/manifest.js.gz");
        is($res->{status}, 200, 'got manifest');
@@ -107,7 +107,7 @@ SKIP: {
        $td = start_script($cmd, $env, { 3 => $sock });
        $sock = undef;
 
-       tiny_test($host, $port);
+       tiny_test($json, $host, $port);
 
        skip 'skipping grok-pull integration test', 2 if !which('grok-pull');