]> Dogcows Code - chaz/git-codeowners/blob - t/app-codeowners-util.t
4d7d224961c871a3fef55a9c973bc75a3f161a61
[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 colorstrip
12 find_codeowners_in_directory
13 find_nearest_codeowners
14 git_ls_files
15 git_toplevel
16 run_command
17 run_git
18 stringf
19 stringify
20 unbackslash
21 zip
22 });
23
24 my $can_git = _can_git();
25
26 subtest 'git_ls_files' => sub {
27 plan skip_all => 'Cannot run git' if !$can_git;
28 my $repodir =_setup_git_repo();
29
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;
32
33 run_git('-C', $repodir, qw{add .})->wait;
34 run_git('-C', $repodir, qw{commit -m}, 'initial commit')->wait;
35
36 (undef, @r) = App::Codeowners::Util::git_ls_files($repodir);
37 is_deeply(\@r, [
38 qw(a/b/c/bar.txt foo.txt)
39 ], 'git ls-files returns correct repo files') or diag explain \@r;
40 };
41
42 subtest 'git_toplevel' => sub {
43 plan skip_all => 'Cannot run git' if !$can_git;
44 my $repodir =_setup_git_repo();
45
46 my $r = App::Codeowners::Util::git_toplevel($repodir);
47 is($r->canonpath, $repodir->canonpath, 'found toplevel directory from toplevel');
48
49 $r = App::Codeowners::Util::git_toplevel($repodir->child('a/b'));
50 is($r->canonpath, $repodir->canonpath, 'found toplevel directory');
51 };
52
53 subtest 'find_nearest_codeowners' => sub {
54 plan skip_all => 'Cannot run git' if !$can_git;
55 my $repodir =_setup_git_repo();
56
57 $repodir->child('docs')->mkpath;
58 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
59
60 my $r = App::Codeowners::Util::find_nearest_codeowners($repodir->child('a/b/c'));
61 is($r, $filepath, 'found CODEOWNERS file');
62 };
63
64 subtest 'find_codeowners_in_directory' => sub {
65 plan skip_all => 'Cannot run git' if !$can_git;
66 my $repodir =_setup_git_repo();
67
68 $repodir->child('docs')->mkpath;
69 my $filepath = _spew_codeowners($repodir->child('docs/CODEOWNERS'));
70
71 my $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
72 is($r, $filepath, 'found CODEOWNERS file in docs');
73
74 $filepath = _spew_codeowners($repodir->child('CODEOWNERS'));
75 $r = App::Codeowners::Util::find_codeowners_in_directory($repodir);
76 is($r, $filepath, 'found CODEOWNERS file in toplevel');
77 };
78
79 done_testing;
80 exit;
81
82 sub _can_git {
83 my (undef, $version) = eval { run_git('--version') };
84 note $@ if $@;
85 note "Found: $version" if $version;
86 return $version && $version ge 'git version 1.8.5'; # for -C flag
87 }
88
89 sub _setup_git_repo {
90 my $repodir = tempdir;
91
92 run_git('-C', $repodir, 'init')->wait;
93 run_git('-C', $repodir, qw{config --local user.email app-codeowners@example.com})->wait;
94 run_git('-C', $repodir, qw{config --local user.name App-Codeowners})->wait;
95
96 $repodir->child('foo.txt')->touchpath;
97 $repodir->child('a/b/c/bar.txt')->touchpath;
98
99 return $repodir;
100 }
101
102 sub _spew_codeowners {
103 my $path = path(shift);
104 $path->spew_utf8(\"foo.txt \@twix\n");
105 return $path;
106 }
This page took 0.051021 seconds and 3 git commands to generate.