]> Sergey Matveev's repositories - public-inbox.git/blob - t/solver_git.t
solver: initial Perl implementation
[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 File::Temp qw(tempdir);
7 use Cwd qw(abs_path);
8 require './t/common.perl';
9 require_git(2.6);
10
11 my @mods = qw(DBD::SQLite Search::Xapian HTTP::Request::Common Plack::Test
12                 URI::Escape Plack::Builder);
13 foreach my $mod (@mods) {
14         eval "require $mod";
15         plan skip_all => "$mod missing for $0" if $@;
16 }
17 chomp(my $git_dir = `git rev-parse --git-dir 2>/dev/null`);
18 plan skip_all => "$0 must be run from a git working tree" if $?;
19 $git_dir = abs_path($git_dir);
20
21 use_ok "PublicInbox::$_" for (qw(Inbox V2Writable MIME Git SolverGit));
22
23 my $mainrepo = tempdir('pi-solver-XXXXXX', TMPDIR => 1, CLEANUP => 1);
24 my $opts = {
25         mainrepo => $mainrepo,
26         name => 'test-v2writable',
27         version => 2,
28         -primary_address => 'test@example.com',
29 };
30 my $ibx = PublicInbox::Inbox->new($opts);
31 my $im = PublicInbox::V2Writable->new($ibx, 1);
32 $im->{parallel} = 0;
33
34 sub deliver_patch ($) {
35         open my $fh, '<', $_[0] or die "open: $!";
36         my $mime = PublicInbox::MIME->new(do { local $/; <$fh> });
37         $im->add($mime);
38         $im->done;
39 }
40
41 deliver_patch('t/solve/0001-simple-mod.patch');
42
43 my $gits = [ PublicInbox::Git->new($git_dir) ];
44 my $solver = PublicInbox::SolverGit->new($gits, [ $ibx ]);
45 open my $log, '+>>', "$mainrepo/solve.log" or die "open: $!";
46 my $res = $solver->solve($log, '69df7d5', {});
47 ok($res, 'solved a blob!');
48 my $wt_git = $res->[0];
49 is(ref($wt_git), 'PublicInbox::Git', 'got a git object for the blob');
50 my $expect = '69df7d565d49fbaaeb0a067910f03dc22cd52bd0';
51 is($res->[1], $expect, 'resolved blob to unabbreviated identifier');
52 is($res->[2], 'blob', 'type specified');
53 is($res->[3], 4405, 'size returned');
54
55 is(ref($wt_git->cat_file($res->[1])), 'SCALAR', 'wt cat-file works');
56 is_deeply([$expect, 'blob', 4405],
57           [$wt_git->check($res->[1])], 'wt check works');
58
59 if (0) { # TODO: check this?
60         seek($log, 0, 0);
61         my $z = do { local $/; <$log> };
62         diag $z;
63 }
64
65 $res = undef;
66 my $wt_git_dir = $wt_git->{git_dir};
67 $wt_git = undef;
68 ok(!-d $wt_git_dir, 'no references to WT held');
69
70 $res = $solver->solve($log, '0'x40, {});
71 is($res, undef, 'no error on z40');
72
73 my $git_v2_20_1_tag = '7a95a1cd084cb665c5c2586a415e42df0213af74';
74 $res = $solver->solve($log, $git_v2_20_1_tag, {});
75 is($res, undef, 'no error on a tag not in our repo');
76
77 deliver_patch('t/solve/0002-rename-with-modifications.patch');
78 $res = $solver->solve($log, '0a92431', {});
79 ok($res, 'resolved without hints');
80
81 my $hints = {
82         oid_a => '3435775',
83         path_a => 'HACKING',
84         path_b => 'CONTRIBUTING'
85 };
86 my $hinted = $solver->solve($log, '0a92431', $hints);
87 # don't compare ::Git objects:
88 shift @$res; shift @$hinted;
89 is_deeply($res, $hinted, 'hints work (or did not hurt :P');
90
91 done_testing();