]> Sergey Matveev's repositories - public-inbox.git/blob - t/import.t
remove most internal Email::MIME usage
[public-inbox.git] / t / import.t
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Eml;
7 use PublicInbox::Git;
8 use PublicInbox::Import;
9 use PublicInbox::Spawn qw(spawn);
10 use Fcntl qw(:DEFAULT SEEK_SET);
11 use File::Temp qw/tempfile/;
12 use PublicInbox::TestCommon;
13 my ($dir, $for_destroy) = tmpdir();
14
15 my $git = PublicInbox::Git->new($dir);
16 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
17 $im->init_bare;
18 my $mime = PublicInbox::Eml->new(<<'EOF');
19 From: a@example.com
20 To: b@example.com
21 Subject: this is a subject
22 Message-ID: <a@example.com>
23 Date: Fri, 02 Oct 1993 00:00:00 +0000
24
25 hello world
26 EOF
27
28 my $v2 = require_git(2.6, 1);
29 my $smsg = {} if $v2;
30 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
31
32 if ($v2) {
33         like($smsg->{blob}, qr/\A[a-f0-9]{40}\z/, 'got last object_id');
34         is($mime->as_string, ${$smsg->{-raw_email}}, 'string matches');
35         is($smsg->{bytes}, length(${$smsg->{-raw_email}}), 'length matches');
36         my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
37         my $in = tempfile();
38         print $in $mime->as_string or die "write failed: $!";
39         $in->flush or die "flush failed: $!";
40         seek($in, 0, SEEK_SET);
41         my $out = tempfile();
42         my $pid = spawn(\@cmd, {}, { 0 => $in, 1 => $out });
43         is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
44         is($?, 0, 'hash-object');
45         seek($out, 0, SEEK_SET);
46         chomp(my $hashed_obj = <$out>);
47         is($hashed_obj, $smsg->{blob}, "blob object_id matches exp");
48 }
49
50 $im->done;
51 my @revs = $git->qx(qw(rev-list HEAD));
52 is(scalar @revs, 1, 'one revision created');
53
54 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
55 $mime->header_set('From', $odd);
56 $mime->header_set('Message-ID', '<b@example.com>');
57 $mime->header_set('Subject', 'msg2');
58 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
59 $im->done;
60 @revs = $git->qx(qw(rev-list HEAD));
61 is(scalar @revs, 2, '2 revisions exist');
62
63 is($im->add($mime), undef, 'message only inserted once');
64 $im->done;
65 @revs = $git->qx(qw(rev-list HEAD));
66 is(scalar @revs, 2, '2 revisions exist');
67
68 foreach my $c ('c'..'z') {
69         $mime->header_set('Message-ID', "<$c\@example.com>");
70         $mime->header_set('Subject', "msg - $c");
71         like($im->add($mime), qr/\A:\d+\z/, "added $c message");
72 }
73 $im->done;
74 @revs = $git->qx(qw(rev-list HEAD));
75 is(scalar @revs, 26, '26 revisions exist after mass import');
76 my ($mark, $msg) = $im->remove($mime);
77 like($mark, qr/\A:\d+\z/, 'got mark');
78 like(ref($msg), qr/\bPublicInbox::(?:Eml|MIME)\b/, 'got old message deleted');
79
80 is(undef, $im->remove($mime), 'remove is idempotent');
81
82 # mismatch on identical Message-ID
83 $mime->header_set('Message-ID', '<a@example.com>');
84 ($mark, $msg) = $im->remove($mime);
85 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
86 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
87 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
88
89 $mime->header_set('Message-Id', '<failcheck@example.com>');
90 is($im->add($mime, sub { undef }), undef, 'check callback fails');
91 is($im->remove($mime), undef, 'message not added, so not removed');
92 is(undef, $im->checkpoint, 'checkpoint works before ->done');
93 $im->done;
94 is(undef, $im->checkpoint, 'checkpoint works after ->done');
95 $im->checkpoint;
96
97 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
98 eval {
99         my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
100         $nope->add($mime);
101 };
102 ok($@, 'Import->add fails on non-existent dir');
103
104 done_testing();