]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
tests: move giant inbox/git dependent tests to xt/
[public-inbox.git] / t / solver_git.t
1 # Copyright (C) 2019 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 Cwd qw(abs_path);
7 require './t/common.perl';
8 require_git(2.6);
9
10 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
11                 URI::Escape Plack::Builder);
12 foreach my $mod (@mods) {
13         eval "require $mod";
14         plan skip_all => "$mod missing for $0" if $@;
15 }
16 chomp(my $git_dir = `git rev-parse --git-dir 2>/dev/null`);
17 plan skip_all => "$0 must be run from a git working tree" if $?;
18
19 # needed for alternates, and --absolute-git-dir is only in git 2.13+
20 $git_dir = abs_path($git_dir);
21
22 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable MIME Git SolverGit));
23
24 my ($inboxdir, $for_destroy) = tmpdir();
25 my $opts = {
26         inboxdir => $inboxdir,
27         name => 'test-v2writable',
28         version => 2,
29         -primary_address => 'test@example.com',
30 };
31 my $ibx = PublicInbox::Inbox->new($opts);
32 my $im = PublicInbox::V2Writable->new($ibx, 1);
33 $im->{parallel} = 0;
34
35 sub deliver_patch ($) {
36         open my $fh, '<', $_[0] or die "open: $!";
37         my $mime = PublicInbox::MIME->new(do { local $/; <$fh> });
38         $im->add($mime);
39         $im->done;
40 }
41
42 deliver_patch('t/solve/0001-simple-mod.patch');
43
44 my $git = PublicInbox::Git->new($git_dir);
45 is('public-inbox 1.0.0',
46         $git->commit_title('cb7c42b1e15577ed2215356a2bf925aef59cdd8d'),
47         'commit_title works on 1.0.0');
48
49 is(undef, $git->commit_title('impossible'), 'undef on impossible object');
50
51 $ibx->{-repo_objs} = [ $git ];
52 my $res;
53 my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
54 open my $log, '+>>', "$inboxdir/solve.log" or die "open: $!";
55 my $psgi_env = { 'psgi.errors' => *STDERR };
56 $solver->solve($psgi_env, $log, '69df7d5', {});
57 ok($res, 'solved a blob!');
58 my $wt_git = $res->[0];
59 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
60 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
61 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
62 is($res->[2], 'blob', 'type specified');
63 is($res->[3], 4405, 'size returned');
64
65 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
66 is_deeply([$expect, 'blob', 4405],
67           [$wt_git->check($res->[1])], 'wt check works');
68
69 if (0) { # TODO: check this?
70         seek($log, 0, 0);
71         my $z = do { local $/; <$log> };
72         diag $z;
73 }
74
75 my $oid = $expect;
76 for my $i (1..2) {
77         my $more;
78         my $s = PublicInbox::SolverGit->new($ibx, sub { $more = $_[0] });
79         $s->solve($psgi_env, $log, $oid, {});
80         is($more->[1], $expect, 'resolved blob to long OID '.$i);
81         chop($oid);
82 }
83
84 $solver = undef;
85 $res = undef;
86 my $wt_git_dir = $wt_git->{git_dir};
87 $wt_git = undef;
88 ok(!-d $wt_git_dir, 'no references to WT held');
89
90 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
91 $solver->solve($psgi_env, $log, '0'x40, {});
92 is($res, undef, 'no error on z40');
93
94 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
95 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
96 $solver->solve($psgi_env, $log, $git_v2_20_1_tag, {});
97 is($res, undef, 'no error on a tag not in our repo');
98
99 deliver_patch('t/solve/0002-rename-with-modifications.patch');
100 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
101 $solver->solve($psgi_env, $log, '0a92431', {});
102 ok($res, 'resolved without hints');
103
104 my $hints = {
105         oid_a => '3435775',
106         path_a => 'HACKING',
107         path_b => 'CONTRIBUTING'
108 };
109 $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
110 $solver->solve($psgi_env, $log, '0a92431', $hints);
111 my $hinted = $res;
112 # don't compare ::Git objects:
113 shift @$res; shift @$hinted;
114 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
115
116 done_testing();