1 # Copyright (C) 2016-2019 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);
11 use Fcntl qw(:DEFAULT);
12 use File::Temp qw/tempdir tempfile/;
13 my $dir = tempdir('pi-import-XXXXXX', TMPDIR => 1, CLEANUP => 1);
14 require './t/common.perl';
16 is(system(qw(git init -q --bare), $dir), 0, 'git init successful');
17 my $git = PublicInbox::Git->new($dir);
19 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
20 my $mime = PublicInbox::MIME->create(
22 From => 'a@example.com',
23 To => 'b@example.com',
24 'Content-Type' => 'text/plain',
25 Subject => 'this is a subject',
26 'Message-ID' => '<a@example.com>',
27 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
29 body => "hello world\n",
31 my $v2 = require_git(2.6, 1);
33 $im->{want_object_info} = 1 if $v2;
34 like($im->add($mime), qr/\A:\d+\z/, 'added one message');
37 my $info = $im->{last_object};
38 like($info->[0], qr/\A[a-f0-9]{40}\z/, 'got last object_id');
39 is($mime->as_string, ${$info->[2]}, 'string matches');
40 is($info->[1], length(${$info->[2]}), 'length matches');
41 my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
43 print $in $mime->as_string or die "write failed: $!";
44 $in->flush or die "flush failed: $!";
45 $in->seek(0, SEEK_SET);
47 my $pid = spawn(\@cmd, {}, { 0 => fileno($in), 1 => fileno($out)});
48 is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
49 is($?, 0, 'hash-object');
50 $out->seek(0, SEEK_SET);
51 chomp(my $hashed_obj = <$out>);
52 is($hashed_obj, $info->[0], "last object_id matches exp");
56 my @revs = $git->qx(qw(rev-list HEAD));
57 is(scalar @revs, 1, 'one revision created');
59 $mime->header_set('Message-ID', '<b@example.com>');
60 $mime->header_set('Subject', 'msg2');
61 like($im->add($mime, sub { $mime }), qr/\A:\d+\z/, 'added 2nd message');
63 @revs = $git->qx(qw(rev-list HEAD));
64 is(scalar @revs, 2, '2 revisions exist');
66 is($im->add($mime), undef, 'message only inserted once');
68 @revs = $git->qx(qw(rev-list HEAD));
69 is(scalar @revs, 2, '2 revisions exist');
71 foreach my $c ('c'..'z') {
72 $mime->header_set('Message-ID', "<$c\@example.com>");
73 $mime->header_set('Subject', "msg - $c");
74 like($im->add($mime), qr/\A:\d+\z/, "added $c message");
77 @revs = $git->qx(qw(rev-list HEAD));
78 is(scalar @revs, 26, '26 revisions exist after mass import');
79 my ($mark, $msg) = $im->remove($mime);
80 like($mark, qr/\A:\d+\z/, 'got mark');
81 is(ref($msg), 'PublicInbox::MIME', 'got old message deleted');
83 is(undef, $im->remove($mime), 'remove is idempotent');
85 # mismatch on identical Message-ID
86 $mime->header_set('Message-ID', '<a@example.com>');
87 ($mark, $msg) = $im->remove($mime);
88 is($mark, 'MISMATCH', 'mark == MISMATCH on mismatch');
89 is($msg->header('Message-ID'), '<a@example.com>', 'Message-ID matches');
90 isnt($msg->header('Subject'), $mime->header('Subject'), 'subject mismatch');
92 $mime->header_set('Message-Id', '<failcheck@example.com>');
93 is($im->add($mime, sub { undef }), undef, 'check callback fails');
94 is($im->remove($mime), undef, 'message not added, so not removed');
95 is(undef, $im->checkpoint, 'checkpoint works before ->done');
97 is(undef, $im->checkpoint, 'checkpoint works after ->done');
100 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
102 my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
105 ok($@, 'Import->add fails on non-existent dir');