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