6 use App
::Codeowners
::Util
qw(run_git);
7 use Path
::Tiny
qw(path tempdir);
10 can_ok
('App::Codeowners::Util', qw{
12 find_codeowners_in_directory
13 find_nearest_codeowners
24 my $can_git = _can_git
();
26 subtest
'git_ls_files' => sub {
27 plan skip_all
=> 'Cannot run git' if !$can_git;
28 my $repodir =_setup_git_repo
();
30 my (undef, @r) = App
::Codeowners
::Util
::git_ls_files
($repodir);
31 is_deeply
(\
@r, [], 'git ls-files returns [] when no repo files') or diag explain \
@r;
33 run_git
('-C', $repodir, qw{add .})->wait;
34 run_git
('-C', $repodir, qw{commit -m}, 'initial commit')->wait;
36 (undef, @r) = App
::Codeowners
::Util
::git_ls_files
($repodir);
38 qw(a/b/c/bar.txt foo.txt)
39 ], 'git ls-files returns correct repo files') or diag explain \
@r;
42 subtest
'git_toplevel' => sub {
43 plan skip_all
=> 'Cannot run git' if !$can_git;
44 my $repodir =_setup_git_repo
();
45 my $repodir_fixed = _fix_win32_path
($repodir);
47 my $r = App
::Codeowners
::Util
::git_toplevel
($repodir);
48 is($r->canonpath, $repodir_fixed->canonpath, 'found toplevel directory from toplevel');
50 $r = App
::Codeowners
::Util
::git_toplevel
($repodir->child('a/b'));
51 is($r->canonpath, $repodir_fixed->canonpath, 'found toplevel directory');
54 subtest
'find_nearest_codeowners' => sub {
55 plan skip_all
=> 'Cannot run git' if !$can_git;
56 my $repodir =_setup_git_repo
();
58 $repodir->child('docs')->mkpath;
59 my $filepath = _spew_codeowners
($repodir->child('docs/CODEOWNERS'));
61 my $r = App
::Codeowners
::Util
::find_nearest_codeowners
($repodir->child('a/b/c'));
62 is($r, $filepath, 'found CODEOWNERS file');
65 subtest
'find_codeowners_in_directory' => sub {
66 plan skip_all
=> 'Cannot run git' if !$can_git;
67 my $repodir =_setup_git_repo
();
69 $repodir->child('docs')->mkpath;
70 my $filepath = _spew_codeowners
($repodir->child('docs/CODEOWNERS'));
72 my $r = App
::Codeowners
::Util
::find_codeowners_in_directory
($repodir);
73 is($r, $filepath, 'found CODEOWNERS file in docs');
75 $filepath = _spew_codeowners
($repodir->child('CODEOWNERS'));
76 $r = App
::Codeowners
::Util
::find_codeowners_in_directory
($repodir);
77 is($r, $filepath, 'found CODEOWNERS file in toplevel');
84 my (undef, $version) = eval { run_git
('--version') };
86 note
"Found: $version" if $version;
87 return $version && $version ge 'git version 1.8.5'; # for -C flag
91 my $repodir = tempdir
;
93 run_git
('-C', $repodir, 'init')->wait;
94 run_git
('-C', $repodir, qw{config --local user.email app-codeowners@example.com})->wait;
95 run_git
('-C', $repodir, qw{config --local user.name App-Codeowners})->wait;
97 $repodir->child('foo.txt')->touchpath;
98 $repodir->child('a/b/c/bar.txt')->touchpath;
103 sub _fix_win32_path
{
105 # Git for Windows shows full paths
106 if (eval { require Win32
}) {
107 $path = path
(Win32
::GetLongPathName
($path));
112 sub _spew_codeowners
{
113 my $path = path
(shift);
114 $path->spew_utf8(\"foo
.txt \
@twix\n");