2 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use PublicInbox::Import;
8 use_ok 'PublicInbox::Admin';
9 my $v1 = create_inbox 'v1', -no_gc => 1, sub {};
10 my ($tmpdir, $for_destroy) = tmpdir();
11 my $git_dir = $v1->{inboxdir};
15 require_mods(qw(DBD::SQLite), 5);
16 require_git(2.6, 1) or skip 5, 'git too old';
17 $v2ibx = create_inbox 'v2', indexlevel => 'basic', version => 2,
25 *resolve_inboxdir = \&PublicInbox::Admin::resolve_inboxdir;
28 is(resolve_inboxdir($git_dir), $git_dir, 'top-level GIT_DIR resolved');
29 is(resolve_inboxdir("$git_dir/objects"), $git_dir, 'GIT_DIR/objects resolved');
31 ok(chdir($git_dir), 'chdir GIT_DIR works');
32 is(resolve_inboxdir(), $git_dir, 'resolve_inboxdir works in GIT_DIR');
34 ok(chdir("$git_dir/objects"), 'chdir GIT_DIR/objects works');
35 is(resolve_inboxdir(), $git_dir, 'resolve_inboxdir works in GIT_DIR');
36 $res = resolve_inboxdir(undef, \$v);
37 is($v, 1, 'version 1 detected');
38 is($res, $git_dir, 'detects directory along with version');
40 # $tmpdir could be inside a git working, directory, so we test '/'
43 # do people version-control "/"?
44 skip "$no_vcs_dir is version controlled by git", 4 if -d '/.git';
45 open my $null, '>', '/dev/null' or die "open /dev/null: $!";
46 open my $olderr, '>&', \*STDERR or die "dup stderr: $!";
48 ok(chdir($no_vcs_dir), 'chdir to a non-inbox');
49 open STDERR, '>&', $null or die "redirect stderr to /dev/null: $!";
50 $res = eval { resolve_inboxdir() };
51 open STDERR, '>&', $olderr or die "restore stderr: $!";
52 is($res, undef, 'fails inside non-version-controlled dir');
54 ok(chdir($tmpdir), 'back to test-specific $tmpdir');
55 open STDERR, '>&', $null or die "redirect stderr to /dev/null: $!";
56 $res = eval { resolve_inboxdir($no_vcs_dir) };
58 open STDERR, '>&', $olderr or die "restore stderr: $!";
59 is($res, undef, 'fails on non-version-controlled dir');
60 ok($err, '$@ set on failure');
65 my $v2_dir = $v2ibx->{inboxdir};
66 is(resolve_inboxdir($v2_dir), $v2_dir,
67 'resolve_inboxdir works on v2_dir');
68 chdir($v2_dir) or BAIL_OUT "chdir v2_dir: $!";
69 is(resolve_inboxdir(), $v2_dir, 'resolve_inboxdir works inside v2_dir');
70 $res = resolve_inboxdir(undef, \$v);
71 is($v, 2, 'version 2 detected');
72 is($res, $v2_dir, 'detects directory along with version');
74 # TODO: should work from inside Xapian dirs, and git dirs, here...
75 my $objdir = "$v2_dir/git/0.git/objects";
76 is($v2_dir, resolve_inboxdir($objdir, \$v), 'at $objdir');
77 is($v, 2, 'version 2 detected at $objdir');
78 chdir($objdir) or BAIL_OUT "chdir objdir: $!";
79 is(resolve_inboxdir(undef, \$v), $v2_dir, 'inside $objdir');
80 is($v, 2, 'version 2 detected inside $objdir');
83 chdir '/' or BAIL_OUT "chdir: $!";
88 '1500K' => 1500 * 1024,
89 '15m' => 15 * (1024 ** 2),
93 my ($in, $out) = splice(@pairs, 0, 2);
95 ok(PublicInbox::Admin::parse_unsigned(\$in), "parse_unsigned $orig");
96 is($in, $out, "got $orig => ($in == $out)");
99 for my $v ('', 'bogus', '1p', '1gig') {
100 ok(!PublicInbox::Admin::parse_unsigned(\$v),
101 "parse_unsigned rejects $v");