]> Sergey Matveev's repositories - public-inbox.git/blob - t/import.t
*idx: pass smsg in even more places
[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::MIME;
7 use PublicInbox::Git;
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();
14
15 is(system(qw(git init -q --bare), $dir), 0, 'git init successful');
16 my $git = PublicInbox::Git->new($dir);
17
18 my $im = PublicInbox::Import->new($git, 'testbox', 'test@example');
19 my $mime = PublicInbox::MIME->create(
20         header => [
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',
27         ],
28         body => "hello world\n",
29 );
30 my $v2 = require_git(2.6, 1);
31 my $smsg = {} if $v2;
32 like($im->add($mime, undef, $smsg), qr/\A:[0-9]+\z/, 'added one message');
33
34 if ($v2) {
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));
39         my $in = tempfile();
40         print $in $mime->as_string or die "write failed: $!";
41         $in->flush or die "flush failed: $!";
42         seek($in, 0, SEEK_SET);
43         my $out = tempfile();
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");
50 }
51
52 $im->done;
53 my @revs = $git->qx(qw(rev-list HEAD));
54 is(scalar @revs, 1, 'one revision created');
55
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');
61 $im->done;
62 @revs = $git->qx(qw(rev-list HEAD));
63 is(scalar @revs, 2, '2 revisions exist');
64
65 is($im->add($mime), undef, 'message only inserted once');
66 $im->done;
67 @revs = $git->qx(qw(rev-list HEAD));
68 is(scalar @revs, 2, '2 revisions exist');
69
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");
74 }
75 $im->done;
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');
81
82 is(undef, $im->remove($mime), 'remove is idempotent');
83
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');
90
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');
95 $im->done;
96 is(undef, $im->checkpoint, 'checkpoint works after ->done');
97 $im->checkpoint;
98
99 my $nogit = PublicInbox::Git->new("$dir/non-existent/dir");
100 eval {
101         my $nope = PublicInbox::Import->new($nogit, 'nope', 'no@example.com');
102         $nope->add($mime);
103 };
104 ok($@, 'Import->add fails on non-existent dir');
105
106 done_testing();