]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners-util.t
initial commit
[chaz/git-codeowners] / t / app-codeowners-util.t
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5
6 use App::Codeowners::Util qw(run_git);
7 use Path::Tiny qw(path tempdir);
8 use Test::More;
9
10 can_ok('App::Codeowners::Util', qw{
11 find_nearest_codeowners
12 find_codeowners_in_directory
13 run_git
14 git_ls_files
15 git_toplevel
16 });
17
18 my $can_git = _can_git();
19
20 subtest 'git_ls_files' => sub {
21 plan skip_all => 'Cannot run git' if !$can_git;
22 my $repodir =_setup_git_repo();
23
24 my $r = App::Codeowners::Util::git_ls_files($repodir);
25 is($r, undef, 'git ls-files returns undef when no repo files') or diag explain $r;
26
27 run_git('-C', $repodir, qw{add .});
28 run_git('-C', $repodir, qw{commit -m}, 'initial commit');
29
30 $r = App::Codeowners::Util::git_ls_files($repodir);
31 is_deeply($r, [
32 qw(a/b/c/bar.txt foo.txt)
33 ], 'git ls-files returns correct repo files') or diag explain $r;
34 };
35
36 subtest 'git_toplevel' => sub {
37 plan skip_all => 'Cannot run git' if !$can_git;
38 my $repodir =_setup_git_repo();
39
40 my $r = App::Codeowners::Util::git_toplevel($repodir);
41 is($r, $repodir, 'found toplevel directory from toplevel');
42
43 $r = App::Codeowners::Util::git_toplevel($repodir->child('a/b'));
44 is($r, $repodir, 'found toplevel directory');
45 };
46
47 subtest 'find_nearest_codeowners' => sub {
48 my $repodir =_setup_git_repo();
49 $repodir->child('docs')->mkpath;
50 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
51
52 my $r = App::Codeowners::Util::find_nearest_codeowners($repodir->child('a/b/c'));
53 is($r, $filepath, 'found CODEOWNERS file');
54 };
55
56 subtest 'find_codeowners_in_directory' => sub {
57 my $repodir =_setup_git_repo();
58 $repodir->child('docs')->mkpath;
59
60 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
61
62 my $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
63 is($r, $filepath, 'found CODEOWNERS file in docs');
64
65 $filepath = _spew_codeowners($repodir->child('CODEOWNERS'));
66 $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
67 is($r, $filepath, 'found CODEOWNERS file in toplevel');
68 };
69
70 done_testing;
71 exit;
72
73 sub _can_git {
74 my ($version) = run_git('--version');
75 return $version;
76 }
77
78 sub _setup_git_repo {
79 my $repodir = tempdir;
80
81 run_git('-C', $repodir, 'init');
82
83 $repodir->child('foo.txt')->touchpath;
84 $repodir->child('a/b/c/bar.txt')->touchpath;
85
86 return $repodir;
87 }
88
89 sub _spew_codeowners {
90 my $path = path(shift);
91 $path->spew_utf8(\"foo.txt \@twix\n");
92 return $path;
93 }
This page took 0.037096 seconds and 4 git commands to generate.