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>
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();
16 my $git = PublicInbox::Git->new($dir);
17 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
19 my $mime = PublicInbox::Eml->new(<<'EOF');
22 Subject: this is a subject
23 Message-ID: <a@example.com>
24 Date: Fri, 02 Oct 1993 00:00:00 +0000
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');
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));
39 print $in $mime->as_string or die "write failed: $!";
40 $in->flush or die "flush failed: $!";
41 seek($in, 0, SEEK_SET);
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");
52 my @revs = $git->qx(qw(rev-list HEAD));
53 is(scalar @revs, 1, 'one revision created');
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');
61 @revs = $git->qx(qw(rev-list HEAD));
62 is(scalar @revs, 2, '2 revisions exist');
64 is($im->add($mime), undef, 'message only inserted once');
66 @revs = $git->qx(qw(rev-list HEAD));
67 is(scalar @revs, 2, '2 revisions exist');
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");
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');
81 is(undef, $im->remove($mime), 'remove is idempotent');
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');
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');
95 is(undef, $im->checkpoint, 'checkpoint works after ->done');
98 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
100 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
103 ok($@, 'Import->add fails on non-existent dir');