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