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 $im->{want_object_info} = 1 if $v2;
33 like($im->add($mime), qr/\A:\d+\z/, 'added one message');
36 my $info = $im->{last_object};
37 like($info->[0], qr/\A[a-f0-9]{40}\z/, 'got last object_id');
38 is($mime->as_string, ${$info->[2]}, 'string matches');
39 is($info->[1], length(${$info->[2]}), 'length matches');
40 my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
42 print $in $mime->as_string or die "write failed: $!";
43 $in->flush or die "flush failed: $!";
44 seek($in, 0, SEEK_SET);
46 my $pid = spawn(\@cmd, {}, { 0 => $in, 1 => $out });
47 is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
48 is($?, 0, 'hash-object');
49 seek($out, 0, SEEK_SET);
50 chomp(my $hashed_obj = <$out>);
51 is($hashed_obj, $info->[0], "last object_id matches exp");
55 my @revs = $git->qx(qw(rev-list HEAD));
56 is(scalar @revs, 1, 'one revision created');
58 my $odd = '"=?iso-8859-1?Q?J_K=FCpper?= <usenet"@example.de';
59 $mime->header_set('From', $odd);
60 $mime->header_set('Message-ID', '<b@example.com>');
61 $mime->header_set('Subject', 'msg2');
62 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
64 @revs = $git->qx(qw(rev-list HEAD));
65 is(scalar @revs, 2, '2 revisions exist');
67 is($im->add($mime), undef, 'message only inserted once');
69 @revs = $git->qx(qw(rev-list HEAD));
70 is(scalar @revs, 2, '2 revisions exist');
72 foreach my $c ('c'..'z') {
73 $mime->header_set('Message-ID', "<$c\@example.com>");
74 $mime->header_set('Subject', "msg - $c");
75 like($im->add($mime), qr/\A:\d+\z/, "added $c message");
78 @revs = $git->qx(qw(rev-list HEAD));
79 is(scalar @revs, 26, '26 revisions exist after mass import');
80 my ($mark, $msg) = $im->remove($mime);
81 like($mark, qr/\A:\d+\z/, 'got mark');
82 is(ref($msg), 'PublicInbox::MIME', 'got old message deleted');
84 is(undef, $im->remove($mime), 'remove is idempotent');
86 # mismatch on identical Message-ID
87 $mime->header_set('Message-ID', '<a@example.com>');
88 ($mark, $msg) = $im->remove($mime);
89 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
90 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
91 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
93 $mime->header_set('Message-Id', '<failcheck@example.com>');
94 is($im->add($mime, sub { undef }), undef, 'check callback fails');
95 is($im->remove($mime), undef, 'message not added, so not removed');
96 is(undef, $im->checkpoint, 'checkpoint works before ->done');
98 is(undef, $im->checkpoint, 'checkpoint works after ->done');
101 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
103 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
106 ok($@, 'Import->add fails on non-existent dir');