]> Sergey Matveev's repositories - public-inbox.git/blob - t/index-git-times.t
8f80c8669363d1892984bfa4487e3c1f779a8135
[public-inbox.git] / t / index-git-times.t
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use Test::More;
4 use PublicInbox::TestCommon;
5 use PublicInbox::Import;
6 use PublicInbox::Config;
7 use PublicInbox::Admin;
8 use File::Path qw(remove_tree);
9
10 require_mods(qw(DBD::SQLite Search::Xapian));
11 use_ok 'PublicInbox::Over';
12
13 my ($tmpdir, $for_destroy) = tmpdir();
14 local $ENV{PI_CONFIG} = "$tmpdir/cfg";
15 my $v1dir = "$tmpdir/v1";
16 my $addr = 'x@example.com';
17 run_script(['-init', '--indexlevel=medium', 'v1', $v1dir,
18                 'http://example.com/x', $addr])
19         or die "init failed";
20
21 {
22         my $data = <<'EOF';
23 blob
24 mark :1
25 data 133
26 From: timeless <t@example.com>
27 To: x <x@example.com>
28 Subject: can I haz the time?
29 Message-ID: <19700101000000-1234@example.com>
30
31 plz
32
33 reset refs/heads/master
34 commit refs/heads/master
35 mark :2
36 author timeless <t@example.com> 749520000 +0100
37 committer x <x@example.com> 1285977600 -0100
38 data 20
39 can I haz the time?
40 M 100644 :1 53/256f6177504c2878d3a302ef5090dacf5e752c
41
42 EOF
43         pipe(my($r, $w)) or die;
44         length($data) <= 512 or die "data too large to fit in POSIX pipe";
45         print $w $data or die;
46         close $w or die;
47         my $cmd = ['git', "--git-dir=$v1dir", 'fast-import', '--quiet'];
48         PublicInbox::Import::run_die($cmd, undef, { 0 => $r });
49 }
50
51 run_script(['-index', '--skip-docdata', $v1dir]) or die 'v1 index failed';
52
53 my $smsg;
54 {
55         my $cfg = PublicInbox::Config->new;
56         my $ibx = $cfg->lookup($addr);
57         my $lvl = PublicInbox::Admin::detect_indexlevel($ibx);
58         is($lvl, 'medium', 'indexlevel detected');
59         is($ibx->{-skip_docdata}, 1, '--skip-docdata flag set on -index');
60         $smsg = $ibx->over->get_art(1);
61         is($smsg->{ds}, 749520000, 'datestamp from git author time');
62         is($smsg->{ts}, 1285977600, 'timestamp from git committer time');
63         my $res = $ibx->search->query("m:$smsg->{mid}");
64         is(scalar @$res, 1, 'got one result for m:');
65         is($res->[0]->{ds}, $smsg->{ds}, 'Xapian stored datestamp');
66         $res = $ibx->search->query('d:19931002..19931002');
67         is(scalar @$res, 1, 'got one result for d:');
68         is($res->[0]->{ds}, $smsg->{ds}, 'Xapian search on datestamp');
69 }
70 SKIP: {
71         require_git(2.6, 1) or skip('git 2.6+ required for v2', 10);
72         my $v2dir = "$tmpdir/v2";
73         run_script(['-convert', $v1dir, $v2dir]) or die 'v2 conversion failed';
74
75         my $check_v2 = sub {
76                 my $ibx = PublicInbox::Inbox->new({inboxdir => $v2dir,
77                                 address => $addr});
78                 my $lvl = PublicInbox::Admin::detect_indexlevel($ibx);
79                 is($lvl, 'medium', 'indexlevel detected after convert');
80                 is($ibx->{-skip_docdata}, 1,
81                         '--skip-docdata preserved after convert');
82                 my $v2smsg = $ibx->over->get_art(1);
83                 is($v2smsg->{ds}, $smsg->{ds},
84                         'v2 datestamp from git author time');
85                 is($v2smsg->{ts}, $smsg->{ts},
86                         'v2 timestamp from git committer time');
87                 my $res = $ibx->search->query("m:$smsg->{mid}");
88                 is($res->[0]->{ds}, $smsg->{ds}, 'Xapian stored datestamp');
89                 $res = $ibx->search->query('d:19931002..19931002');
90                 is(scalar @$res, 1, 'got one result for d:');
91                 is($res->[0]->{ds}, $smsg->{ds}, 'Xapian search on datestamp');
92         };
93         $check_v2->();
94         remove_tree($v2dir);
95
96         # test non-parallelized conversion
97         run_script(['-convert', '-j0', $v1dir, $v2dir]) or
98                 die 'v2 conversion failed';
99         $check_v2->();
100 }
101
102 done_testing;