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>
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();
15 is(system(qw(git init -q --bare), $dir), 0, 'git init successful');
16 my $git = PublicInbox::Git->new($dir);
18 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
19 my $mime = PublicInbox::MIME->create(
21 From => 'a@example.com',
22 To => 'b@example.com',
23 'Content-Type' => 'text/plain',
24 Subject => 'this is a subject',
25 'Message-ID' => '<a@example.com>',
26 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
28 body => "hello world\n",
30 my $v2 = require_git(2.6, 1);
32 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
35 like($smsg->{blob}, qr/\A[a-f0-9]{40}\z/, 'got last object_id');
36 is($mime->as_string, ${$smsg->{-raw_email}}, 'string matches');
37 is($smsg->{bytes}, length(${$smsg->{-raw_email}}), 'length matches');
38 my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
40 print $in $mime->as_string or die "write failed: $!";
41 $in->flush or die "flush failed: $!";
42 seek($in, 0, SEEK_SET);
44 my $pid = spawn(\@cmd, {}, { 0 => $in, 1 => $out });
45 is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
46 is($?, 0, 'hash-object');
47 seek($out, 0, SEEK_SET);
48 chomp(my $hashed_obj = <$out>);
49 is($hashed_obj, $smsg->{blob}, "blob object_id matches exp");
53 my @revs = $git->qx(qw(rev-list HEAD));
54 is(scalar @revs, 1, 'one revision created');
56 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
57 $mime->header_set('From', $odd);
58 $mime->header_set('Message-ID', '<b@example.com>');
59 $mime->header_set('Subject', 'msg2');
60 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
62 @revs = $git->qx(qw(rev-list HEAD));
63 is(scalar @revs, 2, '2 revisions exist');
65 is($im->add($mime), undef, 'message only inserted once');
67 @revs = $git->qx(qw(rev-list HEAD));
68 is(scalar @revs, 2, '2 revisions exist');
70 foreach my $c ('c'..'z') {
71 $mime->header_set('Message-ID', "<$c\@example.com>");
72 $mime->header_set('Subject', "msg - $c");
73 like($im->add($mime), qr/\A:\d+\z/, "added $c message");
76 @revs = $git->qx(qw(rev-list HEAD));
77 is(scalar @revs, 26, '26 revisions exist after mass import');
78 my ($mark, $msg) = $im->remove($mime);
79 like($mark, qr/\A:\d+\z/, 'got mark');
80 is(ref($msg), 'PublicInbox::MIME', 'got old message deleted');
82 is(undef, $im->remove($mime), 'remove is idempotent');
84 # mismatch on identical Message-ID
85 $mime->header_set('Message-ID', '<a@example.com>');
86 ($mark, $msg) = $im->remove($mime);
87 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
88 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
89 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
91 $mime->header_set('Message-Id', '<failcheck@example.com>');
92 is($im->add($mime, sub { undef }), undef, 'check callback fails');
93 is($im->remove($mime), undef, 'message not added, so not removed');
94 is(undef, $im->checkpoint, 'checkpoint works before ->done');
96 is(undef, $im->checkpoint, 'checkpoint works after ->done');
99 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
101 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
104 ok($@, 'Import->add fails on non-existent dir');