]> Sergey Matveev's repositories - public-inbox.git/blob - t/mda.t
flesh out MDA and simplify config setup
[public-inbox.git] / t / mda.t
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 use strict;
4 use warnings;
5 use Test::More;
6 use Email::MIME;
7 use File::Temp qw/tempdir/;
8 use Cwd;
9 use IPC::Run qw(run);
10
11 my $mda = "blib/script/public-inbox-mda";
12 my $tmpdir = tempdir(CLEANUP => 1);
13 my $home = "$tmpdir/pi-home";
14 my $pi_home = "$home/.public-inbox";
15 my $pi_config = "$pi_home/config";
16 my $maindir = "$tmpdir/main.git";
17 my $faildir = "$tmpdir/fail.git";
18 my $main_bin = getcwd()."/t/main-bin";
19 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
20 my $fail_bin = getcwd()."/t/fail-bin";
21 my $fail_path = "$fail_bin:$ENV{PATH}"; # for spamc spam mock
22 my $addr = 'test-public@example.com';
23 my $cfgpfx = "publicinbox.test";
24
25 {
26         ok(-x "$main_bin/spamc",
27                 "spamc ham mock found (run in top of source tree");
28         ok(-x "$fail_bin/spamc",
29                 "spamc mock found (run in top of source tree");
30         ok(-x $mda, "$mda is executable");
31         is(1, mkdir($home, 0755), "setup ~/ for testing");
32         is(1, mkdir($pi_home, 0755), "setup ~/.public-inbox");
33         is(0, system(qw(git init -q --bare), $maindir), "git init (main)");
34         is(0, system(qw(git init -q --bare), $faildir), "git init (fail)");
35
36         my %cfg = (
37                 "$cfgpfx.address" => $addr,
38                 "$cfgpfx.mainrepo" => $maindir,
39                 "$cfgpfx.failrepo" => $faildir,
40         );
41         while (my ($k,$v) = each %cfg) {
42                 is(0, system(qw(git config --file), $pi_config, $k, $v),
43                         "setup $k");
44         }
45 }
46
47 {
48         my $failbox = "$home/fail.mbox";
49         local $ENV{PI_FAILBOX} = $failbox;
50         local $ENV{HOME} = $home;
51         local $ENV{RECIPIENT} = $addr;
52         my $simple = Email::Simple->new(<<EOF);
53 From: Me <me\@example.com>
54 To: You <you\@example.com>
55 Cc: $addr
56 Message-Id: <blah\@example.com>
57 Subject: hihi
58 Date: Thu, 01 Jan 1970 00:00:00 +0000
59
60 EOF
61         my $in = $simple->as_string;
62
63         # ensure successful message delivery
64         {
65                 local $ENV{PATH} = $main_path;
66                 run([$mda], \$in);
67                 local $ENV{GIT_DIR} = $maindir;
68                 my $rev = `git rev-list HEAD`;
69                 like($rev, qr/\A[a-f0-9]{40}/, "good revision committed");
70         }
71
72         # ensure failures work
73         {
74                 local $ENV{PATH} = $fail_path;
75                 run([$mda], \$in);
76                 local $ENV{GIT_DIR} = $faildir;
77                 my $rev = `git rev-list HEAD`;
78                 like($rev, qr/\A[a-f0-9]{40}/, "bad revision committed");
79         }
80         ok(!-e $failbox, "nothing in PI_FAILBOX");
81 }
82
83 done_testing();