1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use_ok 'PublicInbox::Admin', qw(resolve_repo_dir);
8 my ($tmpdir, $for_destroy) = tmpdir();
9 my $git_dir = "$tmpdir/v1";
10 my $v2_dir = "$tmpdir/v2";
13 is(0, system(qw(git init -q --bare), $git_dir), 'git init v1');
16 is(resolve_repo_dir($git_dir), $git_dir, 'top-level GIT_DIR resolved');
17 is(resolve_repo_dir("$git_dir/objects"), $git_dir, 'GIT_DIR/objects resolved');
19 ok(chdir($git_dir), 'chdir GIT_DIR works');
20 is(resolve_repo_dir(), $git_dir, 'resolve_repo_dir works in GIT_DIR');
22 ok(chdir("$git_dir/objects"), 'chdir GIT_DIR/objects works');
23 is(resolve_repo_dir(), $git_dir, 'resolve_repo_dir works in GIT_DIR');
24 $res = resolve_repo_dir(undef, \$v);
25 is($v, 1, 'version 1 detected');
26 is($res, $git_dir, 'detects directory along with version');
28 # $tmpdir could be inside a git working, directory, so we test '/'
31 # do people version-control "/"?
32 skip "$no_vcs_dir is version controlled by git", 4 if -d '/.git';
33 open my $null, '>', '/dev/null' or die "open /dev/null: $!";
34 open my $olderr, '>&', \*STDERR or die "dup stderr: $!";
36 ok(chdir($no_vcs_dir), 'chdir to a non-inbox');
37 open STDERR, '>&', $null or die "redirect stderr to /dev/null: $!";
38 $res = eval { resolve_repo_dir() };
39 open STDERR, '>&', $olderr or die "restore stderr: $!";
40 is($res, undef, 'fails inside non-version-controlled dir');
42 ok(chdir($tmpdir), 'back to test-specific $tmpdir');
43 open STDERR, '>&', $null or die "redirect stderr to /dev/null: $!";
44 $res = eval { resolve_repo_dir($no_vcs_dir) };
46 open STDERR, '>&', $olderr or die "restore stderr: $!";
47 is($res, undef, 'fails on non-version-controlled dir');
48 ok($err, '$@ set on failure');
53 for my $m (qw(DBD::SQLite)) {
54 skip "$m missing", 5 unless eval "require $m";
56 use_ok 'PublicInbox::V2Writable';
57 use_ok 'PublicInbox::Inbox';
58 my $ibx = PublicInbox::Inbox->new({
60 name => 'test-v2writable',
62 -primary_address => 'test@example.com',
63 indexlevel => 'basic',
65 PublicInbox::V2Writable->new($ibx, 1)->idx_init;
67 ok(-e "$v2_dir/inbox.lock", 'exists');
68 is(resolve_repo_dir($v2_dir), $v2_dir,
69 'resolve_repo_dir works on v2_dir');
70 ok(chdir($v2_dir), 'chdir v2_dir OK');
71 is(resolve_repo_dir(), $v2_dir, 'resolve_repo_dir works inside v2_dir');
72 $res = resolve_repo_dir(undef, \$v);
73 is($v, 2, 'version 2 detected');
74 is($res, $v2_dir, 'detects directory along with version');
76 # TODO: should work from inside Xapian dirs, and git dirs, here...