]> Sergey Matveev's repositories - public-inbox.git/blob - t/import.t
public-inbox 1.1.0-pre1
[public-inbox.git] / t / import.t
1 # Copyright (C) 2016-2018 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 IO::File;
11 use Fcntl qw(:DEFAULT);
12 use File::Temp qw/tempdir tempfile/;
13 my $dir = tempdir('pi-import-XXXXXX', TMPDIR => 1, CLEANUP => 1);
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
31 $im->{want_object_info} = 1 if 'v2';
32 like($im->add($mime), qr/\A:\d+\z/, 'added one message');
33
34 if ('v2') {
35         my $info = $im->{last_object};
36         like($info->[0], qr/\A[a-f0-9]{40}\z/, 'got last object_id');
37         is($mime->as_string, ${$info->[2]}, 'string matches');
38         is($info->[1], length(${$info->[2]}), 'length matches');
39         my @cmd = ('git', "--git-dir=$git->{git_dir}", qw(hash-object --stdin));
40         my $in = tempfile();
41         print $in $mime->as_string or die "write failed: $!";
42         $in->flush or die "flush failed: $!";
43         $in->seek(0, SEEK_SET);
44         my $out = tempfile();
45         my $pid = spawn(\@cmd, {}, { 0 => fileno($in), 1 => fileno($out)});
46         is(waitpid($pid, 0), $pid, 'waitpid succeeds on hash-object');
47         is($?, 0, 'hash-object');
48         $out->seek(0, SEEK_SET);
49         chomp(my $hashed_obj = <$out>);
50         is($hashed_obj, $info->[0], "last object_id matches exp");
51 }
52
53 $im->done;
54 my @revs = $git->qx(qw(rev-list HEAD));
55 is(scalar @revs, 1, 'one revision created');
56
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');
60 $im->done;
61 @revs = $git->qx(qw(rev-list HEAD));
62 is(scalar @revs, 2, '2 revisions exist');
63
64 is($im->add($mime), undef, 'message only inserted once');
65 $im->done;
66 @revs = $git->qx(qw(rev-list HEAD));
67 is(scalar @revs, 2, '2 revisions exist');
68
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");
73 }
74 $im->done;
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 is(ref($msg), 'PublicInbox::MIME', 'got old message deleted');
80
81 is(undef, $im->remove($mime), 'remove is idempotent');
82
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');
89
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');
94 $im->done;
95 is(undef, $im->checkpoint, 'checkpoint works after ->done');
96 $im->checkpoint;
97 done_testing();